Leetcode3200. 三角形的最大高度

Every day a Leetcode

题目来源:3200. 三角形的最大高度

解法1:模拟

枚举第一行是红色还是蓝色,再按题意模拟即可。

代码:

c 复制代码
/*
 * @lc app=leetcode.cn id=3200 lang=cpp
 *
 * [3200] 三角形的最大高度
 */

// @lc code=start
class Solution
{
public:
    int maxHeightOfTriangle(int red, int blue)
    {
        if (red <= 0 || blue <= 0)
            return 0;

        return max(helper(red, blue), helper(blue, red));
    }
    // 辅助函数
    int helper(int x, int y)
    {
        int level = 0;
        while (x >= 0 && y >= 0)
        {
            if (level % 2 == 0)
            {
                x -= (level + 1);
                if (x < 0)
                    break;
            }
            else
            {
                y -= (level + 1);
                if (y < 0)
                    break;
            }
            level++;
        }
        return level;
    }
};
// @lc code=end

结果:

复杂度分析:

时间复杂度:O(min(sqrt(red), sqrt(blue)))。

空间复杂度:O(1)。

相关推荐
2301_7634724637 分钟前
C++20概念(Concepts)入门指南
开发语言·c++·算法
阿猿收手吧!1 小时前
【C++】std::promise原理与实战解析
c++
m0_706653231 小时前
分布式系统安全通信
开发语言·c++·算法
Zach_yuan2 小时前
深入浅出 JSONCpp
linux·服务器·网络·c++
寻寻觅觅☆2 小时前
东华OJ-基础题-104-A == B ?(C++)
开发语言·c++
lightqjx2 小时前
【C++】unordered系列的封装
开发语言·c++·stl·unordered系列
alphaTao3 小时前
LeetCode 每日一题 2026/2/2-2026/2/8
算法·leetcode
甄心爱学习3 小时前
【leetcode】判断平衡二叉树
python·算法·leetcode
阿猿收手吧!3 小时前
【C++】string_view:高效字符串处理指南
开发语言·c++
不知名XL3 小时前
day50 单调栈
数据结构·算法·leetcode