반응형
https://leetcode.com/problems/integer-break/description/
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
n을 쪼갠 수들의 곱을 최대화할 수 있는 경우를 찾아줍니다.
dp[n] = max({dp[n], i * dp[n - i], i * (n - i)} // 1 <= i <= n/2
class Solution {
public:
int dp[59] = {0};
int integerBreak(int n) {
if(n == 2) return 1;
if(dp[n]) return dp[n];
for(int i=1; i<=n/2; i++) {
dp[n] = max({dp[n], i * integerBreak(n - i), i * (n - i)});
}
return dp[n];
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 34. Find First and Last Position of Element in Sorted Array (0) | 2023.10.09 |
---|---|
LeetCode 1458. Max Dot Product of Two Subsequences (1) | 2023.10.08 |
LeetCode 1420. Build Array Where You Can Find The Maximum Exactly K Comparisons (0) | 2023.10.07 |
LeetCode 706. Design HashMap (0) | 2023.10.04 |
LeetCode 1512. Number of Good Pairs (0) | 2023.10.03 |