diff --git a/container-with-most-water/Geegong.java b/container-with-most-water/Geegong.java new file mode 100644 index 0000000000..d462873902 --- /dev/null +++ b/container-with-most-water/Geegong.java @@ -0,0 +1,84 @@ +public class Geegong { + + /** + * two pointer 를 사용, 처음에는 height 배열을 정렬해서 높은 순오르 최대 면적을 구하는건가 싶었지만 그럴 필요는 없었음 + * (어차피 모든 원소를 돌아야되기 때문에 소팅의 의미가 없음, 그리고 NLogN 시간 복잡도가 생겨서 더 좋지 않음) + * time complexity : O(N) + * space complexity : O(N) + * + * @param height + * @return + */ + public int maxArea(int[] height) { + + + int leftIdx=0; + int rightIdx = height.length - 1; + int maxVolume = 0; + + while(leftIdx < rightIdx) { + + int leftHeight = height[leftIdx]; + int rightHeight = height[rightIdx]; + int gap = rightIdx - leftIdx; + int currentVolume = gap * Math.min(leftHeight, rightHeight); + maxVolume = Math.max(currentVolume, maxVolume); + + if (leftHeight > rightHeight) { + rightIdx--; + } else { + leftIdx++; + } + + } + + return maxVolume; + + + + + + + + +////////// 아래 부분은 예전 기수에서 풀었던 방법 +// int leftIndex = 0; +// int rightIndex = height.length - 1; +// +// int maxAmount = 0; +// int currentAmount = 0; +// +// while(leftIndex != rightIndex && leftIndex < rightIndex) { +// // 면적을 먼저 구해본다. +// int minHeight = Math.min(height[leftIndex], height[rightIndex]); +// currentAmount = minHeight * (rightIndex - leftIndex); +// +// maxAmount = Math.max(currentAmount, maxAmount); +// // 어느 포인터를 움직일지 결정 +// /** +// * case 1. 단순히 전체를 모두 훑어버리면 Time limit exceeded 발생 +// */ +//// if (leftIndex < rightIndex - 1) { +//// rightIndex--; +//// } else if (leftIndex == rightIndex - 1) { +//// rightIndex = height.length - 1; +//// leftIndex++; +//// } +// +// /** +// * case 2. 포인터가 돌면서 높은 height만 고려해서 포인터가 움직일 때에는 Time limit exceeded 발생 X +// */ +// if (height[leftIndex] < height[rightIndex]) { +// leftIndex++; +// } else if (height[leftIndex] > height[rightIndex]) { +// rightIndex--; +// } else { +// rightIndex--; +// leftIndex++; +// } +// } +// +// return maxAmount; + } +} + diff --git a/jump-game/Geegong.java b/jump-game/Geegong.java new file mode 100644 index 0000000000..17c81ae263 --- /dev/null +++ b/jump-game/Geegong.java @@ -0,0 +1,56 @@ +public class Geegong { + + /** + * greedy algorithms 사용 + * + * time complexity : O(N) + * space complexity : O(N) + * @param nums + * @return + */ + public boolean canJump(int[] nums) { + // greedy algorithm + int farthest = 0; + int lastIdx = nums.length - 1; + for (int idx=0; idx farthest) return false; + int currentFarthest = idx + nums[idx]; + farthest = Math.max(farthest, currentFarthest); + + if (farthest >= lastIdx) return true; + } + + return true; + } + + + // 아래 방법으로 풀면 TLE 발생 +// public boolean canJump(int[] nums) { +// return recursion(0, nums); +// } +// +// public boolean recursion(int idx, int[] nums) { +// int totalLength = nums.length; +// if (idx == totalLength - 1) { +// return true; +// } +// +// if (idx >= totalLength) { +// return false; +// } +// +// int currVal = nums[idx]; +// while(currVal > 0) { +// boolean result = recursion(idx + currVal, nums); +// if (result) { +// return true; +// } +// +// currVal--; +// } +// +// return false; +// } + +} diff --git a/merge-two-sorted-lists/Geegong.java b/merge-two-sorted-lists/Geegong.java new file mode 100644 index 0000000000..3c40ab6b8a --- /dev/null +++ b/merge-two-sorted-lists/Geegong.java @@ -0,0 +1,45 @@ +public class Geegong { + + /** + * 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; } + * } + */ + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + + ListNode root = new ListNode(Integer.MIN_VALUE, list1); + ListNode curr1 = list1; + ListNode curr2 = list2; + + while (curr1 != null && curr2 != null) { + int val1 = curr1.val; + int val2 = curr2.val; + + if (val1 <= val2) { + ListNode temp = curr1.next; + curr1.next = curr2; + curr1 = temp; + } else if (val1 > val2) { + ListNode temp = curr2.next; + curr2.next = curr1; + curr2 = temp; + } + } + + return root.next; + } + + public static class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } + } + +} diff --git a/sum-of-two-integers/Geegong.java b/sum-of-two-integers/Geegong.java new file mode 100644 index 0000000000..1cbddca0c3 --- /dev/null +++ b/sum-of-two-integers/Geegong.java @@ -0,0 +1,25 @@ +public class Geegong { + /** + * XOR, AND 연산을 이용하여 계산 + * XOR : carry 확인하여 1비트씩 왼쪽으로 이동 (<<) + * AND : carry 없이 더하기 결과값만 확인 + * time complexity : O(N) + * space complexity : O(N) + * @param a + * @param b + * @return + */ + public int getSum(int a, int b) { + int a_ = a; + int b_ = b; + + while (a_ != 0) { + int temp = a_; + a_ = (a_ & b_) << 1; + b_ = (temp ^ b_); + } + + return b_; + } + +}