We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c37eac9 commit d2eb309Copy full SHA for d2eb309
1 file changed
โmaximum-subarray/hyeri0903.javaโ
@@ -0,0 +1,23 @@
1
+class Solution {
2
+ public int maxSubArray(int[] nums) {
3
+ /**
4
+ 1. ๋ฌธ์ : ๊ฐ์ฅ ํฐ ํฉ์ ๊ฐ์ง๋ subarray ์ sum ์ ๋ฐํ
5
+ 2. ์กฐ๊ฑด: ์์๊ฐ์ ์์ ~ ์์, ๋ฐฐ์ด ์ต๋ ๊ธธ์ด = 10^5, ์ต์ ๊ธธ์ด = 1
6
+ - time complexity: O(N)
7
+ - space complexity: O(1)
8
+ */
9
+
10
+ if (nums.length == 1) {
11
+ return nums[0];
12
+ }
13
14
+ int maxSum = nums[0]; //์ ์ฒด ์ต๋
15
+ int curSum = nums[0]; //ํ์ฌ ํฉ
16
17
+ for(int i = 1; i<nums.length; i++) {
18
+ curSum = Math.max(nums[i], curSum + nums[i]);
19
+ maxSum = Math.max(maxSum, curSum);
20
21
+ return maxSum;
22
23
+}
0 commit comments