Skip to content

Commit cc2f42f

Browse files
committed
feat: add solutions to lc problems: No.2609~2611
* No.2609.Find the Longest Balanced Substring of a Binary String * No.2610.Convert an Array Into a 2D Array With Conditions * No.2611.Mice and Cheese
1 parent 1272d44 commit cc2f42f

File tree

10 files changed

+278
-3
lines changed

10 files changed

+278
-3
lines changed

solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class Solution {
204204
public:
205205
int findTheLongestBalancedSubstring(string s) {
206206
int zero = 0, one = 0;
207-
int ans = 0, n = s.size();
207+
int ans = 0;
208208
for (char& c : s) {
209209
if (c == '0') {
210210
if (one > 0) {
@@ -287,6 +287,54 @@ func min(a, b int) int {
287287
}
288288
```
289289

290+
### **TypeScript**
291+
292+
```ts
293+
function findTheLongestBalancedSubstring(s: string): number {
294+
const n = s.length;
295+
let ans = 0;
296+
const check = (i: number, j: number): boolean => {
297+
let cnt = 0;
298+
for (let k = i; k <= j; ++k) {
299+
if (s[k] === '1') {
300+
++cnt;
301+
} else if (cnt > 0) {
302+
return false;
303+
}
304+
}
305+
return cnt * 2 === j - i + 1;
306+
};
307+
for (let i = 0; i < n; ++i) {
308+
for (let j = i + 1; j < n; j += 2) {
309+
if (check(i, j)) {
310+
ans = Math.max(ans, j - i + 1);
311+
}
312+
}
313+
}
314+
return ans;
315+
}
316+
```
317+
318+
```ts
319+
function findTheLongestBalancedSubstring(s: string): number {
320+
let zero = 0;
321+
let one = 0;
322+
let ans = 0;
323+
for (const c of s) {
324+
if (c === '0') {
325+
if (one > 0) {
326+
zero = 0;
327+
one = 0;
328+
}
329+
++zero;
330+
} else {
331+
ans = Math.max(ans, 2 * Math.min(zero, ++one));
332+
}
333+
}
334+
return ans;
335+
}
336+
```
337+
290338
### **...**
291339

292340
```

solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README_EN.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class Solution {
175175
public:
176176
int findTheLongestBalancedSubstring(string s) {
177177
int zero = 0, one = 0;
178-
int ans = 0, n = s.size();
178+
int ans = 0;
179179
for (char& c : s) {
180180
if (c == '0') {
181181
if (one > 0) {
@@ -258,6 +258,54 @@ func min(a, b int) int {
258258
}
259259
```
260260

261+
### **TypeScript**
262+
263+
```ts
264+
function findTheLongestBalancedSubstring(s: string): number {
265+
const n = s.length;
266+
let ans = 0;
267+
const check = (i: number, j: number): boolean => {
268+
let cnt = 0;
269+
for (let k = i; k <= j; ++k) {
270+
if (s[k] === '1') {
271+
++cnt;
272+
} else if (cnt > 0) {
273+
return false;
274+
}
275+
}
276+
return cnt * 2 === j - i + 1;
277+
};
278+
for (let i = 0; i < n; ++i) {
279+
for (let j = i + 1; j < n; j += 2) {
280+
if (check(i, j)) {
281+
ans = Math.max(ans, j - i + 1);
282+
}
283+
}
284+
}
285+
return ans;
286+
}
287+
```
288+
289+
```ts
290+
function findTheLongestBalancedSubstring(s: string): number {
291+
let zero = 0;
292+
let one = 0;
293+
let ans = 0;
294+
for (const c of s) {
295+
if (c === '0') {
296+
if (one > 0) {
297+
zero = 0;
298+
one = 0;
299+
}
300+
++zero;
301+
} else {
302+
ans = Math.max(ans, 2 * Math.min(zero, ++one));
303+
}
304+
}
305+
return ans;
306+
}
307+
```
308+
261309
### **...**
262310

263311
```

solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution {
22
public:
33
int findTheLongestBalancedSubstring(string s) {
44
int zero = 0, one = 0;
5-
int ans = 0, n = s.size();
5+
int ans = 0;
66
for (char& c : s) {
77
if (c == '0') {
88
if (one > 0) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function findTheLongestBalancedSubstring(s: string): number {
2+
let zero = 0;
3+
let one = 0;
4+
let ans = 0;
5+
for (const c of s) {
6+
if (c === '0') {
7+
if (one > 0) {
8+
zero = 0;
9+
one = 0;
10+
}
11+
++zero;
12+
} else {
13+
ans = Math.max(ans, 2 * Math.min(zero, ++one));
14+
}
15+
}
16+
return ans;
17+
}

solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,28 @@ func findMatrix(nums []int) (ans [][]int) {
154154
}
155155
```
156156

157+
### **TypeScript**
158+
159+
```ts
160+
function findMatrix(nums: number[]): number[][] {
161+
const ans: number[][] = [];
162+
const n = nums.length;
163+
const cnt: number[] = new Array(n + 1).fill(0);
164+
for (const x of nums) {
165+
++cnt[x];
166+
}
167+
for (let x = 1; x <= n; ++x) {
168+
for (let j = 0; j < cnt[x]; ++j) {
169+
if (ans.length <= j) {
170+
ans.push([]);
171+
}
172+
ans[j].push(x);
173+
}
174+
}
175+
return ans;
176+
}
177+
```
178+
157179
### **...**
158180

159181
```

solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README_EN.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,28 @@ func findMatrix(nums []int) (ans [][]int) {
136136
}
137137
```
138138

139+
### **TypeScript**
140+
141+
```ts
142+
function findMatrix(nums: number[]): number[][] {
143+
const ans: number[][] = [];
144+
const n = nums.length;
145+
const cnt: number[] = new Array(n + 1).fill(0);
146+
for (const x of nums) {
147+
++cnt[x];
148+
}
149+
for (let x = 1; x <= n; ++x) {
150+
for (let j = 0; j < cnt[x]; ++j) {
151+
if (ans.length <= j) {
152+
ans.push([]);
153+
}
154+
ans[j].push(x);
155+
}
156+
}
157+
return ans;
158+
}
159+
```
160+
139161
### **...**
140162

141163
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function findMatrix(nums: number[]): number[][] {
2+
const ans: number[][] = [];
3+
const n = nums.length;
4+
const cnt: number[] = new Array(n + 1).fill(0);
5+
for (const x of nums) {
6+
++cnt[x];
7+
}
8+
for (let x = 1; x <= n; ++x) {
9+
for (let j = 0; j < cnt[x]; ++j) {
10+
if (ans.length <= j) {
11+
ans.push([]);
12+
}
13+
ans[j].push(x);
14+
}
15+
}
16+
return ans;
17+
}

solution/2600-2699/2611.Mice and Cheese/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,48 @@ func miceAndCheese(reward1 []int, reward2 []int, k int) (ans int) {
207207
}
208208
```
209209

210+
### **TypeScript**
211+
212+
```ts
213+
function miceAndCheese(
214+
reward1: number[],
215+
reward2: number[],
216+
k: number,
217+
): number {
218+
const n = reward1.length;
219+
const idx = Array.from({ length: n }, (_, i) => i);
220+
idx.sort((i, j) => reward1[j] - reward2[j] - (reward1[i] - reward2[i]));
221+
let ans = 0;
222+
for (let i = 0; i < k; ++i) {
223+
ans += reward1[idx[i]];
224+
}
225+
for (let i = k; i < n; ++i) {
226+
ans += reward2[idx[i]];
227+
}
228+
return ans;
229+
}
230+
```
231+
232+
```ts
233+
function miceAndCheese(
234+
reward1: number[],
235+
reward2: number[],
236+
k: number,
237+
): number {
238+
const n = reward1.length;
239+
let ans = 0;
240+
for (let i = 0; i < n; ++i) {
241+
ans += reward2[i];
242+
reward1[i] -= reward2[i];
243+
}
244+
reward1.sort((a, b) => b - a);
245+
for (let i = 0; i < k; ++i) {
246+
ans += reward1[i];
247+
}
248+
return ans;
249+
}
250+
```
251+
210252
### **...**
211253

212254
```

solution/2600-2699/2611.Mice and Cheese/README_EN.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,48 @@ func miceAndCheese(reward1 []int, reward2 []int, k int) (ans int) {
189189
}
190190
```
191191

192+
### **TypeScript**
193+
194+
```ts
195+
function miceAndCheese(
196+
reward1: number[],
197+
reward2: number[],
198+
k: number,
199+
): number {
200+
const n = reward1.length;
201+
const idx = Array.from({ length: n }, (_, i) => i);
202+
idx.sort((i, j) => reward1[j] - reward2[j] - (reward1[i] - reward2[i]));
203+
let ans = 0;
204+
for (let i = 0; i < k; ++i) {
205+
ans += reward1[idx[i]];
206+
}
207+
for (let i = k; i < n; ++i) {
208+
ans += reward2[idx[i]];
209+
}
210+
return ans;
211+
}
212+
```
213+
214+
```ts
215+
function miceAndCheese(
216+
reward1: number[],
217+
reward2: number[],
218+
k: number,
219+
): number {
220+
const n = reward1.length;
221+
let ans = 0;
222+
for (let i = 0; i < n; ++i) {
223+
ans += reward2[i];
224+
reward1[i] -= reward2[i];
225+
}
226+
reward1.sort((a, b) => b - a);
227+
for (let i = 0; i < k; ++i) {
228+
ans += reward1[i];
229+
}
230+
return ans;
231+
}
232+
```
233+
192234
### **...**
193235

194236
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function miceAndCheese(
2+
reward1: number[],
3+
reward2: number[],
4+
k: number,
5+
): number {
6+
const n = reward1.length;
7+
const idx = Array.from({ length: n }, (_, i) => i);
8+
idx.sort((i, j) => reward1[j] - reward2[j] - (reward1[i] - reward2[i]));
9+
let ans = 0;
10+
for (let i = 0; i < k; ++i) {
11+
ans += reward1[idx[i]];
12+
}
13+
for (let i = k; i < n; ++i) {
14+
ans += reward2[idx[i]];
15+
}
16+
return ans;
17+
}

0 commit comments

Comments
 (0)