当前位置: 首页 > news >正文

(语法笔记 分析题解语法 二分 “unordered_map与vector<pair<>>“ 快速数组)leetocde 1146

*注:代码和题解思路来源于灵茶山艾府,因为我之前未接触过vector<pair<>>与map的联合使用才写的笔记  链接:灵茶山艾府题解

核心思路:建立 unordered_map<int, vector<pair<int, int>>> history;  令key为下标 vector存放不同快照引索的值,详看下文的输出

 unordered_map<int, vector<pair<int, int>>> history; 

键:int 

值:vector<pair<int, int>>

history[0] 会访问 history 中键为 0 所对应的 std::vector<std::pair<int, int>>。如果该键不存在,会自动创建一个空的 vector。



// 正确访问方式
    if (!history[0].empty()) {
        int firstValue = history[0][0].first;  10
        int secondValue = history[0][0].second;  20
}


在代码里,使用 history[0].emplace_back(10, 20); 向 history 中键为 0 对应的 vector 里"插入"了一个 std::pair<int, int> 对象,其值为 (10, 20)。

emplace_back(10, 20) 会在这个 vector 的末尾直接构造一个 std::pair<int, int> 对象,这个对象的第一个元素是 10,第二个元素是 20。

  history[0].emplace_back(10, 20);
    history[0].emplace_back(30, 40);
    history[1].emplace_back(50, 60);

unordered_map数组的状态

key=index是下标题目说的下标
Key: 0
  (10, 20)history[key][0]  history[index][0].first --10快照索引  history[index][0].second--20
  (30, 40)history[key][0]
Key: 1
  (50, 60)

代码分析:

 int j = ranges::lower_bound(h, pair(snap_id + 1, 0)) - h.begin() - 1;
                中 pair(snap_id + 1, 0)
        假设 h 中有如下元素:
std::vector<std::pair<int, int>> h = { {10, 20}, {30, 40}, {50, 60} };

若 snap_id 为 20,那么 std::pair(snap_id + 1, 0) 就是 std::pair(21, 0)。
std::ranges::lower_bound 算法会依据 std::pair 的第一个元素进行比较,找到第一个大于等于 21 的元素,也就是 (30, 40)。
在这个过程中,std::pair 的第二个元素 0 并不影响查找结果,它只是占位,保证 std::pair 类型的完整性。

class SnapshotArray {
   unordered_map<int,vector<pair<int, int>>> history; 
    int c_snap_id=0;
public:
    SnapshotArray(int length) {
    }
    
    void set(int index, int val) {
        history[index].emplace_back(c_snap_id,val);
    }
    
    int snap() {
        return c_snap_id++;
    }
    
    int get(int index, int snap_id) {
        auto &h=history[index];
        int j=ranges::lower_bound(h,pair(snap_id+1,0))-h.begin()-1;
        return j>=0?h[j].second:0;
    }
};

/**
 * 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);
 */

相关文章:

  • Shardingsphere-jdbc 自定义脱敏规则
  • 分割回文串 (leetcode 131
  • 视频孪生与三维视频融合:重构工业现场的“数字视网膜“
  • 002-告别乱码-libiconv-C++开源库108杰
  • 2025年特种设备作业人员叉车司机考试练习题
  • 【网络协议详解】——BGP/MPLS IP VPN技术(学习笔记)
  • macOS 安装配置 iTerm2 记录
  • 亚远景-企业如何通过ASPICE评估:汽车软件开发的实践指南
  • 小程序SSL证书过期怎么办?
  • python之replace,strip,split命令
  • ai agent开发实例
  • Mysql 的 Query Cache为什么被废弃
  • 设计一个优秀 API 的秘诀
  • HAMSTER: HIERARCHICAL ACTION MODELS FOR OPEN-WORLD ROBOT MANIPULATION
  • 2.4 基于Vitest的单元测试基础设施搭建
  • AutoGen学习笔记系列(十)Advanced - Swarm
  • 大湾区经济网报道:全国已有103个统筹地区实施医保基金即时结算
  • Ubuntu 22.04 安装配置 FTP服务器 教程
  • Vue3的基本使用
  • Nginx 服务器,Apache 服务器,IIS 服务器的区别
  • 宜昌谱写新叙事:长江大保护与高质量发展如何相互成就
  • 秦洪看盘|指标股发力,A股渐有突破态势
  • 【社论】公平有序竞争,外卖行业才能多赢
  • 山东:小伙为救同学耽误考试属实,启用副题安排考试
  • 国台办:实现祖国完全统一是大势所趋、大义所在、民心所向
  • 香港根据《维护国家安全条例》订立附属法例