Skip to content

Commit 96e133d

Browse files
add 2487
1 parent ac2a849 commit 96e133d

File tree

3 files changed

+85
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+85
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
| 2506 | [Count Pairs Of Similar Strings](https://leetcode.com/problems/count-pairs-of-similar-strings/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2506.java) || Easy ||
4444
| 2496 | [Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2496.java) || Easy ||
4545
| 2492 | [Minimum Score of a Path Between Two Cities](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2492.java) || Medium | Union Find
46+
| 2487 | [Remove Nodes From Linked List](https://leetcode.com/problems/remove-nodes-from-linked-list/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2487.java) || Medium |LinkedList, Stack
4647
| 2485 | [Find the Pivot Integer](https://leetcode.com/problems/find-the-pivot-integer/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2485.java) || Easy ||
4748
| 2467 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2469.java) || Easy ||
4849
| 2455 | [Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2455.java) || Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.Deque;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
10+
public class _2487 {
11+
public static class Solution1 {
12+
/**
13+
* This is sort of cheating, i.e. transforming the linked list into an array instead of operating on the linked list itself.
14+
*/
15+
public ListNode removeNodes(ListNode head) {
16+
List<Integer> list = getList(head);
17+
Deque<Integer> rightBiggest = getRightBiggest(list);
18+
ListNode pre = new ListNode(-1);
19+
ListNode tmp = pre;
20+
for (int i = 0; i < list.size(); i++) {
21+
if (list.get(i) >= rightBiggest.pollFirst()) {
22+
tmp.next = new ListNode(list.get(i));
23+
tmp = tmp.next;
24+
}
25+
}
26+
return pre.next;
27+
}
28+
29+
private Deque<Integer> getRightBiggest(List<Integer> list) {
30+
Deque<Integer> result = new LinkedList<>();
31+
int max = list.get(list.size() - 1);
32+
result.addFirst(max);
33+
for (int i = list.size() - 2; i >= 0; i--) {
34+
max = Math.max(max, list.get(i));
35+
result.addFirst(max);
36+
}
37+
return result;
38+
}
39+
40+
private List<Integer> getList(ListNode head) {
41+
ListNode tmp = head;
42+
List<Integer> list = new ArrayList<>();
43+
while (tmp != null) {
44+
list.add(tmp.val);
45+
tmp = tmp.next;
46+
}
47+
return list;
48+
}
49+
}
50+
51+
public static class Solution2 {
52+
//TODO: use stack to solve this problem
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.common.utils.LinkedListUtils;
4+
import com.fishercoder.solutions.thirdthousand._2487;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class _2487Test {
11+
private static _2487.Solution1 solution1;
12+
13+
@BeforeEach
14+
public void setup() {
15+
solution1 = new _2487.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(LinkedListUtils.contructLinkedList(new int[]{13, 8}), solution1.removeNodes(LinkedListUtils.contructLinkedList(new int[]{5, 2, 13, 3, 8})));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(LinkedListUtils.contructLinkedList(new int[]{998, 961, 943, 920, 698}),
26+
solution1.removeNodes(LinkedListUtils.contructLinkedList(
27+
new int[]{138, 466, 216, 67, 642, 978, 264, 136, 463, 331, 60, 600, 223, 275, 856, 809, 167, 101, 846, 165, 575, 276, 409, 590, 733, 200, 839, 515, 852, 615, 8, 584, 250, 337, 537, 63, 797, 900, 670, 636, 112, 701, 334, 422, 780, 552, 912, 506, 313, 474, 183, 792, 822, 661, 37, 164, 601, 271, 902, 792, 501, 184, 559, 140, 506, 94, 161, 167, 622, 288, 457, 953, 700, 464, 785, 203, 729, 725, 422, 76, 191, 195, 157, 854, 730, 577, 503, 401, 517, 692, 42, 135, 823, 883, 255, 111, 334, 365, 513, 338, 65, 600, 926, 607, 193, 763, 366, 674, 145, 229, 700, 11, 984, 36, 185, 475, 204, 604, 191, 898, 876, 762, 654, 770, 774, 575, 276, 165, 610, 649, 235, 749, 440, 607, 962, 747, 891, 943, 839, 403, 655, 22, 705, 416, 904, 765, 905, 574, 214, 471, 451, 774, 41, 365, 703, 895, 327, 879, 414, 821, 363, 30, 130, 14, 754, 41, 494, 548, 76, 825, 899, 499, 188, 982, 8, 890, 563, 438, 363, 32, 482, 623, 864, 161, 962, 678, 414, 659, 612, 332, 164, 580, 14, 633, 842, 969, 792, 777, 705, 436, 750, 501, 395, 342, 838, 493, 998, 112, 660, 961, 943, 721, 480, 522, 133, 129, 276, 362, 616, 52, 117, 300, 274, 862, 487, 715, 272, 232, 543, 275, 68, 144, 656, 623, 317, 63, 908, 565, 880, 12, 920, 467, 559, 91, 698})));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)