Skip to content

Commit d799290

Browse files
authored
feat: update solutions to lc problems (#2178)
* No.2487.Remove Nodes From Linked List * No.2806.Account Balance After Rounded Purchase * No.2807.Insert Greatest Common Divisors in Linked List * No.2808.Minimum Seconds to Equalize a Circular Array * No.2809.Minimum Time to Make Array Sum At Most x * No.2810.Faulty Keyboard * No.2811.Check if it is Possible to Split Array * No.2985.Calculate Compressed Mean * No.2987.Find Expensive Cities * No.2988.Manager of the Largest Department
1 parent 4466cae commit d799290

File tree

25 files changed

+232
-217
lines changed

25 files changed

+232
-217
lines changed

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

+20-38
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,13 @@ class Solution:
101101
# self.next = next
102102
class Solution:
103103
def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
104-
dummy = ListNode(next=head)
104+
dummy = ListNode(inf, head)
105105
cur = head
106-
stk = []
106+
stk = [dummy]
107107
while cur:
108-
while stk and stk[-1].val < cur.val:
108+
while stk[-1].val < cur.val:
109109
stk.pop()
110-
if stk:
111-
stk[-1].next = cur
112-
else:
113-
dummy.next = cur
110+
stk[-1].next = cur
114111
stk.append(cur)
115112
cur = cur.next
116113
return dummy.next
@@ -169,17 +166,14 @@ class Solution {
169166
*/
170167
class Solution {
171168
public ListNode removeNodes(ListNode head) {
172-
ListNode dummy = new ListNode(0, head);
169+
ListNode dummy = new ListNode(1 << 30, head);
173170
Deque<ListNode> stk = new ArrayDeque<>();
171+
stk.offerLast(dummy);
174172
for (ListNode cur = head; cur != null; cur = cur.next) {
175-
while (!stk.isEmpty() && stk.peekLast().val < cur.val) {
173+
while (stk.peekLast().val < cur.val) {
176174
stk.pollLast();
177175
}
178-
if (!stk.isEmpty()) {
179-
stk.peekLast().next = cur;
180-
} else {
181-
dummy.next = cur;
182-
}
176+
stk.peekLast().next = cur;
183177
stk.offerLast(cur);
184178
}
185179
return dummy.next;
@@ -240,18 +234,14 @@ public:
240234
class Solution {
241235
public:
242236
ListNode* removeNodes(ListNode* head) {
243-
ListNode* dummy = new ListNode(0, head);
237+
ListNode* dummy = new ListNode(1e9, head);
244238
ListNode* cur = head;
245-
vector<ListNode*> stk;
239+
vector<ListNode*> stk = {dummy};
246240
for (ListNode* cur = head; cur; cur = cur->next) {
247-
while (stk.size() && stk.back()->val < cur->val) {
241+
while (stk.back()->val < cur->val) {
248242
stk.pop_back();
249243
}
250-
if (stk.size()) {
251-
stk.back()->next = cur;
252-
} else {
253-
dummy->next = cur;
254-
}
244+
stk.back()->next = cur;
255245
stk.push_back(cur);
256246
}
257247
return dummy->next;
@@ -301,17 +291,13 @@ func removeNodes(head *ListNode) *ListNode {
301291
* }
302292
*/
303293
func removeNodes(head *ListNode) *ListNode {
304-
dummy := &ListNode{Next: head}
305-
stk := []*ListNode{}
294+
dummy := &ListNode{1 << 30, head}
295+
stk := []*ListNode{dummy}
306296
for cur := head; cur != nil; cur = cur.Next {
307-
for len(stk) > 0 && stk[len(stk)-1].Val < cur.Val {
297+
for stk[len(stk)-1].Val < cur.Val {
308298
stk = stk[:len(stk)-1]
309299
}
310-
if len(stk) > 0 {
311-
stk[len(stk)-1].Next = cur
312-
} else {
313-
dummy.Next = cur
314-
}
300+
stk[len(stk)-1].Next = cur
315301
stk = append(stk, cur)
316302
}
317303
return dummy.Next
@@ -369,17 +355,13 @@ function removeNodes(head: ListNode | null): ListNode | null {
369355
*/
370356

371357
function removeNodes(head: ListNode | null): ListNode | null {
372-
const dummy = new ListNode(0, head);
373-
const stk: ListNode[] = [];
358+
const dummy = new ListNode(Infinity, head);
359+
const stk: ListNode[] = [dummy];
374360
for (let cur = head; cur; cur = cur.next) {
375-
while (stk.length && stk.at(-1)!.val < cur.val) {
361+
while (stk.at(-1)!.val < cur.val) {
376362
stk.pop();
377363
}
378-
if (stk.length) {
379-
stk.at(-1)!.next = cur;
380-
} else {
381-
dummy.next = cur;
382-
}
364+
stk.at(-1)!.next = cur;
383365
stk.push(cur);
384366
}
385367
return dummy.next;

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

+20-38
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,13 @@ class Solution:
9191
# self.next = next
9292
class Solution:
9393
def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
94-
dummy = ListNode(next=head)
94+
dummy = ListNode(inf, head)
9595
cur = head
96-
stk = []
96+
stk = [dummy]
9797
while cur:
98-
while stk and stk[-1].val < cur.val:
98+
while stk[-1].val < cur.val:
9999
stk.pop()
100-
if stk:
101-
stk[-1].next = cur
102-
else:
103-
dummy.next = cur
100+
stk[-1].next = cur
104101
stk.append(cur)
105102
cur = cur.next
106103
return dummy.next
@@ -157,17 +154,14 @@ class Solution {
157154
*/
158155
class Solution {
159156
public ListNode removeNodes(ListNode head) {
160-
ListNode dummy = new ListNode(0, head);
157+
ListNode dummy = new ListNode(1 << 30, head);
161158
Deque<ListNode> stk = new ArrayDeque<>();
159+
stk.offerLast(dummy);
162160
for (ListNode cur = head; cur != null; cur = cur.next) {
163-
while (!stk.isEmpty() && stk.peekLast().val < cur.val) {
161+
while (stk.peekLast().val < cur.val) {
164162
stk.pollLast();
165163
}
166-
if (!stk.isEmpty()) {
167-
stk.peekLast().next = cur;
168-
} else {
169-
dummy.next = cur;
170-
}
164+
stk.peekLast().next = cur;
171165
stk.offerLast(cur);
172166
}
173167
return dummy.next;
@@ -228,18 +222,14 @@ public:
228222
class Solution {
229223
public:
230224
ListNode* removeNodes(ListNode* head) {
231-
ListNode* dummy = new ListNode(0, head);
225+
ListNode* dummy = new ListNode(1e9, head);
232226
ListNode* cur = head;
233-
vector<ListNode*> stk;
227+
vector<ListNode*> stk = {dummy};
234228
for (ListNode* cur = head; cur; cur = cur->next) {
235-
while (stk.size() && stk.back()->val < cur->val) {
229+
while (stk.back()->val < cur->val) {
236230
stk.pop_back();
237231
}
238-
if (stk.size()) {
239-
stk.back()->next = cur;
240-
} else {
241-
dummy->next = cur;
242-
}
232+
stk.back()->next = cur;
243233
stk.push_back(cur);
244234
}
245235
return dummy->next;
@@ -289,17 +279,13 @@ func removeNodes(head *ListNode) *ListNode {
289279
* }
290280
*/
291281
func removeNodes(head *ListNode) *ListNode {
292-
dummy := &ListNode{Next: head}
293-
stk := []*ListNode{}
282+
dummy := &ListNode{1 << 30, head}
283+
stk := []*ListNode{dummy}
294284
for cur := head; cur != nil; cur = cur.Next {
295-
for len(stk) > 0 && stk[len(stk)-1].Val < cur.Val {
285+
for stk[len(stk)-1].Val < cur.Val {
296286
stk = stk[:len(stk)-1]
297287
}
298-
if len(stk) > 0 {
299-
stk[len(stk)-1].Next = cur
300-
} else {
301-
dummy.Next = cur
302-
}
288+
stk[len(stk)-1].Next = cur
303289
stk = append(stk, cur)
304290
}
305291
return dummy.Next
@@ -357,17 +343,13 @@ function removeNodes(head: ListNode | null): ListNode | null {
357343
*/
358344

359345
function removeNodes(head: ListNode | null): ListNode | null {
360-
const dummy = new ListNode(0, head);
361-
const stk: ListNode[] = [];
346+
const dummy = new ListNode(Infinity, head);
347+
const stk: ListNode[] = [dummy];
362348
for (let cur = head; cur; cur = cur.next) {
363-
while (stk.length && stk.at(-1)!.val < cur.val) {
349+
while (stk.at(-1)!.val < cur.val) {
364350
stk.pop();
365351
}
366-
if (stk.length) {
367-
stk.at(-1)!.next = cur;
368-
} else {
369-
dummy.next = cur;
370-
}
352+
stk.at(-1)!.next = cur;
371353
stk.push(cur);
372354
}
373355
return dummy.next;
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1-
/**
2-
* Definition for singly-linked list.
3-
* struct ListNode {
4-
* int val;
5-
* ListNode *next;
6-
* ListNode() : val(0), next(nullptr) {}
7-
* ListNode(int x) : val(x), next(nullptr) {}
8-
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9-
* };
10-
*/
11-
class Solution {
12-
public:
13-
ListNode* removeNodes(ListNode* head) {
14-
vector<int> nums;
15-
while (head) {
16-
nums.emplace_back(head->val);
17-
head = head->next;
18-
}
19-
vector<int> stk;
20-
for (int v : nums) {
21-
while (!stk.empty() && stk.back() < v) {
22-
stk.pop_back();
23-
}
24-
stk.push_back(v);
25-
}
26-
ListNode* dummy = new ListNode();
27-
head = dummy;
28-
for (int v : stk) {
29-
head->next = new ListNode(v);
30-
head = head->next;
31-
}
32-
return dummy->next;
33-
}
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* removeNodes(ListNode* head) {
14+
ListNode* dummy = new ListNode(1e9, head);
15+
ListNode* cur = head;
16+
vector<ListNode*> stk = {dummy};
17+
for (ListNode* cur = head; cur; cur = cur->next) {
18+
while (stk.back()->val < cur->val) {
19+
stk.pop_back();
20+
}
21+
stk.back()->next = cur;
22+
stk.push_back(cur);
23+
}
24+
return dummy->next;
25+
}
3426
};

solution/2400-2499/2487.Remove Nodes From Linked List/Solution.go

+6-15
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,14 @@
66
* }
77
*/
88
func removeNodes(head *ListNode) *ListNode {
9-
nums := []int{}
10-
for head != nil {
11-
nums = append(nums, head.Val)
12-
head = head.Next
13-
}
14-
stk := []int{}
15-
for _, v := range nums {
16-
for len(stk) > 0 && stk[len(stk)-1] < v {
9+
dummy := &ListNode{1 << 30, head}
10+
stk := []*ListNode{dummy}
11+
for cur := head; cur != nil; cur = cur.Next {
12+
for stk[len(stk)-1].Val < cur.Val {
1713
stk = stk[:len(stk)-1]
1814
}
19-
stk = append(stk, v)
20-
}
21-
dummy := &ListNode{}
22-
head = dummy
23-
for _, v := range stk {
24-
head.Next = &ListNode{Val: v}
25-
head = head.Next
15+
stk[len(stk)-1].Next = cur
16+
stk = append(stk, cur)
2617
}
2718
return dummy.Next
2819
}

solution/2400-2499/2487.Remove Nodes From Linked List/Solution.java

+7-15
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,15 @@
1010
*/
1111
class Solution {
1212
public ListNode removeNodes(ListNode head) {
13-
List<Integer> nums = new ArrayList<>();
14-
while (head != null) {
15-
nums.add(head.val);
16-
head = head.next;
17-
}
18-
Deque<Integer> stk = new ArrayDeque<>();
19-
for (int v : nums) {
20-
while (!stk.isEmpty() && stk.peekLast() < v) {
13+
ListNode dummy = new ListNode(1 << 30, head);
14+
Deque<ListNode> stk = new ArrayDeque<>();
15+
stk.offerLast(dummy);
16+
for (ListNode cur = head; cur != null; cur = cur.next) {
17+
while (stk.peekLast().val < cur.val) {
2118
stk.pollLast();
2219
}
23-
stk.offerLast(v);
24-
}
25-
ListNode dummy = new ListNode();
26-
head = dummy;
27-
while (!stk.isEmpty()) {
28-
head.next = new ListNode(stk.pollFirst());
29-
head = head.next;
20+
stk.peekLast().next = cur;
21+
stk.offerLast(cur);
3022
}
3123
return dummy.next;
3224
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
# Definition for singly-linked list.
2-
# class ListNode:
3-
# def __init__(self, val=0, next=None):
4-
# self.val = val
5-
# self.next = next
6-
class Solution:
7-
def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
8-
nums = []
9-
while head:
10-
nums.append(head.val)
11-
head = head.next
12-
stk = []
13-
for v in nums:
14-
while stk and stk[-1] < v:
15-
stk.pop()
16-
stk.append(v)
17-
dummy = ListNode()
18-
head = dummy
19-
for v in stk:
20-
head.next = ListNode(v)
21-
head = head.next
22-
return dummy.next
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
8+
dummy = ListNode(inf, head)
9+
cur = head
10+
stk = [dummy]
11+
while cur:
12+
while stk[-1].val < cur.val:
13+
stk.pop()
14+
stk[-1].next = cur
15+
stk.append(cur)
16+
cur = cur.next
17+
return dummy.next

0 commit comments

Comments
 (0)