Skip to content

Commit 42be3de

Browse files
committed
3주차 문제 풀이 1개 추가
- Maximum Subarray
1 parent fe1cfaa commit 42be3de

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

maximum-subarray/hwi-middle.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int maxSubArray(vector<int>& nums) {
4+
int cur = 0;
5+
int ans = -1e5;
6+
for (int n : nums)
7+
{
8+
// 이전까지 합이 음수라면 무조건 다시 시작하는게 이득
9+
if (cur < 0)
10+
{
11+
cur = 0;
12+
}
13+
14+
cur += n;
15+
ans = max(cur, ans);
16+
}
17+
18+
return ans;
19+
}
20+
};

0 commit comments

Comments
 (0)