iT邦幫忙

2024 iThome 鐵人賽

DAY 12
0
自我挑戰組

Leetcode 解題之旅:逐日攻克系列 第 12

每日一LeetCode(12)

  • 分享至 

  • xImage
  •  

3200.Maximum Heihgt of a Triangle

題目敘述:

You are given two integers red and blue representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1st row will have 1 ball, the 2nd row will have 2 balls, the 3rd row will have 3 balls, and so on.

All the balls in a particular row should be the same color, and adjacent rows should have different colors.

Return the maximum height of the triangle that can be achieved.

class Solution {
public:
    int maxHeightOfTriangle(int red, int blue) 
    {
        return max(helper(red, blue), helper(blue, red));
    }
    private:
        int helper(int red,int blue)
        {
            int h=0,i=1;

            while(true)
            {
                if(i%2==1)
                {
                    if(red >= i)
                    {
                        red -= i;
                    }
                    else
                        break;
                }
                else 
                {
                    if (blue >= i) 
                    {
                        blue -= i;
                    } else 
                    {
                        break;
                    }
                }
                h++;
                i++;
            }
            return h;
        }
};

上一篇
每日一LeetCode(11)
下一篇
每日一LeetCode(13)
系列文
Leetcode 解題之旅:逐日攻克17
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言