Skip to content

Commit 03b8096

Browse files
committed
feat: add solutions to lc problems: No.2643~2646
* No.2643.Row With Maximum Ones * No.2644.Find the Maximum Divisibility Score * No.2645.Minimum Additions to Make Valid Strin * No.2646.Minimize the Total Price of the Trips
1 parent db03589 commit 03b8096

File tree

26 files changed

+885
-53
lines changed

26 files changed

+885
-53
lines changed

solution/0000-0099/0007.Reverse Integer/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def reverse(self, x: int) -> int:
33
ans = 0
4-
mi, mx = -2**31, 2**31 - 1
4+
mi, mx = -(2**31), 2**31 - 1
55
while x:
66
if ans < mi // 10 + 1 or ans > mx // 10:
77
return 0

solution/0000-0099/0012.Integer to Roman/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class Solution:
22
def intToRoman(self, num: int) -> str:
3-
cs = ('M', 'CM', 'D', 'CD', 'C', 'XC',
4-
'L', 'XL', 'X', 'IX', 'V', 'IV', 'I')
3+
cs = ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I')
54
vs = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
65
ans = []
76
for c, v in zip(cs, vs):

solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def longestDecomposition(self, text: str) -> int:
66
k = 1
77
ok = False
88
while i + k - 1 < j - k + 1:
9-
if text[i: i + k] == text[j - k + 1: j + 1]:
9+
if text[i : i + k] == text[j - k + 1 : j + 1]:
1010
ans += 2
1111
i += k
1212
j -= k

solution/1300-1399/1396.Design Underground System/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,20 @@ class UndergroundSystem {
137137
public UndergroundSystem() {
138138

139139
}
140-
140+
141141
public void checkIn(int id, String stationName, int t) {
142142
ts.put(id, t);
143143
names.put(id, stationName);
144144
}
145-
145+
146146
public void checkOut(int id, String stationName, int t) {
147147
String key = names.get(id) + "-" + stationName;
148148
int[] v = d.getOrDefault(key, new int[2]);
149149
v[0] += t - ts.get(id);
150150
v[1]++;
151151
d.put(key, v);
152152
}
153-
153+
154154
public double getAverageTime(String startStation, String endStation) {
155155
String key = startStation + "-" + endStation;
156156
int[] v = d.get(key);

solution/1300-1399/1396.Design Underground System/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class UndergroundSystem:
2-
32
def __init__(self):
43
self.ts = {}
54
self.d = {}

solution/1600-1699/1649.Create Sorted Array through Instructions/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def createSortedArray(self, instructions: List[int]) -> int:
2121
m = max(instructions)
2222
tree = BinaryIndexedTree(m)
2323
ans = 0
24-
mod = 10 ** 9 + 7
24+
mod = 10**9 + 7
2525
for i, x in enumerate(instructions):
2626
cost = min(tree.query(x - 1), i - tree.query(x))
2727
ans += cost

solution/1700-1799/1756.Design Most Recently Used Queue/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def query(self, x: int) -> int:
1717

1818

1919
class MRUQueue:
20-
2120
def __init__(self, n: int):
2221
self.q = list(range(n + 1))
2322
self.tree = BinaryIndexedTree(n + 2010)
@@ -35,6 +34,7 @@ def fetch(self, k: int) -> int:
3534
self.tree.update(l, 1)
3635
return x
3736

37+
3838
# Your MRUQueue object will be instantiated and called as such:
3939
# obj = MRUQueue(n)
4040
# param_1 = obj.fetch(k)

solution/2600-2699/2641.Cousins in Binary Tree II/Solution.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ def dfs1(root, d):
1818
def dfs2(root, d):
1919
if root is None:
2020
return
21-
t = (root.left.val if root.left else 0) + \
22-
(root.right.val if root.right else 0)
21+
t = (root.left.val if root.left else 0) + (
22+
root.right.val if root.right else 0
23+
)
2324
if root.left:
2425
root.left.val = s[d] - t
2526
if root.right:

solution/2600-2699/2642.Design Graph With Shortest Path Calculator/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Graph:
2-
32
def __init__(self, n: int, edges: List[List[int]]):
43
self.n = n
54
self.g = [[inf] * n for _ in range(n)]

solution/2600-2699/2643.Row With Maximum Ones/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:模拟**
56+
57+
我们直接遍历矩阵,统计每一行中 $1$ 的个数,更新最大值和对应的行下标。注意,如果当前行的 $1$ 的个数与最大值相等,我们需要选择行下标较小的那一行。
58+
59+
时间复杂度 $(m \times n)$,其中 $m$ 和 $n$ 分别为矩阵的行数和列数。空间复杂度 $O(1)$。
60+
5561
<!-- tabs:start -->
5662

5763
### **Python3**
@@ -136,6 +142,22 @@ func rowAndMaximumOnes(mat [][]int) []int {
136142
}
137143
```
138144

145+
### **TypeScript**
146+
147+
```ts
148+
function rowAndMaximumOnes(mat: number[][]): number[] {
149+
const ans: number[] = [0, 0];
150+
for (let i = 0; i < mat.length; ++i) {
151+
const cnt = mat[i].reduce((a, b) => a + b);
152+
if (ans[1] < cnt) {
153+
ans[0] = i;
154+
ans[1] = cnt;
155+
}
156+
}
157+
return ans;
158+
}
159+
```
160+
139161
### **...**
140162

141163
```

solution/2600-2699/2643.Row With Maximum Ones/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ func rowAndMaximumOnes(mat [][]int) []int {
127127
}
128128
```
129129

130+
### **TypeScript**
131+
132+
```ts
133+
function rowAndMaximumOnes(mat: number[][]): number[] {
134+
const ans: number[] = [0, 0];
135+
for (let i = 0; i < mat.length; ++i) {
136+
const cnt = mat[i].reduce((a, b) => a + b);
137+
if (ans[1] < cnt) {
138+
ans[0] = i;
139+
ans[1] = cnt;
140+
}
141+
}
142+
return ans;
143+
}
144+
```
145+
130146
### **...**
131147

132148
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function rowAndMaximumOnes(mat: number[][]): number[] {
2+
const ans: number[] = [0, 0];
3+
for (let i = 0; i < mat.length; ++i) {
4+
const cnt = mat[i].reduce((a, b) => a + b);
5+
if (ans[1] < cnt) {
6+
ans[0] = i;
7+
ans[1] = cnt;
8+
}
9+
}
10+
return ans;
11+
}

solution/2600-2699/2644.Find the Maximum Divisibility Score/README.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ divisors[1] 的可整除性得分为 0 ,因为 nums 中没有任何数字能
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:枚举**
66+
67+
我们可以枚举 $divisors$ 中的每个元素 $div$,计算 $nums$ 中有多少个元素能被 $div$ 整除,记为 $cnt$。
68+
69+
- 如果 $cnt$ 大于当前最大的可整除性得分 $mx$,则更新 $mx = cnt$,并且更新 $ans = div$。
70+
- 如果 $cnt$ 等于 $mx$ 并且 $div$ 小于 $ans$,则更新 $ans = div$。
71+
72+
最后返回 $ans$ 即可。
73+
74+
时间复杂度 $(m \times n)$,其中 $m$ 和 $n$ 分别是 $nums$ 和 $divisors$ 的长度。空间复杂度 $O(1)$。
75+
6576
<!-- tabs:start -->
6677

6778
### **Python3**
@@ -76,8 +87,8 @@ class Solution:
7687
cnt = sum(x % div == 0 for x in nums)
7788
if mx < cnt:
7889
mx, ans = cnt, div
79-
elif mx == cnt:
80-
ans = min(ans, div)
90+
elif mx == cnt and ans > div:
91+
ans = div
8192
return ans
8293
```
8394

@@ -156,6 +167,25 @@ func maxDivScore(nums []int, divisors []int) int {
156167
}
157168
```
158169

170+
### **TypeScript**
171+
172+
```ts
173+
function maxDivScore(nums: number[], divisors: number[]): number {
174+
let ans: number = divisors[0];
175+
let mx: number = 0;
176+
for (const div of divisors) {
177+
const cnt = nums.reduce((a, b) => a + (b % div == 0 ? 1 : 0), 0);
178+
if (mx < cnt) {
179+
mx = cnt;
180+
ans = div;
181+
} else if (mx === cnt && ans > div) {
182+
ans = div;
183+
}
184+
}
185+
return ans;
186+
}
187+
```
188+
159189
### **...**
160190

161191
```

solution/2600-2699/2644.Find the Maximum Divisibility Score/README_EN.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class Solution:
6868
cnt = sum(x % div == 0 for x in nums)
6969
if mx < cnt:
7070
mx, ans = cnt, div
71-
elif mx == cnt:
72-
ans = min(ans, div)
71+
elif mx == cnt and ans > div:
72+
ans = div
7373
return ans
7474
```
7575

@@ -146,6 +146,25 @@ func maxDivScore(nums []int, divisors []int) int {
146146
}
147147
```
148148

149+
### **TypeScript**
150+
151+
```ts
152+
function maxDivScore(nums: number[], divisors: number[]): number {
153+
let ans: number = divisors[0];
154+
let mx: number = 0;
155+
for (const div of divisors) {
156+
const cnt = nums.reduce((a, b) => a + (b % div == 0 ? 1 : 0), 0);
157+
if (mx < cnt) {
158+
mx = cnt;
159+
ans = div;
160+
} else if (mx === cnt && ans > div) {
161+
ans = div;
162+
}
163+
}
164+
return ans;
165+
}
166+
```
167+
149168
### **...**
150169

151170
```

solution/2600-2699/2644.Find the Maximum Divisibility Score/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ def maxDivScore(self, nums: List[int], divisors: List[int]) -> int:
55
cnt = sum(x % div == 0 for x in nums)
66
if mx < cnt:
77
mx, ans = cnt, div
8-
elif mx == cnt:
9-
ans = min(ans, div)
8+
elif mx == cnt and ans > div:
9+
ans = div
1010
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function maxDivScore(nums: number[], divisors: number[]): number {
2+
let ans: number = divisors[0];
3+
let mx: number = 0;
4+
for (const div of divisors) {
5+
const cnt = nums.reduce((a, b) => a + (b % div == 0 ? 1 : 0), 0);
6+
if (mx < cnt) {
7+
mx = cnt;
8+
ans = div;
9+
} else if (mx === cnt && ans > div) {
10+
ans = div;
11+
}
12+
}
13+
return ans;
14+
}

solution/2600-2699/2645.Minimum Additions to Make Valid String/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:贪心 + 双指针**
50+
51+
我们定义字符串 $s$ 为 `"abc"`,用指针 $i$ 和 $j$ 分别指向 $s$ 和 $word$。
52+
53+
如果 $word[j] \neq s[i]$,则需要插入 $s[i]$,我们将答案加 $1$;否则,说明 $word[j]$ 可以与 $s[i]$ 匹配,我们将 $j$ 右移一位。
54+
55+
然后,我们将 $i$ 右移一位,即 $i = (i + 1) \bmod 3$。继续上述操作,直到 $j$ 到达字符串 $word$ 的末尾。
56+
57+
最后,我们判断 $word$ 的最后一个字符是否为 `'b'` 或者 `'a'`,如果是,则需要插入 `'c'` 或者 `'bc'`,我们将答案加 $1$ 或者 $2$ 后返回即可。
58+
59+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $word$ 的长度。空间复杂度 $O(1)$。
60+
4961
<!-- tabs:start -->
5062

5163
### **Python3**
@@ -138,6 +150,29 @@ func addMinimum(word string) (ans int) {
138150
}
139151
```
140152

153+
### **TypeScript**
154+
155+
```ts
156+
function addMinimum(word: string): number {
157+
const s: string = 'abc';
158+
let ans: number = 0;
159+
const n: number = word.length;
160+
for (let i = 0, j = 0; j < n; i = (i + 1) % 3) {
161+
if (word[j] !== s[i]) {
162+
++ans;
163+
} else {
164+
++j;
165+
}
166+
}
167+
if (word[n - 1] === 'b') {
168+
++ans;
169+
} else if (word[n - 1] === 'a') {
170+
ans += 2;
171+
}
172+
return ans;
173+
}
174+
```
175+
141176
### **...**
142177

143178
```

solution/2600-2699/2645.Minimum Additions to Make Valid String/README_EN.md

+23
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,29 @@ func addMinimum(word string) (ans int) {
131131
}
132132
```
133133

134+
### **TypeScript**
135+
136+
```ts
137+
function addMinimum(word: string): number {
138+
const s: string = 'abc';
139+
let ans: number = 0;
140+
const n: number = word.length;
141+
for (let i = 0, j = 0; j < n; i = (i + 1) % 3) {
142+
if (word[j] !== s[i]) {
143+
++ans;
144+
} else {
145+
++j;
146+
}
147+
}
148+
if (word[n - 1] === 'b') {
149+
++ans;
150+
} else if (word[n - 1] === 'a') {
151+
ans += 2;
152+
}
153+
return ans;
154+
}
155+
```
156+
134157
### **...**
135158

136159
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function addMinimum(word: string): number {
2+
const s: string = 'abc';
3+
let ans: number = 0;
4+
const n: number = word.length;
5+
for (let i = 0, j = 0; j < n; i = (i + 1) % 3) {
6+
if (word[j] !== s[i]) {
7+
++ans;
8+
} else {
9+
++j;
10+
}
11+
}
12+
if (word[n - 1] === 'b') {
13+
++ans;
14+
} else if (word[n - 1] === 'a') {
15+
ans += 2;
16+
}
17+
return ans;
18+
}

0 commit comments

Comments
 (0)