Skip to content

Commit 509045b

Browse files
committed
Add leetcode contest 2024-05-11 - 2/4
1 parent 955ea7f commit 509045b

File tree

8 files changed

+352
-8
lines changed

8 files changed

+352
-8
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ zx _genReadme.mjs
3434

3535
| Name | Platform |
3636
| ---- | -------- |
37+
| [`100289-minimum-substring-partition-of-equal-character-frequency`](./challenges/leetcode-100289-minimum-substring-partition-of-equal-character-frequency) | leetcode |
38+
| [`100299-check-if-grid-satisfies-conditions`](./challenges/leetcode-100299-check-if-grid-satisfies-conditions) | leetcode |
39+
| [`100302-maximum-points-inside-the-square`](./challenges/leetcode-100302-maximum-points-inside-the-square) | leetcode |
3740
| [`1d-spreadsheet`](./challenges/codingame-1d-spreadsheet) | codingame |
3841
| [`2481-minimum-cuts-to-divide-a-circle`](./challenges/leetcode-2481-minimum-cuts-to-divide-a-circle) | leetcode |
3942
| [`2482-difference-between-ones-and-zeros-in-row-and-column`](./challenges/leetcode-2482-difference-between-ones-and-zeros-in-row-and-column) | leetcode |
@@ -143,6 +146,9 @@ ___
143146

144147
| Name | Type |
145148
| ---- | ---- |
149+
| [`100289-minimum-substring-partition-of-equal-character-frequency`](./challenges/leetcode-100289-minimum-substring-partition-of-equal-character-frequency) | Classic |
150+
| [`100299-check-if-grid-satisfies-conditions`](./challenges/leetcode-100299-check-if-grid-satisfies-conditions) | Classic |
151+
| [`100302-maximum-points-inside-the-square`](./challenges/leetcode-100302-maximum-points-inside-the-square) | Classic |
146152
| [`2481-minimum-cuts-to-divide-a-circle`](./challenges/leetcode-2481-minimum-cuts-to-divide-a-circle) | Classic |
147153
| [`2482-difference-between-ones-and-zeros-in-row-and-column`](./challenges/leetcode-2482-difference-between-ones-and-zeros-in-row-and-column) | Classic |
148154
| [`2483-minimum-penalty-for-a-shop`](./challenges/leetcode-2483-minimum-penalty-for-a-shop) | Classic |
@@ -177,6 +183,9 @@ ___
177183

178184
| Name | Platform | Type |
179185
| ---- | -------- | ---- |
186+
| [`100289-minimum-substring-partition-of-equal-character-frequency`](./challenges/leetcode-100289-minimum-substring-partition-of-equal-character-frequency) | leetcode | Classic |
187+
| [`100299-check-if-grid-satisfies-conditions`](./challenges/leetcode-100299-check-if-grid-satisfies-conditions) | leetcode | Classic |
188+
| [`100302-maximum-points-inside-the-square`](./challenges/leetcode-100302-maximum-points-inside-the-square) | leetcode | Classic |
180189
| [`1d-spreadsheet`](./challenges/codingame-1d-spreadsheet) | codingame | Classic |
181190
| [`2481-minimum-cuts-to-divide-a-circle`](./challenges/leetcode-2481-minimum-cuts-to-divide-a-circle) | leetcode | Classic |
182191
| [`2482-difference-between-ones-and-zeros-in-row-and-column`](./challenges/leetcode-2482-difference-between-ones-and-zeros-in-row-and-column) | leetcode | Classic |

_genReadme.js renamed to _genReadme.mjs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
const { promises: fs } = require('fs')
2-
const { resolve } = require('path')
1+
// @ts-check
2+
3+
import * as url from 'url'
4+
import * as path from 'path'
5+
import * as fs from 'fs/promises'
6+
7+
const __filename = url.fileURLToPath(import.meta.url)
8+
const __dirname = path.dirname(__filename)
39

410
const readme = (challenges, challengesByPlatform, challengesByType) => `
511
# code-challenges
@@ -48,6 +54,7 @@ ___
4854
4955
### Grouped by platform
5056
${Object.entries(challengesByPlatform)
57+
.sort(([a], [b]) => a.localeCompare(b))
5158
.map(
5259
([platform, challenges]) => `
5360
#### ${platform}
@@ -86,18 +93,16 @@ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
8693
`
8794

8895
const setup = async () => {
89-
const challengesPath = resolve(__dirname, 'challenges')
90-
const challengesRaw = (await fs.readdir(challengesPath)).sort((a, b) =>
91-
a.replace(/^.*?-/, '').localeCompare(b.replace(/^.*?-/, ''))
92-
)
96+
const challengesPath = path.resolve(__dirname, 'challenges')
97+
const challengesRaw = (await fs.readdir(challengesPath)).sort((a, b) => a.replace(/^.*?-/, '').localeCompare(b.replace(/^.*?-/, '')))
9398

9499
let challenges = []
95100

96101
for (const aChallenge of challengesRaw) {
97102
const s = aChallenge.split('-')
98103
const name = s.slice(1).join('-')
99104
const platform = s[0]
100-
const challengeReadme = await fs.readFile(resolve(challengesPath, aChallenge, 'README.md'), { encoding: 'utf-8' })
105+
const challengeReadme = await fs.readFile(path.resolve(challengesPath, aChallenge, 'README.md'), { encoding: 'utf-8' })
101106
const type = challengeReadme.match(/\*\*Type\:\*\*\s(.*)/)[1]
102107

103108
challenges.push({ name, platform, type, raw: aChallenge })
@@ -121,6 +126,6 @@ const setup = async () => {
121126
console.log()
122127
console.log(result)
123128

124-
await fs.writeFile(resolve(__dirname, 'README.md'), `${result.trim()}\n`)
129+
await fs.writeFile(path.resolve(__dirname, 'README.md'), `${result.trim()}\n`)
125130
}
126131
setup()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# leetcode-100289-minimum-substring-partition-of-equal-character-frequency
2+
3+
https://leetcode.com/problems/minimum-substring-partition-of-equal-character-frequency
4+
5+
**Type:** Classic
6+
7+
## Run Code Result
8+
9+
### Your input
10+
11+
<!-- prettier-ignore -->
12+
```js
13+
"abababaccddb"
14+
```
15+
16+
### Your stdout
17+
18+
<!-- prettier-ignore -->
19+
```js
20+
abababaccddb
21+
[
22+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'c', 'c', 'd', 'd', 'b'],
23+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'c', 'c', 'd', 'db'],
24+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'c', 'c', 'dd', 'b'],
25+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'c', 'c', 'ddb'],
26+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'c', 'cd', 'd', 'b'],
27+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'c', 'cd', 'db'],
28+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'c', 'cdd', 'b'],
29+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'c', 'cddb'],
30+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'cc', 'd', 'd', 'b'],
31+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'cc', 'd', 'db'],
32+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'cc', 'dd', 'b'],
33+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'cc', 'ddb'],
34+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'ccd', 'd', 'b'],
35+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'ccd', 'db'],
36+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'ccdd', 'b'],
37+
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'ccddb'],
38+
['a', 'b', 'a', 'b', 'a', 'b', 'ac', 'c', 'd', 'd', 'b'],
39+
['a', 'b', 'a', 'b', 'a', 'b', 'ac', 'c', 'd', 'db'],
40+
['a', 'b', 'a', 'b', 'a', 'b', 'ac', 'c', 'dd', 'b'],
41+
['a', 'b', 'a', 'b', 'a', 'b', 'ac', 'c', 'ddb'],
42+
['a', 'b', 'a', 'b', 'a', 'b', 'ac', 'cd', 'd', 'b'],
43+
['a', 'b', 'a', 'b', 'a', 'b', 'ac', 'cd', 'db'],
44+
['a', 'b', 'a', 'b', 'a', 'b', 'ac', 'cdd', 'b'],
45+
['a', 'b', 'a', 'b', 'a', 'b', 'ac', 'cddb'],
46+
['a', 'b', 'a', 'b', 'a', 'b', 'acc', 'd', 'd', 'b'],
47+
['a', 'b', 'a', 'b', 'a', 'b', 'acc', 'd', 'db'],
48+
['a', 'b', 'a', 'b', 'a', 'b', 'acc', 'dd', 'b'],
49+
['a', 'b', 'a', 'b', 'a', 'b', 'acc', 'ddb'],
50+
['a', 'b', 'a', 'b', 'a', 'b', 'accd', 'd', 'b'],
51+
['a', 'b', 'a', 'b', 'a', 'b', 'accd', 'db'],
52+
['a', 'b', 'a', 'b', 'a', 'b', 'accdd', 'b'],
53+
['a', 'b', 'a', 'b', 'a', 'b', 'accddb'],
54+
['a', 'b', 'a', 'b', 'a', 'ba', 'c', 'c', 'd', 'd', 'b'],
55+
['a', 'b', 'a', 'b', 'a', 'ba', 'c', 'c', 'd', 'db'],
56+
['a', 'b', 'a', 'b', 'a', 'ba', 'c', 'c', 'dd', 'b'],
57+
['a', 'b', 'a', 'b', 'a', 'ba', 'c', 'c', 'ddb'],
58+
['a', 'b', 'a', 'b', 'a', 'ba', 'c', 'cd', 'd', 'b'],
59+
['a', 'b', 'a', 'b', 'a', 'ba', 'c', 'cd', 'db'],
60+
['a', 'b', 'a', 'b', 'a', 'ba', 'c', 'cdd', 'b'],
61+
['a', 'b', 'a', 'b', 'a', 'ba', 'c', 'cddb'],
62+
['a', 'b', 'a', 'b', 'a', 'ba', 'cc', 'd', 'd', 'b'],
63+
['a', 'b', 'a', 'b', 'a', 'ba', 'cc', 'd', 'db'],
64+
['a', 'b', 'a', 'b', 'a', 'ba', 'cc', 'dd', 'b'],
65+
['a', 'b', 'a', 'b', 'a', 'ba', 'cc', 'ddb'],
66+
['a', 'b', 'a', 'b', 'a', 'ba', 'ccd', 'd', 'b'],
67+
['a', 'b', 'a', 'b', 'a', 'ba', 'ccd', 'db'],
68+
['a', 'b', 'a', 'b', 'a', 'ba', 'ccdd', 'b'],
69+
['a', 'b', 'a', 'b', 'a', 'ba', 'ccddb'],
70+
['a', 'b', 'a', 'b', 'a', 'bac', 'c', 'd', 'd', 'b'],
71+
['a', 'b', 'a', 'b', 'a', 'bac', 'c', 'd', 'db'],
72+
['a', 'b', 'a', 'b', 'a', 'bac', 'c', 'dd', 'b'],
73+
['a', 'b', 'a', 'b', 'a', 'bac', 'c', 'ddb'],
74+
['a', 'b', 'a', 'b', 'a', 'bac', 'cd', 'd', 'b'],
75+
['a', 'b', 'a', 'b', 'a', 'bac', 'cd', 'db'],
76+
['a', 'b', 'a', 'b', 'a', 'bac', 'cdd', 'b'],
77+
['a', 'b', 'a', 'b', 'a', 'bac', 'cddb'],
78+
['a', 'b', 'a', 'b', 'a', 'bacc', 'd', 'd', 'b'],
79+
['a', 'b', 'a', 'b', 'a', 'bacc', 'd', 'db'],
80+
['a', 'b', 'a', 'b', 'a', 'bacc', 'dd', 'b'],
81+
['a', 'b', 'a', 'b', 'a', 'bacc', 'ddb'],
82+
['a', 'b', 'a', 'b', 'a', 'baccd', 'd', 'b'],
83+
['a', 'b', 'a', 'b', 'a', 'baccd', 'db'],
84+
['a', 'b', 'a', 'b', 'a', 'baccdd', 'b'],
85+
['a', 'b', 'a', 'b', 'a', 'baccddb'],
86+
['a', 'b', 'a', 'b', 'ab', 'a', 'c', 'c', 'd', 'd', 'b'],
87+
['a', 'b', 'a', 'b', 'ab', 'a', 'c', 'c', 'd', 'db'],
88+
['a', 'b', 'a', 'b', 'ab', 'a', 'c', 'c', 'dd', 'b'],
89+
['a', 'b', 'a', 'b', 'ab', 'a', 'c', 'c', 'ddb'],
90+
['a', 'b', 'a', 'b', 'ab', 'a', 'c', 'cd', 'd', 'b'],
91+
['a', 'b', 'a', 'b', 'ab', 'a', 'c', 'cd', 'db'],
92+
['a', 'b', 'a', 'b', 'ab', 'a', 'c', 'cdd', 'b'],
93+
['a', 'b', 'a', 'b', 'ab', 'a', 'c', 'cddb'],
94+
['a', 'b', 'a', 'b', 'ab', 'a', 'cc', 'd', 'd', 'b'],
95+
['a', 'b', 'a', 'b', 'ab', 'a', 'cc', 'd', 'db'],
96+
['a', 'b', 'a', 'b', 'ab', 'a', 'cc', 'dd', 'b'],
97+
['a', 'b', 'a', 'b', 'ab', 'a', 'cc', 'ddb'],
98+
['a', 'b', 'a', 'b', 'ab', 'a', 'ccd', 'd', 'b'],
99+
['a', 'b', 'a', 'b', 'ab', 'a', 'ccd', 'db'],
100+
['a', 'b', 'a', 'b', 'ab', 'a', 'ccdd', 'b'],
101+
['a', 'b', 'a', 'b', 'ab', 'a', 'ccddb'],
102+
['a', 'b', 'a', 'b', 'ab', 'ac', 'c', 'd', 'd', 'b'],
103+
['a', 'b', 'a', 'b', 'ab', 'ac', 'c', 'd', 'db'],
104+
['a', 'b', 'a', 'b', 'ab', 'ac', 'c', 'dd', 'b'],
105+
['a', 'b', 'a', 'b', 'ab', 'ac', 'c', 'ddb'],
106+
['a', 'b', 'a', 'b', 'ab', 'ac', 'cd', 'd', 'b'],
107+
['a', 'b', 'a', 'b', 'ab', 'ac', 'cd', 'db'],
108+
['a', 'b', 'a', 'b', 'ab', 'ac', 'cdd', 'b'],
109+
['a', 'b', 'a', 'b', 'ab', 'ac', 'cddb'],
110+
['a', 'b', 'a', 'b', 'ab', 'acc', 'd', 'd', 'b'],
111+
['a', 'b', 'a', 'b', 'ab', 'acc', 'd', 'db'],
112+
['a', 'b', 'a', 'b', 'ab', 'acc', 'dd', 'b'],
113+
['a', 'b', 'a', 'b', 'ab', 'acc', 'ddb'],
114+
['a', 'b', 'a', 'b', 'ab', 'accd', 'd', 'b'],
115+
['a', 'b', 'a', 'b', 'ab', 'accd', 'db'],
116+
['a', 'b', 'a', 'b', 'ab', 'accdd', 'b'],
117+
['a', 'b', 'a', 'b', 'ab', 'accddb'],
118+
['a', 'b', 'a', 'b', 'aba', 'c', 'c', 'd', 'd', 'b'],
119+
['a', 'b', 'a', 'b', 'aba', 'c', 'c', 'd', 'db'],
120+
['a', 'b', 'a', 'b', 'aba', 'c', 'c', 'dd', 'b'],
121+
['a', 'b', 'a', 'b', 'aba', 'c', 'c', 'ddb'],
122+
... 1948 more items
123+
]
124+
```
125+
126+
### Your answer
127+
128+
Time Limit Exceeded on submit!
129+
130+
<!-- prettier-ignore -->
131+
```js
132+
2
133+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// @ts-check
2+
3+
// Bruteforce
4+
// Time Limit Exceeded
5+
6+
/**
7+
* @param {string} s
8+
* @return {number}
9+
*/
10+
var minimumSubstringsInPartition = function (s) {
11+
const partitions = minimumSubstringsInPartition2(s, 0, [], [])
12+
console.log(s)
13+
console.log(partitions)
14+
15+
let minPartitionsCount = s.length
16+
for (let i = partitions.length - 1; i >= 0; i--) {
17+
if (partitions[i].every(groupInPartition => isBalancedStr(groupInPartition)) && partitions[i].length < minPartitionsCount) {
18+
// console.log(partitions[i])
19+
minPartitionsCount = partitions[i].length
20+
}
21+
}
22+
return minPartitionsCount
23+
}
24+
25+
/**
26+
* @param {string} s
27+
* @param {number} start
28+
* @param {string[]} currentPartition
29+
* @param {string[][]} partitions
30+
* @return {string[][]}
31+
*/
32+
var minimumSubstringsInPartition2 = function (s, start, currentPartition, partitions) {
33+
if (start === s.length) {
34+
partitions.push([...currentPartition])
35+
}
36+
37+
for (let end = start + 1; end <= s.length; end++) {
38+
currentPartition.push(s.slice(start, end))
39+
minimumSubstringsInPartition2(s, end, currentPartition, partitions)
40+
currentPartition.pop()
41+
}
42+
return partitions
43+
}
44+
45+
function isBalancedStr(str) {
46+
const freqs = {}
47+
for (const letter of str) {
48+
if (!freqs[letter]) freqs[letter] = 0
49+
freqs[letter]++
50+
}
51+
52+
const keys = Object.keys(freqs)
53+
if (keys.length <= 1) return true
54+
55+
for (let i = 0; i < keys.length - 1; i++) {
56+
if (freqs[keys[i]] !== freqs[keys[i + 1]]) return false
57+
}
58+
return true
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# leetcode-100299-check-if-grid-satisfies-conditions
2+
3+
https://leetcode.com/problems/check-if-grid-satisfies-conditions
4+
5+
**Type:** Classic
6+
7+
## Run Code Result
8+
9+
### Your input
10+
11+
<!-- prettier-ignore -->
12+
```js
13+
[[1,0,2],[1,0,2]]
14+
[[1,1,1],[0,0,0]]
15+
[[1],[2],[3]]
16+
[[1,1,6,1,4,6,3,1,0,7]]
17+
```
18+
19+
### Your stdout
20+
21+
<!-- prettier-ignore -->
22+
```js
23+
```
24+
25+
### Your answer
26+
27+
<!-- prettier-ignore -->
28+
```js
29+
true
30+
false
31+
false
32+
false
33+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @ts-check
2+
/**
3+
* @param {number[][]} grid
4+
* @return {boolean}
5+
*/
6+
var satisfiesConditions = function (grid) {
7+
for (let y = 0; y < grid.length; y++) {
8+
for (let x = 0; x < grid[0].length; x++) {
9+
if (x + 1 < grid[0].length && grid[y][x] === grid[y][x + 1]) return false
10+
if (y + 1 < grid.length && grid[y][x] !== grid[y + 1][x]) return false
11+
}
12+
}
13+
return true
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# leetcode-100302-maximum-points-inside-the-square
2+
3+
https://leetcode.com/problems/maximum-points-inside-the-square
4+
5+
**Type:** Classic
6+
7+
## Run Code Result
8+
9+
### Your input
10+
11+
<!-- prettier-ignore -->
12+
```js
13+
[[2,2],[-1,-2],[-4,4],[-3,1],[3,-3]]
14+
"abdca"
15+
[[1,1],[-2,-2],[-2,2]]
16+
"abb"
17+
[[1,1],[-1,-1],[2,-2]]
18+
"ccd"
19+
```
20+
21+
### Your stdout
22+
23+
<!-- prettier-ignore -->
24+
```js
25+
-----
26+
{ '2': [ 'a', 'b' ], '3': [ 'c', 'a' ], '4': [ 'd' ] }
27+
0
28+
2
29+
-----
30+
{ '1': [ 'a' ], '2': [ 'b', 'b' ] }
31+
0
32+
1
33+
-----
34+
{ '1': [ 'c', 'c' ], '2': [ 'd' ] }
35+
0
36+
```
37+
38+
### Your answer
39+
40+
<!-- prettier-ignore -->
41+
```js
42+
2
43+
1
44+
0
45+
```

0 commit comments

Comments
 (0)