Amazon SDE-1 interview | DSA

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2024-07-28Added By Anonymous
Source: Link

Company: Amazon
Role: sde i
Interview Stage: Phone Screen
Location: unknown
Year: unknown

Original Post Content:

Question: An array is given of positive numbers including 0. At each operation you can choose any non empty subarray and decrement 1 from each element of that subarray. Find minimum number of operation required to make all elements of array equal to 0.

For example: Arr = [3,1,4,2]
* 1 : subarray[0:3] -> [2,0,3,1]
* 2: subarray[2:3] -> [2,0,2,0]
* 3: subarray[0:0] -> [0,0,2,0] (two times)
* 4: subarray[2:2] -> [0,0,0,0] (two times)
Total => 1+1+2+2 = 6.
So ans = 6

Constraint: Size of array can be 1e5.

How to do it in O(N)?

Comments