Skip to content

Commit 9506e9e

Browse files
committed
leetcode 5일차
1 parent dca60e0 commit 9506e9e

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode.easy;
2+
3+
/**
4+
* https://leetcode.com/problems/maximum-subarray/
5+
*/
6+
public class maximum_subarray {
7+
public static void main(String[] args) {
8+
int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
9+
System.out.println(maxSubArray(nums));
10+
}
11+
12+
static public int maxSubArray(int[] nums) {
13+
int max = Integer.MIN_VALUE;
14+
int sum = 0;
15+
for (int i = 0; i < nums.length; i++) {
16+
sum += nums[i];
17+
if (max < sum) max = sum;
18+
if (sum < 0) sum = 0;
19+
}
20+
return max;
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode.easy;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* https://leetcode.com/problems/search-insert-position
7+
*/
8+
public class search_insert_position {
9+
public static void main(String[] args) {
10+
int[] nums = {1, 3, 5, 6};
11+
int target = 7;
12+
System.out.println(searchInsert(nums, target));
13+
}
14+
15+
static public int searchInsert(int[] nums, int target) {
16+
int searchedIdx = Arrays.binarySearch(nums, target);
17+
return searchedIdx < 0 ? Math.abs(searchedIdx) - 1 : searchedIdx;
18+
}
19+
}

0 commit comments

Comments
 (0)