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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 22 additions & 0 deletions
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
```

0 commit comments

Comments
 (0)