Skip to content

Commit 517af0d

Browse files
committed
Add leetcode contest 2024-07-20 - 2/4
1 parent 509045b commit 517af0d

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ zx _genReadme.mjs
3737
| [`100289-minimum-substring-partition-of-equal-character-frequency`](./challenges/leetcode-100289-minimum-substring-partition-of-equal-character-frequency) | leetcode |
3838
| [`100299-check-if-grid-satisfies-conditions`](./challenges/leetcode-100299-check-if-grid-satisfies-conditions) | leetcode |
3939
| [`100302-maximum-points-inside-the-square`](./challenges/leetcode-100302-maximum-points-inside-the-square) | leetcode |
40+
| [`100330-minimum-length-of-string-after-operations`](./challenges/leetcode-100330-minimum-length-of-string-after-operations) | leetcode |
41+
| [`100375-find-the-winning-player-in-coin-game`](./challenges/leetcode-100375-find-the-winning-player-in-coin-game) | leetcode |
4042
| [`1d-spreadsheet`](./challenges/codingame-1d-spreadsheet) | codingame |
4143
| [`2481-minimum-cuts-to-divide-a-circle`](./challenges/leetcode-2481-minimum-cuts-to-divide-a-circle) | leetcode |
4244
| [`2482-difference-between-ones-and-zeros-in-row-and-column`](./challenges/leetcode-2482-difference-between-ones-and-zeros-in-row-and-column) | leetcode |
@@ -149,6 +151,8 @@ ___
149151
| [`100289-minimum-substring-partition-of-equal-character-frequency`](./challenges/leetcode-100289-minimum-substring-partition-of-equal-character-frequency) | Classic |
150152
| [`100299-check-if-grid-satisfies-conditions`](./challenges/leetcode-100299-check-if-grid-satisfies-conditions) | Classic |
151153
| [`100302-maximum-points-inside-the-square`](./challenges/leetcode-100302-maximum-points-inside-the-square) | Classic |
154+
| [`100330-minimum-length-of-string-after-operations`](./challenges/leetcode-100330-minimum-length-of-string-after-operations) | Classic |
155+
| [`100375-find-the-winning-player-in-coin-game`](./challenges/leetcode-100375-find-the-winning-player-in-coin-game) | Classic |
152156
| [`2481-minimum-cuts-to-divide-a-circle`](./challenges/leetcode-2481-minimum-cuts-to-divide-a-circle) | Classic |
153157
| [`2482-difference-between-ones-and-zeros-in-row-and-column`](./challenges/leetcode-2482-difference-between-ones-and-zeros-in-row-and-column) | Classic |
154158
| [`2483-minimum-penalty-for-a-shop`](./challenges/leetcode-2483-minimum-penalty-for-a-shop) | Classic |
@@ -186,6 +190,8 @@ ___
186190
| [`100289-minimum-substring-partition-of-equal-character-frequency`](./challenges/leetcode-100289-minimum-substring-partition-of-equal-character-frequency) | leetcode | Classic |
187191
| [`100299-check-if-grid-satisfies-conditions`](./challenges/leetcode-100299-check-if-grid-satisfies-conditions) | leetcode | Classic |
188192
| [`100302-maximum-points-inside-the-square`](./challenges/leetcode-100302-maximum-points-inside-the-square) | leetcode | Classic |
193+
| [`100330-minimum-length-of-string-after-operations`](./challenges/leetcode-100330-minimum-length-of-string-after-operations) | leetcode | Classic |
194+
| [`100375-find-the-winning-player-in-coin-game`](./challenges/leetcode-100375-find-the-winning-player-in-coin-game) | leetcode | Classic |
189195
| [`1d-spreadsheet`](./challenges/codingame-1d-spreadsheet) | codingame | Classic |
190196
| [`2481-minimum-cuts-to-divide-a-circle`](./challenges/leetcode-2481-minimum-cuts-to-divide-a-circle) | leetcode | Classic |
191197
| [`2482-difference-between-ones-and-zeros-in-row-and-column`](./challenges/leetcode-2482-difference-between-ones-and-zeros-in-row-and-column) | leetcode | Classic |
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# leetcode-100330-minimum-length-of-string-after-operations
2+
3+
https://leetcode.com/problems/minimum-length-of-string-after-operations
4+
5+
**Type:** Classic
6+
7+
## Run Code Result
8+
9+
### Your input
10+
11+
<!-- prettier-ignore -->
12+
```js
13+
"abaacbcbb"
14+
```
15+
16+
### Your stdout
17+
18+
<!-- prettier-ignore -->
19+
```js
20+
charIndexes={a=[0, 2, 3], b=[1, 5, 7, 8], c=[4, 6]}
21+
charIndexes={a=[2], b=[1, 5, 7, 8], c=[4, 6]}
22+
charIndexes={a=[2], b=[5, 7], c=[4, 6]}
23+
24+
charIndexes={a=[0, 1]}
25+
```
26+
27+
### Your answer
28+
29+
<!-- prettier-ignore -->
30+
```js
31+
5
32+
2
33+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class Solution {
2+
public int minimumLength(String s) {
3+
final Map<Character, List<Integer>> charIndexes = new HashMap<>();
4+
for (int i = 0; i < s.length(); i++) {
5+
final char c = s.charAt(i);
6+
final List<Integer> listOfSameChar = charIndexes.getOrDefault(c, new ArrayList<>());
7+
listOfSameChar.add(i);
8+
charIndexes.put(c, listOfSameChar);
9+
}
10+
11+
// System.out.println(String.format("charIndexes=%s", charIndexes));
12+
13+
int length = s.length();
14+
15+
for (List<Integer> listOfSameChar : charIndexes.values()) {
16+
while (listOfSameChar.size() >= 3) {
17+
listOfSameChar.remove(listOfSameChar.size() - 1);
18+
listOfSameChar.remove(0);
19+
length -= 2;
20+
// System.out.println(String.format(" charIndexes=%s", charIndexes));
21+
}
22+
}
23+
24+
return length;
25+
26+
//
27+
// Time Limit Exceeded!
28+
//
29+
30+
// for (int i = 1; i < s.length(); i++) {
31+
// //System.out.println(s);
32+
33+
// char target = s.charAt(i);
34+
// int leftIdx = -1;
35+
// int rightIdx = -1;
36+
37+
// for (int j = 0; j < i; j++) {
38+
// char current = s.charAt(j);
39+
// //System.out.println(String.format(" LEFT i=%s, target=%s, current=%s", i, target, current));
40+
// if (current == target) {
41+
// leftIdx = j;
42+
// break;
43+
// }
44+
// }
45+
// if (leftIdx == -1) continue;
46+
47+
// for (int j = i + 1; j < s.length(); j++) {
48+
// char current = s.charAt(j);
49+
// //System.out.println(String.format(" RIGHT i=%s, target=%s, current=%s", i, target, current));
50+
// if (current == target) {
51+
// rightIdx = j;
52+
// break;
53+
// }
54+
// }
55+
56+
// if (leftIdx != -1 && rightIdx != -1) {
57+
// //System.out.println(String.format(" RESULT i=%s, leftIdx=%s, rightIdx=%s", i, leftIdx, rightIdx));
58+
// s = s.substring(0, rightIdx) + s.substring(rightIdx + 1);
59+
// s = s.substring(0, leftIdx) + s.substring(leftIdx + 1);
60+
// i = 1;
61+
// }
62+
// }
63+
64+
// System.out.println(s);
65+
// return s.length();
66+
}
67+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# leetcode-100330-minimum-length-of-string-after-operations
2+
3+
https://leetcode.com/problems/minimum-length-of-string-after-operations
4+
5+
**Type:** Classic
6+
7+
## Run Code Result
8+
9+
### Your input
10+
11+
<!-- prettier-ignore -->
12+
```js
13+
2
14+
7
15+
4
16+
11
17+
```
18+
19+
### Your stdout
20+
21+
<!-- prettier-ignore -->
22+
```js
23+
```
24+
25+
### Your answer
26+
27+
<!-- prettier-ignore -->
28+
```js
29+
"Alice"
30+
"Bob"
31+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public String losingPlayer(int x, int y) {
3+
String player = "Bob";
4+
5+
for (; x > 0 || y > 0;) {
6+
if (x >= 1 && y >= 4) {
7+
x -= 1;
8+
y -= 4;
9+
player = player.equals("Alice") ? "Bob" : "Alice";
10+
} else {
11+
return player;
12+
}
13+
}
14+
return player;
15+
}
16+
}

0 commit comments

Comments
 (0)