Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions 3sum/chjung99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// two pointer
// time: O(N^2)
// space: O(N)

class Solution {
Set<List<Integer>> set = new HashSet<>();
public List<List<Integer>> 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 --;
}
}
}
}


29 changes: 29 additions & 0 deletions combination-sum/chjung99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
Set<List<Integer>> combination = new HashSet<>();
public List<List<Integer>> 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<Integer> 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);
}
}
}



27 changes: 27 additions & 0 deletions container-with-most-water/chjung99.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

48 changes: 48 additions & 0 deletions remove-nth-node-from-end-of-list/chjung99.java
Original file line number Diff line number Diff line change
@@ -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;
}
}


49 changes: 49 additions & 0 deletions search-in-rotated-sorted-array/chjung99.java
Original file line number Diff line number Diff line change
@@ -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;
}
}