力扣1901.寻找峰值II

力扣1901.寻找峰值II

    • 二分每一行 并用函数找出每一行中最大值的下标
    • 若最大值比其下面相邻的元素大 则上方一定存在峰值
    • 若最大值比其下面相邻的元素小 则下方一定存在峰值
cpp 复制代码
  class Solution {
      int indexmax(vector<int> &a)
      {
          return max_element(a.begin(),a.end()) - a.begin();
      }
  public:
      vector<int> findPeakGrid(vector<vector<int>>& mat) {
          int l = 0,r = mat.size() - 1;
          while(l < r)
          {
              //行
              int i = l + r >> 1;
              //列
              int j = indexmax(mat[i]);
              if(mat[i][j] > mat[i+1][j]) r = i;
              else l = i + 1;
          }
          return {l,indexmax(mat[l])};
      }
  };
相关推荐
✎ ﹏梦醒͜ღ҉繁华落℘10 分钟前
菜鸟的算法基础
java·数据结构·算法
爪哇部落算法小助手27 分钟前
每日两题day65
数据结构·c++·算法
麒qiqi35 分钟前
【数据结构核心篇】树与哈希(Hash)的原理、特性及实战应用
数据结构·算法·哈希算法
Swift社区36 分钟前
LeetCode 443. 压缩字符串
leetcode·职场和发展·蓝桥杯
ada7_38 分钟前
LeetCode(python)——543.二叉树的直径
数据结构·python·算法·leetcode·职场和发展
橘颂TA38 分钟前
【剑斩OFFER】算法的暴力美学——颜色分类
数据结构·c++·算法·动态规划
吴秋霖44 分钟前
profileData纯算逆向分析
算法·设备指纹·反爬虫技术
sprintzer1 小时前
11.26-12.05力扣栈刷题
算法·leetcode·职场和发展
sin_hielo1 小时前
leetcode 3578
数据结构·算法·leetcode
ShiMetaPi1 小时前
SAM(通用图像分割基础模型)丨基于BM1684X模型部署指南
人工智能·算法·ai·开源·bm1684x·算力盒子