forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYn3-3xh.java
More file actions
23 lines (21 loc) ยท 758 Bytes
/
Yn3-3xh.java
File metadata and controls
23 lines (21 loc) ยท 758 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
[๋ฌธ์ ํ์ด]
- ์ฃผ์ด์ง ๋ฐฐ์ด์์ ์ฐ์๋ ์ ๋ฐฐ์ด์ ํฉ์ด ํฐ ์ ๊ตฌํ๊ธฐ
- (1) ํ์ฌ์์ ํ์ฌ๊น์ง์ ํฉ ์ค ํฐ ์ ๊ตฌํ๊ธฐ : ํ์ฌ ์ธ๋ฑ์ค๋ถํฐ ์์๋๊ฑฐ๋, ํ์ฌ ์ธ๋ฑ์ค๊น์ง ๋ํด์ง ๊ฒ
- (2) ์ต๋๊ฐ๊ณผ (1)๋ฒ ์ ์ค ํฐ ์ ๊ตฌํ๊ธฐ
time: O(N), space: O(1)
[ํ๊ณ ]
์๋ฃจ์
๊น์ง๋ ๊ทผ์ ํ๋๋ฐ, ๊ฒฐ๊ตญ ํด๊ฒฐ์ ์๊พธ ์๋๋ค..
์ด๋ป๊ฒ ์ ๊ทผํด์ผ ์๋ฃจ์
๊น์ง ๋๋ฌ ํ ์ ์์๊น..
*/
class Solution {
public int maxSubArray(int[] nums) {
int max = nums[0];
int sum = nums[0];
for (int i = 1; i < nums.length; i++) {
sum = Math.max(nums[i], sum + nums[i]);
max = Math.max(max, sum);
}
return max;
}
}