跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客我的书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/10/885 浏览
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:

更多推荐文章

查看全部
  • 网络安全等级保护测评中的渗透测试实施与应用
  • String、StringBuilder、StringBuffer 的区别
  • Spatial Joy 2025 全球 AR&AI 开发大赛参赛指南
  • Python 爬虫实战:批量下载百度图片
  • SpringAI 通过 Ollama 本地部署 Deepseek 模型实现对话机器人
  • IDM 试用管理脚本的技术原理与安全风险解析
  • 飞书自定义机器人 Webhook 接入指南
  • Zotero 论文阅读颜色标记体系
  • 声源定位算法基础:CBF(延时求和波束形成)
  • Python 入门基础:人工智能核心语言与语法详解
  • 基于 Django 与爬虫的招聘信息推荐系统实现
  • SpringBoot 整合 Neo4j 图数据库项目实战详解
  • SpringBoot 整合 LangChain4j 实现 RAG 检索实战详解
  • VS Code 远程连接服务器后 GitHub Copilot 无法使用解决方案
  • GLM-5 大模型代码生成能力深度评测与实战
  • C++ STL 容器 set 与 map 使用详解
  • Stack-Chan 机器人快速入门指南
  • Qt 布局管理器详解:QVBoxLayout、QHBoxLayout、QGridLayout、QFormLayout
  • AI 大模型提示工程(Prompt)核心技巧与工具详解
  • 基于 Higress 网关将 REST API 转换为 MCP Server 工具

相关免费在线工具

  • 加密/解密文本

    使用加密算法(如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