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

相关推荐
superman超哥37 分钟前
仓颉锁竞争优化深度解析
c语言·开发语言·c++·python·仓颉
yaoh.wang1 小时前
力扣(LeetCode) 111: 二叉树的最小深度 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·深度优先
charlie1145141911 小时前
快速在WSL上开发一般的C++上位机程序
开发语言·c++·笔记·学习·环境配置·工程
夏幻灵2 小时前
C++ 中手动重载赋值运算符(operator=)时实现部分复制的思路和方法
开发语言·c++·算法
superman超哥2 小时前
仓颉语言中包与模块系统的深度剖析与工程实践
c语言·开发语言·c++·python·仓颉
x70x802 小时前
C++中不同容器的用法及接口(vector / deque / stack / queue / priority_queue)
开发语言·c++
努力学算法的蒟蒻2 小时前
day42(12.23)——leetcode面试经典150
算法·leetcode·面试
鹿角片ljp3 小时前
力扣226.翻转二叉树-递归
数据结构·算法·leetcode
qq_310658513 小时前
janux源码走读(五)Janus事件处理模块(events/)
服务器·c++·音视频
WBluuue3 小时前
数据结构和算法:Morris遍历
数据结构·c++·算法