Skip to content

feat: add solutions to lc problems: No.1991,1985 #3990

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 1 commit into from
Jan 24, 2025
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
Expand Up @@ -146,31 +146,6 @@ class Solution {
}
```

#### Java

```java
class Solution {
public int minimizeTheDifference(int[][] mat, int target) {
Set<Integer> f = new HashSet<>();
f.add(0);
for (var row : mat) {
Set<Integer> g = new HashSet<>();
for (int a : f) {
for (int b : row) {
g.add(a + b);
}
}
f = g;
}
int ans = 1 << 30;
for (int v : f) {
ans = Math.min(ans, Math.abs(v - target));
}
return ans;
}
}
```

#### C++

```cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,36 +110,6 @@ class Solution:

#### Java

```java
class Solution {
public int minimizeTheDifference(int[][] mat, int target) {
boolean[] f = {true};
for (var row : mat) {
int mx = 0;
for (int x : row) {
mx = Math.max(mx, x);
}
boolean[] g = new boolean[f.length + mx];
for (int x : row) {
for (int j = x; j < f.length + x; ++j) {
g[j] |= f[j - x];
}
}
f = g;
}
int ans = 1 << 30;
for (int j = 0; j < f.length; ++j) {
if (f[j]) {
ans = Math.min(ans, Math.abs(j - target));
}
}
return ans;
}
}
```

#### Java

```java
class Solution {
public int minimizeTheDifference(int[][] mat, int target) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
class Solution {
public int minimizeTheDifference(int[][] mat, int target) {
Set<Integer> f = new HashSet<>();
f.add(0);
boolean[] f = {true};
for (var row : mat) {
Set<Integer> g = new HashSet<>();
for (int a : f) {
for (int b : row) {
g.add(a + b);
int mx = 0;
for (int x : row) {
mx = Math.max(mx, x);
}
boolean[] g = new boolean[f.length + mx];
for (int x : row) {
for (int j = x; j < f.length + x; ++j) {
g[j] |= f[j - x];
}
}
f = g;
}
int ans = 1 << 30;
for (int v : f) {
ans = Math.min(ans, Math.abs(v - target));
for (int j = 0; j < f.length; ++j) {
if (f[j]) {
ans = Math.min(ans, Math.abs(j - target));
}
}
return ans;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ nums 中的数字按非递减顺序排列为 ["0","0"]

<!-- solution:start -->

### 方法一:自定义排序
### 方法一:排序或快速选择

我们可以将 $\textit{nums}$ 数组中的字符串按照整数从大到小排序,然后取第 $k$ 个元素即可。也可以使用快速选择算法,找到第 $k$ 大的整数。

时间复杂度 $O(n \times \log n)$ 或 $O(n)$,其中 $n$ 是 $\textit{nums}$ 数组的长度。空间复杂度 $O(\log n)$ 或 $O(1)$。

<!-- tabs:start -->

Expand All @@ -87,13 +91,7 @@ nums 中的数字按非递减顺序排列为 ["0","0"]
```python
class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
def cmp(a, b):
if len(a) != len(b):
return len(b) - len(a)
return 1 if b > a else -1

nums.sort(key=cmp_to_key(cmp))
return nums[k - 1]
return nlargest(k, nums, key=lambda x: int(x))[k - 1]
```

#### Java
Expand All @@ -114,8 +112,9 @@ class Solution {
class Solution {
public:
string kthLargestNumber(vector<string>& nums, int k) {
auto cmp = [](const string& a, const string& b) { return a.size() == b.size() ? a > b : a.size() > b.size(); };
sort(nums.begin(), nums.end(), cmp);
nth_element(nums.begin(), nums.begin() + k - 1, nums.end(), [](const string& a, const string& b) {
return a.size() == b.size() ? a > b : a.size() > b.size();
});
return nums[k - 1];
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ The 2<sup>nd</sup> largest integer in nums is &quot;0&quot;.

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting or Quickselect

We can sort the strings in the $\textit{nums}$ array in descending order as integers, and then take the $k$-th element. Alternatively, we can use the quickselect algorithm to find the $k$-th largest integer.

The time complexity is $O(n \times \log n)$ or $O(n)$, where $n$ is the length of the $\textit{nums}$ array. The space complexity is $O(\log n)$ or $O(1)$.

<!-- tabs:start -->

Expand All @@ -85,13 +89,7 @@ The 2<sup>nd</sup> largest integer in nums is &quot;0&quot;.
```python
class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
def cmp(a, b):
if len(a) != len(b):
return len(b) - len(a)
return 1 if b > a else -1

nums.sort(key=cmp_to_key(cmp))
return nums[k - 1]
return nlargest(k, nums, key=lambda x: int(x))[k - 1]
```

#### Java
Expand All @@ -112,8 +110,9 @@ class Solution {
class Solution {
public:
string kthLargestNumber(vector<string>& nums, int k) {
auto cmp = [](const string& a, const string& b) { return a.size() == b.size() ? a > b : a.size() > b.size(); };
sort(nums.begin(), nums.end(), cmp);
nth_element(nums.begin(), nums.begin() + k - 1, nums.end(), [](const string& a, const string& b) {
return a.size() == b.size() ? a > b : a.size() > b.size();
});
return nums[k - 1];
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class Solution {
public:
string kthLargestNumber(vector<string>& nums, int k) {
auto cmp = [](const string& a, const string& b) { return a.size() == b.size() ? a > b : a.size() > b.size(); };
sort(nums.begin(), nums.end(), cmp);
nth_element(nums.begin(), nums.begin() + k - 1, nums.end(), [](const string& a, const string& b) {
return a.size() == b.size() ? a > b : a.size() > b.size();
});
return nums[k - 1];
}
};
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
class Solution:
def kthLargestNumber(self, nums: List[str], k: int) -> str:
def cmp(a, b):
if len(a) != len(b):
return len(b) - len(a)
return 1 if b > a else -1

nums.sort(key=cmp_to_key(cmp))
return nums[k - 1]
return nlargest(k, nums, key=lambda x: int(x))[k - 1]
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,17 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: State Compression Dynamic Programming + Subset Enumeration

We note that $n$ does not exceed $14$, so we can consider using state compression dynamic programming to solve this problem.

We use a binary number $i$ of length $n$ to represent the current task state, where the $j$-th bit of $i$ is $1$ if and only if the $j$-th task is completed. We use $f[i]$ to represent the minimum number of work sessions needed to complete all tasks with state $i$.

We can enumerate all subsets $j$ of $i$, where each bit of the binary representation of $j$ is a subset of the corresponding bit of the binary representation of $i$, i.e., $j \subseteq i$. If the tasks corresponding to $j$ can be completed in one work session, then we can update $f[i]$ using $f[i \oplus j] + 1$, where $i \oplus j$ represents the bitwise XOR of $i$ and $j$.

The final answer is $f[2^n - 1]$.

The time complexity is $O(n \times 3^n)$, and the space complexity is $O(2^n)$. Here, $n$ is the number of tasks.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ The unique good subsequences are &quot;1&quot; and &quot;11&quot;.</pre>
<pre>
<strong>Input:</strong> binary = &quot;101&quot;
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good subsequences of binary are [&quot;1&quot;, &quot;0&quot;, &quot;1&quot;, &quot;10&quot;, &quot;11&quot;, &quot;101&quot;].
<strong>Explanation:</strong> The good subsequences of binary are [&quot;1&quot;, &quot;0&quot;, &quot;1&quot;, &quot;10&quot;, &quot;11&quot;, &quot;101&quot;].
The unique good subsequences are &quot;0&quot;, &quot;1&quot;, &quot;10&quot;, &quot;11&quot;, and &quot;101&quot;.
</pre>

Expand All @@ -72,7 +72,22 @@ The unique good subsequences are &quot;0&quot;, &quot;1&quot;, &quot;10&quot;, &

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming

We define $f$ as the number of distinct good subsequences ending with $1$, and $g$ as the number of distinct good subsequences ending with $0$ and starting with $1$. Initially, $f = g = 0$.

For a binary string, we can traverse each bit from left to right. Suppose the current bit is $c$:

- If $c = 0$, we can append $c$ to the $f$ and $g$ distinct good subsequences, so update $g = (g + f) \bmod (10^9 + 7)$;
- If $c = 1$, we can append $c$ to the $f$ and $g$ distinct good subsequences, and also append $c$ alone, so update $f = (f + g + 1) \bmod (10^9 + 7)$.

If the string contains $0$, the final answer is $f + g + 1$, otherwise the answer is $f + g$.

The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.

Similar problems:

- [940. Distinct Subsequences II](https://github.com/doocs/leetcode/blob/main/solution/0900-0999/0940.Distinct%20Subsequences%20II/README_EN.md)

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ tags:

我们可以用两个指针 $i$ 和 $j$ 指向鬼和非鬼的人,初始时 $i=0$, $j=0$。

然后我们从左到右遍历数组,当遇到鬼时,即 $team[i]=1$ 时,如果此时 $j \lt n$ 并且 $team[j]=1$ 或者 $i - j \gt dist$,则指针 $j$ 循环右移,也即是说,我们要找到第一个不是鬼的人,且 $i$ 和 $j$ 之间的距离不超过 $dist$。如果找到了这样的人,则将指针 $j$ 右移一位,表示我们已经抓住了这个人,同时答案加一。继续遍历数组,直到遍历完整个数组。
然后我们从左到右遍历数组,当遇到鬼时,即 $team[i]=1$ 时,如果此时 $j \lt n$ 并且 $\textit{team}[j]=1$ 或者 $i - j \gt \textit{dist}$,则指针 $j$ 循环右移,也即是说,我们要找到第一个不是鬼的人,且 $i$ 和 $j$ 之间的距离不超过 $\textit{dist}$。如果找到了这样的人,则将指针 $j$ 右移一位,表示我们已经抓住了这个人,同时答案加一。继续遍历数组,直到遍历完整个数组。

最后返回答案即可。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{team}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ There are no people who are not &quot;it&quot; to catch.

<!-- solution:start -->

### Solution 1
### Solution 1: Two Pointers

We can use two pointers $i$ and $j$ to point to the ghost and non-ghost people, initially $i=0$, $j=0$.

Then we traverse the array from left to right. When we encounter a ghost, i.e., $team[i]=1$, if $j \lt n$ and $\textit{team}[j]=1$ or $i - j \gt \textit{dist}$, then move pointer $j$ to the right in a loop. This means we need to find the first non-ghost person such that the distance between $i$ and $j$ does not exceed $\textit{dist}$. If such a person is found, move pointer $j$ one step to the right, indicating that we have caught this person, and increment the answer by one. Continue traversing the array until the entire array is processed.

Finally, return the answer.

The time complexity is $O(n)$, where $n$ is the length of the array $\textit{team}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
Loading
Loading