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;
    }
};
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ