-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3231-MinimumNumberOfIncreasingSubsequenceToBeRemoved.go
More file actions
65 lines (56 loc) · 1.53 KB
/
3231-MinimumNumberOfIncreasingSubsequenceToBeRemoved.go
File metadata and controls
65 lines (56 loc) · 1.53 KB
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
59
60
61
62
63
64
65
package main
// 3231. Minimum Number of Increasing Subsequence to Be Removed
// Given an array of integers nums, you are allowed to perform the following operation any number of times:
// Remove a strictly increasing subsequence from the array.
// Your task is to find the minimum number of operations required to make the array empty.
// Example 1:
// Input: nums = [5,3,1,4,2]
// Output: 3
// Explanation:
// We remove subsequences [1, 2], [3, 4], [5].
// Example 2:
// Input: nums = [1,2,3,4,5]
// Output: 1
// Example 3:
// Input: nums = [5,4,3,2,1]
// Output: 5
// Constraints:
// 1 <= nums.length <= 10^5
// 1 <= nums[i] <= 10^5
import "fmt"
func minOperations(nums []int) int {
group := []int{}
for _, v := range nums {
l, r := 0, len(group)
for l < r {
mid := (l + r) >> 1
if group[mid] < v {
r = mid
} else {
l = mid + 1
}
}
if l == len(group) {
group = append(group, v)
} else {
group[l] = v
}
}
return len(group)
}
func main() {
// Example 1:
// Input: nums = [5,3,1,4,2]
// Output: 3
// Explanation:
// We remove subsequences [1, 2], [3, 4], [5].
fmt.Println(minOperations([]int{5,3,1,4,2})) // 3
// Example 2:
// Input: nums = [1,2,3,4,5]
// Output: 1
fmt.Println(minOperations([]int{1,2,3,4,5})) // 1
// Example 3:
// Input: nums = [5,4,3,2,1]
// Output: 5
fmt.Println(minOperations([]int{5,4,3,2,1})) // 5
}