|
@@ -0,0 +1,45 @@
|
|
|
|
|
+package leetcode.p3200;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @ProjectName: LeetCode
|
|
|
|
|
+ * @FileName: Solution
|
|
|
|
|
+ * @Author: 杨逸
|
|
|
|
|
+ * @Data:2024/10/15 11:12
|
|
|
|
|
+ * @Description: https://leetcode.cn/problems/maximum-height-of-a-triangle/
|
|
|
|
|
+ * 3200. 三角形的最大高度
|
|
|
|
|
+ */
|
|
|
|
|
+public class Solution {
|
|
|
|
|
+ public int maxHeightOfTriangle(int red, int blue) {
|
|
|
|
|
+ if (red==0||blue==0)return 1;
|
|
|
|
|
+ int ans = 0,temp = 0;
|
|
|
|
|
+ //红的在第一层
|
|
|
|
|
+ ans = f(red, blue, 0);
|
|
|
|
|
+ //蓝的在第一层
|
|
|
|
|
+ ans = Math.max(ans,f(red, blue, 1));
|
|
|
|
|
+ return ans;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private int f(int r,int b,int flag){
|
|
|
|
|
+ int result = 0;
|
|
|
|
|
+ while (true){
|
|
|
|
|
+ if (flag == 0){
|
|
|
|
|
+ if ((r-(result+1))>=0){
|
|
|
|
|
+ result++;
|
|
|
|
|
+ r -= result;
|
|
|
|
|
+ flag = 1;
|
|
|
|
|
+ }else {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }else {
|
|
|
|
|
+ if ((b-(result+1))>=0){
|
|
|
|
|
+ result++;
|
|
|
|
|
+ b -= result;
|
|
|
|
|
+ flag = 0;
|
|
|
|
|
+ }else {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|