-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat: add Java solutions to lc problems: No.2869~2872 #1728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
solution/2800-2899/2869.Minimum Operations to Collect Elements/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# [2869. Minimum Operations to Collect Elements](https://leetcode.cn/problems/minimum-operations-to-collect-elements/) | ||
|
||
[English Version](/solution/2800-2899/2869.Minimum%20Operations%20to%20Collect%20Elements/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- 这里写题目描述 --> | ||
|
||
<p>一次操作中,你可以将数组的最后一个元素删除,将该元素添加到一个集合中。</p> | ||
|
||
<p>请你返回收集元素 <code>1, 2, ..., k</code> 需要的 <strong>最少操作次数</strong> 。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong class="example">示例 1:</strong></p> | ||
|
||
<pre><b>输入:</b>nums = [3,1,5,4,2], k = 2 | ||
<b>输出:</b>4 | ||
<b>解释:</b>4 次操作后,集合中的元素依次添加了 2 ,4 ,5 和 1 。此时集合中包含元素 1 和 2 ,所以答案为 4 。 | ||
</pre> | ||
|
||
<p><strong class="example">示例 2:</strong></p> | ||
|
||
<pre><b>输入:</b>nums = [3,1,5,4,2], k = 5 | ||
<b>输出:</b>5 | ||
<b>解释:</b>5 次操作后,集合中的元素依次添加了 2 ,4 ,5 ,1 和 3 。此时集合中包含元素 1 到 5 ,所以答案为 5 。 | ||
</pre> | ||
|
||
<p><strong class="example">示例 3:</strong></p> | ||
|
||
<pre><b>输入:</b>nums = [3,2,5,3,1], k = 3 | ||
<b>输出:</b>4 | ||
<b>解释:</b>4 次操作后,集合中的元素依次添加了 1 ,3 ,5 和 2 。此时集合中包含元素 1 到 3 ,所以答案为 4 。 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 50</code></li> | ||
<li><code>1 <= nums[i] <= nums.length</code></li> | ||
<li><code>1 <= k <= nums.length</code></li> | ||
<li>输入保证你可以收集到元素 <code>1, 2, ..., k</code> 。</li> | ||
</ul> | ||
|
||
## 解法 | ||
|
||
<!-- 这里可写通用的实现逻辑 --> | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```python | ||
|
||
``` | ||
|
||
### **Java** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```java | ||
class Solution { | ||
public int minOperations(List<Integer> nums, int k) { | ||
boolean[] isAdded = new boolean[k]; | ||
int n = nums.size(); | ||
int count = 0; | ||
for (int i = n - 1; i >= 0; i--) { | ||
if (nums.get(i) > k || isAdded[nums.get(i) - 1]) { | ||
continue; | ||
} | ||
isAdded[nums.get(i) - 1] = true; | ||
count++; | ||
if (count == k) { | ||
return n - i; | ||
} | ||
} | ||
return n; | ||
} | ||
} | ||
|
||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
|
||
``` | ||
|
||
### **TypeScript** | ||
|
||
```ts | ||
|
||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
|
||
``` | ||
|
||
<!-- tabs:end --> |
101 changes: 101 additions & 0 deletions
solution/2800-2899/2869.Minimum Operations to Collect Elements/README_EN.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# [2869. Minimum Operations to Collect Elements](https://leetcode.com/problems/minimum-operations-to-collect-elements/) | ||
|
||
[中文文档](/solution/2800-2899/2869.Minimum%20Operations%20to%20Collect%20Elements/README.md) | ||
|
||
## Description | ||
|
||
<p>In one operation, you can remove the last element of the array and add it to your collection.</p> | ||
|
||
<p>Return <em>the <strong>minimum number of operations</strong> needed to collect elements</em> <code>1, 2, ..., k</code>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums = [3,1,5,4,2], k = 2 | ||
<strong>Output:</strong> 4 | ||
<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. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums = [3,1,5,4,2], k = 5 | ||
<strong>Output:</strong> 5 | ||
<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. | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre><strong>Input:</strong> nums = [3,2,5,3,1], k = 3 | ||
<strong>Output:</strong> 4 | ||
<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. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 50</code></li> | ||
<li><code>1 <= nums[i] <= nums.length</code></li> | ||
<li><code>1 <= k <= nums.length</code></li> | ||
<li>The input is generated such that you can collect elements <code>1, 2, ..., k</code>.</li> | ||
</ul> | ||
|
||
## Solutions | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
```python | ||
|
||
``` | ||
|
||
### **Java** | ||
|
||
```java | ||
class Solution { | ||
public int minOperations(List<Integer> nums, int k) { | ||
boolean[] isAdded = new boolean[k]; | ||
int n = nums.size(); | ||
int count = 0; | ||
for (int i = n - 1; i >= 0; i--) { | ||
if (nums.get(i) > k || isAdded[nums.get(i) - 1]) { | ||
continue; | ||
} | ||
isAdded[nums.get(i) - 1] = true; | ||
count++; | ||
if (count == k) { | ||
return n - i; | ||
} | ||
} | ||
return n; | ||
} | ||
} | ||
|
||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
|
||
``` | ||
|
||
### **TypeScript** | ||
|
||
```ts | ||
|
||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
|
||
``` | ||
|
||
<!-- tabs:end --> |
18 changes: 18 additions & 0 deletions
solution/2800-2899/2869.Minimum Operations to Collect Elements/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution { | ||
public int minOperations(List<Integer> nums, int k) { | ||
boolean[] isAdded = new boolean[k]; | ||
int n = nums.size(); | ||
int count = 0; | ||
for (int i = n - 1; i >= 0; i--) { | ||
if (nums.get(i) > k || isAdded[nums.get(i) - 1]) { | ||
continue; | ||
} | ||
isAdded[nums.get(i) - 1] = true; | ||
count++; | ||
if (count == k) { | ||
return n - i; | ||
} | ||
} | ||
return n; | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
solution/2800-2899/2870.Minimum Number of Operations to Make Array Empty/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# [2870. Minimum Number of Operations to Make Array Empty](https://leetcode.cn/problems/minimum-number-of-operations-to-make-array-empty/) | ||
|
||
[English Version](/solution/2800-2899/2870.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20Empty/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- 这里写题目描述 --> | ||
|
||
<p>你可以对数组执行以下两种操作 <strong>任意次</strong> :</p> | ||
|
||
<ul> | ||
<li>从数组中选择 <strong>两个</strong> 值 <strong>相等</strong> 的元素,并将它们从数组中 <strong>删除</strong> 。</li> | ||
<li>从数组中选择 <strong>三个</strong> 值 <strong>相等</strong> 的元素,并将它们从数组中 <strong>删除</strong> 。</li> | ||
</ul> | ||
|
||
<p>请你返回使数组为空的 <strong>最少</strong> 操作次数,如果无法达成,请返回 <code>-1</code> 。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong class="example">示例 1:</strong></p> | ||
|
||
<pre><strong>输入:</strong>nums = [2,3,3,2,2,4,2,3,4] | ||
<b>输出:</b>4 | ||
<b>解释:</b>我们可以执行以下操作使数组为空: | ||
- 对下标为 0 和 3 的元素执行第一种操作,得到 nums = [3,3,2,4,2,3,4] 。 | ||
- 对下标为 2 和 4 的元素执行第一种操作,得到 nums = [3,3,4,3,4] 。 | ||
- 对下标为 0 ,1 和 3 的元素执行第二种操作,得到 nums = [4,4] 。 | ||
- 对下标为 0 和 1 的元素执行第一种操作,得到 nums = [] 。 | ||
至少需要 4 步操作使数组为空。 | ||
</pre> | ||
|
||
<p><strong class="example">示例 2:</strong></p> | ||
|
||
<pre><b>输入:</b>nums = [2,1,2,2,3,3] | ||
<b>输出:</b>-1 | ||
<b>解释:</b>无法使数组为空。 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li> | ||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li> | ||
</ul> | ||
|
||
## 解法 | ||
|
||
<!-- 这里可写通用的实现逻辑 --> | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```python | ||
|
||
``` | ||
|
||
### **Java** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```java | ||
class Solution { | ||
public int minOperations(int[] nums) { | ||
Map<Integer, Integer> count = new HashMap<>(); | ||
for (int num : nums) { | ||
// count.put(num, count.getOrDefault(num, 0) + 1); | ||
count.merge(num, 1, Integer::sum); | ||
} | ||
int ans = 0; | ||
for (Integer c : count.values()) { | ||
if (c < 2) { | ||
return -1; | ||
} | ||
int r = c % 3; | ||
int d = c / 3; | ||
switch (r) { | ||
case (0) -> { | ||
ans += d; | ||
} | ||
default -> { | ||
ans += d + 1; | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
} | ||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
|
||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
|
||
``` | ||
|
||
### **TypeScript** | ||
|
||
```ts | ||
|
||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
|
||
``` | ||
|
||
<!-- tabs:end --> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.