Skip to content

Commit 8bce341

Browse files
add 3237
1 parent fa257a9 commit 8bce341

File tree

3 files changed

+101
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+101
-0
lines changed

Diff for: paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3237 | [Alt and Tab Simulation](https://leetcode.com/problems/alt-and-tab-simulation/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3237.java) | | Medium |
34
| 3234 | [Count the Number of Substrings With Dominant Ones](https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3234.java) | | Medium | Sliding Window
45
| 3233 | [Find the Count of Numbers Which Are Not Special](https://leetcode.com/problems/find-the-count-of-numbers-which-are-not-special/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3233.java) | | Medium | Math
56
| 3232 | [Find if Digit Game Can Be Won](https://leetcode.com/problems/find-if-digit-game-can-be-won/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3232.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _3237 {
7+
public static class Solution1 {
8+
/**
9+
* My completely original solution, very natural to think of doubly linked list + hashmap.
10+
*/
11+
public int[] simulationResult(int[] windows, int[] queries) {
12+
Map<Integer, DoublyLinkedListNode> map = new HashMap<>();
13+
DoublyLinkedListNode pre = buildList(windows, map);
14+
for (int q : queries) {
15+
moveToHead(q, pre, map);
16+
}
17+
return backToArray(pre, windows.length);
18+
}
19+
20+
private int[] backToArray(DoublyLinkedListNode pre, int length) {
21+
DoublyLinkedListNode tmp = pre;
22+
int[] ans = new int[length];
23+
for (int i = 0; i < length; i++) {
24+
ans[i] = tmp.next.val;
25+
tmp = tmp.next;
26+
}
27+
return ans;
28+
}
29+
30+
private void moveToHead(int q, DoublyLinkedListNode headPrev, Map<Integer, DoublyLinkedListNode> map) {
31+
DoublyLinkedListNode node = map.get(q);
32+
//if this window is already at the head, then we don't need to do anything
33+
if (headPrev.next == node) {
34+
return;
35+
}
36+
//get this node's next and prev pointers
37+
DoublyLinkedListNode next = node.next;
38+
DoublyLinkedListNode prev = node.prev;
39+
//connect it's next to its previous' next, essentially cutting the current node out of the chain
40+
prev.next = next;
41+
//in case this is tail, we don't need to re-assign its next pointer
42+
if (next != null) {
43+
next.prev = prev;
44+
}
45+
DoublyLinkedListNode oldHead = headPrev.next;
46+
headPrev.next = node;
47+
node.next = oldHead;
48+
oldHead.prev = node;
49+
}
50+
51+
private DoublyLinkedListNode buildList(int[] windows, Map<Integer, DoublyLinkedListNode> map) {
52+
DoublyLinkedListNode pre = new DoublyLinkedListNode(-1);
53+
DoublyLinkedListNode tmp = pre;
54+
for (int i = 0; i < windows.length; i++) {
55+
DoublyLinkedListNode next = new DoublyLinkedListNode(windows[i]);
56+
next.prev = tmp;
57+
tmp.next = next;
58+
map.put(windows[i], next);
59+
tmp = tmp.next;
60+
}
61+
return pre;
62+
}
63+
64+
public static class DoublyLinkedListNode {
65+
DoublyLinkedListNode prev;
66+
DoublyLinkedListNode next;
67+
int val;
68+
69+
public DoublyLinkedListNode(int val) {
70+
this.val = val;
71+
}
72+
}
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3237;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
8+
9+
public class _3237Test {
10+
private static _3237.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3237.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{2, 3, 1}, solution1.simulationResult(new int[]{1, 2, 3}, new int[]{3, 3, 2}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertArrayEquals(new int[]{3, 1, 4, 2}, solution1.simulationResult(new int[]{1, 4, 2, 3}, new int[]{4, 1, 3}));
25+
}
26+
}

0 commit comments

Comments
 (0)