-
-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathjinvicky.java
More file actions
39 lines (34 loc) ยท 1.37 KB
/
jinvicky.java
File metadata and controls
39 lines (34 loc) ยท 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
// ์ต์๊ฐ์ ์์๊ฐ ๊ณฑํด์ง ๋ ์ต๋๊ฐ์ด ๋ ์ ์๊ธฐ ๋๋ฌธ์ ์ต์, ์ต๋๋ฅผ ๊ฐ๊ฐ dp[]๋ก ๊ด๋ฆฌํด์ผ ํ๋ค.
public int maxProduct(int[] nums) {
if (nums.length == 1)
return nums[0];
if (nums.length == 2) {
return Math.max(nums[0], Math.max(nums[0] * nums[1], nums[1]));
}
int len = nums.length;
int[] max = new int[len];
int[] min = new int[len];
int overall = 0;
max[0] = min[0] = overall = nums[0];
for (int i = 1; i < len; i++) {
// ํ๋ณด 3์ ์ค๋น
/**
* ํ์ฌ ์ธ๋ฑ์ค๊ฐ (justNum)
* ์ด์ ์ธ๋ฑ์ค ์ต์๊ฐ x ํ์ฌ ์ธ๋ฑ์ค ๊ฐ (reverse)
* ์ด์ ์ธ๋ฑ์ค ์ต๋๊ฐ x ํ์ฌ ์ธ๋ฑ์ค ๊ฐ (keep)
*/
int justNum = nums[i];
// ๊ณ์ ๋ํ ๊ฐ
int keep = justNum * max[i-1];
// ์ด์ ์ต์์ ์์ ๊ณฑํด์ ๋ฆฌ๋ฒ์ค
int reverse = justNum * min[i-1];
// max์ min ๋ฐฐ์ด์ ์
๋ฐ์ดํธ
max[i] = Math.max(justNum, Math.max(keep, reverse));
min[i] = Math.min(justNum, Math.min(keep, reverse));
// overall์ ์
๋ฐ์ดํธ, ๋์ ๋น๊ต๋ก ์ต๋ ์ ์ญ ์ ์ง
overall = Math.max(overall, max[i]);
}
return overall;
}
}