Skip to content

Commit 2e97100

Browse files
committed
feat: update script
1 parent 4577d80 commit 2e97100

File tree

11 files changed

+251
-161
lines changed

11 files changed

+251
-161
lines changed

main.py

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,41 @@
22
import os
33
import re
44

5-
code_block_dict = {
6-
"python": ("Python3", "py"),
5+
sorted_suffixes = [
6+
"py",
7+
"java",
8+
"cpp",
9+
"go",
10+
"ts",
11+
"rs",
12+
"js",
13+
"cs",
14+
"php",
15+
"c",
16+
"scala",
17+
"swift",
18+
"rb",
19+
"kt",
20+
"nim",
21+
"sql",
22+
]
23+
code_dict = {
24+
"py": ("Python3", "python"),
725
"java": ("Java", "java"),
826
"cpp": ("C++", "cpp"),
9-
"c": ("C", "c"),
1027
"go": ("Go", "go"),
1128
"ts": ("TypeScript", "ts"),
29+
"rs": ("Rust", "rust"),
1230
"js": ("JavaScript", "js"),
13-
"php": ("PHP", "php"),
1431
"cs": ("C#", "cs"),
15-
"rust": ("Rust", "rs"),
16-
"sql": ("MySQL", "sql"),
17-
"nim": ("Nim", "nim"),
32+
"php": ("PHP", "php"),
33+
"c": ("C", "c"),
1834
"scala": ("Scala", "scala"),
1935
"swift": ("Swift", "swift"),
2036
"rb": ("Ruby", "rb"),
21-
"kotlin": ("Kotlin", "kt"),
37+
"kt": ("Kotlin", "kotlin"),
38+
"nim": ("Nim", "nim"),
39+
"sql": ("MySQL", "sql"),
2240
}
2341

2442

@@ -42,7 +60,7 @@ def extract_code():
4260
if i == -1:
4361
continue
4462
content = content[i + len(mark) :]
45-
for suf, (_, suffix) in code_block_dict.items():
63+
for suffix, (_, suf) in code_dict.items():
4664
res = re.findall(f"```{suf}\n(.*?)```", content, re.S)
4765
if not res:
4866
continue
@@ -111,11 +129,36 @@ def extract_solution_paragraph():
111129
["**Solution 1:", "**Solution 2:", "**Solution 3:", "**Solution 4:"],
112130
)
113131

132+
prefix = path[: path.rfind("/")]
133+
for i in range(1, 5):
134+
codes = []
135+
for suf in sorted_suffixes:
136+
seq = '' if i == 1 else str(i)
137+
file_name = f"{prefix}/Solution{seq}.{suf}"
138+
try:
139+
with open(file_name, "r", encoding="utf-8") as f:
140+
code = f.read().strip()
141+
code = '```' + code_dict[suf][1] + '\n' + code + '\n```'
142+
codes.append(code)
143+
except:
144+
continue
145+
if codes:
146+
if i > len(blocks):
147+
seq_dict = {1: '一', 2: '二', 3: '三', 4: '四'}
148+
title = f"### 方法{seq_dict[i]}" if is_cn else f"### Solution {i}"
149+
block = title + '\n\n' + '\n\n'.join(codes)
150+
blocks.append(block)
151+
else:
152+
block = blocks[i - 1] + '\n\n' + '\n\n'.join(codes)
153+
blocks[i - 1] = block
154+
114155
if blocks:
115-
prefix = path[: path.rfind("/")]
116-
name = f"{prefix}/Solution.md" if is_cn else f"{prefix}/Solution_EN.md"
117-
with open(name, "w", encoding="utf-8") as f:
118-
f.write("\n\n".join(blocks))
156+
start = '## 解法' if is_cn else '## Solutions'
157+
content = (
158+
content[: content.find(start)] + start + '\n\n' + '\n\n'.join(blocks)
159+
)
160+
with open(path, "w", encoding="utf-8") as f:
161+
f.write(content)
119162

120163

121164
if __name__ == "__main__":

solution/1400-1499/1483.Kth Ancestor of a Tree Node/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ func (this *TreeAncestor) GetKthAncestor(node int, k int) int {
246246
### **TypeScript**
247247

248248
```ts
249-
class TreeAncestor {
250249
class TreeAncestor {
251250
private p: number[][];
252251

solution/1400-1499/1483.Kth Ancestor of a Tree Node/Solution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ class TreeAncestor {
3535
* Your TreeAncestor object will be instantiated and called as such:
3636
* TreeAncestor* obj = new TreeAncestor(n, parent);
3737
* int param_1 = obj->getKthAncestor(node,k);
38-
*/
38+
*/

solution/1400-1499/1483.Kth Ancestor of a Tree Node/Solution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ func (this *TreeAncestor) GetKthAncestor(node int, k int) int {
3737
* Your TreeAncestor object will be instantiated and called as such:
3838
* obj := Constructor(n, parent);
3939
* param_1 := obj.GetKthAncestor(node,k);
40-
*/
40+
*/

solution/1400-1499/1483.Kth Ancestor of a Tree Node/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ public int getKthAncestor(int node, int k) {
3636
* Your TreeAncestor object will be instantiated and called as such:
3737
* TreeAncestor obj = new TreeAncestor(n, parent);
3838
* int param_1 = obj.getKthAncestor(node,k);
39-
*/
39+
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const closestCost = function (baseCosts, toppingCosts, target) {
2+
let closestDessertCost = -Infinity;
3+
function dfs(dessertCost, j) {
4+
const tarCurrDiff = Math.abs(target - dessertCost);
5+
const tarCloseDiff = Math.abs(target - closestDessertCost);
6+
if (tarCurrDiff < tarCloseDiff) {
7+
closestDessertCost = dessertCost;
8+
} else if (tarCurrDiff === tarCloseDiff && dessertCost < closestDessertCost) {
9+
closestDessertCost = dessertCost;
10+
}
11+
if (dessertCost > target) return;
12+
if (j === toppingCosts.length) return;
13+
for (let count = 0; count <= 2; count++) {
14+
dfs(dessertCost + count * toppingCosts[j], j + 1);
15+
}
16+
}
17+
for (let i = 0; i < baseCosts.length; i++) {
18+
dfs(baseCosts[i], 0);
19+
}
20+
return closestDessertCost;
21+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[][]} matches
3+
* @return {number[][]}
4+
*/
5+
var findWinners = function (matches) {
6+
const onlyWins = new Set(),
7+
oneLose = new Set(),
8+
moreLosses = new Set();
9+
10+
for (const [winner, loser] of matches) {
11+
if (!moreLosses.has(loser)) {
12+
if (oneLose.has(loser)) {
13+
oneLose.delete(loser);
14+
moreLosses.add(loser);
15+
} else {
16+
onlyWins.delete(loser);
17+
oneLose.add(loser);
18+
}
19+
}
20+
21+
if (!moreLosses.has(winner) && !oneLose.has(winner)) {
22+
onlyWins.add(winner);
23+
}
24+
}
25+
26+
return [[...onlyWins].sort((a, b) => a - b), [...oneLose].sort((a, b) => a - b)];
27+
};
Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
class Solution {
2-
public:
3-
long long findMaximumNumber(long long k, int x) {
4-
using ll = long long;
5-
ll l = 1, r = 1e17;
6-
ll num = 0;
7-
ll f[65][65];
8-
function<ll(int, int, bool)> dfs = [&](int pos, int cnt, bool limit) -> ll {
9-
if (pos == 0) {
10-
return cnt;
11-
}
12-
if (!limit && f[pos][cnt] != -1) {
13-
return f[pos][cnt];
14-
}
15-
int up = limit ? num >> (pos - 1) & 1 : 1;
16-
ll ans = 0;
17-
for (int i = 0; i <= up; ++i) {
18-
ans += dfs(pos - 1, cnt + (i == 1 && pos % x == 0), limit && i == up);
19-
}
20-
if (!limit) {
21-
f[pos][cnt] = ans;
22-
}
23-
return ans;
24-
};
25-
while (l < r) {
26-
ll mid = (l + r + 1) >> 1;
27-
num = mid;
28-
memset(f, -1, sizeof(f));
29-
int pos = 64 - __builtin_clzll(mid);
30-
if (dfs(pos, 0, true) <= k) {
31-
l = mid;
32-
} else {
33-
r = mid - 1;
34-
}
35-
}
36-
return l;
37-
}
1+
class Solution {
2+
public:
3+
long long findMaximumNumber(long long k, int x) {
4+
using ll = long long;
5+
ll l = 1, r = 1e17;
6+
ll num = 0;
7+
ll f[65][65];
8+
function<ll(int, int, bool)> dfs = [&](int pos, int cnt, bool limit) -> ll {
9+
if (pos == 0) {
10+
return cnt;
11+
}
12+
if (!limit && f[pos][cnt] != -1) {
13+
return f[pos][cnt];
14+
}
15+
int up = limit ? num >> (pos - 1) & 1 : 1;
16+
ll ans = 0;
17+
for (int i = 0; i <= up; ++i) {
18+
ans += dfs(pos - 1, cnt + (i == 1 && pos % x == 0), limit && i == up);
19+
}
20+
if (!limit) {
21+
f[pos][cnt] = ans;
22+
}
23+
return ans;
24+
};
25+
while (l < r) {
26+
ll mid = (l + r + 1) >> 1;
27+
num = mid;
28+
memset(f, -1, sizeof(f));
29+
int pos = 64 - __builtin_clzll(mid);
30+
if (dfs(pos, 0, true) <= k) {
31+
l = mid;
32+
} else {
33+
r = mid - 1;
34+
}
35+
}
36+
return l;
37+
}
3838
};
Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
func findMaximumNumber(k int64, x int) int64 {
2-
var l, r int64 = 1, 1e17
3-
var num int64
4-
var f [65][65]int64
5-
var dfs func(pos, cnt int, limit bool) int64
6-
dfs = func(pos, cnt int, limit bool) int64 {
7-
if pos == 0 {
8-
return int64(cnt)
9-
}
10-
if !limit && f[pos][cnt] != -1 {
11-
return f[pos][cnt]
12-
}
13-
var ans int64
14-
up := 1
15-
if limit {
16-
up = int(num >> (pos - 1) & 1)
17-
}
18-
for i := 0; i <= up; i++ {
19-
v := cnt
20-
if i == 1 && pos%x == 0 {
21-
v++
22-
}
23-
ans += dfs(pos-1, v, limit && i == up)
24-
}
25-
if !limit {
26-
f[pos][cnt] = ans
27-
}
28-
return ans
29-
}
30-
for l < r {
31-
mid := (l + r + 1) >> 1
32-
num = mid
33-
m := bits.Len(uint(num))
34-
for i := range f {
35-
for j := range f[i] {
36-
f[i][j] = -1
37-
}
38-
}
39-
if dfs(m, 0, true) <= k {
40-
l = mid
41-
} else {
42-
r = mid - 1
43-
}
44-
}
45-
return l
1+
func findMaximumNumber(k int64, x int) int64 {
2+
var l, r int64 = 1, 1e17
3+
var num int64
4+
var f [65][65]int64
5+
var dfs func(pos, cnt int, limit bool) int64
6+
dfs = func(pos, cnt int, limit bool) int64 {
7+
if pos == 0 {
8+
return int64(cnt)
9+
}
10+
if !limit && f[pos][cnt] != -1 {
11+
return f[pos][cnt]
12+
}
13+
var ans int64
14+
up := 1
15+
if limit {
16+
up = int(num >> (pos - 1) & 1)
17+
}
18+
for i := 0; i <= up; i++ {
19+
v := cnt
20+
if i == 1 && pos%x == 0 {
21+
v++
22+
}
23+
ans += dfs(pos-1, v, limit && i == up)
24+
}
25+
if !limit {
26+
f[pos][cnt] = ans
27+
}
28+
return ans
29+
}
30+
for l < r {
31+
mid := (l + r + 1) >> 1
32+
num = mid
33+
m := bits.Len(uint(num))
34+
for i := range f {
35+
for j := range f[i] {
36+
f[i][j] = -1
37+
}
38+
}
39+
if dfs(m, 0, true) <= k {
40+
l = mid
41+
} else {
42+
r = mid - 1
43+
}
44+
}
45+
return l
4646
}

0 commit comments

Comments
 (0)