Grid Game | Detailed Explanation | Complete Ry Run | Leetcode 2017 | codestorywithMIK

  Рет қаралды 2,529

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 40
@codestorywithMIK
@codestorywithMIK 6 сағат бұрын
If you are wondering why maximising Robot-1’s points doesn’t ultimately give the least score for Robot-2 : Let’s take this example, if grid = [[2, 4, 6], [8, 9, 10]], the first robot could take the path 2 -> 8 -> 9 -> 10 to maximize its points, leaving 4 and 6 for the second robot. But the better strategy is for the first robot to turn down at index 1, leaving either 6 or 8 for the second robot, which would then get max(6, 8) = 8 points instead of 10. You can clearly see that here the point of Robot2 is prioritised instead of maximising the points of Robot1
@Letstudy-p7y
@Letstudy-p7y 5 сағат бұрын
yes now i got it. i was trying to solve this problem by maximising the robot1 points and got wrong asnwer. Now i clearly understand . Thankyou for your awesome explanation and test case.
@shreysaxena738
@shreysaxena738 4 сағат бұрын
Thank you, this is the explanation was looking for.
@chinmaygupta2277
@chinmaygupta2277 55 минут бұрын
amazing explanation bhaiya !! wait for leetcode contest solutions 🤞🏻🤞🏻
@bhuppidhamii
@bhuppidhamii 7 сағат бұрын
no way, that I could come up with this solution, I was thinink of dp.
@Zoro_tao
@Zoro_tao 5 сағат бұрын
I tried solving with dp as well but failed Did you find out how it cannot be solved by dp?
@bhuppidhamii
@bhuppidhamii 3 сағат бұрын
@Zoro_tao no
@entertainmentWithCoder
@entertainmentWithCoder 2 сағат бұрын
great explaination
@RajManish-b8m
@RajManish-b8m 7 сағат бұрын
itne easy way me samjh Aaaa gya
@cse080chandrasekhar9
@cse080chandrasekhar9 5 сағат бұрын
i feel that its nice problem we cant say hard and cant say medium ,but its the defination of problem!
@nishantdehariya5769
@nishantdehariya5769 2 сағат бұрын
nice soln, but I think this problem should be marked hard as its really difficult to get into the intuition of moving around dfs, bfs, greedy and finally concluding prefix sum and solving it in 40 mins
@krishna-kaushall
@krishna-kaushall 3 сағат бұрын
all my college friends are like robot1.💀
@unknown__899
@unknown__899 2 сағат бұрын
😢Same
@pickled8262
@pickled8262 Сағат бұрын
🤣
@rahulsihara8946
@rahulsihara8946 4 сағат бұрын
i did DP approach as that was most intuitive, this problem seems a specific case where row count is 2. If its not, then i assume DP is only way to solve it in exponential time. I got TLE with my DP solution.
@aizad786iqbal
@aizad786iqbal 6 сағат бұрын
one doubt, why robot 1 taking max for himself will not result in min for robot 2, ideally same hi baat hui na ? can you give any example ? jisme robot 1 max take kare but robot2 ko min points na mile..
@codestorywithMIK
@codestorywithMIK 6 сағат бұрын
Glad someone asked. Let’s take this example, if grid = [[2, 4, 6], [8, 9, 10]], the first robot could take the path 2 -> 8 -> 9 -> 10 to maximize its points, leaving 4 and 6 for the second robot. But the better strategy is for the first robot to turn down at index 1, leaving either 6 or 8 for the second robot, which would then get max(6, 8) = 8 points instead of 10. You can clearly see that here the point of Robot2 is prioritised instead of maximising the points of Robot1
@sahilgupta-dv9zb
@sahilgupta-dv9zb 6 сағат бұрын
agr robot1 max take krega .. toh i think jo points bche hua h unme se hee robot 2 ko max lene honge ... this is what i think
@VikasMishra-q9w
@VikasMishra-q9w 2 сағат бұрын
Hi Mik, Thanks for explaining it. I have a small doubt here. When we approach this problem and it tell us that we want to go either in right or bottom, the first thing came to my mind was recursion. What I implemented is recursively iterate all the possible path for the first robo and keep track of the maximum sum and its path. Once you have the path, set all the indexes of the path to 0 and implement the same logic for second robo and you will get the pathSum for the second Robo. NOTE: MY CODE IS NOT ABLE TO PASS ALL THE TESTCASE EXCEPT THE ONE MENTIONED IN THE EXAMPLE. I would love to know your point on this, why its not working. Here is my submitted code class Solution { int[][] directions = {{1, 0}, {0, 1}}; long roboSum; List roboPath; public long gridGame(int[][] grid) { roboPath = new ArrayList(); // check path for first robo solve(grid, 0, 0, new ArrayList(), 0); // mark the first robo path to 0; for (int[] location : roboPath) grid[location[0]][location[1]] = 0; // resetting sum roboSum = 0; // check path for second robo solve(grid, 0, 0, new ArrayList(), 0); return roboSum; } public void solve(int[][] grid, int row, int col, List path, long pathSum) { int m = grid.length; int n = grid[0].length; // out of index if (row >= m || col >= n) return; // update path and sum pathSum += grid[row][col]; path.add(new int[]{row, col}); // target cell if (row == m - 1 && col == n - 1) { if (pathSum > roboSum) { roboSum = pathSum; roboPath = new ArrayList(path); } path.remove(path.size() - 1); pathSum -= grid[row][col]; return; } for (int[] dir : directions) { int newRow = row + dir[0]; int newCol = col + dir[1]; solve(grid, newRow, newCol, path, pathSum); } path.remove(path.size() - 1); pathSum -= grid[row][col]; } }
@EkanshVerma-dg1jq
@EkanshVerma-dg1jq 12 минут бұрын
This approach won't work maximizing robot 1 path sum will not minimize robot2 path sum try this example [5, 12, 14], [20, 7, 17]
@amanpatel8575
@amanpatel8575 3 сағат бұрын
Hint + Quote: There is difference between maximizing one's gain and minimizing other's gains. Iykyk.
@Afif-d7n
@Afif-d7n 7 сағат бұрын
bhai can u kindly make a separate playlist solving the porblems at neetcode150?? most of them are already done by u but some r left so it'd be great for us if u could make a separate playlist containing only neetcode150
@janaSdj
@janaSdj 7 сағат бұрын
Thanks...
@utkarshsinghchouhan8819
@utkarshsinghchouhan8819 5 сағат бұрын
Motivation : "किस्मत के भरोसे रहने वालों को वो हर चीज़ मिलती है, जो मेहनत करने वाले थूक जाते हैं।"
@shaundabre5552
@shaundabre5552 Сағат бұрын
Pressure make Diamond, Simple as that
@Amanbiragi17
@Amanbiragi17 26 минут бұрын
Motivation for Tomorrow -- "Perfection will come, through Practice".
@navamsharma1888
@navamsharma1888 Сағат бұрын
First time I got confused from your video.
@codestorywithMIK
@codestorywithMIK 41 минут бұрын
Let’s get it. What part was confusing ? Kindly let me know, i will try to explain ❤️🙏
@souravRawat-mr7zk
@souravRawat-mr7zk 3 сағат бұрын
my noob arse was thinking of using BFS, like maximizing the robot1 points and then making the path cost 0, and then again BFS the robot2 for his maximum path.... hth anyone could come up with the solution like this...
@hemantvishwakarma8751
@hemantvishwakarma8751 Сағат бұрын
Java code:- class Solution { public long gridGame(int[][] grid) { int m=grid[0].length; long min = Long.MAX_VALUE; long firstRowScore = 0; for(int num:grid[0]){ firstRowScore+=num; } long secondRowScore = 0; for(int i=0;i0) secondRowScore+=grid[1][i-1]; long r2MaxScore = Math.max(firstRowScore,secondRowScore); min=Math.min(min,r2MaxScore); } return min; } }
@gauravmundhada1647
@gauravmundhada1647 5 сағат бұрын
No way i would have thought of prefix sum
@viratkohli4863
@viratkohli4863 4 сағат бұрын
JAVA CODE ---> class Solution { public long gridGame(int[][] grid) { long firstrow=0; long secondrow=0; for(int i=0;i
@YT_LUCKY-444
@YT_LUCKY-444 7 сағат бұрын
I think about a dp solution but that not work How prefix sum approach get correct I don't think about prefix sum method 😢😢😢
@_Aryan_Chandel
@_Aryan_Chandel 6 сағат бұрын
Bhaiya dsa ka course laao na
@indianengineer5802
@indianengineer5802 5 сағат бұрын
What is wrong in this solution ? I just found the turningPoint of prefixSum and does calculation accordingly. What is wrong with logic? class Solution { public long gridGame(int[][] grid) { int n = grid[0].length; int[][] prefix = new int[2][n]; prefix[0][n - 1] = grid[0][n - 1]; prefix[1][n - 1] = grid[1][n - 1]; for (int i = n - 2; i >= 0; i--) { prefix[0][i] = grid[0][i] + prefix[0][i + 1]; prefix[1][i] = grid[1][i] + prefix[1][i + 1]; } int turningPoint = -1; for (int i = 0; i < n; i++) { long top = i + 1 < n ? prefix[0][i + 1] : 0; // Remaining sum in the top row after the turning point long bottom = i > 0 ? prefix[1][i - 1] : 0; // Sum in the bottom row before the turning point if (top < bottom) { turningPoint = i; break; } } // Handle turning points if (turningPoint == -1) { long sum = 0; for (int i = 0; i < n - 1; i++) { sum += grid[1][i]; } return sum; } else if (turningPoint == 0) { long sum = 0; for (int i = 1; i < n; i++) { sum += grid[0][i]; } return sum; } else { long leftHalf = 0; long rightHalf = 0; for (int i = 0; i < turningPoint; i++) leftHalf += grid[1][i]; for (int i = turningPoint + 1; i < n; i++) rightHalf += grid[0][i]; return Math.max(leftHalf, rightHalf); } } }
@Mahidhar-w8p
@Mahidhar-w8p 7 сағат бұрын
class Solution: def gridGame(self, grid: List[List[int]]) -> int: top_sum = sum(grid[0]) bottom_sum = 0 N = len(grid[0]) res = float("inf") for i in range(N): top_sum -= grid[0][i] res = min(res, max(top_sum,bottom_sum)) bottom_sum += grid[1][i] return res
@lavisharma3603
@lavisharma3603 7 сағат бұрын
🎉🎉🎉🎉
@madmaxgaming5864
@madmaxgaming5864 5 сағат бұрын
Bhai Maine toh DP soncha tha, i could not come with this solution.
@himanshuyadav0600
@himanshuyadav0600 5 сағат бұрын
same
@Anime-ub7vs
@Anime-ub7vs 4 сағат бұрын
i was thinking dp
БОЙКАЛАР| bayGUYS | 27 шығарылым
28:49
bayGUYS
Рет қаралды 1,1 МЛН
SLIDE #shortssprintbrasil
0:31
Natan por Aí
Рет қаралды 49 МЛН
Why the US Dollar is Surging (and why it’s bad for Trump)
8:28
TLDR News Global
Рет қаралды 400 М.
From Street View To AI - How Google Maps Mapped The World
15:19
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 213 М.
How I'd Learn AI in 2025 (If I could start over)
10:18
Sankho kun
Рет қаралды 15 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 241 М.
WORLD CHAMP GUKESH IS BACK!
20:12
GMHikaru
Рет қаралды 166 М.