1. ๋ฌธ์ : https://leetcode.com/problems/maximum-product-subarray/
ํ์ array ๊ณฑ ์ค ์ต๋๊ฐ
2. ํ์ด
์ค์๊ฐ์ผ๋ก max, min ๊ฐ์ ๊ฐ์ง๊ณ ํ์ฌ ๊ฐ์ ๊ณฑํด์ฃผ๊ณ ๊ฐ์ ๋น๊ตํ๋ ๋ฐฉ์์ผ๋ก ์ต๋๊ฐ์ update
dp ๊ธฐ๋ณธ๋ฌธ์ ..ใ ใ
3. ์ฝ๋
class Solution {
public int maxProduct(int[] nums) {
int maxProducts = nums[0];
int minProducts = nums[0];
int ans = nums[0];
for(int i=1;i<nums.length;i++){
int now = nums[i];
int a = maxProducts * now;
int b = minProducts * now;
maxProducts = Math.max(now, Math.max(a, b));
minProducts = Math.min(now, Math.min(a, b));
ans = Math.max(ans, maxProducts);
}
return ans;
}
};'๐ป Coding Problems Solving > Dynamic Programming' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [LeetCode] Longest Increasing Subsequence (0) | 2023.03.30 |
|---|---|
| [LeetCode] Coin Change (0) | 2023.03.29 |
| [ํ๋ก๊ทธ๋๋จธ์ค] LV.2 2*n ํ์ผ๋ง (์๋ฐ java) (0) | 2023.03.15 |
| [ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ ๋ฐ๋จน๊ธฐ (์๋ฐ java) (0) | 2023.03.10 |
| [ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ฉ๋ฆฌ ๋ฐ๊ธฐ (์๋ฐ java) (0) | 2023.01.12 |




์ต๊ทผ๋๊ธ