Will the below mentioned solution work


  • 0
    S

    given array is 0 1 0 2 1 0 1 3 2 1 2 1

    1. so I traverse from start
    2. Reach an index whose value is equal to or greater than the number.
      3.find the minimum of the values.
    3. subtract the values of arrays from that number and add them up.

    In the above example first 0 the greatest index next is 1 minimum is 0. There is nothing in between them so sol+=0 from 1 I again go till 2 minimum is 1 so 1-0 = 1 sol+=1 and then from 2 I go till 3 minimum is 2 sol+=(2-1)+(2-0)+(2-1) and from 2. So on but my approach is O(n^2). Will this work? Any modification or caching by which I can make this O(n)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.