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

相关推荐
迷迭所归处5 小时前
C++ —— 关于vector
开发语言·c++·算法
CV工程师小林6 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
white__ice6 小时前
2024.9.19
c++
天玑y6 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
姜太公钓鲸2337 小时前
c++ static(详解)
开发语言·c++
菜菜想进步7 小时前
内存管理(C++版)
c语言·开发语言·c++
sjsjs117 小时前
【数据结构-一维差分】力扣1893. 检查是否区域内所有整数都被覆盖
数据结构·算法·leetcode
m0_571957587 小时前
Java | Leetcode Java题解之第406题根据身高重建队列
java·leetcode·题解
Joker100857 小时前
C++初阶学习——探索STL奥秘——模拟实现list类
c++
山脚ice7 小时前
【Hot100】LeetCode—72. 编辑距离
算法·leetcode