-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jinhyungrhee] WEEK 02 solutions #1220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b7f97fe
add solution of valid-anagram
jinhyungrhee dc58154
Merge remote-tracking branch 'origin/main'
jinhyungrhee 3483854
add a new line
jinhyungrhee d66407a
add solution of climbing-stairs
jinhyungrhee 429ae9a
add a new line
jinhyungrhee e0b83d2
add solution of product-of-array-except-self
jinhyungrhee d6e3de9
add solution of 3sum
jinhyungrhee 9f0c962
add solution of valid-binary-search-tree
jinhyungrhee 03c56f4
add solution of valid-binary-search-tree using iterative DFS
jinhyungrhee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import java.util.*; | ||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
|
||
/** | ||
runtime : 33ms | ||
memory : 51.15mb | ||
*/ | ||
|
||
// [idea] (1)정렬 (2)기준 인덱스를 하나 잡고 그 이후에 등장하는 수들에 대해서 two pointer 수행 | ||
// (중요) **연속된 수들의 중복**이 있는지 체크하는 로직 필요! | ||
// -> 정렬된 배열에서는 같은 숫자가 연속되어있으면 중복된 조합(=경우의 수)이 발생함 | ||
// -> 정렬된 배열의 앞 뒤 숫자들을 비교하며, 다음 수가 중복이 아닐때까지 넘기는 과정 필요 | ||
|
||
// [time-complexity] : O(N^2) | ||
// [space-complexity] : O(K)(k=결과 조합의 개수) | ||
|
||
// 1.sort | ||
List<List<Integer>> result = new ArrayList<>(); | ||
Arrays.sort(nums); | ||
|
||
for (int i = 0; i < nums.length - 2; i++) { // start 인덱스가 (i+1)이므로 lenght-2까지만 순회 | ||
|
||
// *중복 경우의 수 체크* | ||
if (i > 0 && nums[i] == nums[i-1]) continue; | ||
|
||
// 2.two pointer | ||
int start = i + 1; | ||
int end = nums.length - 1; | ||
|
||
while (start < end) { | ||
|
||
int sum = nums[i] + nums[start] + nums[end]; | ||
|
||
if (sum == 0) { | ||
|
||
result.add(List.of(nums[i], nums[start], nums[end])); | ||
|
||
// // --------------- *중복 경우의 수 체크* --------------- | ||
while (start < end && nums[start] == nums[start + 1]) { | ||
start++; | ||
} | ||
while (end > start && nums[end] == nums[end - 1]) { | ||
end--; | ||
} | ||
// ---------------------------------------------------- | ||
|
||
// 정답 찾았으므로(sum==0), 포인터 이동하여 다음 경우 탐색 | ||
start++; | ||
end--; | ||
|
||
} | ||
else if (sum < 0) { | ||
start++; | ||
} | ||
else if (sum > 0) { | ||
end--; | ||
} | ||
|
||
} | ||
} | ||
|
||
return result; | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import java.util.*; | ||
class Solution { | ||
public int climbStairs(int n) { | ||
|
||
// METHOD1 : recursive DFS | ||
// int resuslt = recursiveDFS(0, n); | ||
|
||
// METHOD2 : recursive DFS + memoization | ||
// int[] memo = new int[n + 1]; | ||
// Arrays.fill(memo, -1); | ||
// int result = memoizationDFS(0, n, memo); | ||
|
||
// METHOD3 : DP | ||
int[] memo = new int[n + 1]; | ||
Arrays.fill(memo, -1); | ||
int result = dp(n, memo); | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
runtime : 0ms | ||
memory : 40.04mb | ||
*/ | ||
|
||
// METHOD3 : DP (Bottom-Up) | ||
// time-complexity : O(N) | ||
// space-complexity : O(N) | ||
public int dp(int n, int[] memo) { | ||
if (n <= 2) return n; | ||
memo[1] = 1; | ||
memo[2] = 2; | ||
for (int i = 3; i < n + 1; i++) { | ||
memo[i] = memo[i - 1] + memo[i - 2]; | ||
} | ||
return memo[n]; | ||
} | ||
|
||
/** | ||
runtime : 0ms | ||
memory : 40.30mb | ||
*/ | ||
|
||
// METHOD2 : DFS + memoization (Top-Down) | ||
// time-complexity : O(N) -> 각 i에 대해 dfs(i)는 최대 한번만 호출됨 | ||
// space-complexity : O(N) | ||
public int memoizationDFS(int i, int n, int[] memo) { | ||
if (i > n) return 0; | ||
if (i == n) return 1; | ||
if (memo[i] != -1) return memo[i]; | ||
memo[i] = memoizationDFS(i + 1 , n, memo) + memoizationDFS(i + 2, n, memo); | ||
return memo[i]; | ||
} | ||
|
||
/** | ||
Time Limit Exceeded | ||
*/ | ||
|
||
// METHOD1 : recursive DFS => TIME-OUT 발생 | ||
// time-complexity : O(2^N) -> n이 커질수록 중복 호출 발생 | ||
// space-complexity : O(N) | ||
public int recursiveDFS(int i, int n) { | ||
if (i > n) return 0; | ||
if (i == n) return 1; | ||
return recursiveDFS(i + 1, n) + recursiveDFS(i + 2, n); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
class Solution { | ||
public int[] productExceptSelf(int[] nums) { | ||
|
||
/** | ||
runtime : 2ms | ||
memory : 55.44mb | ||
*/ | ||
|
||
// [idea 03] : extra space-complexity optimization | ||
// prefixProduct -> result 배열과 공유 | ||
// suffixProduct -> 변수로 대체 | ||
// [time-complexity] : O(N) | ||
// [space-complexity] : O(N)(=> extra space-complexity : O(1)) | ||
|
||
int[] result = new int[nums.length]; | ||
result[0] = 1; | ||
|
||
// 1. prefix product 계산하여 result 배열에 저장 | ||
for (int i = 1; i < result.length; i++) { | ||
result[i] = result[i - 1] * nums[i - 1]; | ||
} | ||
|
||
// 2. suffix product 계산하여 바로 result 배열에 반영 | ||
int suffix = 1; | ||
for (int i = nums.length - 1; i >= 0; i--) { | ||
result[i] = result[i] * suffix; | ||
suffix = nums[i] * suffix; | ||
/** | ||
(1) suffix = 1 | ||
(2) suffix = 4 | ||
(3) suffix = 12 | ||
(4) suffix = 24 | ||
*/ | ||
} | ||
|
||
return result; | ||
|
||
/** | ||
runtime : 2ms | ||
memory : 56.05mb | ||
*/ | ||
|
||
// [idea 02] : production of prefix product and suffix product | ||
// prefixProduct - 인덱스 기준, 자기 자신을 제외한 '이전 값들의 곱' 계산 및 저장 | ||
// suffixProduct - 인덱스 기준, 자기 자신을 제외한 '이후 값들의 곱' 계산 및 저장 | ||
// prefixProduct와 suffixProduct의 각 인덱스 값을 곱하면, 결국 자기 자신을 제외한 값들의 곱이 계산됨 | ||
// [time-complexity] : O(N) | ||
// [space-complexity] : O(N) | ||
|
||
int[] prefixProduct = new int[nums.length]; | ||
int[] suffixProduct = new int[nums.length]; | ||
int[] result = new int[nums.length]; | ||
|
||
prefixProduct[0] = 1; | ||
suffixProduct[suffixProduct.length - 1] = 1; | ||
|
||
for (int i = 1; i < prefixProduct.length; i++) { | ||
prefixProduct[i] = prefixProduct[i - 1] * nums[i - 1]; | ||
} | ||
|
||
for (int i = suffixProduct.length - 2; i >=0 ; i--) { | ||
suffixProduct[i] = suffixProduct[i + 1] * nums[i + 1]; | ||
} | ||
|
||
for (int i = 0; i < prefixProduct.length; i++) { | ||
result[i] = prefixProduct[i] * suffixProduct[i]; | ||
} | ||
|
||
return result; | ||
|
||
/** | ||
"Time Limit Exeeded" | ||
*/ | ||
|
||
// [idea 01] : Brute Force | ||
// [time complexity] : O(N^2) | ||
// [space complexity] : O(N) | ||
|
||
int[] result = new int[nums.length]; | ||
for (int i = 0; i < nums.length; i++) { | ||
int value = 1; | ||
for (int j = 0; j < nums.length; j++) { | ||
if (i == j) continue; | ||
value *= nums[j]; | ||
} | ||
result[i] = value; | ||
} | ||
return result; | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
알고 계실 수도 있으시겠지만, 동일한
Bottom-up
방식으로 O(1)의 공간 복잡도를 갖는 방식이 존재합니다 😃알고달레 참고 링크 남겨드립니다 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
참고해보겠습니다 감사합니다!