aboutsummaryrefslogtreecommitdiff
path: root/steps_arr_increasing.cpp
blob: 2beb625630291d3f0154950fd91f0dce78de0406 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 2289. Steps to Make Array Non-decreasing
#include "leetcode.h"

/*
 * given a 0-indexed integer array 'nums'. in one step, remofe all elements
 * 'nums[i]' where 'nums[i - 1] > nums[i]' for all '0 < i < nums.size()'. return
 * the number of steps performed until 'nums' becomes non-decreasing
 */

class Solution {
public:
  int totalSteps(vector<int> &nums) {
    int n = nums.size(), ans = 0;
    vector<int> stack, dp(n);
    for (int i = n - 1; i >= 0; --i) {
      while (!stack.empty() && nums[i] > nums[stack.back()]) {
        dp[i] = max(++dp[i], dp[stack.back()]);
        stack.pop_back();
        ans = max(ans, dp[i]);
      }
      stack.push_back(i);
    }
    return ans;
  }
};

int main() {
  Solution obj;
  vector<int> n1 = {5, 3, 4, 4, 7, 3, 6, 11, 8, 5, 11}, n2 = {4, 5, 7, 7, 13};
  printf("%d\n", obj.totalSteps(n1)); // expect: 3
  printf("%d\n", obj.totalSteps(n2)); // expect: 0
}