力扣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])};
      }
  };
相关推荐
ltl6 分钟前
HNSW:图索引如何击败树索引
算法
嗯哼python14 分钟前
从 0 到 1 构建 AI 模拟面试系统:RAG + LangGraph 的工程实践
人工智能·面试·职场和发展
曹牧2 小时前
C#:数字的定义和表示方式
算法·c#
Nil2082 小时前
leetcode 138随机链表的复制
算法·leetcode·链表
疯狂打码的少年4 小时前
【数据结构】图的遍历:深度优先搜索(DFS)
数据结构·笔记·算法·深度优先
-凌凌漆-5 小时前
【freertos】Task创建(v2)
java·开发语言·算法
Nil2085 小时前
leetcode 24两两交换链表中的节点
算法·leetcode·链表
.格子衫.5 小时前
033动态规划之状态压缩DP——算法备赛
算法·动态规划
ysa0510305 小时前
c++常用自带函数用法与注意
c++·笔记·算法