Skip to content

Commit 12e8bc7

Browse files
authored
feat: add solutions to lc problems: No.1980,1981 (doocs#2448)
* No.1980.Find Unique Binary String * No.1981.Minimize the Difference Between Target and Chosen Elements
1 parent d8d7eb3 commit 12e8bc7

File tree

5 files changed

+123
-71
lines changed

5 files changed

+123
-71
lines changed

solution/1900-1999/1980.Find Unique Binary String/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
然后我们从 $0$ 开始枚举长度为 $n$ 的二进制字符串中 `'1'` 出现的次数 $i$,如果 $mask$ 的第 $i$ 位为 $0$,则说明长度为 $n$ 的二进制字符串中 `'1'` 出现次数为 $i$ 的字符串不存在,我们可以将这个字符串作为答案返回。
5959

60-
时间复杂度 $O(L)$,空间复杂度 $O(1)$。其中 $L$ 为 `nums` 中字符串的总长度。
60+
时间复杂度 $O(L)$,其中 $L$ 为 `nums` 中字符串的总长度。空间复杂度 $O(1)$
6161

6262
<!-- tabs:start -->
6363

@@ -127,6 +127,21 @@ func findDifferentBinaryString(nums []string) string {
127127
}
128128
```
129129

130+
```ts
131+
function findDifferentBinaryString(nums: string[]): string {
132+
let mask = 0;
133+
for (let x of nums) {
134+
const cnt = x.split('').filter(c => c === '1').length;
135+
mask |= 1 << cnt;
136+
}
137+
for (let i = 0; ; ++i) {
138+
if (((mask >> i) & 1) === 0) {
139+
return '1'.repeat(i) + '0'.repeat(nums.length - i);
140+
}
141+
}
142+
}
143+
```
144+
130145
```cs
131146
public class Solution {
132147
public string FindDifferentBinaryString(string[] nums) {

solution/1900-1999/1980.Find Unique Binary String/README_EN.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@
4646

4747
## Solutions
4848

49-
### Solution 1
49+
### Solution 1: Counting + Enumeration
50+
51+
Since the number of occurrences of '1' in a binary string of length $n$ can be $0, 1, 2, \cdots, n$ (there are $n + 1$ possibilities), we can certainly find a new binary string that has a different number of '1's from every string in `nums`.
52+
53+
We can use an integer $mask$ to record the occurrence of '1' in all strings, i.e., the $i$-th bit of $mask$ is $1$ indicates that there is a string of length $n$ in which '1' appears $i$ times, otherwise it does not exist.
54+
55+
Then we start to enumerate the number of times '1' appears in a binary string of length $n$ from $0$. If the $i$-th bit of $mask$ is $0$, it means that there is no string of length $n$ in which '1' appears $i$ times. We can return this string as the answer.
56+
57+
The time complexity is $O(L)$, where $L$ is the total length of the strings in `nums`. The space complexity is $O(1)$.
5058

5159
<!-- tabs:start -->
5260

@@ -116,6 +124,21 @@ func findDifferentBinaryString(nums []string) string {
116124
}
117125
```
118126

127+
```ts
128+
function findDifferentBinaryString(nums: string[]): string {
129+
let mask = 0;
130+
for (let x of nums) {
131+
const cnt = x.split('').filter(c => c === '1').length;
132+
mask |= 1 << cnt;
133+
}
134+
for (let i = 0; ; ++i) {
135+
if (((mask >> i) & 1) === 0) {
136+
return '1'.repeat(i) + '0'.repeat(nums.length - i);
137+
}
138+
}
139+
}
140+
```
141+
119142
```cs
120143
public class Solution {
121144
public string FindDifferentBinaryString(string[] nums) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function findDifferentBinaryString(nums: string[]): string {
2+
let mask = 0;
3+
for (let x of nums) {
4+
const cnt = x.split('').filter(c => c === '1').length;
5+
mask |= 1 << cnt;
6+
}
7+
for (let i = 0; ; ++i) {
8+
if (((mask >> i) & 1) === 0) {
9+
return '1'.repeat(i) + '0'.repeat(nums.length - i);
10+
}
11+
}
12+
}

solution/1900-1999/1981.Minimize the Difference Between Target and Chosen Elements/README.md

+28-34
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,34 @@ class Solution:
9898
return min(abs(v - target) for v in f)
9999
```
100100

101+
```java
102+
class Solution {
103+
public int minimizeTheDifference(int[][] mat, int target) {
104+
boolean[] f = {true};
105+
for (var row : mat) {
106+
int mx = 0;
107+
for (int x : row) {
108+
mx = Math.max(mx, x);
109+
}
110+
boolean[] g = new boolean[f.length + mx];
111+
for (int x : row) {
112+
for (int j = x; j < f.length + x; ++j) {
113+
g[j] |= f[j - x];
114+
}
115+
}
116+
f = g;
117+
}
118+
int ans = 1 << 30;
119+
for (int j = 0; j < f.length; ++j) {
120+
if (f[j]) {
121+
ans = Math.min(ans, Math.abs(j - target));
122+
}
123+
}
124+
return ans;
125+
}
126+
}
127+
```
128+
101129
```java
102130
class Solution {
103131
public int minimizeTheDifference(int[][] mat, int target) {
@@ -179,38 +207,4 @@ func abs(x int) int {
179207

180208
<!-- tabs:end -->
181209

182-
### 方法二
183-
184-
<!-- tabs:start -->
185-
186-
```java
187-
class Solution {
188-
public int minimizeTheDifference(int[][] mat, int target) {
189-
boolean[] f = {true};
190-
for (var row : mat) {
191-
int mx = 0;
192-
for (int x : row) {
193-
mx = Math.max(mx, x);
194-
}
195-
boolean[] g = new boolean[f.length + mx];
196-
for (int x : row) {
197-
for (int j = x; j < f.length + x; ++j) {
198-
g[j] |= f[j - x];
199-
}
200-
}
201-
f = g;
202-
}
203-
int ans = 1 << 30;
204-
for (int j = 0; j < f.length; ++j) {
205-
if (f[j]) {
206-
ans = Math.min(ans, Math.abs(j - target));
207-
}
208-
}
209-
return ans;
210-
}
211-
}
212-
```
213-
214-
<!-- tabs:end -->
215-
216210
<!-- end -->

solution/1900-1999/1981.Minimize the Difference Between Target and Chosen Elements/README_EN.md

+43-35
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,21 @@ The absolute difference is 1.
6161

6262
## Solutions
6363

64-
### Solution 1
64+
### Solution 1: Dynamic Programming (Grouped Knapsack)
65+
66+
Let $f[i][j]$ represent whether it is possible to select elements from the first $i$ rows with a sum of $j$. Then we have the state transition equation:
67+
68+
$$
69+
f[i][j] = \begin{cases} 1 & \text{if there exists } x \in row[i] \text{ such that } f[i - 1][j - x] = 1 \\ 0 & \text{otherwise} \end{cases}
70+
$$
71+
72+
where $row[i]$ represents the set of elements in the $i$-th row.
73+
74+
Since $f[i][j]$ is only related to $f[i - 1][j]$, we can use a rolling array to optimize the space complexity.
75+
76+
Finally, we traverse the $f$ array to find the smallest absolute difference.
77+
78+
The time complexity is $O(m^2 \times n \times C)$ and the space complexity is $O(m \times C)$. Here, $m$ and $n$ are the number of rows and columns of the matrix, respectively, and $C$ is the maximum value of the matrix elements.
6579

6680
<!-- tabs:start -->
6781

@@ -74,6 +88,34 @@ class Solution:
7488
return min(abs(v - target) for v in f)
7589
```
7690

91+
```java
92+
class Solution {
93+
public int minimizeTheDifference(int[][] mat, int target) {
94+
boolean[] f = {true};
95+
for (var row : mat) {
96+
int mx = 0;
97+
for (int x : row) {
98+
mx = Math.max(mx, x);
99+
}
100+
boolean[] g = new boolean[f.length + mx];
101+
for (int x : row) {
102+
for (int j = x; j < f.length + x; ++j) {
103+
g[j] |= f[j - x];
104+
}
105+
}
106+
f = g;
107+
}
108+
int ans = 1 << 30;
109+
for (int j = 0; j < f.length; ++j) {
110+
if (f[j]) {
111+
ans = Math.min(ans, Math.abs(j - target));
112+
}
113+
}
114+
return ans;
115+
}
116+
}
117+
```
118+
77119
```java
78120
class Solution {
79121
public int minimizeTheDifference(int[][] mat, int target) {
@@ -155,38 +197,4 @@ func abs(x int) int {
155197

156198
<!-- tabs:end -->
157199

158-
### Solution 2
159-
160-
<!-- tabs:start -->
161-
162-
```java
163-
class Solution {
164-
public int minimizeTheDifference(int[][] mat, int target) {
165-
boolean[] f = {true};
166-
for (var row : mat) {
167-
int mx = 0;
168-
for (int x : row) {
169-
mx = Math.max(mx, x);
170-
}
171-
boolean[] g = new boolean[f.length + mx];
172-
for (int x : row) {
173-
for (int j = x; j < f.length + x; ++j) {
174-
g[j] |= f[j - x];
175-
}
176-
}
177-
f = g;
178-
}
179-
int ans = 1 << 30;
180-
for (int j = 0; j < f.length; ++j) {
181-
if (f[j]) {
182-
ans = Math.min(ans, Math.abs(j - target));
183-
}
184-
}
185-
return ans;
186-
}
187-
}
188-
```
189-
190-
<!-- tabs:end -->
191-
192200
<!-- end -->

0 commit comments

Comments
 (0)