力扣1146.快照数组

力扣1146.快照数组

  • 如果每一次快照都开一个新数组 会爆内存

  • 因为每次只会修改几个数 所以用哈希表 把每个数修改的信息存储下来

    • 搜的时候根据快照编号二分
  •   class SnapshotArray {
          int cur_snap_id = 0;
           unordered_map<int,vector<pair<int,int>>> history;
      public:
          SnapshotArray(int length){}
          
          void set(int index, int val) {
              history[index].emplace_back(cur_snap_id,val);
          }
          
          int snap() {
              return cur_snap_id++;
          }
          
          int get(int index, int snap_id) {
              auto &p = history[index];
              //找>=snap_id+1的那组修改信息
              //如果不+1 当前snap_id存在 那么会找到snap_id
              //当前snap_id不存在 那么会找到>snap_id的那组
              //不统一了
              int j = ranges::lower_bound(p,make_pair(snap_id+1,0)) - p.begin() - 1;
              return j >= 0 ? p[j].second:0;
          }
      };
    

相关推荐

  1. 1146 快照数组

    2024-06-12 10:48:05       16 阅读
  2. 1146.快照数组

    2024-06-12 10:48:05       12 阅读
  3. leetcode1146--快照数组

    2024-06-12 10:48:05       13 阅读
  4. LeetCode 每日一题 ---- 【1146.快照数组

    2024-06-12 10:48:05       15 阅读
  5. 100】146.LRU缓存

    2024-06-12 10:48:05       48 阅读
  6. 记录)146. LRU 缓存

    2024-06-12 10:48:05       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-12 10:48:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-12 10:48:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-12 10:48:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-12 10:48:05       20 阅读

热门阅读

  1. C++中的享元模式

    2024-06-12 10:48:05       9 阅读
  2. Ubuntu系统介绍

    2024-06-12 10:48:05       7 阅读
  3. $(this) 和 this 关键字在 jQuery 中有何不同?

    2024-06-12 10:48:05       8 阅读
  4. 他很意外,我竟然是女程序员?

    2024-06-12 10:48:05       8 阅读
  5. 掉电安全文件系统littlefs移植

    2024-06-12 10:48:05       4 阅读
  6. 等保测评和安全运维

    2024-06-12 10:48:05       9 阅读
  7. 安全等保评测-什么是“等保“?

    2024-06-12 10:48:05       7 阅读
  8. @vue/cli source and destination must not be the same

    2024-06-12 10:48:05       6 阅读
  9. 网络安全(补充)

    2024-06-12 10:48:05       9 阅读