Skip to content

Latest commit

 

History

History
26 lines (20 loc) · 712 Bytes

File metadata and controls

26 lines (20 loc) · 712 Bytes
class Solution {
    public int maxSubArray(int[] nums) {
        int maxSum = Integer.MIN_VALUE;
        int currentSum = 0;

        for (int i = 0; i < nums.length; i++) {
            currentSum += nums[i];
            maxSum = Math.max(maxSum, currentSum);
            currentSum = Math.max(currentSum, 0);
        }

        return maxSum;
    }
}
  • currentSum이 maxSum보다 클 경우 maxSum을 갱신한다.
  • currentSum은 음수일 경우 (0보다 작을 경우) 0으로 초기화 한다.

TC, SC

시간 복잡도는 O(n), 공간 복잡도는 O(1) 이다.