Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit768a893

Browse files
add a solution for 716
1 parent4c640c0 commit768a893

File tree

2 files changed

+136
-73
lines changed

2 files changed

+136
-73
lines changed

‎src/main/java/com/fishercoder/solutions/_716.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
packagecom.fishercoder.solutions;
22

33
importjava.util.ArrayList;
4+
importjava.util.Deque;
45
importjava.util.Iterator;
6+
importjava.util.LinkedList;
57
importjava.util.List;
68
importjava.util.Stack;
79
importjava.util.TreeMap;
@@ -191,4 +193,60 @@ public int popMax() {
191193
}
192194
}
193195
}
196+
197+
publicstaticclassSolution3 {
198+
/**
199+
* My completely original solution on 10/25/2021.
200+
* popMax() takes O(n) time, all other operations take O(1) time.
201+
*/
202+
203+
publicstaticclassMaxStack {
204+
205+
Deque<int[]>stack;
206+
Deque<int[]>tmp;
207+
208+
publicMaxStack() {
209+
stack =newLinkedList<>();
210+
tmp =newLinkedList<>();
211+
}
212+
213+
publicvoidpush(intx) {
214+
if (stack.isEmpty()) {
215+
stack.addLast(newint[]{x,x});
216+
}else {
217+
int[]last =stack.peekLast();
218+
stack.addLast(newint[]{x,Math.max(last[1],x)});
219+
}
220+
}
221+
222+
publicintpop() {
223+
returnstack.pollLast()[0];
224+
}
225+
226+
publicinttop() {
227+
returnstack.peekLast()[0];
228+
}
229+
230+
publicintpeekMax() {
231+
returnstack.peekLast()[1];
232+
}
233+
234+
publicintpopMax() {
235+
tmp.clear();
236+
while (stack.peekLast()[0] !=stack.peekLast()[1]) {
237+
tmp.addLast(stack.pollLast());
238+
}
239+
int[]max =stack.pollLast();
240+
while (!tmp.isEmpty()) {
241+
int[]curr =tmp.pollLast();
242+
if (!stack.isEmpty()) {
243+
stack.addLast(newint[]{curr[0],Math.max(curr[0],stack.peekLast()[1])});
244+
}else {
245+
stack.addLast(newint[]{curr[0],curr[0]});
246+
}
247+
}
248+
returnmax[0];
249+
}
250+
}
251+
}
194252
}

‎src/test/java/com/fishercoder/_716Test.java

Lines changed: 78 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,84 +7,89 @@
77
importstaticorg.junit.Assert.assertEquals;
88

99
publicclass_716Test {
10-
privatestatic_716.Solution1.MaxStackmaxStackSolution1;
11-
privatestatic_716.Solution2.MaxStackmaxStackSolution2;
10+
privatestatic_716.Solution1.MaxStackmaxStackSolution1;
11+
privatestatic_716.Solution2.MaxStackmaxStackSolution2;
12+
privatestatic_716.Solution3.MaxStackmaxStackSolution3;
1213

13-
@Before
14-
publicvoidsetup() {
15-
maxStackSolution1 =new_716.Solution1.MaxStack();
16-
maxStackSolution2 =new_716.Solution2.MaxStack();
17-
}
14+
@Before
15+
publicvoidsetup() {
16+
maxStackSolution1 =new_716.Solution1.MaxStack();
17+
maxStackSolution2 =new_716.Solution2.MaxStack();
18+
maxStackSolution3 =new_716.Solution3.MaxStack();
19+
}
1820

19-
@Test
20-
publicvoidtest1() {
21-
maxStackSolution1.push(5);
22-
assertEquals(5,maxStackSolution1.peekMax());
23-
assertEquals(5,maxStackSolution1.popMax());
24-
}
21+
@Test
22+
publicvoidtest1() {
23+
maxStackSolution1.push(5);
24+
assertEquals(5,maxStackSolution1.peekMax());
25+
maxStackSolution2.push(5);
26+
assertEquals(5,maxStackSolution2.popMax());
27+
maxStackSolution3.push(5);
28+
assertEquals(5,maxStackSolution3.popMax());
29+
}
2530

26-
@Test
27-
publicvoidtest2() {
28-
maxStackSolution1.push(5);
29-
maxStackSolution1.push(1);
30-
assertEquals(5,maxStackSolution1.popMax());
31-
assertEquals(1,maxStackSolution1.peekMax());
32-
}
31+
@Test
32+
publicvoidtest2() {
33+
maxStackSolution1.push(5);
34+
maxStackSolution1.push(1);
35+
assertEquals(5,maxStackSolution1.popMax());
36+
assertEquals(1,maxStackSolution1.peekMax());
37+
}
3338

34-
@Test
35-
publicvoidtest3() {
36-
maxStackSolution1.push(74);
37-
assertEquals(74,maxStackSolution1.popMax());
38-
maxStackSolution1.push(89);
39-
maxStackSolution1.push(67);
40-
assertEquals(89,maxStackSolution1.popMax());
41-
assertEquals(67,maxStackSolution1.pop());
42-
maxStackSolution1.push(61);
43-
maxStackSolution1.push(-77);
44-
assertEquals(61,maxStackSolution1.peekMax());
45-
assertEquals(61,maxStackSolution1.popMax());
46-
maxStackSolution1.push(81);
47-
assertEquals(81,maxStackSolution1.peekMax());
48-
assertEquals(81,maxStackSolution1.popMax());
49-
maxStackSolution1.push(81);
50-
assertEquals(81,maxStackSolution1.pop());
51-
maxStackSolution1.push(-71);
52-
maxStackSolution1.push(32);
53-
}
39+
@Test
40+
publicvoidtest3() {
41+
maxStackSolution1.push(74);
42+
assertEquals(74,maxStackSolution1.popMax());
43+
maxStackSolution1.push(89);
44+
maxStackSolution1.push(67);
45+
assertEquals(89,maxStackSolution1.popMax());
46+
assertEquals(67,maxStackSolution1.pop());
47+
maxStackSolution1.push(61);
48+
maxStackSolution1.push(-77);
49+
assertEquals(61,maxStackSolution1.peekMax());
50+
assertEquals(61,maxStackSolution1.popMax());
51+
maxStackSolution1.push(81);
52+
assertEquals(81,maxStackSolution1.peekMax());
53+
assertEquals(81,maxStackSolution1.popMax());
54+
maxStackSolution1.push(81);
55+
assertEquals(81,maxStackSolution1.pop());
56+
maxStackSolution1.push(-71);
57+
maxStackSolution1.push(32);
58+
}
5459

55-
@Test
56-
publicvoidtest4() {
57-
maxStackSolution2.push(5);
58-
assertEquals(5,maxStackSolution2.peekMax());
59-
assertEquals(5,maxStackSolution2.popMax());
60-
}
60+
@Test
61+
publicvoidtest4() {
62+
maxStackSolution2.push(5);
63+
assertEquals(5,maxStackSolution2.peekMax());
64+
assertEquals(5,maxStackSolution2.popMax());
65+
}
6166

62-
@Test
63-
publicvoidtest5() {
64-
maxStackSolution2.push(5);
65-
maxStackSolution2.push(1);
66-
assertEquals(5,maxStackSolution2.popMax());
67-
assertEquals(1,maxStackSolution2.peekMax());
68-
}
67+
@Test
68+
publicvoidtest5() {
69+
maxStackSolution2.push(5);
70+
maxStackSolution2.push(1);
71+
assertEquals(5,maxStackSolution2.popMax());
72+
assertEquals(1,maxStackSolution2.peekMax());
73+
}
6974

70-
@Test
71-
publicvoidtest6() {
72-
maxStackSolution2.push(74);
73-
assertEquals(74,maxStackSolution2.popMax());
74-
maxStackSolution2.push(89);
75-
maxStackSolution2.push(67);
76-
assertEquals(89,maxStackSolution2.popMax());
77-
assertEquals(67,maxStackSolution2.pop());
78-
maxStackSolution2.push(61);
79-
maxStackSolution2.push(-77);
80-
assertEquals(61,maxStackSolution2.peekMax());
81-
assertEquals(61,maxStackSolution2.popMax());
82-
maxStackSolution2.push(81);
83-
assertEquals(81,maxStackSolution2.peekMax());
84-
assertEquals(81,maxStackSolution2.popMax());
85-
maxStackSolution2.push(81);
86-
assertEquals(81,maxStackSolution2.pop());
87-
maxStackSolution2.push(-71);
88-
maxStackSolution2.push(32);
89-
}
75+
@Test
76+
publicvoidtest6() {
77+
maxStackSolution2.push(74);
78+
assertEquals(74,maxStackSolution2.popMax());
79+
maxStackSolution2.push(89);
80+
maxStackSolution2.push(67);
81+
assertEquals(89,maxStackSolution2.popMax());
82+
assertEquals(67,maxStackSolution2.pop());
83+
maxStackSolution2.push(61);
84+
maxStackSolution2.push(-77);
85+
assertEquals(61,maxStackSolution2.peekMax());
86+
assertEquals(61,maxStackSolution2.popMax());
87+
maxStackSolution2.push(81);
88+
assertEquals(81,maxStackSolution2.peekMax());
89+
assertEquals(81,maxStackSolution2.popMax());
90+
maxStackSolution2.push(81);
91+
assertEquals(81,maxStackSolution2.pop());
92+
maxStackSolution2.push(-71);
93+
maxStackSolution2.push(32);
94+
}
9095
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp