You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2000-2099/2095.Delete the Middle Node of a Linked List/README_EN.md
+11-6
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,13 @@ Node 0 with value 2 is the only node remaining after removing node 1.</pre>
73
73
74
74
<!-- solution:start -->
75
75
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)$.
Copy file name to clipboardexpand all lines: solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/README_EN.md
+56-6
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,20 @@ No subsequence of nums with length 1 has an even sum.
67
67
68
68
<!-- solution:start -->
69
69
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.
71
84
72
85
<!-- tabs:start -->
73
86
@@ -94,7 +107,7 @@ class Solution:
94
107
else:
95
108
mi1 = x
96
109
ans =max(ans - mi1 + mx1, ans - mi2 + mx2, -1)
97
-
return-1if ans %2else ans
110
+
return-1if ans <0else ans
98
111
```
99
112
100
113
#### Java
@@ -128,8 +141,8 @@ class Solution {
128
141
mi1 = nums[i];
129
142
}
130
143
}
131
-
ans =Math.max(-1, Math.max(ans - mi1 + mx1, ans - mi2 + mx2));
0 commit comments