Hello bhaiya, Can you please start a series called "LeetCode Contest Questions Explained" after every Weekly and Biweekly Contest happening on LeetCode? We hustle alot to get the correct approach/solution of contests questions. Guys please do like if y'll also want this to get bhaiya's attention.
Real maths fun starts from 15:01 remaining part of the problem is easy. I couldn't understand from Leetcode editorial, thank you.
Very nice...i liked that nth element idea!...quite helpful😊
Lag hi raha tha aaj sir Maths proof lekar to zaroor aaenge. thanks a lot. watching now
Maths dekh kar maza hi agaya 🔥 That’s the difference between you and other YouTubers. You go to details which help a lot
I've already solved the problem. I'm just here for your in-depth explanation.
My intuition is to store the elements in sorted order, then draw a vertical line. I realized that it is always optimal to move toward the middle lines to minimize the number of operations.
KYA BAAT HAI,SUBEH SUBEH MAN KHUSH HO GYA KUCHH NAYA SEEKH KR, you cant convert a number x to a if both remianders are diff when divided by the increment
Bhaiya aap awsome hoo... || Your Approch and way of understanding Thats I love ❤
Nice Explanation MIK. Another small optimization i could think of is to put the check while grid[i][j]%x!=remainder at the time of creating the 1D array from 2D array. Here is my java solution public static int minOperations(int[][] grid, int x) { List<Integer> flatList = new ArrayList<>(); int expectedRem = grid[0][0]%x; int row = grid.length; int col = grid[0].length; for(int i = 0;i<row;i++){ for(int j = 0;j<col;j++){ if(grid[i][j]%x!=expectedRem){ return -1; //// return -1 as the condition of common point can never be met. }else { flatList.add(grid[i][j]); } } } Collections.sort(flatList); int lstSize = flatList.size(); int mid = lstSize/2; int steps = 0; for(int num : flatList){ steps += Math.abs(num-flatList.get(mid))/x; } return steps; }
Hi, I see that binary search is possible in this question. The search space would make a V, and the bottom part of V is our answer. i got the idea of when to move left and right as well but what if for some element from search space we're not able to make every value same. in that case, what to do?? Hoping your reply as my brain has given up.
thanks so much sir, for very good explanation.
my logic was,agr 'a' ko 'b' me convert krna h by an increment of x then the abs diff both must be divisible by x i.e abs(a-b)%x==0,else return -1; class Solution { public: int minOperations(vector<vector<int>>& grid, int x) { vector<int>nums; for(int i=0;i<grid.size();i++) for(int j=0;j<grid[0].size();j++) nums.push_back(grid[i][j]); sort(nums.begin(),nums.end()); int mid = nums[nums.size()/2]; int ans =0; for(int i=0;i<nums.size();i++) { if(abs(nums[i]-mid) % x) return -1; ans+=abs(mid-nums[i])/x; } return ans; } };
brother , I am sure that you are the person ,who always get 100 out of 100 in maths . And thank for this easier explaination. ✊
Instead of using the mathematical observation, I simply check if the number can be changed to median by seeing if adding or subtracting x can change number to median. Python code: class Solution: def minOperations(self, grid: List[List[int]], x: int) -> int: # Flatten the grid flat_array = [num for row in grid for num in row] # Sort the array flat_array.sort() # Find the median median = flat_array[len(flat_array) // 2] # Calculate total operations total_ops = 0 for num in flat_array: # Check if the difference is divisible by x diff = abs(num - median) if diff % x != 0: return -1 # Calculate operations needed total_ops += diff // x return total_ops
Your explanations are great. Please also start LeetCode contest Question solution
Thanku so much mik, you made maths easy for us ❤
I think the proof you did was good, but it unnecessarily complicated the question because we could just see the difference between the target and the current element of `nums`, and if it is not divisible, then we could return `-1` from there itself.
Solved by myself. 😊. And yes Math is my love too❤❤
@leetcode-j7k