From e0a19223f26668196c8629b0fadb8c8bbf72b5ec Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Tue, 1 Apr 2025 10:38:12 +0900 Subject: [PATCH 1/3] add: two-sum --- two-sum/YoungSeok-Choi.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 two-sum/YoungSeok-Choi.java diff --git a/two-sum/YoungSeok-Choi.java b/two-sum/YoungSeok-Choi.java new file mode 100644 index 000000000..d0134b0ad --- /dev/null +++ b/two-sum/YoungSeok-Choi.java @@ -0,0 +1,20 @@ +// time complexity: O(n); + +// 특정 nums[i] 가 target이 되기위한 보수 (target - nums[i])를 candidate 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]; + } +} \ No newline at end of file From c7800fe4a5bb6cd03ba086a52dc425a293ff4e94 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Wed, 2 Apr 2025 14:03:00 +0900 Subject: [PATCH 2/3] add: week 1 problem solution --- contains-duplicate/YoungSeok-Choi.java | 16 ++++ house-robber/YoungSeok-Choi.java | 84 +++++++++++++++++++ .../YoungSeok-Choi.java | 35 ++++++++ .../YoungSeok-Choi.java | 47 +++++++++++ 4 files changed, 182 insertions(+) create mode 100644 contains-duplicate/YoungSeok-Choi.java create mode 100644 house-robber/YoungSeok-Choi.java create mode 100644 longest-consecutive-sequence/YoungSeok-Choi.java create mode 100644 product-of-array-except-self/YoungSeok-Choi.java diff --git a/contains-duplicate/YoungSeok-Choi.java b/contains-duplicate/YoungSeok-Choi.java new file mode 100644 index 000000000..26fb3ef05 --- /dev/null +++ b/contains-duplicate/YoungSeok-Choi.java @@ -0,0 +1,16 @@ +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; + } +} \ No newline at end of file diff --git a/house-robber/YoungSeok-Choi.java b/house-robber/YoungSeok-Choi.java new file mode 100644 index 000000000..4f7acbce8 --- /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]; + } +} \ No newline at end of file diff --git a/longest-consecutive-sequence/YoungSeok-Choi.java b/longest-consecutive-sequence/YoungSeok-Choi.java new file mode 100644 index 000000000..0b8a324e9 --- /dev/null +++ b/longest-consecutive-sequence/YoungSeok-Choi.java @@ -0,0 +1,35 @@ +import java.util.Arrays; +import java.util.*; + +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); + } +} \ No newline at end of file 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..02f042e81 --- /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; + } +} \ No newline at end of file From f38c7aff6b3752af33c8f7e078f1c457ccae8802 Mon Sep 17 00:00:00 2001 From: "Yongseok.choi" Date: Fri, 4 Apr 2025 08:11:42 +0900 Subject: [PATCH 3/3] fix: lint --- contains-duplicate/YoungSeok-Choi.java | 5 ++++- house-robber/YoungSeok-Choi.java | 2 +- longest-consecutive-sequence/YoungSeok-Choi.java | 3 +-- product-of-array-except-self/YoungSeok-Choi.java | 2 +- two-sum/YoungSeok-Choi.java | 7 +++++-- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/contains-duplicate/YoungSeok-Choi.java b/contains-duplicate/YoungSeok-Choi.java index 26fb3ef05..829ab78e2 100644 --- a/contains-duplicate/YoungSeok-Choi.java +++ b/contains-duplicate/YoungSeok-Choi.java @@ -1,3 +1,6 @@ +import java.util.HashMap; +import java.util.Map; + class Solution { // 시간복잡도 O(n) public boolean containsDuplicate(int[] nums) { @@ -13,4 +16,4 @@ public boolean containsDuplicate(int[] nums) { return false; } -} \ No newline at end of file +} diff --git a/house-robber/YoungSeok-Choi.java b/house-robber/YoungSeok-Choi.java index 4f7acbce8..ba78f1f01 100644 --- a/house-robber/YoungSeok-Choi.java +++ b/house-robber/YoungSeok-Choi.java @@ -81,4 +81,4 @@ public void dfs(int[] arr, int idx) { visit[idx] = false; curSum -= arr[idx]; } -} \ No newline at end of file +} diff --git a/longest-consecutive-sequence/YoungSeok-Choi.java b/longest-consecutive-sequence/YoungSeok-Choi.java index 0b8a324e9..129633de9 100644 --- a/longest-consecutive-sequence/YoungSeok-Choi.java +++ b/longest-consecutive-sequence/YoungSeok-Choi.java @@ -1,5 +1,4 @@ import java.util.Arrays; -import java.util.*; class Solution { public int longestConsecutive(int[] nums) { @@ -32,4 +31,4 @@ public int longestConsecutive(int[] nums) { return Math.max(maxSeq, curSeq); } -} \ No newline at end of file +} diff --git a/product-of-array-except-self/YoungSeok-Choi.java b/product-of-array-except-self/YoungSeok-Choi.java index 02f042e81..06886f67f 100644 --- a/product-of-array-except-self/YoungSeok-Choi.java +++ b/product-of-array-except-self/YoungSeok-Choi.java @@ -44,4 +44,4 @@ public int[] productExceptSelf(int[] nums) { return result; } -} \ No newline at end of file +} diff --git a/two-sum/YoungSeok-Choi.java b/two-sum/YoungSeok-Choi.java index d0134b0ad..09e8d136a 100644 --- a/two-sum/YoungSeok-Choi.java +++ b/two-sum/YoungSeok-Choi.java @@ -1,6 +1,9 @@ // 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<>(); @@ -17,4 +20,4 @@ public int[] twoSum(int[] nums, int target) { } return new int[0]; } -} \ No newline at end of file +}