diff --git a/contains-duplicate/YoungSeok-Choi.java b/contains-duplicate/YoungSeok-Choi.java new file mode 100644 index 000000000..829ab78e2 --- /dev/null +++ b/contains-duplicate/YoungSeok-Choi.java @@ -0,0 +1,19 @@ +import java.util.HashMap; +import java.util.Map; + +class Solution { + // 시간복잡도 O(n) + public boolean containsDuplicate(int[] nums) { + Map dupMap = new HashMap<>(); + + for(int i = 0; i < nums.length; i++) { + if(dupMap.containsKey(nums[i])) { + return true; + } + + dupMap.put(nums[i], true); + } + + return false; + } +} diff --git a/house-robber/YoungSeok-Choi.java b/house-robber/YoungSeok-Choi.java new file mode 100644 index 000000000..ba78f1f01 --- /dev/null +++ b/house-robber/YoungSeok-Choi.java @@ -0,0 +1,84 @@ +import java.util.HashMap; +import java.util.Map; + +// 시간복잡도: O(n) +// TODO: DP 방식으로 풀어보기 +class Solution { + public Map robMap = new HashMap<>(); + public int rob(int[] nums) { + return dfs(nums, 0); + } + + public int dfs(int[] nums, int index) { + if(nums.length == 0) { + return 0; + } + + if(index >= nums.length) { + return 0; + } + + // 이미 털었던 집이라면, 해 + if(robMap.containsKey(index)) { + return robMap.get(index); + } + + // 이번 집을 털게되는 경우 + int robThis = nums[index] + dfs(nums, index + 2); + + // 이번 집을 털지않고 건너뛰는 경우,. + int skipThis = dfs(nums, index + 1); + + robMap.put(index, Math.max(robThis, skipThis)); + + return robMap.get(index); + } +} + +// TODO: 비효율적으로 작성한 알고리즘의 동작 방식을 도식화 해서 그려보기. +// NOTE: dfs를 사용한 완전탐색 +// 탐색 방식이 매우 비효율적이라, 정답은 맞추지만 N이 커지면 시간초과 +// 시간복잡도: O(2^n) + alpha(중복탐색) +class WrongSolution { + public boolean[] visit; + public int mx = -987654321; + public int curSum = 0; + + public int rob(int[] nums) { + if(nums.length == 1) { + return nums[0]; + } + + visit = new boolean[nums.length]; + dfs(nums, 0); + dfs(nums, 1); + + return mx; + } + + public void dfs(int[] arr, int idx) { + int len = arr.length; + int prevIdx = idx - 1; + int nextIdx = idx + 1; + + + if(idx == 0) { + if(visit[idx]) return; + } else { + if(idx >= len || visit[idx] || visit[prevIdx]) { + return; + } + } + + visit[idx] = true; + curSum += arr[idx]; + mx = Math.max(mx, curSum); + + for(int i = idx; i < len; i++) { + dfs(arr, i); + } + + visit[idx] = false; + curSum -= arr[idx]; + } +} diff --git a/longest-consecutive-sequence/YoungSeok-Choi.java b/longest-consecutive-sequence/YoungSeok-Choi.java new file mode 100644 index 000000000..129633de9 --- /dev/null +++ b/longest-consecutive-sequence/YoungSeok-Choi.java @@ -0,0 +1,34 @@ +import java.util.Arrays; + +class Solution { + public int longestConsecutive(int[] nums) { + int curSeq = 1; + int maxSeq = -987654321; + + if(nums.length == 0) { + return 0; + } + + Arrays.sort(nums); + + int cur = nums[0]; + for(int i = 1; i < nums.length; i++) { + if(cur == nums[i]) { + continue; + } + + if(cur < nums[i] && Math.abs(nums[i] - cur) == 1) { + curSeq++; + cur = nums[i]; + continue; + } + + // NOTE: 수열의 조건이 깨졌을 때 + maxSeq = Math.max(maxSeq, curSeq); + curSeq = 1; + cur = nums[i]; + } + + return Math.max(maxSeq, curSeq); + } +} diff --git a/product-of-array-except-self/YoungSeok-Choi.java b/product-of-array-except-self/YoungSeok-Choi.java new file mode 100644 index 000000000..06886f67f --- /dev/null +++ b/product-of-array-except-self/YoungSeok-Choi.java @@ -0,0 +1,47 @@ +class Solution { + // 시간복잡도: O(3n) -> O(n) + public int[] productExceptSelf(int[] nums) { + + int zeroCount = 0; + int[] result = new int[nums.length]; + int productExceptZero = 1; + int zeroIdx = 0; + + for(int i = 0; i < nums.length; i++) { + if(nums[i] == 0) { + zeroCount++; + } + } + + // NOTE: 0이 두개 이상일 때, 모든 배열의 원소가 0; + if(zeroCount >= 2) { + return result; + } + + // NOTE: 0이 1개일 때, 0인 index만을 제외하고 모두 곱 + if(zeroCount == 1) { + for(int i = 0; i < nums.length; i++) { + if(nums[i] == 0) { + zeroIdx = i; + continue; + } + productExceptZero *= nums[i]; + } + + result[zeroIdx] = productExceptZero; + return result; + } + + // NOTE: 0이 없을 때 모든수를 곱한 뒤 idx를 나누기. + for(int i = 0; i < nums.length; i++) { + productExceptZero *= nums[i]; + } + + for(int i = 0; i < nums.length; i++) { + int copy = productExceptZero; + result[i] = copy / nums[i]; + } + + return result; + } +} diff --git a/two-sum/YoungSeok-Choi.java b/two-sum/YoungSeok-Choi.java new file mode 100644 index 000000000..09e8d136a --- /dev/null +++ b/two-sum/YoungSeok-Choi.java @@ -0,0 +1,23 @@ +// time complexity: O(n); +// 특정 nums[i] 가 target이 되기위한 보수 (target - nums[i])를 candidate Map에서 찾으면 종료 + +import java.util.HashMap; +import java.util.Map; + +class Solution { + public int[] twoSum(int[] nums, int target) { + Map candidate = new HashMap<>(); + + for(int i = 0; i < nums.length; i++) { + + int diff = target - nums[i]; + + if(candidate.containsKey(diff)) { + return new int[] { candidate.get(diff), i }; + } + + candidate.put(nums[i], i); + } + return new int[0]; + } +}