Skip to content

Commit a76d40a

Browse files
authored
feat: update lc problems (#3310)
1 parent 3913dfb commit a76d40a

File tree

47 files changed

+424
-304
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+424
-304
lines changed

lcof2/剑指 Offer II 073. 狒狒吃香蕉/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,22 @@ class Solution {
189189
func minEatingSpeed(_ piles: [Int], _ h: Int) -> Int {
190190
var left = 1
191191
var right = piles.max() ?? 0
192-
192+
193193
while left < right {
194194
let mid = (left + right) / 2
195195
var hours = 0
196-
196+
197197
for pile in piles {
198198
hours += (pile + mid - 1) / mid
199199
}
200-
200+
201201
if hours <= h {
202202
right = mid
203203
} else {
204204
left = mid + 1
205205
}
206206
}
207-
207+
208208
return left
209209
}
210210
}

solution/2000-2099/2095.Delete the Middle Node of a Linked List/README.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ tags:
8181

8282
<!-- solution:start -->
8383

84-
### 方法一
84+
### 方法一:快慢指针
85+
86+
快慢指针法是一种用于解决链表中的问题的常用技巧。我们可以维护两个指针,一个慢指针 $\textit{slow}$ 和一个快指针 $\textit{fast}$。初始时 $\textit{slow}$ 指向一个虚拟节点,该虚拟节点的 $\textit{next}$ 指针指向链表的头节点 $\textit{head}$,而 $\textit{fast}$ 指向链表的头节点 $\textit{head}$。
87+
88+
然后,我们每次将慢指针向后移动一个位置,将快指针向后移动两个位置,直到快指针到达链表的末尾。此时,慢指针指向的节点的下一个节点就是链表的中间节点。我们将慢指针指向的节点的 $\textit{next}$ 指针指向下下个节点,即可删除中间节点。
89+
90+
时间复杂度 $O(n)$,其中 $n$ 是链表的长度。空间复杂度 $O(1)$。
8591

8692
<!-- tabs:start -->
8793

@@ -197,15 +203,14 @@ func deleteMiddle(head *ListNode) *ListNode {
197203
*/
198204

199205
function deleteMiddle(head: ListNode | null): ListNode | null {
200-
if (!head || !head.next) return null;
201-
let fast = head.next,
202-
slow = head;
203-
while (fast.next && fast.next.next) {
206+
const dummy = new ListNode(0, head);
207+
let [slow, fast] = [dummy, head];
208+
while (fast && fast.next) {
204209
slow = slow.next;
205210
fast = fast.next.next;
206211
}
207212
slow.next = slow.next.next;
208-
return head;
213+
return dummy.next;
209214
}
210215
```
211216

solution/2000-2099/2095.Delete the Middle Node of a Linked List/README_EN.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ Node 0 with value 2 is the only node remaining after removing node 1.</pre>
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Fast and Slow Pointers
77+
78+
The fast and slow pointer technique is a common method used to solve problems related to linked lists. We can maintain two pointers, a slow pointer $\textit{slow}$ and a fast pointer $\textit{fast}$. Initially, $\textit{slow}$ points to a dummy node, whose $\textit{next}$ pointer points to the head node $\textit{head}$ of the list, while $\textit{fast}$ points to the head node $\textit{head}$.
79+
80+
Then, we move the slow pointer one position backward and the fast pointer two positions backward each time, until the fast pointer reaches the end of the list. At this point, the node next to the node pointed by the slow pointer is the middle node of the list. We can remove the middle node by setting the $\textit{next}$ pointer of the node pointed by the slow pointer to point to the next next node.
81+
82+
The time complexity is $O(n)$, where $n$ is the length of the list. The space complexity is $O(1)$.
7783

7884
<!-- tabs:start -->
7985

@@ -189,15 +195,14 @@ func deleteMiddle(head *ListNode) *ListNode {
189195
*/
190196

191197
function deleteMiddle(head: ListNode | null): ListNode | null {
192-
if (!head || !head.next) return null;
193-
let fast = head.next,
194-
slow = head;
195-
while (fast.next && fast.next.next) {
198+
const dummy = new ListNode(0, head);
199+
let [slow, fast] = [dummy, head];
200+
while (fast && fast.next) {
196201
slow = slow.next;
197202
fast = fast.next.next;
198203
}
199204
slow.next = slow.next.next;
200-
return head;
205+
return dummy.next;
201206
}
202207
```
203208

solution/2000-2099/2095.Delete the Middle Node of a Linked List/Solution.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
*/
1212

1313
function deleteMiddle(head: ListNode | null): ListNode | null {
14-
if (!head || !head.next) return null;
15-
let fast = head.next,
16-
slow = head;
17-
while (fast.next && fast.next.next) {
14+
const dummy = new ListNode(0, head);
15+
let [slow, fast] = [dummy, head];
16+
while (fast && fast.next) {
1817
slow = slow.next;
1918
fast = fast.next.next;
2019
}
2120
slow.next = slow.next.next;
22-
return head;
21+
return dummy.next;
2322
}

solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/README.md

+42-5
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class Solution:
107107
else:
108108
mi1 = x
109109
ans = max(ans - mi1 + mx1, ans - mi2 + mx2, -1)
110-
return -1 if ans % 2 else ans
110+
return -1 if ans < 0 else ans
111111
```
112112

113113
#### Java
@@ -141,8 +141,8 @@ class Solution {
141141
mi1 = nums[i];
142142
}
143143
}
144-
ans = Math.max(-1, Math.max(ans - mi1 + mx1, ans - mi2 + mx2));
145-
return ans % 2 != 0 ? -1 : ans;
144+
ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2);
145+
return ans < 0 ? -1 : ans;
146146
}
147147
}
148148
```
@@ -180,7 +180,7 @@ public:
180180
}
181181
}
182182
ans = max(ans - mi1 + mx1, ans - mi2 + mx2);
183-
return ans % 2 || ans < 0 ? -1 : ans;
183+
return ans < 0 ? -1 : ans;
184184
}
185185
};
186186
```
@@ -216,13 +216,50 @@ func largestEvenSum(nums []int, k int) int64 {
216216
}
217217
}
218218
ans = max(-1, max(ans-mi1+mx1, ans-mi2+mx2))
219-
if ans%2 != 0 {
219+
if ans%2 < 0 {
220220
return -1
221221
}
222222
return int64(ans)
223223
}
224224
```
225225

226+
#### TypeScript
227+
228+
```ts
229+
function largestEvenSum(nums: number[], k: number): number {
230+
nums.sort((a, b) => a - b);
231+
let ans = 0;
232+
const n = nums.length;
233+
for (let i = 0; i < k; ++i) {
234+
ans += nums[n - i - 1];
235+
}
236+
if (ans % 2 === 0) {
237+
return ans;
238+
}
239+
const inf = 1 << 29;
240+
let mx1 = -inf,
241+
mx2 = -inf;
242+
for (let i = 0; i < n - k; ++i) {
243+
if (nums[i] % 2 === 1) {
244+
mx1 = nums[i];
245+
} else {
246+
mx2 = nums[i];
247+
}
248+
}
249+
let mi1 = inf,
250+
mi2 = inf;
251+
for (let i = n - 1; i >= n - k; --i) {
252+
if (nums[i] % 2 === 1) {
253+
mi2 = nums[i];
254+
} else {
255+
mi1 = nums[i];
256+
}
257+
}
258+
ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2);
259+
return ans < 0 ? -1 : ans;
260+
}
261+
```
262+
226263
<!-- tabs:end -->
227264

228265
<!-- solution:end -->

solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/README_EN.md

+56-6
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,20 @@ No subsequence of nums with length 1 has an even sum.
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Greedy + Sorting
71+
72+
We notice that the problem involves selecting a subsequence, so we can consider sorting the array first.
73+
74+
Next, we greedily select the largest $k$ numbers. If the sum of these numbers is even, we directly return this sum $ans$.
75+
76+
Otherwise, we have two greedy strategies:
77+
78+
1. Among the largest $k$ numbers, find the smallest even number $mi1$, and then among the remaining $n - k$ numbers, find the largest odd number $mx1$. Replace $mi1$ with $mx1$. If such a replacement exists, then the sum after replacement $ans - mi1 + mx1$ is guaranteed to be even;
79+
2. Among the largest $k$ numbers, find the smallest odd number $mi2$, and then among the remaining $n - k$ numbers, find the largest even number $mx2$. Replace $mi2$ with $mx2$. If such a replacement exists, then the sum after replacement $ans - mi2 + mx2$ is guaranteed to be even.
80+
81+
We take the largest even sum as the answer. If no even sum exists, return $-1$.
82+
83+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array.
7184

7285
<!-- tabs:start -->
7386

@@ -94,7 +107,7 @@ class Solution:
94107
else:
95108
mi1 = x
96109
ans = max(ans - mi1 + mx1, ans - mi2 + mx2, -1)
97-
return -1 if ans % 2 else ans
110+
return -1 if ans < 0 else ans
98111
```
99112

100113
#### Java
@@ -128,8 +141,8 @@ class Solution {
128141
mi1 = nums[i];
129142
}
130143
}
131-
ans = Math.max(-1, Math.max(ans - mi1 + mx1, ans - mi2 + mx2));
132-
return ans % 2 != 0 ? -1 : ans;
144+
ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2);
145+
return ans < 0 ? -1 : ans;
133146
}
134147
}
135148
```
@@ -167,7 +180,7 @@ public:
167180
}
168181
}
169182
ans = max(ans - mi1 + mx1, ans - mi2 + mx2);
170-
return ans % 2 || ans < 0 ? -1 : ans;
183+
return ans < 0 ? -1 : ans;
171184
}
172185
};
173186
```
@@ -203,13 +216,50 @@ func largestEvenSum(nums []int, k int) int64 {
203216
}
204217
}
205218
ans = max(-1, max(ans-mi1+mx1, ans-mi2+mx2))
206-
if ans%2 != 0 {
219+
if ans%2 < 0 {
207220
return -1
208221
}
209222
return int64(ans)
210223
}
211224
```
212225

226+
#### TypeScript
227+
228+
```ts
229+
function largestEvenSum(nums: number[], k: number): number {
230+
nums.sort((a, b) => a - b);
231+
let ans = 0;
232+
const n = nums.length;
233+
for (let i = 0; i < k; ++i) {
234+
ans += nums[n - i - 1];
235+
}
236+
if (ans % 2 === 0) {
237+
return ans;
238+
}
239+
const inf = 1 << 29;
240+
let mx1 = -inf,
241+
mx2 = -inf;
242+
for (let i = 0; i < n - k; ++i) {
243+
if (nums[i] % 2 === 1) {
244+
mx1 = nums[i];
245+
} else {
246+
mx2 = nums[i];
247+
}
248+
}
249+
let mi1 = inf,
250+
mi2 = inf;
251+
for (let i = n - 1; i >= n - k; --i) {
252+
if (nums[i] % 2 === 1) {
253+
mi2 = nums[i];
254+
} else {
255+
mi1 = nums[i];
256+
}
257+
}
258+
ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2);
259+
return ans < 0 ? -1 : ans;
260+
}
261+
```
262+
213263
<!-- tabs:end -->
214264

215265
<!-- solution:end -->

solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ class Solution {
2828
}
2929
}
3030
ans = max(ans - mi1 + mx1, ans - mi2 + mx2);
31-
return ans % 2 || ans < 0 ? -1 : ans;
31+
return ans < 0 ? -1 : ans;
3232
}
3333
};

solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func largestEvenSum(nums []int, k int) int64 {
2626
}
2727
}
2828
ans = max(-1, max(ans-mi1+mx1, ans-mi2+mx2))
29-
if ans%2 != 0 {
29+
if ans%2 < 0 {
3030
return -1
3131
}
3232
return int64(ans)

solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public long largestEvenSum(int[] nums, int k) {
2626
mi1 = nums[i];
2727
}
2828
}
29-
ans = Math.max(-1, Math.max(ans - mi1 + mx1, ans - mi2 + mx2));
30-
return ans % 2 != 0 ? -1 : ans;
29+
ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2);
30+
return ans < 0 ? -1 : ans;
3131
}
3232
}

solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ def largestEvenSum(self, nums: List[int], k: int) -> int:
1818
else:
1919
mi1 = x
2020
ans = max(ans - mi1 + mx1, ans - mi2 + mx2, -1)
21-
return -1 if ans % 2 else ans
21+
return -1 if ans < 0 else ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function largestEvenSum(nums: number[], k: number): number {
2+
nums.sort((a, b) => a - b);
3+
let ans = 0;
4+
const n = nums.length;
5+
for (let i = 0; i < k; ++i) {
6+
ans += nums[n - i - 1];
7+
}
8+
if (ans % 2 === 0) {
9+
return ans;
10+
}
11+
const inf = 1 << 29;
12+
let mx1 = -inf,
13+
mx2 = -inf;
14+
for (let i = 0; i < n - k; ++i) {
15+
if (nums[i] % 2 === 1) {
16+
mx1 = nums[i];
17+
} else {
18+
mx2 = nums[i];
19+
}
20+
}
21+
let mi1 = inf,
22+
mi2 = inf;
23+
for (let i = n - 1; i >= n - k; --i) {
24+
if (nums[i] % 2 === 1) {
25+
mi2 = nums[i];
26+
} else {
27+
mi1 = nums[i];
28+
}
29+
}
30+
ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2);
31+
return ans < 0 ? -1 : ans;
32+
}

0 commit comments

Comments
 (0)