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 小时前
C++编程建造器设计模式
java·c++·设计模式
做怪小疯子1 小时前
LeetCode 热题 100——链表——相交链表
算法·leetcode·链表
while(努力):进步2 小时前
5G与物联网:连接万物的数字化未来
leetcode
喵个咪2 小时前
Qt 6 实战:C++ 调用 QML 回调方法(异步场景完整实现)
前端·c++·qt
2501_941804324 小时前
C++在高性能互联网服务开发与系统优化中的应用与实战经验解析
leetcode
希望有朝一日能如愿以偿5 小时前
力扣每日一题:可被三整除的最大和
数据结构·算法·leetcode
阿波茨的鹅5 小时前
VSCode C++ 项目配置教程
c++·ide·vscode
程序员与背包客_CoderZ5 小时前
C/C++版LLM推理框架Llama.cpp——入门与编码实战
c语言·开发语言·网络·c++·人工智能·语言模型·llama
喵了几个咪5 小时前
C++ IDE:最适合 C++ 初学者的 IDE 是什么?
开发语言·c++·ide
2501_941802485 小时前
C++高性能并发编程实战:从多线程管理到内存优化与任务调度全流程解析
java·开发语言·c++