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

相关推荐
暮冬-  Gentle°8 小时前
C++中的命令模式实战
开发语言·c++·算法
㓗冽11 小时前
分解质因数-进阶题10
c++
图图的点云库11 小时前
高斯滤波实现算法
c++·算法·最小二乘法
一叶落43812 小时前
题目:15. 三数之和
c语言·数据结构·算法·leetcode
CoderCodingNo12 小时前
【GESP】C++七级考试大纲知识点梳理, (1) 数学库常用函数
开发语言·c++
老鱼说AI12 小时前
CUDA架构与高性能程序设计:异构数据并行计算
开发语言·c++·人工智能·算法·架构·cuda
big_rabbit050213 小时前
[算法][力扣222]完全二叉树的节点个数
数据结构·算法·leetcode
张李浩14 小时前
Leetcode 15三题之和
算法·leetcode·职场和发展
2301_7938046914 小时前
C++中的适配器模式变体
开发语言·c++·算法
x_xbx14 小时前
LeetCode:206. 反转链表
算法·leetcode·链表