classSolution: defminimumOperations(self, nums: List[int]) -> int: g = [] for x in nums: j = bisect_right(g, x) if j == len(g): g.append(x) else: g[j] = x returnlen(nums) - len(g)
classSolution { public: intminimumOperations(vector<int> &nums){ vector<int> g; for (int x : nums) { auto it = upper_bound(g.begin(), g.end(), x); if (it == g.end()) g.push_back(x); else *it = x; } return nums.size() - g.size(); } };
[sol-Go]
1 2 3 4 5 6 7 8 9 10 11 12
funcminimumOperations(nums []int)int { g := []int{} for _, x := range nums { p := sort.SearchInts(g, x+1) if p < len(g) { g[p] = x } else { g = append(g, x) } } returnlen(nums) - len(g) }
classSolution: defminimumOperations(self, nums: List[int]) -> int: f = [0] * 4 for x in nums: for j inrange(3, 0, -1): f[j] = min(f[k] for k inrange(1, j + 1)) + (j != x) returnmin(f[1:])
[sol-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { publicintminimumOperations(List<Integer> nums) { varf=newint[4]; for (int x : nums) { for (intj=3; j > 0; j--) { for (intk=1; k <= j; k++) f[j] = Math.min(f[j], f[k]); if (j != x) f[j]++; } } intans= nums.size(); for (intj=1; j < 4; j++) ans = Math.min(ans, f[j]); return ans; } }
[sol-C++]
1 2 3 4 5 6 7 8 9 10
classSolution { public: intminimumOperations(vector<int> &nums){ int f[4]{}; for (int x: nums) for (int j = 3; j; j--) f[j] = *min_element(f + 1, f + j + 1) + (j != x); return *min_element(f + 1, f + 4); } };
funcminimumOperations(nums []int)int { f := [4]int{} for _, x := range nums { for j := 3; j > 0; j-- { for k := 1; k <= j; k++ { f[j] = min(f[j], f[k]) } if j != x { f[j]++ } } } ans := len(nums) for _, v := range f[1:] { ans = min(ans, v) } return ans }
funcmin(a, b int)int { if b < a { return b }; return a }