@@ -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