Skip to content

Commit 9707c83

Browse files
committed
add maximum subarray solution
1 parent 2358623 commit 9707c83

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

โ€Žmaximum-subarray/Tessa1217.java

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

0 commit comments

Comments
ย (0)