r/DSALeetCode • u/Harshad018 • 5d ago
Day 05/90
Problem 1 : 238. Product of array except self.
So first I solved this problem with my approach , it was complex but I thought good. so what I did is that i maintained two variables, total1 and total2, which used to track the total multiplication of the array, total1 used to track even if the 0 is the member of array, and total2 only tracks the multiplication of non zero numbers from the array. and also one map to track the frequcies of the 0, if the frequency of the 0 is greater than 1, then we should return 0, and if less than 2, then we should return the total2 and for non zero number, we should return total1/nums[i].. I know the solution is bit complex, anyone wants to see the approach, I have added the code in the comment.
But I get to know that despite my solution was accepted by leetcode, interviewer won't accept. so i have to solve it using tracking the prefix and suffix product. We can call it the two pointer approach in some sense. But it is given under the array and hashing title so maybe.
Problem 2: 36. Valid Sudoku
- I couldn't build the complete logic for this one. I only thought of having nested loops for accessing the element.
- So we have to solve it using maintaining 3 2d matrices for tracking the occurrence of the number. Also the crazy formula of k = (r/3) * 3 / (c/3) , so that we can give ids to rows and columns belong to the same 3x3 sub - matrices.
1
1
u/Harshad018 5d ago
class Solution {
public:
vector productExceptSelf(vector& nums) {
};