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

reLeetCode 热题 100- 42 接雨水 - MKT

image

 

image

 

class Solution {
public:/*关键  左边界  height[zuo]>height[zuo+1]右边界  1 是否比height[you]》height[zuo] break;2  不是最后一个 height[you]>height[you-1]  &&  height[you]>height[you+1] 3 最后一个  height[you]>height[you-1] */// 1 失败 找右侧边界 忽略了第一个比自己大的int trap1(vector<int>& height) {int left =0;int right =2;int current=1;int current_i=0;int current_i_cut=0;int current_all=0;for(int i=1;i<(height.size()-1);++i){cout<< i << "  "<< " left "<< left << "  right  "<<right<<"  current_all "<< current_all<<endl;current=i;if(height[left]>height[left+1]){right=current+1;bool findright=0;if(right==(height.size()-1)){ // 最后一个右边界findright=1;}if(height[current]<height[right] && height[right] > height[right+1] && (right+1)<height.size()){ // 正常内部的右边界findright=1;}if(findright==1){// 找到右边界current_i_cut=0;for(int j = left+1;j<right;j++) {current_i_cut=current_i_cut+height[j];}current_i= min(height[left],height[right])*(right-left-1);current_all=current_all+current_i-current_i_cut;cout<<"i "<< i << "找到到右边界  "<< " left "<< left << " / "<<height[left]<< "  right  "<<right << " / "<<height[right]<< " w = "<< (right-left-1) << " h = "<<  min(height[left],height[right]) << " current_i_cut  "<< current_i_cut << " current_i  "<< current_i << " current_all "<< current_all<<endl;current_i_cut=0;current_i=0;left=right;i=left;// cout<<"i "<< i << "找到到右边界 更新数据  "// << " left "<< left << " / "<<height[left]// << "  right  "<<right << " / "<<height[right]// << " w = "<< (right-left-1) // << " h = "<<  min(height[left],height[right]) // << " current_i_cut  "<< current_i_cut // << " current_i  "<< current_i // << " current_all "<< current_all// <<endl;}else{//current_i_cut=current_i_cut+height[i];// cout<<" 减去i " << i << "  数值 "<< height[i]<<endl;}}else{current_i_cut=0;left=i;     }// cout<<"i "<< i << "...最后统计 "// << " left "<< left << " / "<<height[left]// << "  right  "<<right << " / "<<height[right]// << " w = "<< (right-left-1) // << " h = "<<  min(height[left],height[right]) // << " current_i_cut  "<< current_i_cut // << " current_i  "<< current_i // << " current_all "<< current_all// <<endl;}return current_all;}// 2 通过但是超时int trap2(vector<int>& height) {int left =0;int right =2;int current=1;int current_i=0;int current_i_cut=0;int current_all=0;for(int i=1;i<(height.size()-1);++i){current=i;if(height[left]>height[left+1]){int right=left;int max_num=0;int max_index=0;for(int j=left+1;j<height.size();j++){if(height[j]>max_num){max_num=height[j];right=j;if(max_num>height[left])break; // 只需要找到第一个大的}}current_i_cut=0;int w=right-left-1;int h=min(height[right],height[left]);for(int k=left+1;k<right;k++){current_i_cut=current_i_cut+height[k];}current_i=w*h;current_all=current_all+current_i-current_i_cut;//  cout<<"i "<< i << "找到到右边界 更新数据  "//     << " left "<< left << " / "<<height[left]//     << "  right  "<<right << " / "<<height[right]//     << " w = "<< (right-left-1) //     << " h = "<<  min(height[left],height[right]) //     << " current_i_cut  "<< current_i_cut //     << " current_i  "<< current_i //     << " current_all "<< current_all//     <<endl;left=right;i=left;}else{left=i;     }}return current_all;}// 双指针通过 参考网友思路 官方费解int trap2_shuangzhizhen(vector<int>& height) {/*关于双指针解法,我使用直接比较lmax与rmax的解法,可以AC。我思考了一下,有一个说明其正确性的解释:
由于两个指针都走过了各自的区域,即left指针左边已经遍历,right指针右边已经遍历,因此可以保证lmax和rmax是各自遍历区域内的最大值。因此较小者一定是对应指针所在位置接水时的较低端。
举例说明即:
考虑有lmax<=rmax,此时无论left到right之间的数有多大,left指针位置能接到多少水由lmax决定,因此此时直接统计该位置对答案的贡献是正确的。当lmax>rmax的情况也同理。*/int right=0;int left=height.size()-1;int right_max,left_max=0;int all_water=0;while(right<left){// 当前位置的水 由左右两侧区间的最短边决定。只要找到两侧谁是最短,就可以先决定一侧。right_max=max(right_max,height[right]);// 0-right 区间最大left_max=max(left_max,height[left]); // left-N 区间最大// right -  left 区间目前未知,但是左右两侧的最小边,决定了对应左右两侧位置的最小边if(right_max<left_max){ // 右侧最大小于左侧最大// 右侧可以确定,(右侧到左侧中间无论什么情况,左侧最大兜底,右侧不可能在大了)int curent_water=right_max-height[right];  all_water=all_water+  curent_water;//等效all_water += right_max-height[right]right++;}else{// 左侧可以确定,(右侧到左侧中间无论什么情况,右侧最大兜底,左侧不可能在大了int curent_water=left_max-height[left];  all_water=all_water+  curent_water;left--;}}return all_water;}int trap(vector<int>& height) {int right=0;int left=height.size()-1;int right_max,left_max=0;int all_water=0;while(right<left){right_max=max(right_max,height[right]);left_max=max(left_max,height[left]);if(right_max<left_max){int curent_water=right_max-height[right];  all_water=all_water+  curent_water;right++;}else{int curent_water=left_max-height[left];  all_water=all_water+  curent_water;left--;}}return all_water;}};

  

http://www.hskmm.com/?act=detail&tid=20124

相关文章:

  • 2025 防撞软包生产厂家权威推荐排行榜:E0 级环保 + B1 级阻燃,公检法 / 幼儿园场景最新优选厂家谈话室/留置病房/教育中心/体育馆防撞软包厂家推荐
  • ssti模板注入
  • 2025 年章丘二手磁选机厂家最新权威推荐排行榜:TOP 级企业设备全型号覆盖与五年质保深度解析二手立环磁选机/二手华特磁选机/章丘二手磁选机厂家推荐
  • 中位数定理
  • 数据集Dataset
  • 2025 年三维扫描仪厂家最新权威推荐排行榜:覆盖空间 / 高精度 / 专业 / 手持激光 / 工业等类型,精选实力企业深度解析
  • 2025 年染井吉野樱厂商最新推荐排行榜:权威筛选优质苗木供应商,聚焦分枝点规格与景观适配五公分/十公分/染井吉野樱批发厂商推荐
  • 2025 货架厂家权威推荐排行榜: 实力厂家深度解析,金塔领衔全业态定制服务新标杆云南/昆明/西南货架厂家推荐
  • 国标GB28181视频平台EasyGBS公网平台实时视频播放方案
  • 2025 展会搭建公司权威推荐排行榜:服务商创意定制与全流程服务能力深度解析站台展会搭建/展台搭建活动策划/展台搭建展台制作公司推荐
  • Volcano——配置理解
  • 国标GB28181视频平台EasyGBS:强大的视频监控与一站式视频服务解决方案
  • 题解:AT_abc425_f [ABC425F] Inserting Process
  • [转]bat/cmd将命令执行的结果赋值给变量
  • 题解:P13507 [OOI 2024] Three Arrays
  • 题解:AT_abc424_f [ABC424F] Adding Chords
  • 如何在不同区域/网络环境下评估 reCAPTCHA 的表现 - 详解
  • 2025 年最新编织袋生产厂家权威推荐排行榜:聚焦 TOP5 优质企业,助力企业精准甄选可靠合作伙伴牛皮纸/塑料/PP彩膜/化工/化肥编织袋厂家推荐
  • P11854 [CSP-J2022 山东] 宴会
  • 2025 年试验机厂家权威推荐榜:TOP5 优质厂家综合实力解析,助力科研与工业客户精准选型电子万能材料/橡胶拉力/塑料拉力/扬州拉力试验机厂家推荐
  • win 系统安装
  • 2025 年节能咨询公司最新权威推荐排行榜:覆盖工业 / 建筑 / 数据中心等领域 TOP5 优质企业综合测评与选型指南发电厂/燃气/全域增效/服务器节能公司推荐
  • 微算法科技(NASDAQ MLGO)探索全同态加密与安全多方计算融合,开启区块链隐私执行新时代
  • 国产SUB-1G芯片DP4363F支持119-1050mhz超低功耗 - 动能世纪
  • 2025 年棕刚玉源头厂家最新推荐排行榜:TOP 级生产厂家原料与烧结工艺权威解析,助力企业精准选购一级棕刚玉/棕刚玉磨料/优质棕刚玉/棕刚玉喷砂废料回收厂家推荐
  • 杀疯了!GitHub 发布 Copilot CLI!!!
  • 2025 年无尘金刚砂源头厂家最新推荐排行榜:权威精选企业产能与品质深度解析无尘金刚砂材料/无尘金刚砂批发/无尘金刚砂喷砂厂家推荐
  • 学习率调整策略
  • PySide6 之简易音乐播放器
  • langgraph-genui