Skip to content

1671. Minimum Number of Removals to Make Mountain Array 👍

  • Time: $O(n\log n)$
  • Space: $O(n)$
 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
 public:
  int minimumMountainRemovals(vector<int>& nums) {
    vector<int> l = lengthOfLIS(nums);
    vector<int> r = reversed(lengthOfLIS(reversed(nums)));
    int maxMountainSeq = 0;

    for (int i = 0; i < nums.size(); ++i)
      if (l[i] > 1 && r[i] > 1)
        maxMountainSeq = max(maxMountainSeq, l[i] + r[i] - 1);

    return nums.size() - maxMountainSeq;
  }

 private:
  vector<int> lengthOfLIS(vector<int> nums) {
    // tail[i] := the minimum tail of all increasing subseqs having length i + 1
    // it's easy to see that tail must be an increasing array
    vector<int> tail;

    // dp[i] := length of LIS ending at nums[i]
    vector<int> dp(nums.size());

    for (int i = 0; i < nums.size(); ++i) {
      const int num = nums[i];
      if (tail.empty() || num > tail.back()) {
        tail.push_back(num);
      } else {
        int l = 0;
        int r = tail.size();
        // find the first index l in tail s.t. tail[l] >= num
        while (l < r) {
          const int m = (l + r) / 2;
          if (tail[m] >= num)
            r = m;
          else
            l = m + 1;
        }
        tail[l] = num;
      }
      dp[i] = tail.size();
    }

    return dp;
  }

  vector<int> reversed(const vector<int>& nums) {
    return {rbegin(nums), rend(nums)};
  }
};
 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
  public int minimumMountainRemovals(int[] nums) {
    int[] l = lengthOfLIS(nums);
    int[] r = reversed(lengthOfLIS(reversed(nums)));
    int maxMountainSeq = 0;

    for (int i = 0; i < nums.length; ++i)
      if (l[i] > 1 && r[i] > 1)
        maxMountainSeq = Math.max(maxMountainSeq, l[i] + r[i] - 1);

    return nums.length - maxMountainSeq;
  }

  private int[] lengthOfLIS(int[] nums) {
    int maxLength = 0;
    // tail[i] := the minimum tail of all increasing subseqs with length i + 1
    // it's easy to see that tail must be an increasing array
    int[] tail = new int[nums.length];

    // dp[i] := length of LIS ending at nums[i]
    int[] dp = new int[nums.length];

    for (int i = 0; i < nums.length; ++i) {
      final int num = nums[i];
      // find the first index k in tail[0..maxLength) s.t. tail[k] >= num
      int k = Arrays.binarySearch(tail, 0, maxLength, num);
      // k = -(insertion point) - 1
      // insertion point = -(k + 1)
      if (k < 0)
        k = -(k + 1);
      tail[k] = num;
      // if didn't find any number in tail[0..maxLength) that is >= num, it means we
      // can append num in the current increasing subsequence
      if (k == maxLength)
        ++maxLength;
      dp[i] = maxLength;
    }

    return dp;
  }

  private int[] reversed(int[] nums) {
    int[] A = nums.clone();
    int l = 0;
    int r = nums.length - 1;

    while (l < r)
      swap(A, l++, r--);

    return A;
  }

  private void swap(int[] A, int i, int j) {
    final int temp = A[i];
    A[i] = A[j];
    A[j] = temp;
  }
}