Skip to content

Commit ae39c46

Browse files
committed
update 1146 java
1 parent 956d4e1 commit ae39c46

File tree

3 files changed

+130
-20
lines changed

3 files changed

+130
-20
lines changed

leetcode_java/src/main/java/LeetCodeJava/Array/MyCalendar1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,5 @@ public boolean book(int start, int end) {
220220
}
221221
}
222222
}
223-
223+
224224
}

leetcode_java/src/main/java/LeetCodeJava/Design/SnapshotArray.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public class SnapshotArray {
8383
// }
8484
// }
8585

86-
// V0_
86+
// V0_1
8787
// IDEA : (fix by GPT)
8888
/**
8989
* Key Optimizations
@@ -139,6 +139,56 @@ public int get(int index, int snap_id) {
139139
}
140140
}
141141

142+
// V0_2
143+
// IDEA : HASHMAP + LIST (fixed by gpt) + binary search
144+
class SnapshotArray_0_2 {
145+
146+
private int snapId;
147+
private Map<Integer, List<int[]>> snapshots;
148+
149+
public SnapshotArray_0_2(int length) {
150+
this.snapId = 0;
151+
this.snapshots = new HashMap<>();
152+
// Initialize each index with a snapshot at snapId 0, where the value is 0
153+
for (int i = 0; i < length; i++) {
154+
snapshots.put(i, new ArrayList<>());
155+
snapshots.get(i).add(new int[]{0, 0}); // {snapId, value}
156+
}
157+
}
158+
159+
public void set(int index, int val) {
160+
// Store the new value along with the current snapId
161+
List<int[]> snapshotList = snapshots.get(index);
162+
// If the last snapshot has the same snapId, just update the value
163+
if (!snapshotList.isEmpty() && snapshotList.get(snapshotList.size() - 1)[0] == snapId) {
164+
snapshotList.get(snapshotList.size() - 1)[1] = val;
165+
} else {
166+
snapshotList.add(new int[]{snapId, val});
167+
}
168+
}
169+
170+
public int snap() {
171+
return snapId++;
172+
}
173+
174+
public int get(int index, int snap_id) {
175+
List<int[]> snapshotList = snapshots.get(index);
176+
177+
// Perform a binary search to find the largest snapId <= snap_id
178+
int low = 0, high = snapshotList.size() - 1;
179+
while (low < high) {
180+
int mid = (low + high + 1) / 2;
181+
if (snapshotList.get(mid)[0] <= snap_id) {
182+
low = mid; // Move right if the current snapId is valid
183+
} else {
184+
high = mid - 1; // Move left if the current snapId is too large
185+
}
186+
}
187+
188+
return snapshotList.get(low)[1];
189+
}
190+
}
191+
142192
// V1
143193
// IDEA : Binary Search
144194
// https://leetcode.com/problems/snapshot-array/editorial/

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

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,42 +1478,102 @@ public boolean areSentencesSimilarTwo(){
14781478

14791479
// LC 1146
14801480
// https://leetcode.com/problems/snapshot-array/
1481-
// 7.56 pm - 8.20 pm
1481+
// 4.29 pm - 4.50 pm
1482+
14821483
/**
1484+
* idea 1:
14831485
* {snap_id: cnt}
1484-
*
1486+
* <p>
1487+
* idea 2:
1488+
* <p>
1489+
* use TreeMap to record "UPDATED records with its index"
1490+
* {snap_id : [[new_val_1, idx_1], [new_val_2, idx_2] ....]}
1491+
* <p>
1492+
* -> whenever get() is called
1493+
* -> read from TreeMap (with snapshot_id) and update elements
1494+
* -> then return res
14851495
*/
14861496
class SnapshotArray {
14871497

1488-
Integer[] elements;
1489-
Map<Integer, Integer[]> snapshotMap;
1490-
Integer snapshotCount;
1498+
//TreeMap<Integer, List<List<Integer>>> map;
1499+
Map<Integer, List<List<Integer>>> map;
1500+
Integer[] values;
1501+
1502+
int snapshot_id;
1503+
1504+
List<List<Integer>> updatedValues;
14911505

14921506
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());
1507+
this.map = new HashMap<>();
1508+
this.values = new Integer[length];
1509+
this.snapshot_id = 0;
1510+
this.updatedValues = new ArrayList<>();
14981511
}
14991512

15001513
public void set(int index, int val) {
1501-
// Set value in the current snapshot (current version of elements)
1502-
this.elements[index] = val;
1514+
List<Integer> newValue = new ArrayList<>();
1515+
newValue.add(val);
1516+
newValue.add(index);
1517+
this.updatedValues.add(newValue);
15031518
}
15041519

15051520
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++;
1521+
map.putIfAbsent(snapshot_id, this.updatedValues);
1522+
// init again
1523+
this.updatedValues = new ArrayList<>();
1524+
this.snapshot_id += 1;
1525+
return this.snapshot_id - 1;
15101526
}
15111527

15121528
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];
1529+
if (this.map.containsKey(snap_id)){
1530+
//int key = this.map.floorKey(index);
1531+
//List<List<Integer>> updatedValues = this.map.get(key);
1532+
for (List<Integer> value : updatedValues) {
1533+
int idx = value.get(1);
1534+
if (idx == index) {
1535+
return value.get(0);
1536+
}
1537+
}
1538+
}
1539+
// if val not updated (not in updatedValues)
1540+
// means val is unchanged, then return 0, as default
1541+
return 0;
15151542
}
15161543
}
1544+
1545+
1546+
// class SnapshotArray {
1547+
//
1548+
// Integer[] elements;
1549+
// Map<Integer, Integer[]> snapshotMap;
1550+
// Integer snapshotCount;
1551+
//
1552+
// public SnapshotArray(int length) {
1553+
// this.elements = new Integer[length];
1554+
// this.snapshotMap = new HashMap<>();
1555+
// this.snapshotCount = 0;
1556+
// // Store the initial snapshot (snapshot 0)
1557+
// this.snapshotMap.put(this.snapshotCount, this.elements.clone());
1558+
// }
1559+
//
1560+
// public void set(int index, int val) {
1561+
// // Set value in the current snapshot (current version of elements)
1562+
// this.elements[index] = val;
1563+
// }
1564+
//
1565+
// public int snap() {
1566+
// // Take a snapshot of the current elements array by creating a new copy
1567+
// snapshotMap.put(snapshotCount, elements.clone());
1568+
// // Increment snapshotCount to prepare for the next snapshot
1569+
// return snapshotCount++;
1570+
// }
1571+
//
1572+
// public int get(int index, int snap_id) {
1573+
// // Retrieve the value from the snapshot with the given snap_id
1574+
// return snapshotMap.get(snap_id)[index];
1575+
// }
1576+
// }
15171577
// class SnapshotArray {
15181578
//
15191579
// Integer[] elements;

0 commit comments

Comments
 (0)