Skip to content

Commit 7de2d0a

Browse files
authored
feat: add Java solutions to lc problems: No.2869~2872 (#1728)
1 parent 9c132c0 commit 7de2d0a

File tree

14 files changed

+1008
-0
lines changed

14 files changed

+1008
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# [2869. Minimum Operations to Collect Elements](https://leetcode.cn/problems/minimum-operations-to-collect-elements/)
2+
3+
[English Version](/solution/2800-2899/2869.Minimum%20Operations%20to%20Collect%20Elements/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>一次操作中,你可以将数组的最后一个元素删除,将该元素添加到一个集合中。</p>
10+
11+
<p>请你返回收集元素&nbsp;<code>1, 2, ..., k</code>&nbsp;需要的&nbsp;<strong>最少操作次数</strong>&nbsp;。</p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong class="example">示例 1:</strong></p>
16+
17+
<pre><b>输入:</b>nums = [3,1,5,4,2], k = 2
18+
<b>输出:</b>4
19+
<b>解释:</b>4 次操作后,集合中的元素依次添加了 2 ,4 ,5 和 1 。此时集合中包含元素 1 和 2 ,所以答案为 4 。
20+
</pre>
21+
22+
<p><strong class="example">示例 2:</strong></p>
23+
24+
<pre><b>输入:</b>nums = [3,1,5,4,2], k = 5
25+
<b>输出:</b>5
26+
<b>解释:</b>5 次操作后,集合中的元素依次添加了 2 ,4 ,5 ,1 和 3 。此时集合中包含元素 1 到 5 ,所以答案为 5 。
27+
</pre>
28+
29+
<p><strong class="example">示例 3:</strong></p>
30+
31+
<pre><b>输入:</b>nums = [3,2,5,3,1], k = 3
32+
<b>输出:</b>4
33+
<b>解释:</b>4 次操作后,集合中的元素依次添加了 1 ,3 ,5 和 2 。此时集合中包含元素 1 到 3 ,所以答案为 4 。
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
38+
<p><strong>提示:</strong></p>
39+
40+
<ul>
41+
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
42+
<li><code>1 &lt;= nums[i] &lt;= nums.length</code></li>
43+
<li><code>1 &lt;= k &lt;= nums.length</code></li>
44+
<li>输入保证你可以收集到元素&nbsp;<code>1, 2, ..., k</code> 。</li>
45+
</ul>
46+
47+
## 解法
48+
49+
<!-- 这里可写通用的实现逻辑 -->
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
<!-- 这里可写当前语言的特殊实现逻辑 -->
56+
57+
```python
58+
59+
```
60+
61+
### **Java**
62+
63+
<!-- 这里可写当前语言的特殊实现逻辑 -->
64+
65+
```java
66+
class Solution {
67+
public int minOperations(List<Integer> nums, int k) {
68+
boolean[] isAdded = new boolean[k];
69+
int n = nums.size();
70+
int count = 0;
71+
for (int i = n - 1; i >= 0; i--) {
72+
if (nums.get(i) > k || isAdded[nums.get(i) - 1]) {
73+
continue;
74+
}
75+
isAdded[nums.get(i) - 1] = true;
76+
count++;
77+
if (count == k) {
78+
return n - i;
79+
}
80+
}
81+
return n;
82+
}
83+
}
84+
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
91+
```
92+
93+
### **Go**
94+
95+
```go
96+
97+
```
98+
99+
### **TypeScript**
100+
101+
```ts
102+
103+
```
104+
105+
### **...**
106+
107+
```
108+
109+
```
110+
111+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [2869. Minimum Operations to Collect Elements](https://leetcode.com/problems/minimum-operations-to-collect-elements/)
2+
3+
[中文文档](/solution/2800-2899/2869.Minimum%20Operations%20to%20Collect%20Elements/README.md)
4+
5+
## Description
6+
7+
<p>In one operation, you can remove the last element of the array and add it to your collection.</p>
8+
9+
<p>Return <em>the <strong>minimum number of operations</strong> needed to collect elements</em> <code>1, 2, ..., k</code>.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre><strong>Input:</strong> nums = [3,1,5,4,2], k = 2
15+
<strong>Output:</strong> 4
16+
<strong>Explanation:</strong> After 4 operations, we collect elements 2, 4, 5, and 1, in this order. Our collection contains elements 1 and 2. Hence, the answer is 4.
17+
</pre>
18+
19+
<p><strong class="example">Example 2:</strong></p>
20+
21+
<pre><strong>Input:</strong> nums = [3,1,5,4,2], k = 5
22+
<strong>Output:</strong> 5
23+
<strong>Explanation:</strong> After 5 operations, we collect elements 2, 4, 5, 1, and 3, in this order. Our collection contains elements 1 through 5. Hence, the answer is 5.
24+
</pre>
25+
26+
<p><strong class="example">Example 3:</strong></p>
27+
28+
<pre><strong>Input:</strong> nums = [3,2,5,3,1], k = 3
29+
<strong>Output:</strong> 4
30+
<strong>Explanation:</strong> After 4 operations, we collect elements 1, 3, 5, and 2, in this order. Our collection contains elements 1 through 3. Hence, the answer is 4.
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
38+
<li><code>1 &lt;= nums[i] &lt;= nums.length</code></li>
39+
<li><code>1 &lt;= k &lt;= nums.length</code></li>
40+
<li>The input is generated such that you can collect elements <code>1, 2, ..., k</code>.</li>
41+
</ul>
42+
43+
## Solutions
44+
45+
<!-- tabs:start -->
46+
47+
### **Python3**
48+
49+
```python
50+
51+
```
52+
53+
### **Java**
54+
55+
```java
56+
class Solution {
57+
public int minOperations(List<Integer> nums, int k) {
58+
boolean[] isAdded = new boolean[k];
59+
int n = nums.size();
60+
int count = 0;
61+
for (int i = n - 1; i >= 0; i--) {
62+
if (nums.get(i) > k || isAdded[nums.get(i) - 1]) {
63+
continue;
64+
}
65+
isAdded[nums.get(i) - 1] = true;
66+
count++;
67+
if (count == k) {
68+
return n - i;
69+
}
70+
}
71+
return n;
72+
}
73+
}
74+
75+
```
76+
77+
### **C++**
78+
79+
```cpp
80+
81+
```
82+
83+
### **Go**
84+
85+
```go
86+
87+
```
88+
89+
### **TypeScript**
90+
91+
```ts
92+
93+
```
94+
95+
### **...**
96+
97+
```
98+
99+
```
100+
101+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int minOperations(List<Integer> nums, int k) {
3+
boolean[] isAdded = new boolean[k];
4+
int n = nums.size();
5+
int count = 0;
6+
for (int i = n - 1; i >= 0; i--) {
7+
if (nums.get(i) > k || isAdded[nums.get(i) - 1]) {
8+
continue;
9+
}
10+
isAdded[nums.get(i) - 1] = true;
11+
count++;
12+
if (count == k) {
13+
return n - i;
14+
}
15+
}
16+
return n;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# [2870. Minimum Number of Operations to Make Array Empty](https://leetcode.cn/problems/minimum-number-of-operations-to-make-array-empty/)
2+
3+
[English Version](/solution/2800-2899/2870.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20Empty/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>你可以对数组执行以下两种操作 <strong>任意次</strong>&nbsp;:</p>
10+
11+
<ul>
12+
<li>从数组中选择 <strong>两个</strong>&nbsp;值 <strong>相等</strong>&nbsp;的元素,并将它们从数组中 <strong>删除</strong>&nbsp;。</li>
13+
<li>从数组中选择 <strong>三个</strong>&nbsp;值 <strong>相等</strong>&nbsp;的元素,并将它们从数组中 <strong>删除</strong>&nbsp;。</li>
14+
</ul>
15+
16+
<p>请你返回使数组为空的 <strong>最少</strong>&nbsp;操作次数,如果无法达成,请返回 <code>-1</code>&nbsp;。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong class="example">示例 1:</strong></p>
21+
22+
<pre><strong>输入:</strong>nums = [2,3,3,2,2,4,2,3,4]
23+
<b>输出:</b>4
24+
<b>解释:</b>我们可以执行以下操作使数组为空:
25+
- 对下标为 0 和 3 的元素执行第一种操作,得到 nums = [3,3,2,4,2,3,4] 。
26+
- 对下标为 2 和 4 的元素执行第一种操作,得到 nums = [3,3,4,3,4] 。
27+
- 对下标为 0 ,1 和 3 的元素执行第二种操作,得到 nums = [4,4] 。
28+
- 对下标为 0 和 1 的元素执行第一种操作,得到 nums = [] 。
29+
至少需要 4 步操作使数组为空。
30+
</pre>
31+
32+
<p><strong class="example">示例 2:</strong></p>
33+
34+
<pre><b>输入:</b>nums = [2,1,2,2,3,3]
35+
<b>输出:</b>-1
36+
<b>解释:</b>无法使数组为空。
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
45+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
46+
</ul>
47+
48+
## 解法
49+
50+
<!-- 这里可写通用的实现逻辑 -->
51+
52+
<!-- tabs:start -->
53+
54+
### **Python3**
55+
56+
<!-- 这里可写当前语言的特殊实现逻辑 -->
57+
58+
```python
59+
60+
```
61+
62+
### **Java**
63+
64+
<!-- 这里可写当前语言的特殊实现逻辑 -->
65+
66+
```java
67+
class Solution {
68+
public int minOperations(int[] nums) {
69+
Map<Integer, Integer> count = new HashMap<>();
70+
for (int num : nums) {
71+
// count.put(num, count.getOrDefault(num, 0) + 1);
72+
count.merge(num, 1, Integer::sum);
73+
}
74+
int ans = 0;
75+
for (Integer c : count.values()) {
76+
if (c < 2) {
77+
return -1;
78+
}
79+
int r = c % 3;
80+
int d = c / 3;
81+
switch (r) {
82+
case (0) -> {
83+
ans += d;
84+
}
85+
default -> {
86+
ans += d + 1;
87+
}
88+
}
89+
}
90+
return ans;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
99+
```
100+
101+
### **Go**
102+
103+
```go
104+
105+
```
106+
107+
### **TypeScript**
108+
109+
```ts
110+
111+
```
112+
113+
### **...**
114+
115+
```
116+
117+
```
118+
119+
<!-- tabs:end -->

0 commit comments

Comments
 (0)