Skip to content

Commit 5f45dcc

Browse files
committed
100523
1 parent 2b50e9f commit 5f45dcc

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

src/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import leetcode.editor.en.Q2466.CountWaysToBuildGoodStrings;
99
import leetcode.editor.en.Q311.SparseMatrixMultiplication;
1010
import leetcode.editor.en.Q354.RussianDollEnvelopes;
11+
import leetcode.editor.en.Q376.WiggleSubsequence;
1112
import leetcode.editor.en.Q377.CombinationSumIv;
1213
import leetcode.editor.en.Q474.OnesAndZeroes;
1314
import leetcode.editor.en.Q518.CoinChangeII;
1415
import leetcode.editor.en.Q54.SpiralMatrix;
1516
import leetcode.editor.en.Q649.Dota2Senate;
17+
import leetcode.editor.en.Q651.FourKeysKeyboard;
1618
import leetcode.editor.en.Q91.DecodeWays;
1719
import org.json.JSONArray;
1820
import org.json.JSONTokener;
@@ -27,7 +29,7 @@
2729

2830
public class Main {
2931
public static void main(String[] args) throws IOException {
30-
System.out.println(new CountVowelsPermutation().countVowelPermutation(2));
32+
System.out.println(new FourKeysKeyboard().maxA(9));
3133

3234
}
3335

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package leetcode.editor.en.Q376;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
private static final int LESSER = 0;
10+
private static final int GREATER = 1;
11+
12+
private Integer[][] cache;
13+
14+
public int wiggleMaxLength(int[] nums) {
15+
int[] NGE = nextGreaterElement(nums);
16+
int[] NLE = nextLesserElement(nums);
17+
cache = new Integer[nums.length][2];
18+
19+
int ans = 0;
20+
for (int i = 0; i < nums.length; i++) {
21+
ans = Math.max(wiggleMaxLength(i, GREATER, NGE, NLE), ans);
22+
ans = Math.max(wiggleMaxLength(i, LESSER, NGE, NLE), ans);
23+
}
24+
return ans;
25+
26+
}
27+
28+
private int wiggleMaxLength(int i, int direction, int[] NGE, int[] NLE) {
29+
if (i == NGE.length) {
30+
return 0;
31+
}
32+
if (cache[i][direction] != null) {
33+
return cache[i][direction];
34+
}
35+
36+
int ans = 0;
37+
int greaterDir = 0;
38+
int lesserDir = 0;
39+
if (direction == GREATER) {
40+
greaterDir = 1 + wiggleMaxLength(NGE[i], LESSER, NGE, NLE);
41+
ans = greaterDir;
42+
} else {
43+
lesserDir = 1 + wiggleMaxLength(NLE[i], GREATER, NGE, NLE);
44+
ans = lesserDir;
45+
}
46+
cache[i][direction] = ans;
47+
return ans;
48+
}
49+
50+
51+
private int[] nextGreaterElement(int[] nums) {
52+
int[] NGE = new int[nums.length];
53+
Arrays.fill(NGE, nums.length);
54+
LinkedList<Integer> stack = new LinkedList<>();
55+
for (int i = 0; i < nums.length; i++) {
56+
while (!stack.isEmpty() && nums[stack.peekLast()] < nums[i]) {
57+
NGE[stack.pop()] = i;
58+
}
59+
stack.add(i);
60+
}
61+
return NGE;
62+
}
63+
64+
private int[] nextLesserElement(int[] nums) {
65+
int[] NLE = new int[nums.length];
66+
Arrays.fill(NLE, nums.length);
67+
LinkedList<Integer> stack = new LinkedList<>();
68+
for (int i = 0; i < nums.length; i++) {
69+
while (!stack.isEmpty() && nums[i] < nums[stack.peekLast()]) {
70+
NLE[stack.pop()] = i;
71+
}
72+
stack.add(i);
73+
}
74+
return NLE;
75+
}
76+
}
77+
//leetcode submit region end(Prohibit modification and deletion)
78+
79+
80+
public class WiggleSubsequence extends Solution {
81+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package leetcode.editor.en.Q651;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
10+
11+
public int maxA(int n) {
12+
13+
}
14+
15+
public int maxAHelper(int n) {
16+
if (n == 0) {
17+
return 0;
18+
}
19+
int ans = 0;
20+
int bufferToDouble = maxAHelper(n - 3);
21+
if (n >= 3) {
22+
23+
}
24+
}
25+
26+
27+
}
28+
//leetcode submit region end(Prohibit modification and deletion)
29+
30+
31+
public class FourKeysKeyboard extends Solution {
32+
}

0 commit comments

Comments
 (0)