Skip to content

Commit 47bb09d

Browse files
committed
add 900 java, progress
1 parent 776dfa5 commit 47bb09d

File tree

7 files changed

+242
-19
lines changed

7 files changed

+242
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,8 +1196,8 @@
11961196
0715| [Range Module](https://leetcode.com/problems/range-module/) | [C++](./C++/range-module.cpp) [Python](./leetcode_python/Design/range-module.py) | add: _O(n)_<br> remove: _O(n)_<br> query: _O(logn)_ | _O(n)_ | Hard ||
11971197
716| [Max Stack](https://leetcode.com/problems/max-stack/) | [Python](./leetcode_python/Design/max-stack.py) | push: _O(logn)_<br> pop: _O(logn)_<br> popMax: _O(logn)_<br> top: _O(1)_<br> peekMax: _O(1)_ | _O(n)_ | Easy |basic, stack, `amazon`| again*
11981198
745| [Prefix and Suffix Search](https://leetcode.com/problems/prefix-and-suffix-search/) | [Python](./leetcode_python/Design/prefix-and-suffix-search.py) | ctor: _O(w * l^2)_<br> search : _O(p + s)_ | _O(t)_ | Hard | Trie, `fb`| Not start* (1) (good basic)
1199-
900| [RLE Iterator](https://leetcode.com/problems/rle-iterator/) | [C++](./C++/rle-iterator.cpp) [Python](./leetcode_python/Design/rle-iterator.py) | _O(n)_ | _O(1)_ | Medium ||
1200-
1146| [Snapshot Array](https://leetcode.com/problems/snapshot-array/) |[Java](./leetcode_java/src/main/java/LeetCodeJava/Design/SnapshotArray.java)| set: _O(1)_<br> get: _O(logn)_ | _O(n)_ | Medium |treeMap, binary search, google| AGAIN*** (1)
1199+
900| [RLE Iterator](https://leetcode.com/problems/rle-iterator/) | [C++](./C++/rle-iterator.cpp) [Python](./leetcode_python/Design/rle-iterator.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Design/RLEIterator.java) | _O(n)_ | _O(1)_ | Medium |google, treemap, array| AGAIN (not start) (1)
1200+
1146| [Snapshot Array](https://leetcode.com/problems/snapshot-array/)|[Java](./leetcode_java/src/main/java/LeetCodeJava/Design/SnapshotArray.java)| set: _O(1)_<br> get: _O(logn)_ | _O(n)_ | Medium |treeMap, binary search, google| AGAIN*** (1)
12011201
1166| [Design File System](https://leetcode.com/problems/design-file-system/) | [C++](./C++/design-file-system.cpp) [Python](./leetcode_python/Design/design-file-system.py) | create: _O(n)_<br>get: _O(n)_ | _O(n)_ | Medium |🔒, design, google, airbnb, amazon| AGAIN***** (1)
12021202
1172| [Dinner Plate Stacks](https://leetcode.com/problems/dinner-plate-stacks/) | [C++](./C++/dinner-plate-stacks.cpp) [Python](./leetcode_python/Design/dinner-plate-stacks.py) | push: _O(logn)_<br>pop: _O(1)_, amortized<br>popAtStack: _(logn)_ | _O(n * c)_ | Hard | |
12031203
1206| [Design Skiplist](https://leetcode.com/problems/design-skiplist/) | [C++](./C++/design-skiplist.cpp) [Python](./leetcode_python/Design/design-skiplist.py) | _O(logn)_, on average | _O(n)_ | Hard | |

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-11-03
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-11-02
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241103:
1+
20241103: 900
22
20241102: 26,27
33
20241102: 802,1197,26
44
20241027: 855,846

data/to_review.txt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
2024-12-28 -> ['26,27']
2-
2024-12-27 -> ['802,1197,26']
1+
2024-12-28 -> ['900']
2+
2024-12-27 -> ['26,27', '802,1197,26']
33
2024-12-21 -> ['855,846']
44
2024-12-20 -> ['932']
55
2024-12-18 -> ['951,792']
66
2024-12-14 -> ['163,1048']
77
2024-12-13 -> ['298,729']
88
2024-12-12 -> ['1146']
99
2024-12-08 -> ['737']
10-
2024-12-07 -> ['26,27', '686,734,737']
11-
2024-12-06 -> ['802,1197,26', '353']
10+
2024-12-07 -> ['900', '686,734,737']
11+
2024-12-06 -> ['26,27', '802,1197,26', '353']
1212
2024-12-05 -> ['528,334']
1313
2024-12-03 -> ['1145']
1414
2024-11-30 -> ['855,846', '1145,1219']
1515
2024-11-29 -> ['932']
1616
2024-11-27 -> ['951,792', '524,221,889']
1717
2024-11-26 -> ['743,889']
1818
2024-11-25 -> ['837']
19-
2024-11-24 -> ['26,27']
20-
2024-11-23 -> ['802,1197,26', '163,1048', '981']
19+
2024-11-24 -> ['900']
20+
2024-11-23 -> ['26,27', '802,1197,26', '163,1048', '981']
2121
2024-11-22 -> ['298,729', '1087']
2222
2024-11-21 -> ['1146']
2323
2024-11-20 -> ['939']
2424
2024-11-18 -> ['430']
2525
2024-11-17 -> ['855,846', '737', '363']
26-
2024-11-16 -> ['26,27', '932', '686,734,737', '1032,844,1011']
27-
2024-11-15 -> ['802,1197,26', '353', '947']
26+
2024-11-16 -> ['900', '932', '686,734,737', '1032,844,1011']
27+
2024-11-15 -> ['26,27', '802,1197,26', '353', '947']
2828
2024-11-14 -> ['951,792', '528,334']
2929
2024-11-12 -> ['1145', '753']
30-
2024-11-11 -> ['26,27', '727']
31-
2024-11-10 -> ['802,1197,26', '163,1048']
30+
2024-11-11 -> ['900', '727']
31+
2024-11-10 -> ['26,27', '802,1197,26', '163,1048']
3232
2024-11-09 -> ['855,846', '298,729', '1145,1219']
33-
2024-11-08 -> ['26,27', '932', '1146']
34-
2024-11-07 -> ['802,1197,26']
35-
2024-11-06 -> ['26,27', '951,792', '524,221,889']
36-
2024-11-05 -> ['26,27', '802,1197,26', '743,889']
37-
2024-11-04 -> ['26,27', '802,1197,26', '855,846', '737', '837', '659']
38-
2024-11-03 -> ['802,1197,26', '932', '686,734,737', '801,552']
33+
2024-11-08 -> ['900', '932', '1146']
34+
2024-11-07 -> ['26,27', '802,1197,26']
35+
2024-11-06 -> ['900', '951,792', '524,221,889']
36+
2024-11-05 -> ['900', '26,27', '802,1197,26', '743,889']
37+
2024-11-04 -> ['900', '26,27', '802,1197,26', '855,846', '737', '837', '659']
38+
2024-11-03 -> ['26,27', '802,1197,26', '932', '686,734,737', '801,552']
3939
2024-11-02 -> ['163,1048', '353', '981', '1057,1066,1110']
4040
2024-11-01 -> ['855,846', '951,792', '298,729', '528,334', '1087']
4141
2024-10-31 -> ['932', '1146']
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package LeetCodeJava.Design;
2+
3+
// https://leetcode.com/problems/rle-iterator/description/
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.TreeMap;
8+
9+
/**
10+
* 900. RLE Iterator
11+
* Medium
12+
* Topics
13+
* Companies
14+
* We can use run-length encoding (i.e., RLE) to encode a sequence of integers. In a run-length encoded array of even length encoding (0-indexed), for all even i, encoding[i] tells us the number of times that the non-negative integer value encoding[i + 1] is repeated in the sequence.
15+
*
16+
* For example, the sequence arr = [8,8,8,5,5] can be encoded to be encoding = [3,8,2,5]. encoding = [3,8,0,9,2,5] and encoding = [2,8,1,8,2,5] are also valid RLE of arr.
17+
* Given a run-length encoded array, design an iterator that iterates through it.
18+
*
19+
* Implement the RLEIterator class:
20+
*
21+
* RLEIterator(int[] encoded) Initializes the object with the encoded array encoded.
22+
* int next(int n) Exhausts the next n elements and returns the last element exhausted in this way. If there is no element left to exhaust, return -1 instead.
23+
*
24+
*
25+
* Example 1:
26+
*
27+
* Input
28+
* ["RLEIterator", "next", "next", "next", "next"]
29+
* [[[3, 8, 0, 9, 2, 5]], [2], [1], [1], [2]]
30+
* Output
31+
* [null, 8, 8, 5, -1]
32+
*
33+
* Explanation
34+
* RLEIterator rLEIterator = new RLEIterator([3, 8, 0, 9, 2, 5]); // This maps to the sequence [8,8,8,5,5].
35+
* rLEIterator.next(2); // exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5].
36+
* rLEIterator.next(1); // exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5].
37+
* rLEIterator.next(1); // exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5].
38+
* rLEIterator.next(2); // exhausts 2 terms, returning -1. This is because the first term exhausted was 5,
39+
* but the second term did not exist. Since the last term exhausted does not exist, we return -1.
40+
*
41+
*
42+
* Constraints:
43+
*
44+
* 2 <= encoding.length <= 1000
45+
* encoding.length is even.
46+
* 0 <= encoding[i] <= 109
47+
* 1 <= n <= 109
48+
* At most 1000 calls will be made to next.
49+
*
50+
*/
51+
52+
public class RLEIterator {
53+
/**
54+
* Your RLEIterator object will be instantiated and called as such:
55+
* RLEIterator obj = new RLEIterator(encoding);
56+
* int param_1 = obj.next(n);
57+
*/
58+
// V0
59+
// TODO : implement
60+
// class RLEIterator {
61+
//
62+
// public RLEIterator(int[] encoding) {
63+
//
64+
// }
65+
//
66+
// public int next(int n) {
67+
//
68+
// }
69+
// }
70+
71+
72+
// V1-1
73+
// IDEA : TREEMAP
74+
// https://youtu.be/Y_x4H9nMps0?si=O83hIcI5zilPGiWK
75+
class RLEIterator_1 {
76+
private long next;
77+
private TreeMap<Long, Integer> tm;
78+
79+
public RLEIterator_1(int[] encoding) {
80+
next = 0;
81+
tm = new TreeMap<>();
82+
long total = 0;
83+
for (int i = 0; i < encoding.length; i += 2) {
84+
if (encoding[i] == 0)
85+
continue;
86+
total += encoding[i];
87+
tm.put(total, encoding[i + 1]);
88+
}
89+
}
90+
91+
public int next(int n) {
92+
next += n;
93+
return tm.ceilingKey(next) != null ? tm.get(tm.ceilingKey(next)) : -1;
94+
}
95+
}
96+
97+
// V1-2
98+
// IDEA : BRUTE FORCE (TLE)
99+
// https://youtu.be/Y_x4H9nMps0?si=O83hIcI5zilPGiWK
100+
class RLEIterator_1_1 {
101+
int index;
102+
List<Integer> list;
103+
104+
public RLEIterator_1_1(int[] A) {
105+
index = 0;
106+
list = new ArrayList<>();
107+
for (int i = 0; i < A.length; i += 2) {
108+
for (int j = A[i]; j > 0; j--) {
109+
list.add(A[i + 1]);
110+
}
111+
}
112+
}
113+
114+
public int next(int n) {
115+
index += n;
116+
return index <= list.size() ? list.get(index - 1) : -1;
117+
}
118+
}
119+
120+
// V2
121+
// https://leetcode.com/problems/rle-iterator/solutions/1418913/java-faster-than-100/
122+
class RLEIterator_2 {
123+
int index;
124+
int[] encoding;
125+
public RLEIterator_2(int[] encoding) {
126+
index = 0;
127+
this.encoding = encoding;
128+
}
129+
130+
public int next(int n) {
131+
while (index < encoding.length && n > encoding[index]) {
132+
n -= encoding[index];
133+
index += 2;
134+
135+
}
136+
if (index >= encoding.length) {
137+
return -1;
138+
}
139+
encoding[index] -= n;
140+
return encoding[index + 1];
141+
}
142+
}
143+
144+
// V3
145+
// IDEA : RECURSION
146+
// https://leetcode.com/problems/rle-iterator/solutions/1139032/java-o-n-time-o-1-space-recursive-approach/
147+
class RLEIterator_3 {
148+
149+
int index;
150+
int[] A;
151+
152+
public RLEIterator_3(int[] A) {
153+
this.A = A;
154+
index = 0;
155+
}
156+
157+
public int next(int n) {
158+
if (index >= A.length) {
159+
return -1;
160+
}
161+
162+
if (n <= A[index]) {
163+
A[index] -= n;
164+
return A[index + 1];
165+
} else {
166+
index += 2;
167+
return next(n - A[index - 2]);
168+
}
169+
}
170+
}
171+
172+
// V4
173+
// https://leetcode.com/problems/rle-iterator/solutions/4470485/short-concise-java-solution-beats-97/
174+
class RLEIterator_4 {
175+
int[] encodingArr;
176+
int cursor;
177+
public RLEIterator_4(int[] encoding) {
178+
this.encodingArr = new int[encoding.length];
179+
for(int i = 0;i<encoding.length;i++){
180+
encodingArr[i] = encoding[i];
181+
}
182+
this.cursor = 0;
183+
}
184+
185+
public int next(int n) {
186+
while(cursor < encodingArr.length){
187+
if(encodingArr[cursor] >= n){
188+
encodingArr[cursor] -= n;
189+
return encodingArr[cursor+1];
190+
}else{
191+
n -= encodingArr[cursor];
192+
cursor +=2;
193+
}
194+
}
195+
return -1;
196+
197+
}
198+
}
199+
200+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,6 +2513,21 @@ public int removeElement(int[] nums, int val) {
25132513
// return s;
25142514
// }
25152515

2516+
// LC 900
2517+
// https://leetcode.com/problems/rle-iterator/
2518+
// 5.07 pm - 5.25 pm
2519+
class RLEIterator {
2520+
2521+
public RLEIterator(int[] encoding) {
2522+
2523+
}
2524+
2525+
public int next(int n) {
2526+
return 0;
2527+
}
2528+
}
2529+
2530+
25162531
}
25172532

25182533

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# V0
2+
3+
# V1
4+
5+
# V2

0 commit comments

Comments
 (0)