forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathse6816.java
More file actions
40 lines (37 loc) ยท 1.05 KB
/
se6816.java
File metadata and controls
40 lines (37 loc) ยท 1.05 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
40
/**
dp๋ฅผ ์ด์ฉํ์ฌ ์ง์์ ์ผ๋ก ์ต๋์ ๊ฐ์ ๊ธฐ๋กํ๋ฉด์ ์ฐ์ ๋ถ๋ถ ๋ฐฐ์ด์ ์ต๋ ํฉ์ ๊ตฌํ๋ ๋ฐฉ์
nums ์ ๊ธธ์ด -> N
์๊ฐ ๋ณต์ก๋ : O(N)
๊ณต๊ฐ ๋ณต์ก๋ : O(N)
*/
class Solution2 {
public int maxSubArray(int[] nums) {
int[] dp = new int[nums.length];
dp[0] = nums[0];
for(int i = 1; i < nums.length; i++) {
dp[i] = Math.max(nums[i], dp[i-1] + nums[i]);
}
for(int i = 0; i < dp.length; i++) {
}
return Arrays.stream(dp)
.max()
.getAsInt();
}
}
/**
์ด์ ๋ฐฉ์๊ณผ ๋์ผํ๋ ๋ณ์ ํ๋๋ก ์ด์ ๊ฐ๋ง ์ ์งํ๋ ๋ฐฉ์
nums ์ ๊ธธ์ด -> N
์๊ฐ ๋ณต์ก๋ : O(N)
๊ณต๊ฐ ๋ณต์ก๋ : O(1)
*/
class Solution {
public int maxSubArray(int[] nums) {
int prevSum = nums[0];
int result = prevSum;
for(int i = 1; i < nums.length; i++) {
prevSum = Math.max(nums[i], prevSum + nums[i]);
result = Math.max(result, prevSum);
}
return result;
}
}