Merge pull request #2743 from tony8888lrz/master

Update 0343.整数拆分.md Java版本增加贪心算法,清晰注释
This commit is contained in:
程序员Carl
2024-09-27 10:41:13 +08:00
committed by GitHub

View File

@ -243,6 +243,29 @@ class Solution {
}
}
```
贪心
```Java
class Solution {
public int integerBreak(int n) {
// with 贪心
// 通过数学原理拆出更多的3乘积越大
/**
@Param: an int, the integer we need to break.
@Return: an int, the maximum integer after breaking
@Method: Using math principle to solve this problem
@Time complexity: O(1)
**/
if(n == 2) return 1;
if(n == 3) return 2;
int result = 1;
while(n > 4) {
n-=3;
result *=3;
}
return result*n;
}
}
```
### Python
动态规划版本一