Parcourir la source

#feat:leetcode p3200题题解

yangyi il y a 2 semaines
Parent
commit
7c40c5c57c
2 fichiers modifiés avec 64 ajouts et 0 suppressions
  1. 45 0
      src/leetcode/p3200/Solution.java
  2. 19 0
      src/leetcode/p3200/SolutionTest.java

+ 45 - 0
src/leetcode/p3200/Solution.java

@@ -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;
+    }
+}

+ 19 - 0
src/leetcode/p3200/SolutionTest.java

@@ -0,0 +1,19 @@
+package leetcode.p3200;
+
+import org.junit.Test;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: SolutionTest
+ * @Author: 杨逸
+ * @Data:2024/10/15 11:28
+ * @Description:
+ */
+public class SolutionTest {
+
+    @Test
+    public void maxHeightOfTriangle() {
+        Solution solution = new Solution();
+        System.out.println(solution.maxHeightOfTriangle(2, 4));
+    }
+}