Skip to content

Commit 4792e0a

Browse files
committed
feat: add solutions to lc problem: No.1671
No.1671.Minimum Number of Removals to Make Mountain Array
1 parent f07c53c commit 4792e0a

File tree

9 files changed

+294
-8
lines changed

9 files changed

+294
-8
lines changed

solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
那么最终答案就是 $n - \max(left[i] + right[i] - 1)$,其中 $1 \leq i \leq n$,并且 $left[i] \gt 1$ 且 $right[i] \gt 1$。
6262

63-
时间复杂度 $O(n^2)$。
63+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度
6464

6565
<!-- tabs:start -->
6666

@@ -195,6 +195,37 @@ func max(a, b int) int {
195195
}
196196
```
197197

198+
### **TypeScript**
199+
200+
```ts
201+
function minimumMountainRemovals(nums: number[]): number {
202+
const n = nums.length;
203+
const left = new Array(n).fill(1);
204+
const right = new Array(n).fill(1);
205+
for (let i = 1; i < n; ++i) {
206+
for (let j = 0; j < i; ++j) {
207+
if (nums[i] > nums[j]) {
208+
left[i] = Math.max(left[i], left[j] + 1);
209+
}
210+
}
211+
}
212+
for (let i = n - 2; i >= 0; --i) {
213+
for (let j = i + 1; j < n; ++j) {
214+
if (nums[i] > nums[j]) {
215+
right[i] = Math.max(right[i], right[j] + 1);
216+
}
217+
}
218+
}
219+
let ans = 0;
220+
for (let i = 0; i < n; ++i) {
221+
if (left[i] > 1 && right[i] > 1) {
222+
ans = Math.max(ans, left[i] + right[i] - 1);
223+
}
224+
}
225+
return n - ans;
226+
}
227+
```
228+
198229
### **...**
199230

200231
```

solution/1600-1699/1671.Minimum Number of Removals to Make Mountain Array/README_EN.md

+31
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,37 @@ func max(a, b int) int {
175175
}
176176
```
177177

178+
### **TypeScript**
179+
180+
```ts
181+
function minimumMountainRemovals(nums: number[]): number {
182+
const n = nums.length;
183+
const left = new Array(n).fill(1);
184+
const right = new Array(n).fill(1);
185+
for (let i = 1; i < n; ++i) {
186+
for (let j = 0; j < i; ++j) {
187+
if (nums[i] > nums[j]) {
188+
left[i] = Math.max(left[i], left[j] + 1);
189+
}
190+
}
191+
}
192+
for (let i = n - 2; i >= 0; --i) {
193+
for (let j = i + 1; j < n; ++j) {
194+
if (nums[i] > nums[j]) {
195+
right[i] = Math.max(right[i], right[j] + 1);
196+
}
197+
}
198+
}
199+
let ans = 0;
200+
for (let i = 0; i < n; ++i) {
201+
if (left[i] > 1 && right[i] > 1) {
202+
ans = Math.max(ans, left[i] + right[i] - 1);
203+
}
204+
}
205+
return n - ans;
206+
};
207+
```
208+
178209
### **...**
179210

180211
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function minimumMountainRemovals(nums: number[]): number {
2+
const n = nums.length;
3+
const left = new Array(n).fill(1);
4+
const right = new Array(n).fill(1);
5+
for (let i = 1; i < n; ++i) {
6+
for (let j = 0; j < i; ++j) {
7+
if (nums[i] > nums[j]) {
8+
left[i] = Math.max(left[i], left[j] + 1);
9+
}
10+
}
11+
}
12+
for (let i = n - 2; i >= 0; --i) {
13+
for (let j = i + 1; j < n; ++j) {
14+
if (nums[i] > nums[j]) {
15+
right[i] = Math.max(right[i], right[j] + 1);
16+
}
17+
}
18+
}
19+
let ans = 0;
20+
for (let i = 0; i < n; ++i) {
21+
if (left[i] > 1 && right[i] > 1) {
22+
ans = Math.max(ans, left[i] + right[i] - 1);
23+
}
24+
}
25+
return n - ans;
26+
};

solution/1600-1699/1672.Richest Customer Wealth/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class Solution {
8080
public int maximumWealth(int[][] accounts) {
8181
int ans = 0;
8282
for (var e : accounts) {
83+
// int s = Arrays.stream(e).sum();
8384
int s = 0;
8485
for (int v : e) {
8586
s += v;

solution/1600-1699/1672.Richest Customer Wealth/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class Solution {
6767
public int maximumWealth(int[][] accounts) {
6868
int ans = 0;
6969
for (var e : accounts) {
70+
// int s = Arrays.stream(e).sum();
7071
int s = 0;
7172
for (int v : e) {
7273
s += v;

solution/1600-1699/1672.Richest Customer Wealth/Solution.java

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class Solution {
22
public int maximumWealth(int[][] accounts) {
33
int ans = 0;
44
for (var e : accounts) {
5+
// int s = Arrays.stream(e).sum();
56
int s = 0;
67
for (int v : e) {
78
s += v;

solution/1600-1699/1679.Max Number of K-Sum Pairs/README.md

+103-7
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,26 @@
4949

5050
**方法一:排序**
5151

52-
nums 进行排序。然后 l, r 分别指向 nums 首尾元素,判断两整数之和 sk 的大小关系。
52+
我们对 `nums` 进行排序。然后 $l$, $r$ 分别指向 `nums` 首尾元素,判断两整数之和 $s$$k$ 的大小关系。
5353

54-
-`s == k`,说明找到了两个整数,满足和为 k,答案 ans 加 1,然后 l, r 向中间移动;
55-
-`s > k`,则 r 指针向左移动;
56-
-`s > k`,则 l 指针向右移动;
57-
- 继续循环判断,直至 l >= r
54+
-$s = k$,说明找到了两个整数,满足和为 $k$,答案加一,然后 $l$, $r$ 向中间移动;
55+
-$s \gt k$,则 $r$ 指针向左移动;
56+
-$s \lt k$,则 $l$ 指针向右移动;
57+
- 继续循环判断,直至 $l \geq r$
5858

59-
循环结束,返回 ans
59+
循环结束,返回答案
6060

61-
时间复杂度 $O(n\times \log n)$,其中 $n$ 为 `nums` 的长度。
61+
时间复杂度 $O(n\times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为 `nums` 的长度。
62+
63+
**方法二:哈希表**
64+
65+
我们使用哈希表 `cnt` 记录当前剩余整数及其出现的次数。
66+
67+
遍历 `nums`,对于当前整数 $x$,判断 $k - x$ 是否在 `cnt` 中,若存在,则说明找到了两个整数,满足和为 $k$,答案加一,然后将 $k - x$ 的出现次数减一;否则,将 $x$ 的出现次数加一。
68+
69+
遍历结束,返回答案。
70+
71+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 `nums` 的长度。
6272

6373
<!-- tabs:start -->
6474

@@ -83,6 +93,20 @@ class Solution:
8393
return ans
8494
```
8595

96+
```python
97+
class Solution:
98+
def maxOperations(self, nums: List[int], k: int) -> int:
99+
cnt = Counter()
100+
ans = 0
101+
for x in nums:
102+
if cnt[k - x]:
103+
ans += 1
104+
cnt[k - x] -= 1
105+
else:
106+
cnt[x] += 1
107+
return ans
108+
```
109+
86110
### **Java**
87111

88112
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -110,6 +134,26 @@ class Solution {
110134
}
111135
```
112136

137+
```java
138+
class Solution {
139+
public int maxOperations(int[] nums, int k) {
140+
Map<Integer, Integer> cnt = new HashMap<>();
141+
int ans = 0;
142+
for (int x : nums) {
143+
if (cnt.containsKey(k - x)) {
144+
++ans;
145+
if (cnt.merge(k - x, -1, Integer::sum) == 0) {
146+
cnt.remove(k - x);
147+
}
148+
} else {
149+
cnt.merge(x, 1, Integer::sum);
150+
}
151+
}
152+
return ans;
153+
}
154+
}
155+
```
156+
113157
### **C++**
114158

115159
```cpp
@@ -135,6 +179,25 @@ public:
135179
};
136180
```
137181
182+
```cpp
183+
class Solution {
184+
public:
185+
int maxOperations(vector<int>& nums, int k) {
186+
unordered_map<int, int> cnt;
187+
int ans = 0;
188+
for (int& x : nums) {
189+
if (cnt[k - x]) {
190+
--cnt[k - x];
191+
++ans;
192+
} else {
193+
++cnt[x];
194+
}
195+
}
196+
return ans;
197+
}
198+
};
199+
```
200+
138201
### **Go**
139202

140203
```go
@@ -157,6 +220,39 @@ func maxOperations(nums []int, k int) int {
157220
}
158221
```
159222

223+
```go
224+
func maxOperations(nums []int, k int) (ans int) {
225+
cnt := map[int]int{}
226+
for _, x := range nums {
227+
if cnt[k-x] > 0 {
228+
cnt[k-x]--
229+
ans++
230+
} else {
231+
cnt[x]++
232+
}
233+
}
234+
return
235+
}
236+
```
237+
238+
### **TypeScript**
239+
240+
```ts
241+
function maxOperations(nums: number[], k: number): number {
242+
const cnt = new Map();
243+
let ans = 0;
244+
for (const x of nums) {
245+
if (cnt.get(k - x)) {
246+
cnt.set(k - x, cnt.get(k - x) - 1);
247+
++ans;
248+
} else {
249+
cnt.set(x, (cnt.get(x) | 0) + 1);
250+
}
251+
}
252+
return ans;
253+
}
254+
```
255+
160256
### **...**
161257

162258
```

solution/1600-1699/1679.Max Number of K-Sum Pairs/README_EN.md

+86
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ class Solution:
6262
return ans
6363
```
6464

65+
```python
66+
class Solution:
67+
def maxOperations(self, nums: List[int], k: int) -> int:
68+
cnt = Counter()
69+
ans = 0
70+
for x in nums:
71+
if cnt[k - x]:
72+
ans += 1
73+
cnt[k - x] -= 1
74+
else:
75+
cnt[x] += 1
76+
return ans
77+
```
78+
6579
### **Java**
6680

6781
```java
@@ -87,6 +101,26 @@ class Solution {
87101
}
88102
```
89103

104+
```java
105+
class Solution {
106+
public int maxOperations(int[] nums, int k) {
107+
Map<Integer, Integer> cnt = new HashMap<>();
108+
int ans = 0;
109+
for (int x : nums) {
110+
if (cnt.containsKey(k - x)) {
111+
++ans;
112+
if (cnt.merge(k - x, -1, Integer::sum) == 0) {
113+
cnt.remove(k - x);
114+
}
115+
} else {
116+
cnt.merge(x, 1, Integer::sum);
117+
}
118+
}
119+
return ans;
120+
}
121+
}
122+
```
123+
90124
### **C++**
91125

92126
```cpp
@@ -112,6 +146,25 @@ public:
112146
};
113147
```
114148
149+
```cpp
150+
class Solution {
151+
public:
152+
int maxOperations(vector<int>& nums, int k) {
153+
unordered_map<int, int> cnt;
154+
int ans = 0;
155+
for (int& x : nums) {
156+
if (cnt[k - x]) {
157+
--cnt[k - x];
158+
++ans;
159+
} else {
160+
++cnt[x];
161+
}
162+
}
163+
return ans;
164+
}
165+
};
166+
```
167+
115168
### **Go**
116169

117170
```go
@@ -134,6 +187,39 @@ func maxOperations(nums []int, k int) int {
134187
}
135188
```
136189

190+
```go
191+
func maxOperations(nums []int, k int) (ans int) {
192+
cnt := map[int]int{}
193+
for _, x := range nums {
194+
if cnt[k-x] > 0 {
195+
cnt[k-x]--
196+
ans++
197+
} else {
198+
cnt[x]++
199+
}
200+
}
201+
return
202+
}
203+
```
204+
205+
### **TypeScript**
206+
207+
```ts
208+
function maxOperations(nums: number[], k: number): number {
209+
const cnt = new Map();
210+
let ans = 0;
211+
for (const x of nums) {
212+
if (cnt.get(k - x)) {
213+
cnt.set(k - x, cnt.get(k - x) - 1);
214+
++ans;
215+
} else {
216+
cnt.set(x, (cnt.get(x) | 0) + 1);
217+
}
218+
}
219+
return ans;
220+
}
221+
```
222+
137223
### **...**
138224

139225
```

0 commit comments

Comments
 (0)