Skip to content

Commit 1693f0e

Browse files
committed
add 1146 java, progress
1 parent d4f7ff5 commit 1693f0e

File tree

6 files changed

+266
-6
lines changed

6 files changed

+266
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@
11951195
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*
11961196
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)
11971197
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 ||
1198-
1146| [Snapshot Array](https://leetcode.com/problems/snapshot-array/) | [C++](./C++/snapshot-array.cpp) [Python](./leetcode_python/Design/snapshot-array.py) | set: _O(1)_<br> get: _O(logn)_ | _O(n)_ | Medium ||
1198+
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)
11991199
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)
12001200
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 | |
12011201
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-10-18
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-10-13
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241018: 1146
12
20241014: 737
23
20241013: 686,734,737
34
20241012: 353

data/to_review.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2024-12-12 -> ['1146']
12
2024-12-08 -> ['737']
23
2024-12-07 -> ['686,734,737']
34
2024-12-06 -> ['353']
@@ -9,6 +10,7 @@
910
2024-11-25 -> ['837']
1011
2024-11-23 -> ['981']
1112
2024-11-22 -> ['1087']
13+
2024-11-21 -> ['1146']
1214
2024-11-20 -> ['939']
1315
2024-11-18 -> ['430']
1416
2024-11-17 -> ['737', '363']
@@ -18,23 +20,25 @@
1820
2024-11-12 -> ['1145', '753']
1921
2024-11-11 -> ['727']
2022
2024-11-09 -> ['1145,1219']
23+
2024-11-08 -> ['1146']
2124
2024-11-06 -> ['524,221,889']
2225
2024-11-05 -> ['743,889']
2326
2024-11-04 -> ['737', '837', '659']
2427
2024-11-03 -> ['686,734,737', '801,552']
2528
2024-11-02 -> ['353', '981', '1057,1066,1110']
2629
2024-11-01 -> ['528,334', '1087']
30+
2024-10-31 -> ['1146']
2731
2024-10-30 -> ['1145', '939']
2832
2024-10-28 -> ['430']
2933
2024-10-27 -> ['737', '1145,1219', '363']
30-
2024-10-26 -> ['686,734,737', '1032,844,1011']
34+
2024-10-26 -> ['1146', '686,734,737', '1032,844,1011']
3135
2024-10-25 -> ['353', '947', '1110, 1055']
3236
2024-10-24 -> ['528,334', '524,221,889']
33-
2024-10-23 -> ['743,889']
37+
2024-10-23 -> ['1146', '743,889']
3438
2024-10-22 -> ['737', '1145', '837', '753']
35-
2024-10-21 -> ['686,734,737', '727']
36-
2024-10-20 -> ['353', '981']
37-
2024-10-19 -> ['737', '528,334', '1145,1219', '1087']
39+
2024-10-21 -> ['1146', '686,734,737', '727']
40+
2024-10-20 -> ['1146', '353', '981']
41+
2024-10-19 -> ['1146', '737', '528,334', '1145,1219', '1087']
3842
2024-10-18 -> ['686,734,737', '359,1057,1055(todo)']
3943
2024-10-17 -> ['737', '353', '1145', '939']
4044
2024-10-16 -> ['737', '686,734,737', '528,334', '524,221,889']
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package LeetCodeJava.Design;
2+
3+
// https://leetcode.com/problems/snapshot-array/description/
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.TreeMap;
8+
9+
/**
10+
* 1146. Snapshot Array
11+
* Medium
12+
* Topics
13+
* Companies
14+
* Hint
15+
* Implement a SnapshotArray that supports the following interface:
16+
*
17+
* SnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0.
18+
* void set(index, val) sets the element at the given index to be equal to val.
19+
* int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.
20+
* int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_id
21+
*
22+
*
23+
* Example 1:
24+
*
25+
* Input: ["SnapshotArray","set","snap","set","get"]
26+
* [[3],[0,5],[],[0,6],[0,0]]
27+
* Output: [null,null,0,null,5]
28+
* Explanation:
29+
* SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
30+
* snapshotArr.set(0,5); // Set array[0] = 5
31+
* snapshotArr.snap(); // Take a snapshot, return snap_id = 0
32+
* snapshotArr.set(0,6);
33+
* snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5
34+
*
35+
*
36+
* Constraints:
37+
*
38+
* 1 <= length <= 5 * 104
39+
* 0 <= index < length
40+
* 0 <= val <= 109
41+
* 0 <= snap_id < (the total number of times we call snap())
42+
* At most 5 * 104 calls will be made to set, snap, and get.
43+
*
44+
*/
45+
public class SnapshotArray {
46+
47+
/**
48+
* Your SnapshotArray object will be instantiated and called as such:
49+
* SnapshotArray obj = new SnapshotArray(length);
50+
* obj.set(index,val);
51+
* int param_2 = obj.snap();
52+
* int param_3 = obj.get(index,snap_id);
53+
*/
54+
// V0
55+
// (TLE) : TODO : optimize
56+
// class SnapshotArray_ {
57+
//
58+
// Integer[] elements;
59+
// Map<Integer, Integer[]> snapshotMap;
60+
// Integer snapshotCount;
61+
//
62+
// public SnapshotArray_(int length) {
63+
// this.elements = new Integer[length];
64+
// this.snapshotMap = new HashMap<>();
65+
// this.snapshotCount = 0;
66+
// // Store the initial snapshot (snapshot 0)
67+
// this.snapshotMap.put(this.snapshotCount, this.elements.clone());
68+
// }
69+
//
70+
// public void set(int index, int val) {
71+
// // Set value in the current snapshot (current version of elements)
72+
// this.elements[index] = val;
73+
// }
74+
//
75+
// public int snap() {
76+
// // Take a snapshot of the current elements array by creating a new copy
77+
// snapshotMap.put(snapshotCount, elements.clone());
78+
// // Increment snapshotCount to prepare for the next snapshot
79+
// return snapshotCount++;
80+
// }
81+
//
82+
// public int get(int index, int snap_id) {
83+
// // Retrieve the value from the snapshot with the given snap_id
84+
// return snapshotMap.get(snap_id)[index];
85+
// }
86+
// }
87+
88+
// V0_
89+
// IDEA : (fix by GPT)
90+
/**
91+
* Key Optimizations
92+
*
93+
* 1. Sparse Storage:
94+
* •Instead of copying the whole array for each snapshot,
95+
* the TreeMap for each index only stores the values that
96+
* have been set at different snapshot versions.
97+
* This avoids unnecessary duplication of data.
98+
*
99+
* 2. Efficient Retrieval:
100+
* •Using TreeMap.floorEntry() allows us to efficiently
101+
* retrieve the value of an index at the given snapshot,
102+
* or the most recent value before the snapshot.
103+
*
104+
* 3. Memory Efficiency:
105+
* •The memory usage is optimized because we store
106+
* only the changes at each snapshot. If no change
107+
* occurs for an element, we do not store multiple
108+
* copies of the same value.
109+
*
110+
* -> This approach ensures that both set() and get() operations
111+
* remain efficient, with logarithmic time complexity due to
112+
* the use of TreeMap, while significantly reducing memory
113+
* usage compared to the original approach.
114+
*
115+
*/
116+
class SnapshotArray_0_1 {
117+
118+
private int snapId;
119+
private Map<Integer, TreeMap<Integer, Integer>> snapshots;
120+
121+
public SnapshotArray_0_1(int length) {
122+
this.snapId = 0;
123+
this.snapshots = new HashMap<>();
124+
for (int i = 0; i < length; i++) {
125+
snapshots.put(i, new TreeMap<>());
126+
snapshots.get(i).put(0, 0); // Initially, every element is 0 at snap_id 0
127+
}
128+
}
129+
130+
public void set(int index, int val) {
131+
snapshots.get(index).put(snapId, val);
132+
}
133+
134+
public int snap() {
135+
return snapId++;
136+
}
137+
138+
public int get(int index, int snap_id) {
139+
// Get the greatest key less than or equal to snap_id
140+
return snapshots.get(index).floorEntry(snap_id).getValue();
141+
}
142+
}
143+
144+
// V1
145+
// IDEA : Binary Search
146+
// https://leetcode.com/problems/snapshot-array/editorial/
147+
class SnapshotArray_1_1 {
148+
int snapId = 0;
149+
TreeMap<Integer, Integer>[] historyRecords;
150+
151+
public SnapshotArray_1_1(int length) {
152+
historyRecords = new TreeMap[length];
153+
for (int i = 0; i < length; i++) {
154+
historyRecords[i] = new TreeMap<Integer, Integer>();
155+
historyRecords[i].put(0, 0);
156+
}
157+
}
158+
159+
public void set(int index, int val) {
160+
historyRecords[index].put(snapId, val);
161+
}
162+
163+
public int snap() {
164+
return snapId++;
165+
}
166+
167+
public int get(int index, int snapId) {
168+
return historyRecords[index].floorEntry(snapId).getValue();
169+
}
170+
}
171+
172+
// V2
173+
}

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,85 @@ public boolean areSentencesSimilar(
14751475
public boolean areSentencesSimilarTwo(){
14761476
return false;
14771477
}
1478+
1479+
// LC 1146
1480+
// https://leetcode.com/problems/snapshot-array/
1481+
// 7.56 pm - 8.20 pm
1482+
/**
1483+
* {snap_id: cnt}
1484+
*
1485+
*/
1486+
class SnapshotArray {
1487+
1488+
Integer[] elements;
1489+
Map<Integer, Integer[]> snapshotMap;
1490+
Integer snapshotCount;
1491+
1492+
public SnapshotArray(int length) {
1493+
this.elements = new Integer[length];
1494+
this.snapshotMap = new HashMap<>();
1495+
this.snapshotCount = 0;
1496+
// Store the initial snapshot (snapshot 0)
1497+
this.snapshotMap.put(this.snapshotCount, this.elements.clone());
1498+
}
1499+
1500+
public void set(int index, int val) {
1501+
// Set value in the current snapshot (current version of elements)
1502+
this.elements[index] = val;
1503+
}
1504+
1505+
public int snap() {
1506+
// Take a snapshot of the current elements array by creating a new copy
1507+
snapshotMap.put(snapshotCount, elements.clone());
1508+
// Increment snapshotCount to prepare for the next snapshot
1509+
return snapshotCount++;
1510+
}
1511+
1512+
public int get(int index, int snap_id) {
1513+
// Retrieve the value from the snapshot with the given snap_id
1514+
return snapshotMap.get(snap_id)[index];
1515+
}
1516+
}
1517+
// class SnapshotArray {
1518+
//
1519+
// Integer[] elements;
1520+
// Map<Integer, Integer[]> snapshotMap;
1521+
//
1522+
// Integer snapshotCount;
1523+
//
1524+
// public SnapshotArray(int length) {
1525+
// this.elements = new Integer[length];
1526+
// this.snapshotMap = new HashMap<>();
1527+
// this.snapshotCount = 0;
1528+
// // this.elements.clone() ???
1529+
// this.snapshotMap.put(this.snapshotCount, this.elements.clone());
1530+
// System.out.println(">>> this.snapshotMap = " + this.snapshotMap);
1531+
// }
1532+
//
1533+
// public void set(int index, int val) {
1534+
//// Integer[] curElements = this.snapshotMap.get(this.snapshotCount);
1535+
//// System.out.println(">>> curElements = " + curElements);
1536+
//// curElements[index] = val;
1537+
//// //this.elements[index] = val;
1538+
//// this.snapshotMap.put(this.snapshotCount, curElements);
1539+
// this.elements[index] = val;
1540+
// }
1541+
//
1542+
// public int snap() {
1543+
//// int curSnapShotCnt = this.snapshotCount;
1544+
//// snapshotMap.put(curSnapShotCnt+1, snapshotMap.get(snapshotMap));
1545+
//// this.snapshotCount += 1; // ?
1546+
//// return this.snapshotCount -1;
1547+
// this.snapshotMap.put(this.snapshotCount, this.elements.clone());
1548+
// this.snapshotCount += 1;
1549+
// return this.snapshotCount;
1550+
// }
1551+
//
1552+
// public int get(int index, int snap_id) {
1553+
// return this.snapshotMap.get(snap_id)[index];
1554+
// }
1555+
// }
1556+
14781557
}
14791558

14801559

0 commit comments

Comments
 (0)