File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Conditions:
3+ - 1 <= nums.length <= 10^5
4+ - -10^4 <= nums[i] <= 10^4
5+
6+ Time Complexity: O(n)
7+ - λ°°μ΄μ ν λ²λ§ μνν¨
8+
9+ Space Complexity: O(1)
10+ - λ λ³μ μ΄μΈμ μΆκ° 곡κ°μ μ¬μ©νμ§ μμ
11+
12+ νμ΄λ°©λ²:
13+ 1. Base case: If nums is empty, return 0
14+ 2. Initialize variables (current_sum and max_sum as the first value in the array)
15+ 3. Traverse from the value at 1st index to the last, update current_sum
16+ - Decide whether to add the current value (num) to the existing subarray or start a new one
17+ 4. Update max_sum
18+ - Choose the larger between the updated current_sum and the previous max_sum
19+ """
20+ class Solution :
21+ def maxSubArray (self , nums : List [int ]) -> int :
22+ if not nums :
23+ return 0
24+
25+ current_sum = max_sum = nums [0 ]
26+
27+ for num in nums [1 :]:
28+ current_sum = max (num , current_sum + num )
29+ max_sum = max (current_sum , max_sum )
30+
31+ return max_sum
You canβt perform that action at this time.
0 commit comments