跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客GitHub 精选镜像AI 生图工具UI配色美学隐私政策关于联系
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
C算法

LeetCode 962. 最大宽度坡 C 语言解法

LeetCode 第 962 题“最大宽度坡”的 C 语言解法。题目要求在整数数组中找到满足 i < j 且 nums[i] <= nums[j] 的最大宽度 j - i。解决方案采用单调栈策略:首先从左到右构建一个存储递减元素索引的单调栈,然后从右向左遍历数组,利用栈顶元素与当前值比较计算宽度并更新最大值。该方法时间复杂度为 O(n),空间复杂度为 O(n)

颠三倒四发布于 2026/3/22更新于 2026/7/539 浏览
LeetCode 962. 最大宽度坡 C 语言解法

962. Maximum Width Ramp

A ramp in an integer array nums is a pair (i, j) for which i < j and nums[i] <= nums[j]. The width of such a ramp is j - i.

Given an integer array nums, return the maximum width of a ramp in nums. If there is no ramp in nums, return 0.

Example 1:

Input: nums = [6,0,8,2,1,5] Output: 4 Explanation: The maximum width ramp is achieved at (i, j) = (1, 5): nums[1] = 0 and nums[5] = 5.

Example 2:

Input: nums = [9,8,1,0,1,9,4,0,4,1] Output: 7 Explanation: The maximum width ramp is achieved at (i, j) = (2, 9): nums[2] = 1 and nums[9] = 1.

Constraints:

  • 2 <= nums.length <= 5 * 10^4
  • 0 <= nums[i] <= 5 * 10^4

Solution:

Ideas:

  • Build a monotonic decreasing stack of indices from left to right. → Stack keeps positions where nums[i] is a new minimum.
  • A smaller left value has the best chance to form the widest ramp later.
  • Traverse from the right to left (j from end to start): → If nums[stack[top]] <= nums[j], then (stack[top], j) forms a valid ramp.
  • Calculate width j - stack[top], update maximum width.
  • Pop the index from the stack because earlier j will produce smaller widths.
  • Continue until stack is empty or j is done.
  • Return the maximum width found.

Code:

int maxWidthRamp(int* nums, int numsSize) {
    if (numsSize < 2) return 0;

    // Monotonic decreasing stack of indices
    int* stack = (int*)malloc(numsSize * sizeof(int));
    int top = -1;

    // Build stack: store indices where nums[i] is a new minimum from the left
    for (int i = 0; i < numsSize; ++i) {
        if (top == -1 || nums[i] < nums[stack[top]]) {
            stack[++top] = i;
        }
    }

    int maxWidth = 0;

    // Scan from the right, try to widen ramps using the stack
    for (int j = numsSize - 1; j >= 0 && top >= 0; --j) {
        // While current value can form a ramp with stack[top]
        while (top >= 0 && nums[stack[top]] <= nums[j]) {
            int width = j - stack[top];
            if (width > maxWidth) maxWidth = width;
            --top; // Pop because any earlier j will only give smaller width
        }
    }

    free(stack);
    return maxWidth;
}

目录

  1. 962. Maximum Width Ramp
  2. Example 1:
  3. Example 2:
  4. Constraints:
  5. Solution:
  6. Ideas:
  7. Code:
  • 免费图片AI生成工具免费生成了解详情
  • Magick API 一键接入全球大模型注册送1000万token查看
  • 免费图片视频在线生成30秒,将你的创意变成现实开始设计
  • X/Twitter免费视频下载器免登陆无限额度免费视频解析下载了解详情
  • 100+免费在线小游戏爽一把
极客日志微信公众号二维码

微信扫一扫,关注极客日志

微信公众号「极客日志V2」,在微信中扫描左侧二维码关注。展示文案:极客日志V2 zeeklog

更多推荐文章

查看全部
  • Le Git Graph 浏览器扩展:GitHub 提交历史可视化指南
  • SpringBoot + Vue + Netty 实现 WebRTC 实时视频通话
  • ChatGLM3-6B 大模型本地及云端部署与使用指南
  • Java 面向对象核心:包管理、封装设计与代码块详解
  • 英伟达打造机器人大脑:从 44 万小时人类视频中学会灵巧操作
  • Milvus 实战:Attu 可视化安装与 Python SDK 整合指南
  • 数据结构:C 语言单链表实现与原理
  • 二叉树算法实战:美国血统重建与深度宽度计算
  • Qwen2.5-VL 视觉理解案例:Ollama 部署解析设计稿生成前端代码
  • LeetCode 2612 最少翻转次数 C++ 解法
  • Java Web 项目创建及 web.xml 缺失解决方法
  • HashCalculator 文件哈希批量计算工具使用指南
  • Android PopupMenu 弹出菜单实现方法
  • Python爬虫实战:爬取Chrome Web Store扩展商店数据及导出
  • ToDesk、顺网云与海马云部署 DeepSeek 对比评测
  • 转行网络安全学习指南与核心技能路径
  • Python 实现 MCP 客户端调用高德地图天气查询示例
  • Android 不规则封闭区域填充色彩实现
  • Sql Server 中 ISNULL、IIF 与 CASE WHEN 的空值判断用法
  • uv 多版本 Python 安装与管理使用指南

相关免费在线工具

  • 加密/解密文本

    使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online

  • Gemini 图片去水印

    基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online

  • Base64 字符串编码/解码

    将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

  • Base64 文件转换器

    将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online

  • Markdown转HTML

    将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online

  • HTML转Markdown

    将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online