跳到主要内容
极客日志极客日志面向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/8/1847 浏览
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

更多推荐文章

查看全部
  • 代理 IP 服务商横向测评:基于动态住宅代理的 AI 任务管理
  • 基于 MS-Swift 框架的 DeepSeek-R1 部署与微调指南
  • .NET 8 跨平台自动化开发实战:从设备监控到路径规划
  • Transformer 架构详解与大模型应用实战指南
  • Win11 上折腾 Dify 本地 AI Agent 的记录
  • LangGraph4J 构建 Java 多智能体工作流指南
  • Redis Cluster 哈希槽分片机制与 Linux 集群搭建实操
  • Java 21 虚拟线程日志优化与百万级并发追踪最佳实践
  • C++ 手写 Web 服务器:基于 JSON 与 HTTP 协议的实现
  • 10 分钟搭建专属 AI Agent 全流程实操指南
  • Java 分治算法实战:快速与归并排序
  • AR 健身教练:形随心动 - 基于 Rokid CXR-M SDK 的实践落地
  • C++ 二叉搜索树详解:增删查改与 key/value 场景实现
  • Rust 语言的前世今生与核心技术解析
  • Ubuntu 本地部署 OpenClaw:接入 Ollama 推理与飞书通道
  • Dify AI 智能体部署与使用指南
  • Dev-C++ 安装、配置与快捷键使用指南
  • ChatGPT 降低 AIGC 率指令实战:从原理到最佳实践
  • 理解 IDE 调用大模型的 Session 机制与管理
  • 用 LLama-Factory 微调个性化 AI 角色

相关免费在线工具

  • 加密/解密文本

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