Skip to content

Commit 557ca27

Browse files
committed
maximum subarray
1 parent 5c2fc91 commit 557ca27

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Runtime: 1ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 76.88MB
6+
* Space Complexity: O(1)
7+
*
8+
* Approach: ์นด๋ฐ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜
9+
* - ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ํ•ฉ์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋Š” ๊ฐ’์„ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
10+
* 1) ์ด์ „ ํ•ฉ๋ณด๋‹ค ํ˜„์žฌ ์ˆซ์ž๊ฐ€ ๋” ํฌ๋ฉด ํ˜„์žฌ ์ˆซ์ž๋กœ sum์„ ์ดˆ๊ธฐํ™”
11+
*/
12+
class Solution {
13+
public int maxSubArray(int[] nums) {
14+
int sum = nums[0];
15+
int max = nums[0];
16+
17+
for (int i=1; i<nums.length; i++) {
18+
sum = Math.max(nums[i], sum+nums[i]);
19+
max = Math.max(max, sum);
20+
}
21+
22+
return max;
23+
}
24+
}

0 commit comments

Comments
ย (0)