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)。

相关推荐
智者知已应修善业几秒前
【冰雹猜想过程逆序输出】2025-4-19
c语言·c++·经验分享·笔记·算法
美好的事情能不能发生在我身上16 分钟前
Leetcode热题100中的:哈希专题
算法·leetcode·哈希算法
wefg122 分钟前
【算法】倍增思想(快速幂)
数据结构·c++·算法
锅包一切40 分钟前
一、C++ 发展与程序创建
开发语言·c++·后端·学习·编程
逆境不可逃1 小时前
LeetCode 热题 100 之 41.缺失的第一个正数
算法·leetcode·职场和发展
power 雀儿1 小时前
LibTorch激活函数&LayerNorm归一化
c++·人工智能
枷锁—sha1 小时前
【CTFshow-pwn系列】03_栈溢出【pwn 051】详解:C++字符串替换引发的血案与 Ret2Text
开发语言·网络·c++·笔记·安全·网络安全
YXXY3132 小时前
C++11的介绍(上)
c++
We་ct2 小时前
LeetCode 173. 二叉搜索树迭代器:BSTIterator类 实现与解析
前端·算法·leetcode·typescript
sycmancia3 小时前
C++——析构函数的调用顺序、const修饰对象、类成员
开发语言·c++