Skip to content

Commit 160d1d8

Browse files
authored
Merge pull request #2158 from chjung99/main
[chjung99] WEEK 05 solutions
2 parents 3970805 + 1f03fb7 commit 160d1d8

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed

3sum/chjung99.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// two pointer
2+
// time: O(N^2)
3+
// space: O(N)
4+
5+
class Solution {
6+
Set<List<Integer>> set = new HashSet<>();
7+
public List<List<Integer>> threeSum(int[] nums) {
8+
Arrays.sort(nums);
9+
10+
for (int i = 0; i < nums.length; i++){
11+
twoSum(nums, i);
12+
}
13+
14+
return new ArrayList<>(set);
15+
}
16+
public void twoSum(int[] nums, int targetIdx){
17+
int left = 0;
18+
int right = nums.length - 1;
19+
20+
while (left < right) {
21+
if (left == targetIdx){
22+
left ++;
23+
continue;
24+
}
25+
if (right == targetIdx){
26+
right--;
27+
continue;
28+
}
29+
if (nums[left] + nums[right] == -nums[targetIdx]) {
30+
if (nums[left] > nums[targetIdx]){
31+
set.add(List.of(nums[targetIdx], nums[left], nums[right]));
32+
}
33+
else if (nums[targetIdx] > nums[right]){
34+
set.add(List.of(nums[left],nums[right],nums[targetIdx]));
35+
} else{
36+
set.add(List.of(nums[left], nums[targetIdx], nums[right]));
37+
}
38+
left++;
39+
} else if (nums[left] + nums[right] < -nums[targetIdx]){
40+
left ++;
41+
} else {
42+
right --;
43+
}
44+
}
45+
}
46+
}
47+
48+

combination-sum/chjung99.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
Set<List<Integer>> combination = new HashSet<>();
3+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
4+
Arrays.sort(candidates);
5+
findCombination(candidates, target, 0, 0, new ArrayList<>());
6+
return new ArrayList<>(combination);
7+
}
8+
9+
public void findCombination(int[] candidates, int target, int curIdx, int curSum, List<Integer> curList) {
10+
if (curSum == target) {
11+
combination.add(new ArrayList<>(curList));
12+
return;
13+
}
14+
15+
if (curSum > target) {
16+
return;
17+
}
18+
19+
for (int i = curIdx; i < candidates.length; i++){
20+
21+
curList.add(candidates[i]);
22+
findCombination(candidates, target, i, curSum + candidates[i], curList);
23+
curList.remove(curList.size()-1);
24+
}
25+
}
26+
}
27+
28+
29+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// two pointer
2+
// time: O(N)
3+
// space: O(1)
4+
class Solution {
5+
public int maxArea(int[] height) {
6+
int left = 0;
7+
int right = height.length-1;
8+
int answer = 0;
9+
10+
int waterWidth = 0;
11+
int waterHeight = 0;
12+
13+
while (left < right){
14+
waterWidth = (right - left);
15+
waterHeight = Math.min(height[left], height[right]);
16+
answer = Math.max(answer, waterWidth * waterHeight);
17+
if (height[left] <= height[right]) {
18+
left ++;
19+
}else{
20+
right --;
21+
}
22+
}
23+
24+
return answer;
25+
}
26+
}
27+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode removeNthFromEnd(ListNode head, int n) {
13+
int size = getSizeOfListNode(head);
14+
15+
if (size - n - 1 >= 0) {
16+
ListNode prev = moveNth(head, size - n - 1);
17+
ListNode target = moveNth(head, size - n);
18+
prev.next = target.next;
19+
} else {
20+
head = head.next;
21+
}
22+
23+
24+
return head;
25+
}
26+
public ListNode moveNth(ListNode head, int n){
27+
while (head != null){
28+
if (n == 0) break;
29+
head = head.next;
30+
n--;
31+
}
32+
return head;
33+
}
34+
public void removeNth(ListNode head, int n){
35+
int cnt = n;
36+
37+
}
38+
public int getSizeOfListNode(ListNode head){
39+
int size = 0;
40+
while (head != null) {
41+
size ++;
42+
head = head.next;
43+
}
44+
return size;
45+
}
46+
}
47+
48+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
public int search(int[] nums, int target) {
3+
int leftSide = 0;
4+
int rightSide = nums.length - 1;
5+
int left = 0;
6+
int right = nums.length;
7+
int mid;
8+
9+
if (nums.length == 1) {
10+
if (nums[0] == target) return 0;
11+
else return -1;
12+
}
13+
14+
while (left + 1 < right) {
15+
mid = (int) (left + right) / 2;
16+
17+
if (nums[mid]==target) {
18+
break;
19+
}
20+
else if (nums[mid] < target){ // 값이 증가해야함
21+
if (nums[mid] >= nums[leftSide]){ // 왼쪽 그룹에 속하는 지
22+
left = mid;
23+
} else { // 오른쪽 그룹에 속하는 지
24+
if (nums[rightSide] < target) {
25+
right = mid;
26+
} else {
27+
left = mid;
28+
}
29+
}
30+
} else{ // nums[mid] > target // 값이 감소해야함
31+
if (nums[mid] >= nums[leftSide]){ // 왼쪽 그룹에 속하는 지
32+
if (nums[leftSide] <= target) {
33+
right = mid;
34+
} else {
35+
left = mid;
36+
}
37+
} else { // 오른쪽 그룹에 속하는 지
38+
right = mid;
39+
}
40+
41+
}
42+
}
43+
mid = (int)((left + right) / 2);
44+
if (nums[mid]==target) return mid;
45+
else return -1;
46+
}
47+
}
48+
49+

0 commit comments

Comments
 (0)