Skip to content

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 4 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>请你返回收集元素&nbsp;<code>1, 2, ..., k</code>&nbsp;需要的&nbsp;<strong>最少操作次数</strong>&nbsp;。</p>

<p>&nbsp;</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>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
<li><code>1 &lt;= nums[i] &lt;= nums.length</code></li>
<li><code>1 &lt;= k &lt;= nums.length</code></li>
<li>输入保证你可以收集到元素&nbsp;<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 -->
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>&nbsp;</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>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
<li><code>1 &lt;= nums[i] &lt;= nums.length</code></li>
<li><code>1 &lt;= k &lt;= 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 -->
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;
}
}
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>&nbsp;:</p>

<ul>
<li>从数组中选择 <strong>两个</strong>&nbsp;值 <strong>相等</strong>&nbsp;的元素,并将它们从数组中 <strong>删除</strong>&nbsp;。</li>
<li>从数组中选择 <strong>三个</strong>&nbsp;值 <strong>相等</strong>&nbsp;的元素,并将它们从数组中 <strong>删除</strong>&nbsp;。</li>
</ul>

<p>请你返回使数组为空的 <strong>最少</strong>&nbsp;操作次数,如果无法达成,请返回 <code>-1</code>&nbsp;。</p>

<p>&nbsp;</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>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 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 -->
Loading