Skip to content

Commit a650368

Browse files
committed
maximum subarray solution
1 parent 28137fd commit a650368

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
[๋ฌธ์ œํ’€์ด]
3+
- ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ ์—ฐ์†๋œ ์ˆ˜ ๋ฐฐ์—ด์˜ ํ•ฉ์ด ํฐ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
4+
- (1) ํ˜„์žฌ์ˆ˜์™€ ํ˜„์žฌ๊นŒ์ง€์˜ ํ•ฉ ์ค‘ ํฐ ์ˆ˜ ๊ตฌํ•˜๊ธฐ : ํ˜„์žฌ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์‹œ์ž‘๋˜๊ฑฐ๋‚˜, ํ˜„์žฌ ์ธ๋ฑ์Šค๊นŒ์ง€ ๋”ํ•ด์งˆ ๊ฒƒ
5+
- (2) ์ตœ๋Œ“๊ฐ’๊ณผ (1)๋ฒˆ ์ˆ˜ ์ค‘ ํฐ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
6+
time: O(N), space: O(1)
7+
8+
[ํšŒ๊ณ ]
9+
์†”๋ฃจ์…˜๊นŒ์ง€๋Š” ๊ทผ์ ‘ํ•˜๋Š”๋ฐ, ๊ฒฐ๊ตญ ํ•ด๊ฒฐ์€ ์ž๊พธ ์•ˆ๋œ๋‹ค..
10+
์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•ด์•ผ ์†”๋ฃจ์…˜๊นŒ์ง€ ๋„๋‹ฌ ํ•  ์ˆ˜ ์žˆ์„๊นŒ..
11+
*/
12+
class Solution {
13+
public int maxSubArray(int[] nums) {
14+
int max = nums[0];
15+
int sum = nums[0];
16+
for (int i = 1; i < nums.length; i++) {
17+
sum = Math.max(nums[i], sum + nums[i]);
18+
max = Math.max(max, sum);
19+
}
20+
return max;
21+
}
22+
}
23+

0 commit comments

Comments
ย (0)