From f41279e8d5b7addbbe24a23bab9078c4435d9096 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Tue, 9 Dec 2025 00:01:04 +0900 Subject: [PATCH 1/5] container-with-most-water --- container-with-most-water/chjung99.java | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 container-with-most-water/chjung99.java diff --git a/container-with-most-water/chjung99.java b/container-with-most-water/chjung99.java new file mode 100644 index 000000000..fe5f05ea4 --- /dev/null +++ b/container-with-most-water/chjung99.java @@ -0,0 +1,27 @@ +// two pointer +// time: O(N) +// space: O(1) +class Solution { + public int maxArea(int[] height) { + int left = 0; + int right = height.length-1; + int answer = 0; + + int waterWidth = 0; + int waterHeight = 0; + + while (left < right){ + waterWidth = (right - left); + waterHeight = Math.min(height[left], height[right]); + answer = Math.max(answer, waterWidth * waterHeight); + if (height[left] <= height[right]) { + left ++; + }else{ + right --; + } + } + + return answer; + } +} + From 6092ac101d3d5153124e5f2718b79970959a08a2 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 13 Dec 2025 12:55:16 +0900 Subject: [PATCH 2/5] 3sum --- 3sum/chjung99.java | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 3sum/chjung99.java diff --git a/3sum/chjung99.java b/3sum/chjung99.java new file mode 100644 index 000000000..daa28d81b --- /dev/null +++ b/3sum/chjung99.java @@ -0,0 +1,48 @@ +// two pointer +// time: O(N^2) +// space: O(N) + +class Solution { + Set> set = new HashSet<>(); + public List> threeSum(int[] nums) { + Arrays.sort(nums); + + for (int i = 0; i < nums.length; i++){ + twoSum(nums, i); + } + + return new ArrayList<>(set); + } + public void twoSum(int[] nums, int targetIdx){ + int left = 0; + int right = nums.length - 1; + + while (left < right) { + if (left == targetIdx){ + left ++; + continue; + } + if (right == targetIdx){ + right--; + continue; + } + if (nums[left] + nums[right] == -nums[targetIdx]) { + if (nums[left] > nums[targetIdx]){ + set.add(List.of(nums[targetIdx], nums[left], nums[right])); + } + else if (nums[targetIdx] > nums[right]){ + set.add(List.of(nums[left],nums[right],nums[targetIdx])); + } else{ + set.add(List.of(nums[left], nums[targetIdx], nums[right])); + } + left++; + } else if (nums[left] + nums[right] < -nums[targetIdx]){ + left ++; + } else { + right --; + } + } + } +} + + From fd1b11bc92a66f968d60cd7b02ffa729703fee8d Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 13 Dec 2025 12:55:29 +0900 Subject: [PATCH 3/5] combination-sum --- combination-sum/chjung99.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 combination-sum/chjung99.java diff --git a/combination-sum/chjung99.java b/combination-sum/chjung99.java new file mode 100644 index 000000000..da80c2e5f --- /dev/null +++ b/combination-sum/chjung99.java @@ -0,0 +1,29 @@ +class Solution { + Set> combination = new HashSet<>(); + public List> combinationSum(int[] candidates, int target) { + Arrays.sort(candidates); + findCombination(candidates, target, 0, 0, new ArrayList<>()); + return new ArrayList<>(combination); + } + + public void findCombination(int[] candidates, int target, int curIdx, int curSum, List curList) { + if (curSum == target) { + combination.add(new ArrayList<>(curList)); + return; + } + + if (curSum > target) { + return; + } + + for (int i = curIdx; i < candidates.length; i++){ + + curList.add(candidates[i]); + findCombination(candidates, target, i, curSum + candidates[i], curList); + curList.remove(curList.size()-1); + } + } +} + + + From 683ad2c9bd413c75c0146c9eb68cc9cd391a91c5 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 13 Dec 2025 12:55:41 +0900 Subject: [PATCH 4/5] remove-nth-node-from-end-of-list --- .../chjung99.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 remove-nth-node-from-end-of-list/chjung99.java diff --git a/remove-nth-node-from-end-of-list/chjung99.java b/remove-nth-node-from-end-of-list/chjung99.java new file mode 100644 index 000000000..d020ea063 --- /dev/null +++ b/remove-nth-node-from-end-of-list/chjung99.java @@ -0,0 +1,48 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + int size = getSizeOfListNode(head); + + if (size - n - 1 >= 0) { + ListNode prev = moveNth(head, size - n - 1); + ListNode target = moveNth(head, size - n); + prev.next = target.next; + } else { + head = head.next; + } + + + return head; + } + public ListNode moveNth(ListNode head, int n){ + while (head != null){ + if (n == 0) break; + head = head.next; + n--; + } + return head; + } + public void removeNth(ListNode head, int n){ + int cnt = n; + + } + public int getSizeOfListNode(ListNode head){ + int size = 0; + while (head != null) { + size ++; + head = head.next; + } + return size; + } +} + + From 1f03fb74585b8794330ef1801940511bd925e53d Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 13 Dec 2025 12:55:56 +0900 Subject: [PATCH 5/5] search-in-rotated-sorted-array --- search-in-rotated-sorted-array/chjung99.java | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 search-in-rotated-sorted-array/chjung99.java diff --git a/search-in-rotated-sorted-array/chjung99.java b/search-in-rotated-sorted-array/chjung99.java new file mode 100644 index 000000000..a627df996 --- /dev/null +++ b/search-in-rotated-sorted-array/chjung99.java @@ -0,0 +1,49 @@ +class Solution { + public int search(int[] nums, int target) { + int leftSide = 0; + int rightSide = nums.length - 1; + int left = 0; + int right = nums.length; + int mid; + + if (nums.length == 1) { + if (nums[0] == target) return 0; + else return -1; + } + + while (left + 1 < right) { + mid = (int) (left + right) / 2; + + if (nums[mid]==target) { + break; + } + else if (nums[mid] < target){ // 값이 증가해야함 + if (nums[mid] >= nums[leftSide]){ // 왼쪽 그룹에 속하는 지 + left = mid; + } else { // 오른쪽 그룹에 속하는 지 + if (nums[rightSide] < target) { + right = mid; + } else { + left = mid; + } + } + } else{ // nums[mid] > target // 값이 감소해야함 + if (nums[mid] >= nums[leftSide]){ // 왼쪽 그룹에 속하는 지 + if (nums[leftSide] <= target) { + right = mid; + } else { + left = mid; + } + } else { // 오른쪽 그룹에 속하는 지 + right = mid; + } + + } + } + mid = (int)((left + right) / 2); + if (nums[mid]==target) return mid; + else return -1; + } +} + +