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

相关推荐
凡人叶枫1 小时前
Effective C++ 条款04:确定对象被使用前已先被初始化
java·linux·开发语言·c++·嵌入式开发
不想写代码的星星1 小时前
std::move 根本不移动,就像老婆饼里没有老婆
c++
redaijufeng2 小时前
C++雾中风景7:闭包
c++·算法·风景
小欣加油2 小时前
leetcode287寻找重复数
数据结构·c++·算法·leetcode
思麟呀2 小时前
C++11 核心特性(三):强类型枚举、static_assert 与 std::tuple
开发语言·c++
一拳一个呆瓜2 小时前
【STL】C++程序的启动与终止
c++·stl
凡人叶枫3 小时前
Effective C++ 条款07:为多态基类声明 virtual 析构函数
linux·c语言·开发语言·c++
凡人叶枫3 小时前
Effective C++ 条款10:令 operator= 返回一个 reference to *this
java·linux·服务器·开发语言·c++·effective c++
王老师青少年编程3 小时前
2026年全国青少年信息素养大赛算法应用主题赛(C++赛项-复赛模拟卷6:文末附答案)
c++·答案·模拟卷·复赛·2026年·青少年信息素养大赛·算法应用主题赛
怪兽学LLM3 小时前
LeetCode 438 找到字符串中所有字母异位词(Python 固定滑动窗口+字符计数解法)
python·算法·leetcode