Advertisement
veryinnocentboy

amazon sde2 reject july

Jul 16th, 2021
1,630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1.  
  2. In a 2d matrix N*N. Make all col and rows 0 where at least one 0 is found.
  3. 1 1 1    .     1 0 1
  4. 1 0 1  =>      0 0 0
  5. 1 1 1     .    1 0 1
  6.  
  7. 0 1 2 0        0 0 0 0
  8. 1 2 3 4  =>    0 2 3 0
  9. 5 4 3 2        0 4 3 0
  10.  
  11. Note: matrix is having only positive integers
  12.  
  13. ## My approach
  14. I gave two approaches:
  15. 1.  
  16.   - Do iteration once. save cols and rows in two sets. [O(N*N)]
  17.   - Then iterate onece again and look if that col is present in set COLSET  OR row is present in ROWSET. If yes then make it Zero(0) . O(N*N)
  18.   - Total complexity O(N*N)
  19.  
  20.  2. Other is without using extra space
  21.   - first make all zeros as -1 so that we keep track of which cell has already zero and on with we have placed.
  22.   - Then go one by one on each cell and look if this row and col has at least one -1 present.
  23.       - if yes then mark it Zero(0)
  24.  - at last iterate once again to make all -1s 0 again
  25.  - Total complexity is O(n*n*n)  N cube in worst case. [because for each cell we are looking row and col]
  26.  
  27. Then he said do it less than O n cube complexity that too without using extra space.
  28. I could not think of anything.  Brain Jammed
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. Find Next Bigger element on Right side of an. element in array
  36. and -1 if no found
  37. Example:
  38. [13 2 1 4 5 6 10]
  39.  
  40. [-1 4 4 5 6 10-1]
  41.  
  42. ## My approach
  43. By this time my brain was already jammed and stuck on first question. He moved on to second question.
  44. Here i explained Brute force first then moved on to find better soln.
  45. After trying to fit all Data structured that i know of I was back to -  somehow store intermediate answers into an array.
  46. Came up with a temporary solution but it was still ON*N in worst case.
  47.  
  48. Note: Had I solved this question before I could easily do it.  It uses stack  geeksforgeeks.org/next-greater-element/ :facepalm:
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement