Skip to content

Commit 975a7a1

Browse files
committed
rearrange the dictionary
1 parent c18deb5 commit 975a7a1

File tree

349 files changed

+1086
-49
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

349 files changed

+1086
-49
lines changed
File renamed without changes.

Java/src/leetcode/solution/DataStruct/Solution.java

+740
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
package leetcode.solution.stack;
2+
3+
import java.util.*;
4+
5+
/**
6+
* 394. Decode String
7+
*/
8+
public class DecodeString {
9+
10+
public static void main(String[] args) {
11+
String s = "3[a]2[bc]";
12+
DecodeString decodeString = new DecodeString();
13+
14+
int[] tokens = {100, 200, 300, 400};
15+
int power = 200;
16+
System.out.println(decodeString.getScore(tokens, power));
17+
}
18+
19+
20+
public int getScore(int[] tokens, int power) {
21+
int score = 0;
22+
int n = tokens.length;
23+
24+
int right = n - 1;
25+
for (int i = 0; i <= right; ) {
26+
int token = tokens[i];
27+
if (token <= power) {
28+
score++;
29+
power -= token;
30+
i++;
31+
} else {
32+
if (score > 0 && tokens[right] > token) {
33+
score--;
34+
power += tokens[right];
35+
right--;
36+
} else {
37+
break;
38+
}
39+
}
40+
}
41+
42+
43+
return score;
44+
}
45+
46+
public int lengthOfLIS(int[] nums, int k) {
47+
TreeMap<Integer, Integer> map = new TreeMap<>();
48+
49+
int ans = 0;
50+
for (int num : nums) {
51+
52+
int max = 1;
53+
54+
SortedMap<Integer, Integer> sub = map.subMap(num - k, num);
55+
56+
for (Map.Entry<Integer, Integer> entry : sub.entrySet()) {
57+
int count = entry.getValue();
58+
max = Math.max(max, count + 1);
59+
}
60+
61+
62+
map.put(num, max);
63+
64+
ans = Math.max(ans, max);
65+
}
66+
67+
return ans;
68+
}
69+
70+
public int v(int[][] intervals) {
71+
int n = intervals.length;
72+
int[] starts = new int[n];
73+
74+
for (int i = 0; i < n; i++) {
75+
starts[i] = intervals[i][0];
76+
}
77+
78+
int[] ends = new int[n];
79+
80+
for (int i = 0; i < n; i++) {
81+
ends[i] = intervals[i][1];
82+
}
83+
84+
Arrays.sort(starts);
85+
Arrays.sort(ends);
86+
87+
int max = 0;
88+
int i = 0;
89+
int j = 0;
90+
int cur = 0;
91+
while (i < n && j < n) {
92+
if (starts[i] < ends[j]) {
93+
cur++;
94+
i++;
95+
} else {
96+
cur--;
97+
j++;
98+
}
99+
100+
101+
max = Math.max(max, cur);
102+
}
103+
104+
return max;
105+
106+
}
107+
108+
public int partitionString(String s) {
109+
int ans = 0;
110+
int n = s.length();
111+
Set<Character> set = new HashSet<>();
112+
int i = 0;
113+
while (i < n) {
114+
char c = s.charAt(i);
115+
if (set.contains(c)) {
116+
ans++;
117+
set.clear();
118+
continue;
119+
}
120+
121+
set.add(c);
122+
i++;
123+
}
124+
125+
if (!set.isEmpty()) {
126+
ans++;
127+
}
128+
129+
return ans;
130+
}
131+
132+
public int mostFrequentEven(int[] nums) {
133+
Map<Integer, Integer> map = new HashMap<>();
134+
for (int num : nums) {
135+
if (num % 2 == 0) {
136+
map.put(num, map.getOrDefault(num, 0) + 1);
137+
}
138+
}
139+
140+
int ans = -1;
141+
int count = 0;
142+
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
143+
if (e.getValue() > count) {
144+
count = e.getValue();
145+
ans = e.getKey();
146+
} else if (e.getValue() == count) {
147+
if (e.getKey() < ans) {
148+
ans = e.getKey();
149+
}
150+
}
151+
}
152+
153+
return ans;
154+
}
155+
156+
public int count(int[] numbers, int k) {
157+
int n = numbers.length;
158+
int left = 0;
159+
int right = 0;
160+
Map<Integer, Integer> memo = new HashMap<>();
161+
162+
int ans = 0;
163+
int count = 0;
164+
while (right < n) {
165+
int num = numbers[right];
166+
right++;
167+
memo.put(num, memo.getOrDefault(num, 0) + 1);
168+
if (memo.get(num) % 2 == 0) {
169+
count++;
170+
}
171+
172+
while (count == k) {
173+
ans += n - right + 1;
174+
int leftNum = numbers[left];
175+
left++;
176+
memo.put(leftNum, memo.getOrDefault(leftNum, 0) - 1);
177+
if (memo.get(leftNum) % 2 != 0) {
178+
count--;
179+
}
180+
}
181+
182+
}
183+
184+
return ans;
185+
}
186+
187+
188+
private Map<String, Boolean> memo;
189+
190+
private int x2;
191+
192+
private int y2;
193+
194+
private int c;
195+
196+
private String ff(int c, int x1, int y1, int x2, int y2) {
197+
if (x1 > x2 || y1 > y2) {
198+
return "No";
199+
}
200+
this.memo = new HashMap<>();
201+
this.c = c;
202+
this.x2 = x2;
203+
this.y2 = y2;
204+
205+
if (dfs(x1, y1)) {
206+
return "Yes";
207+
}
208+
209+
return "No";
210+
}
211+
212+
213+
private boolean dfs(int x, int y) {
214+
if (x == x2 && y == y2) {
215+
return true;
216+
}
217+
218+
if (x > x2 || y > y2) {
219+
return false;
220+
}
221+
222+
223+
String key = x + "," + y;
224+
if (memo.containsKey(key)) {
225+
return memo.get(key);
226+
}
227+
228+
int sq = (int) Math.sqrt(x + y);
229+
if (sq * sq == x + y) {
230+
memo.put(key, false);
231+
return false;
232+
}
233+
234+
boolean ans = dfs(x + y, y) || dfs(x, x + y) || dfs(x + c, y + c);
235+
236+
memo.put(key, ans);
237+
238+
return ans;
239+
}
240+
241+
public String decodeString(String s) {
242+
Stack<StringBuilder> charStack = new Stack<>();
243+
Stack<Integer> countStack = new Stack<>();
244+
StringBuilder current = new StringBuilder();
245+
246+
int k = 0;
247+
for (int i = 0; i < s.length(); i++) {
248+
char ch = s.charAt(i);
249+
if (Character.isDigit(ch)) {
250+
k = k * 10 + ch - '0';
251+
} else if (Character.isLetter(ch)) {
252+
current.append(ch);
253+
} else if ('[' == ch) {
254+
countStack.push(k);
255+
k = 0;
256+
charStack.push(current);
257+
current = new StringBuilder();
258+
} else if (']' == ch) {
259+
StringBuilder encodeStr = current;
260+
int times = countStack.pop();
261+
StringBuilder existStr = charStack.pop();
262+
for (int j = times; j > 0; j--) {
263+
existStr.append(encodeStr);
264+
}
265+
current = existStr;
266+
}
267+
}
268+
269+
return current.toString();
270+
}
271+
272+
273+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package leetcode.solution.stack.parentheses;
2+
3+
import leetcode.solution.stack.DecodeString;
4+
5+
import java.util.Arrays;
6+
import java.util.PriorityQueue;
7+
8+
public class Solution {
9+
10+
public static void main(String[] args) {
11+
Solution solution = new Solution();
12+
13+
int[] tokens = {4, 2, 1, 3};
14+
int power = 2;
15+
System.out.println(Arrays.toString(solution.getGreatestElements(tokens, power)));
16+
tokens = new int[]{3, 2, 4, 5, 1};
17+
power = 4;
18+
System.out.println(Arrays.toString(solution.getGreatestElements(tokens, power)));
19+
tokens = new int[]{3, 1, 4, 5, 2};
20+
power = 2;
21+
System.out.println(Arrays.toString(solution.getGreatestElements(tokens, power)));
22+
}
23+
24+
public int[] getGreatestElements(int[] arr, int k) {
25+
int n = arr.length;
26+
PriorityQueue<Integer> minQ = new PriorityQueue<>();
27+
int target = 0;
28+
29+
for (int i = 0; i < k - 1; i++) {
30+
minQ.add(arr[i]);
31+
}
32+
33+
34+
int[] ans = new int[n - k + 1];
35+
int index = 0;
36+
for (int i = k - 1; i < n; i++) {
37+
minQ.add(arr[i]);
38+
target = Math.max(target, minQ.poll());
39+
ans[index] = target;
40+
index++;
41+
}
42+
43+
return ans;
44+
}
45+
46+
47+
public int getScore(int[] tokens, int power) {
48+
int score = 0;
49+
int n = tokens.length;
50+
51+
int right = n - 1;
52+
for (int i = 0; i <= right; ) {
53+
int token = tokens[i];
54+
if (token <= power) {
55+
score++;
56+
power -= token;
57+
i++;
58+
} else {
59+
if (score > 0 && tokens[right] > token) {
60+
score--;
61+
power += tokens[right];
62+
right--;
63+
} else {
64+
break;
65+
}
66+
}
67+
}
68+
69+
70+
return score;
71+
}
72+
73+
}

0 commit comments

Comments
 (0)