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;
}
}