Skip to content

Commit fd94208

Browse files
authored
feat: add solutions to lc problems: No.2397,2487,2980+ (#2174)
1 parent 8077754 commit fd94208

File tree

29 files changed

+1391
-79
lines changed

29 files changed

+1391
-79
lines changed

solution/2300-2399/2397.Maximum Rows Covered by Columns/README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
**方法一:二进制枚举**
7272

73-
我们先将矩阵中的每一行转换成一个二进制数,记录在数组 $rows$ 中,其中 $rows[i]$ 表示第 $i$ 行对应的二进制数,而 $rows[i]$ 的第 $j$ 位表示第 $i$ 行第 $j$ 列的值。
73+
我们先将矩阵中的每一行转换成一个二进制数,记录在数组 $rows$ 中,其中 $rows[i]$ 表示第 $i$ 行对应的二进制数,而 $rows[i]$ 这个二进制数的第 $j$ 位表示第 $i$ 行第 $j$ 列的值。
7474

7575
接下来,我们枚举所有的 $2^n$ 种列选择方案,其中 $n$ 为矩阵的列数。对于每一种列选择方案,我们判断是否选中了 `numSelect` 列,如果不是,则跳过。否则,我们统计矩阵中有多少行中的所有 $1$ 都被选中的列覆盖,即统计有多少行的二进制数 $rows[i]$ 与列选择方案 $mask$ 按位与的结果等于 $rows[i]$,并更新最大的行数。
7676

@@ -199,7 +199,40 @@ func maximumRows(matrix [][]int, numSelect int) (ans int) {
199199
### **TypeScript**
200200

201201
```ts
202+
function maximumRows(matrix: number[][], numSelect: number): number {
203+
const [m, n] = [matrix.length, matrix[0].length];
204+
const rows: number[] = Array(m).fill(0);
205+
for (let i = 0; i < m; ++i) {
206+
for (let j = 0; j < n; ++j) {
207+
if (matrix[i][j]) {
208+
rows[i] |= 1 << j;
209+
}
210+
}
211+
}
212+
let ans = 0;
213+
for (let mask = 1; mask < 1 << n; ++mask) {
214+
if (bitCount(mask) !== numSelect) {
215+
continue;
216+
}
217+
let t = 0;
218+
for (const x of rows) {
219+
if ((x & mask) === x) {
220+
++t;
221+
}
222+
}
223+
ans = Math.max(ans, t);
224+
}
225+
return ans;
226+
}
202227

228+
function bitCount(i: number): number {
229+
i = i - ((i >>> 1) & 0x55555555);
230+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
231+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
232+
i = i + (i >>> 8);
233+
i = i + (i >>> 16);
234+
return i & 0x3f;
235+
}
203236
```
204237

205238
### **...**

solution/2300-2399/2397.Maximum Rows Covered by Columns/README_EN.md

+41
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ Therefore, we return 2.
5555

5656
## Solutions
5757

58+
**Solution 1: Binary Enumeration**
59+
60+
First, we convert each row of the matrix into a binary number and record it in the array $rows$. Here, $rows[i]$ represents the binary number corresponding to the $i$-th row, and the $j$-th bit of this binary number $rows[i]$ represents the value of the $i$-th row and $j$-th column.
61+
62+
Next, we enumerate all $2^n$ column selection schemes, where $n$ is the number of columns in the matrix. For each column selection scheme, we check whether `numSelect` columns have been selected. If not, we skip it. Otherwise, we count how many rows in the matrix are covered by the selected columns, i.e., how many binary numbers $rows[i]$ are equal to the bitwise AND of $rows[i]$ and the column selection scheme $mask$. We then update the maximum number of rows.
63+
64+
The time complexity is $O(2^n \times m)$, and the space complexity is $O(m)$. Where $m$ and $n$ are the number of rows and columns in the matrix, respectively.
65+
5866
<!-- tabs:start -->
5967

6068
### **Python3**
@@ -174,7 +182,40 @@ func maximumRows(matrix [][]int, numSelect int) (ans int) {
174182
### **TypeScript**
175183

176184
```ts
185+
function maximumRows(matrix: number[][], numSelect: number): number {
186+
const [m, n] = [matrix.length, matrix[0].length];
187+
const rows: number[] = Array(m).fill(0);
188+
for (let i = 0; i < m; ++i) {
189+
for (let j = 0; j < n; ++j) {
190+
if (matrix[i][j]) {
191+
rows[i] |= 1 << j;
192+
}
193+
}
194+
}
195+
let ans = 0;
196+
for (let mask = 1; mask < 1 << n; ++mask) {
197+
if (bitCount(mask) !== numSelect) {
198+
continue;
199+
}
200+
let t = 0;
201+
for (const x of rows) {
202+
if ((x & mask) === x) {
203+
++t;
204+
}
205+
}
206+
ans = Math.max(ans, t);
207+
}
208+
return ans;
209+
}
177210

211+
function bitCount(i: number): number {
212+
i = i - ((i >>> 1) & 0x55555555);
213+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
214+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
215+
i = i + (i >>> 8);
216+
i = i + (i >>> 16);
217+
return i & 0x3f;
218+
}
178219
```
179220

180221
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function maximumRows(matrix: number[][], numSelect: number): number {
2+
const [m, n] = [matrix.length, matrix[0].length];
3+
const rows: number[] = Array(m).fill(0);
4+
for (let i = 0; i < m; ++i) {
5+
for (let j = 0; j < n; ++j) {
6+
if (matrix[i][j]) {
7+
rows[i] |= 1 << j;
8+
}
9+
}
10+
}
11+
let ans = 0;
12+
for (let mask = 1; mask < 1 << n; ++mask) {
13+
if (bitCount(mask) !== numSelect) {
14+
continue;
15+
}
16+
let t = 0;
17+
for (const x of rows) {
18+
if ((x & mask) === x) {
19+
++t;
20+
}
21+
}
22+
ans = Math.max(ans, t);
23+
}
24+
return ans;
25+
}
26+
27+
function bitCount(i: number): number {
28+
i = i - ((i >>> 1) & 0x55555555);
29+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
30+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
31+
i = i + (i >>> 8);
32+
i = i + (i >>> 16);
33+
return i & 0x3f;
34+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
class Solution:
2-
def maximumRobots(
3-
self, chargeTimes: List[int], runningCosts: List[int], budget: int
4-
) -> int:
5-
q = deque()
6-
ans = j = s = 0
7-
for i, (a, b) in enumerate(zip(chargeTimes, runningCosts)):
8-
while q and chargeTimes[q[-1]] <= a:
9-
q.pop()
10-
q.append(i)
11-
s += b
12-
while q and chargeTimes[q[0]] + (i - j + 1) * s > budget:
13-
if q[0] == j:
14-
q.popleft()
15-
s -= runningCosts[j]
16-
j += 1
17-
ans = max(ans, i - j + 1)
18-
return ans
1+
class Solution:
2+
def maximumRobots(
3+
self, chargeTimes: List[int], runningCosts: List[int], budget: int
4+
) -> int:
5+
q = deque()
6+
ans = j = s = 0
7+
for i, (a, b) in enumerate(zip(chargeTimes, runningCosts)):
8+
while q and chargeTimes[q[-1]] <= a:
9+
q.pop()
10+
q.append(i)
11+
s += b
12+
while q and chargeTimes[q[0]] + (i - j + 1) * s > budget:
13+
if q[0] == j:
14+
q.popleft()
15+
s -= runningCosts[j]
16+
j += 1
17+
ans = max(ans, i - j + 1)
18+
return ans

solution/2400-2499/2487.Remove Nodes From Linked List/README.md

+45-6
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@
5050

5151
**方法一:单调栈模拟**
5252

53-
我们可以先将链表中的节点值存入数组,然后遍历数组,维护一个从栈底到栈顶单调递减的栈,如果当前元素比栈顶元素大,则将栈顶元素出栈,直到当前元素小于等于栈顶元素,将当前元素入栈。最后将栈中的元素逆序,构造得到的链表即为答案
53+
我们可以先将链表中的节点值存入数组 $nums$,然后遍历数组 $nums$,维护一个从栈底到栈顶单调递减的栈 $stk$,如果当前元素比栈顶元素大,则将栈顶元素出栈,直到当前元素小于等于栈顶元素,将当前元素入栈。
5454

55-
时间复杂度为 $O(n)$,空间复杂度为 $O(n)$。
55+
最后,我们从栈底到栈顶构造出结果链表,即为答案。
56+
57+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是链表的长度。
5658

5759
<!-- tabs:start -->
5860

@@ -109,15 +111,15 @@ class Solution {
109111
}
110112
Deque<Integer> stk = new ArrayDeque<>();
111113
for (int v : nums) {
112-
while (!stk.isEmpty() && stk.peek() < v) {
113-
stk.pop();
114+
while (!stk.isEmpty() && stk.peekLast() < v) {
115+
stk.pollLast();
114116
}
115-
stk.push(v);
117+
stk.offerLast(v);
116118
}
117119
ListNode dummy = new ListNode();
118120
head = dummy;
119121
while (!stk.isEmpty()) {
120-
head.next = new ListNode(stk.pollLast());
122+
head.next = new ListNode(stk.pollFirst());
121123
head = head.next;
122124
}
123125
return dummy.next;
@@ -197,6 +199,43 @@ func removeNodes(head *ListNode) *ListNode {
197199
}
198200
```
199201

202+
### **TypeScript**
203+
204+
```ts
205+
/**
206+
* Definition for singly-linked list.
207+
* class ListNode {
208+
* val: number
209+
* next: ListNode | null
210+
* constructor(val?: number, next?: ListNode | null) {
211+
* this.val = (val===undefined ? 0 : val)
212+
* this.next = (next===undefined ? null : next)
213+
* }
214+
* }
215+
*/
216+
217+
function removeNodes(head: ListNode | null): ListNode | null {
218+
const nums = [];
219+
for (; head; head = head.next) {
220+
nums.push(head.val);
221+
}
222+
const stk: number[] = [];
223+
for (const v of nums) {
224+
while (stk.length && stk.at(-1)! < v) {
225+
stk.pop();
226+
}
227+
stk.push(v);
228+
}
229+
const dummy = new ListNode();
230+
head = dummy;
231+
for (const v of stk) {
232+
head.next = new ListNode(v);
233+
head = head.next;
234+
}
235+
return dummy.next;
236+
}
237+
```
238+
200239
### **...**
201240

202241
```

solution/2400-2499/2487.Remove Nodes From Linked List/README_EN.md

+49-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040

4141
## Solutions
4242

43+
**Solution 1: Monotonic Stack Simulation**
44+
45+
We can first store the node values of the linked list into an array $nums$. Then, we traverse the array $nums$, maintaining a stack $stk$ that is monotonically decreasing from the bottom to the top. If the current element is larger than the top element of the stack, we pop the top element of the stack until the current element is less than or equal to the top element, and then we push the current element into the stack.
46+
47+
Finally, we construct the resulting linked list from the bottom to the top of the stack, which is the answer.
48+
49+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the linked list.
50+
4351
<!-- tabs:start -->
4452

4553
### **Python3**
@@ -91,15 +99,15 @@ class Solution {
9199
}
92100
Deque<Integer> stk = new ArrayDeque<>();
93101
for (int v : nums) {
94-
while (!stk.isEmpty() && stk.peek() < v) {
95-
stk.pop();
102+
while (!stk.isEmpty() && stk.peekLast() < v) {
103+
stk.pollLast();
96104
}
97-
stk.push(v);
105+
stk.offerLast(v);
98106
}
99107
ListNode dummy = new ListNode();
100108
head = dummy;
101109
while (!stk.isEmpty()) {
102-
head.next = new ListNode(stk.pollLast());
110+
head.next = new ListNode(stk.pollFirst());
103111
head = head.next;
104112
}
105113
return dummy.next;
@@ -179,6 +187,43 @@ func removeNodes(head *ListNode) *ListNode {
179187
}
180188
```
181189

190+
### **TypeScript**
191+
192+
```ts
193+
/**
194+
* Definition for singly-linked list.
195+
* class ListNode {
196+
* val: number
197+
* next: ListNode | null
198+
* constructor(val?: number, next?: ListNode | null) {
199+
* this.val = (val===undefined ? 0 : val)
200+
* this.next = (next===undefined ? null : next)
201+
* }
202+
* }
203+
*/
204+
205+
function removeNodes(head: ListNode | null): ListNode | null {
206+
const nums = [];
207+
for (; head; head = head.next) {
208+
nums.push(head.val);
209+
}
210+
const stk: number[] = [];
211+
for (const v of nums) {
212+
while (stk.length && stk.at(-1)! < v) {
213+
stk.pop();
214+
}
215+
stk.push(v);
216+
}
217+
const dummy = new ListNode();
218+
head = dummy;
219+
for (const v of stk) {
220+
head.next = new ListNode(v);
221+
head = head.next;
222+
}
223+
return dummy.next;
224+
}
225+
```
226+
182227
### **...**
183228

184229
```

0 commit comments

Comments
 (0)