Skip to content

Commit

Permalink
add 1146 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 18, 2024
1 parent d4f7ff5 commit 1693f0e
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@
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*
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)
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 ||
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 ||
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)
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)
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 | |
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 | |
Expand Down
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Progress

# 2024-10-18
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

# 2024-10-13
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20241018: 1146
20241014: 737
20241013: 686,734,737
20241012: 353
Expand Down
14 changes: 9 additions & 5 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2024-12-12 -> ['1146']
2024-12-08 -> ['737']
2024-12-07 -> ['686,734,737']
2024-12-06 -> ['353']
Expand All @@ -9,6 +10,7 @@
2024-11-25 -> ['837']
2024-11-23 -> ['981']
2024-11-22 -> ['1087']
2024-11-21 -> ['1146']
2024-11-20 -> ['939']
2024-11-18 -> ['430']
2024-11-17 -> ['737', '363']
Expand All @@ -18,23 +20,25 @@
2024-11-12 -> ['1145', '753']
2024-11-11 -> ['727']
2024-11-09 -> ['1145,1219']
2024-11-08 -> ['1146']
2024-11-06 -> ['524,221,889']
2024-11-05 -> ['743,889']
2024-11-04 -> ['737', '837', '659']
2024-11-03 -> ['686,734,737', '801,552']
2024-11-02 -> ['353', '981', '1057,1066,1110']
2024-11-01 -> ['528,334', '1087']
2024-10-31 -> ['1146']
2024-10-30 -> ['1145', '939']
2024-10-28 -> ['430']
2024-10-27 -> ['737', '1145,1219', '363']
2024-10-26 -> ['686,734,737', '1032,844,1011']
2024-10-26 -> ['1146', '686,734,737', '1032,844,1011']
2024-10-25 -> ['353', '947', '1110, 1055']
2024-10-24 -> ['528,334', '524,221,889']
2024-10-23 -> ['743,889']
2024-10-23 -> ['1146', '743,889']
2024-10-22 -> ['737', '1145', '837', '753']
2024-10-21 -> ['686,734,737', '727']
2024-10-20 -> ['353', '981']
2024-10-19 -> ['737', '528,334', '1145,1219', '1087']
2024-10-21 -> ['1146', '686,734,737', '727']
2024-10-20 -> ['1146', '353', '981']
2024-10-19 -> ['1146', '737', '528,334', '1145,1219', '1087']
2024-10-18 -> ['686,734,737', '359,1057,1055(todo)']
2024-10-17 -> ['737', '353', '1145', '939']
2024-10-16 -> ['737', '686,734,737', '528,334', '524,221,889']
Expand Down
173 changes: 173 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Design/SnapshotArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package LeetCodeJava.Design;

// https://leetcode.com/problems/snapshot-array/description/

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
* 1146. Snapshot Array
* Medium
* Topics
* Companies
* Hint
* Implement a SnapshotArray that supports the following interface:
*
* SnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0.
* void set(index, val) sets the element at the given index to be equal to val.
* int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.
* int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_id
*
*
* Example 1:
*
* Input: ["SnapshotArray","set","snap","set","get"]
* [[3],[0,5],[],[0,6],[0,0]]
* Output: [null,null,0,null,5]
* Explanation:
* SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
* snapshotArr.set(0,5); // Set array[0] = 5
* snapshotArr.snap(); // Take a snapshot, return snap_id = 0
* snapshotArr.set(0,6);
* snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5
*
*
* Constraints:
*
* 1 <= length <= 5 * 104
* 0 <= index < length
* 0 <= val <= 109
* 0 <= snap_id < (the total number of times we call snap())
* At most 5 * 104 calls will be made to set, snap, and get.
*
*/
public class SnapshotArray {

/**
* Your SnapshotArray object will be instantiated and called as such:
* SnapshotArray obj = new SnapshotArray(length);
* obj.set(index,val);
* int param_2 = obj.snap();
* int param_3 = obj.get(index,snap_id);
*/
// V0
// (TLE) : TODO : optimize
// class SnapshotArray_ {
//
// Integer[] elements;
// Map<Integer, Integer[]> snapshotMap;
// Integer snapshotCount;
//
// public SnapshotArray_(int length) {
// this.elements = new Integer[length];
// this.snapshotMap = new HashMap<>();
// this.snapshotCount = 0;
// // Store the initial snapshot (snapshot 0)
// this.snapshotMap.put(this.snapshotCount, this.elements.clone());
// }
//
// public void set(int index, int val) {
// // Set value in the current snapshot (current version of elements)
// this.elements[index] = val;
// }
//
// public int snap() {
// // Take a snapshot of the current elements array by creating a new copy
// snapshotMap.put(snapshotCount, elements.clone());
// // Increment snapshotCount to prepare for the next snapshot
// return snapshotCount++;
// }
//
// public int get(int index, int snap_id) {
// // Retrieve the value from the snapshot with the given snap_id
// return snapshotMap.get(snap_id)[index];
// }
// }

// V0_
// IDEA : (fix by GPT)
/**
* Key Optimizations
*
* 1. Sparse Storage:
* •Instead of copying the whole array for each snapshot,
* the TreeMap for each index only stores the values that
* have been set at different snapshot versions.
* This avoids unnecessary duplication of data.
*
* 2. Efficient Retrieval:
* •Using TreeMap.floorEntry() allows us to efficiently
* retrieve the value of an index at the given snapshot,
* or the most recent value before the snapshot.
*
* 3. Memory Efficiency:
* •The memory usage is optimized because we store
* only the changes at each snapshot. If no change
* occurs for an element, we do not store multiple
* copies of the same value.
*
* -> This approach ensures that both set() and get() operations
* remain efficient, with logarithmic time complexity due to
* the use of TreeMap, while significantly reducing memory
* usage compared to the original approach.
*
*/
class SnapshotArray_0_1 {

private int snapId;
private Map<Integer, TreeMap<Integer, Integer>> snapshots;

public SnapshotArray_0_1(int length) {
this.snapId = 0;
this.snapshots = new HashMap<>();
for (int i = 0; i < length; i++) {
snapshots.put(i, new TreeMap<>());
snapshots.get(i).put(0, 0); // Initially, every element is 0 at snap_id 0
}
}

public void set(int index, int val) {
snapshots.get(index).put(snapId, val);
}

public int snap() {
return snapId++;
}

public int get(int index, int snap_id) {
// Get the greatest key less than or equal to snap_id
return snapshots.get(index).floorEntry(snap_id).getValue();
}
}

// V1
// IDEA : Binary Search
// https://leetcode.com/problems/snapshot-array/editorial/
class SnapshotArray_1_1 {
int snapId = 0;
TreeMap<Integer, Integer>[] historyRecords;

public SnapshotArray_1_1(int length) {
historyRecords = new TreeMap[length];
for (int i = 0; i < length; i++) {
historyRecords[i] = new TreeMap<Integer, Integer>();
historyRecords[i].put(0, 0);
}
}

public void set(int index, int val) {
historyRecords[index].put(snapId, val);
}

public int snap() {
return snapId++;
}

public int get(int index, int snapId) {
return historyRecords[index].floorEntry(snapId).getValue();
}
}

// V2
}
79 changes: 79 additions & 0 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,85 @@ public boolean areSentencesSimilar(
public boolean areSentencesSimilarTwo(){
return false;
}

// LC 1146
// https://leetcode.com/problems/snapshot-array/
// 7.56 pm - 8.20 pm
/**
* {snap_id: cnt}
*
*/
class SnapshotArray {

Integer[] elements;
Map<Integer, Integer[]> snapshotMap;
Integer snapshotCount;

public SnapshotArray(int length) {
this.elements = new Integer[length];
this.snapshotMap = new HashMap<>();
this.snapshotCount = 0;
// Store the initial snapshot (snapshot 0)
this.snapshotMap.put(this.snapshotCount, this.elements.clone());
}

public void set(int index, int val) {
// Set value in the current snapshot (current version of elements)
this.elements[index] = val;
}

public int snap() {
// Take a snapshot of the current elements array by creating a new copy
snapshotMap.put(snapshotCount, elements.clone());
// Increment snapshotCount to prepare for the next snapshot
return snapshotCount++;
}

public int get(int index, int snap_id) {
// Retrieve the value from the snapshot with the given snap_id
return snapshotMap.get(snap_id)[index];
}
}
// class SnapshotArray {
//
// Integer[] elements;
// Map<Integer, Integer[]> snapshotMap;
//
// Integer snapshotCount;
//
// public SnapshotArray(int length) {
// this.elements = new Integer[length];
// this.snapshotMap = new HashMap<>();
// this.snapshotCount = 0;
// // this.elements.clone() ???
// this.snapshotMap.put(this.snapshotCount, this.elements.clone());
// System.out.println(">>> this.snapshotMap = " + this.snapshotMap);
// }
//
// public void set(int index, int val) {
//// Integer[] curElements = this.snapshotMap.get(this.snapshotCount);
//// System.out.println(">>> curElements = " + curElements);
//// curElements[index] = val;
//// //this.elements[index] = val;
//// this.snapshotMap.put(this.snapshotCount, curElements);
// this.elements[index] = val;
// }
//
// public int snap() {
//// int curSnapShotCnt = this.snapshotCount;
//// snapshotMap.put(curSnapShotCnt+1, snapshotMap.get(snapshotMap));
//// this.snapshotCount += 1; // ?
//// return this.snapshotCount -1;
// this.snapshotMap.put(this.snapshotCount, this.elements.clone());
// this.snapshotCount += 1;
// return this.snapshotCount;
// }
//
// public int get(int index, int snap_id) {
// return this.snapshotMap.get(snap_id)[index];
// }
// }

}


0 comments on commit 1693f0e

Please sign in to comment.