Vedant Rawale’s Post

View profile for Vedant Rawale, graphic

Guardian @Leetcode (Max. 2259) | Expert @Codeforces (Max. 1643) | 5⭐ @Codechef (Max. 2011) | Flutter App Developer | BTech CSE(AI) Student @VIT Pune '26

Let's discuss the 4th problem of today's LeetCode Weekly Contest 414 ... It personally took me a lot of time to optimize it before getting AC but the problem was really good... Concepts => BFS + BitMaskDP Only reason I could think of BitMask was because the size of positions vector was atmax 15 so each mask can ideally represent the state of the board. Now we also need a state to show whose turn it is which can be 0/1 where 0 represents it's Alice's turn and 1 represents it's Bob's turn. We also need information of current position of Knight. Now your Knight can move across different boxes but we just need the information when the Knight is at the position where it kills any pawn because that's when the other player will take it's turn so it will be at max 16 boxes where 15 can be those from positions vector and 1 of the initial position of Knight. We will precompute the minimum distances between any two positions in grid (here min moves) using BFS or Dijkstra. Knight can move at max 8 adjacent positions . Also we don't need to find this for each box of grid , just find it for those 16 boxes which are of our concern. Now we can define the state of DP : dp[ind][mask][0] => minimum number of moves needed to kill all pawns which are remaining (the ones whose bit is set in mask) if we are standing at {x,y} => {position[ind][0], position[ind][1]} place in grid. dp[ind][mask][1] => maximum number of moves needed to kill all pawns which are remaining (the ones whose bit is set in mask) if we are standing at {x,y} => {position[ind][0], position[ind][1]} place in grid. State Transitions : We can kill any pawn and check for further states. Let's say our current Mask is : "mask", current index is : "ind" current turn = "turn" // turn = 1 for alice and turn = 0 for bob. if(mask==0) return 0; // all pawns are killed if(dp[ind][mask][turn]!=-1) return dp[ind][mask][turn]; int ans = (turn ? -1 : 1e10); for(int i = 0;i<15;i++) { if(mask&(1<<i)) => this means the pawn at {positions[i][0],positions[i][1]} is alive so we can kill this one and get answer for further state which is : int newMask = mask& (~(1<<i)); // unset i'th bit int nextStateAns = solve(i,newMask, !turn); int currReqMoves = dist[ind][positions[i][0]][positions[i][1]]; if(turn==1) ans = max(ans, currReqMoves + nextStateAns); else ans = min(ans, currReqMoves + nextStateAns); } return dp[ind][mask][turn] = ans; You can set index of {kx,ky} as positions.size() ; Answer => dp[index of {kx,ky} ][all bits set][1]; Try to figure out the time and space complexity for this approach and mention it in comments below. Optimizations were required here just to be able to fit into ideal number of operations which are 1e7 or 1e8 at max in one second. My submission : https://lnkd.in/dAM9cpb4 #leetcode #leetcodecontest #codingcommunity #weeklycontest #competitiveprogramming #codingchallenge #cppprogramming #cpp #contest #approach #solutions #datastructures #algorithms

  • text
Mayank Pushpjeet

Upcoming SDE-1 at Flipkart || Former SDE intern at Astrotalk || Former Intern at Adobe || Candidate Master at Codeforces (max rating-1949) || 5 ⭐ at Codechef || Guardian at Leetcode || IIT Kanpur

3mo

The 0,1 is not needed because we can determine it by the number of set bits in the mask so basically a particular mask will have either 0 or 1 only. If the mask has even bits that means it's Alice turn and vice versa

Vishnuvardhan Morishetti

SDE @Zscaler | 2*ICPC Regionalist 2021, 2022 | Guardian @LeetCode (Top 1%) | Codechef 4*

3mo

Vedant Rawale it took me 5-6 attempts to get Ac, optimised 4dp to 3rd dp. Previously i used co-ordnaties then i realised we can get the coordinates this from prevIdx. And also optimised the precomputation part. I would say it's a good problem.

NIKHIL GURJAR

GET @Sopra Steria | 1st Runner-up @ISIM HACKTHON 2.0 | 2 🌟 @Codechef | 1449 (max) @Leetcode | Ex-Intern @Gyan Eduversity | MERN STACK DEV. | @CSE-24

3mo

Thanks for mentioning the topics, I will first read about them, and try again the problems.

Ridham Anand

Full Stack Web Developer @GDSC | Ex-Web Intern @HerbsMagic | National Coding Olympiad Winner | CSE Student at Vishwakarma Institute Of Technology |

3mo

Useful tips

See more comments

To view or add a comment, sign in

Explore topics