Skip to content

Commit 72762dd

Browse files
add 1670
1 parent a90a210 commit 72762dd

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ _If you like this project, please leave me a star._ ★
3838
|1675|[Minimize Deviation in Array](https://leetcode.com/problems/minimize-deviation-in-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1675.java) ||Hard|Heap, Ordered Map|
3939
|1673|[Find the Most Competitive Subsequence](https://leetcode.com/problems/find-the-most-competitive-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1673.java) ||Medium|Stack, Greedy|
4040
|1672|[Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1672.java) ||Easy|Array|
41+
|1670|[Design Front Middle Back Queue](https://leetcode.com/problems/design-front-middle-back-queue/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1670.java) ||Medium|Linked List, Design, Dequeu|
4142
|1669|[Merge In Between Linked Lists](https://leetcode.com/problems/merge-in-between-linked-lists/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1669.java) ||Medium|LinedList|
4243
|1668|[Maximum Repeating Substring](https://leetcode.com/problems/maximum-repeating-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1668.java) ||Easy|String|
4344
|1664|[Ways to Make a Fair Array](https://leetcode.com/problems/ways-to-make-a-fair-array/)|[Javascript](./javascript/_1664.js) ||Medium|Greedy|
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _1670 {
7+
public static class Solution1 {
8+
/**
9+
* This is a brute force approach.
10+
* TODO: use two Deques to implement a solution.
11+
*/
12+
public static class FrontMiddleBackQueue {
13+
List<Integer> list;
14+
15+
public FrontMiddleBackQueue() {
16+
list = new ArrayList<>();
17+
}
18+
19+
public void pushFront(int val) {
20+
list.add(0, val);
21+
}
22+
23+
public void pushMiddle(int val) {
24+
list.add(list.size() / 2, val);
25+
}
26+
27+
public void pushBack(int val) {
28+
list.add(val);
29+
}
30+
31+
public int popFront() {
32+
if (list.size() > 0) {
33+
return list.remove(0);
34+
}
35+
return -1;
36+
}
37+
38+
public int popMiddle() {
39+
if (list.size() > 0) {
40+
return list.remove(list.size() % 2 == 0 ? list.size() / 2 - 1 : list.size() / 2);
41+
}
42+
return -1;
43+
}
44+
45+
public int popBack() {
46+
if (list.size() > 0) {
47+
return list.remove(list.size() - 1);
48+
}
49+
return -1;
50+
}
51+
}
52+
}
53+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1670;
4+
import com.fishercoder.solutions._62;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static junit.framework.Assert.assertEquals;
9+
10+
public class _1670Test {
11+
private static _1670.Solution1.FrontMiddleBackQueue solution1;
12+
13+
@Test
14+
public void test1() {
15+
solution1 = new _1670.Solution1.FrontMiddleBackQueue();
16+
solution1.pushFront(1);
17+
solution1.pushBack(2);
18+
solution1.pushMiddle(3);
19+
solution1.pushMiddle(4);
20+
assertEquals(1, solution1.popFront());
21+
assertEquals(3, solution1.popMiddle());
22+
assertEquals(4, solution1.popMiddle());
23+
assertEquals(2, solution1.popFront());
24+
}
25+
}

0 commit comments

Comments
 (0)