Amazon Interview SDE1 | DSA

Anonymous User
9
Jul 28, 2024

Question :- Given an array of positive integers including 0. At each operation, you can choose a non empty subarray and decrement all element in it by 1. Find minimum number of operation required to make all elements of array equal to 0.

for eg : arr = [3,1,4,2]

  • subarray[0:3] -> [2,0,3,1]
  • subarray[2:3] -> [2,0,2,0]
  • subarray[0:0] (2 times) -> [0,0,2,0]
  • subarray[2:2] (2 times) -> [0,0,0,0]

total => 1+1+2+2 = 6
so. ans = 6

Constraint : size of array can be 1e5.

Expected to do it in O(NlogN) or better?

Comments (0)