Skip to content

Commit

Permalink
Time: 39 ms (42.86%), Space: 44 MB (88.44%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit-S-Sahu committed Oct 30, 2024
1 parent c5d42f8 commit 98ade50
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public int minimumMountainRemovals(int[] nums) {
int n = nums.length;
if (n < 3) return 0;

int[] inc = new int[n];
int[] dec = new int[n];

for (int i = 0; i < n; i++) {
inc[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) inc[i] = Math.max(inc[i], inc[j] + 1);
}
}

for (int i = n - 1; i >= 0; i--) {
dec[i] = 1;
for (int j = n - 1; j > i; j--) {
if (nums[i] > nums[j]) dec[i] = Math.max(dec[i], dec[j] + 1);
}
}

int ans = 0;
for (int i = 1; i < n - 1; i++) {
if (inc[i] > 1 && dec[i] > 1) ans = Math.max(ans, inc[i] + dec[i] - 1);
}

return n - ans;
}
}

0 comments on commit 98ade50

Please sign in to comment.