-
-
Notifications
You must be signed in to change notification settings - Fork 195
[YoungSeok-Choi] Week 2 Solutions #1232
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
4 commits
Select commit
Hold shift + click to select a range
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,65 @@ | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
// NOTE: 실행시간 1.5초.. | ||
// 시간 복잡도: O(n^2) | ||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
|
||
Arrays.sort(nums); | ||
Set<List<Integer>> result = new HashSet<>(); | ||
|
||
for(int i = 0; i < nums.length - 2; i++) { | ||
int startIdx = i + 1; | ||
int endIdx = nums.length - 1; | ||
while(startIdx < endIdx) { | ||
int sum = nums[i] + nums[startIdx] + nums[endIdx]; | ||
if(sum == 0) { | ||
result.add(Arrays.asList(nums[i], nums[startIdx], nums[endIdx])); | ||
} | ||
|
||
if(sum > 0) { | ||
endIdx--; | ||
} else { | ||
startIdx++; | ||
} | ||
} | ||
} | ||
|
||
return new ArrayList<>(result); | ||
} | ||
} | ||
|
||
|
||
|
||
// 309 / 314 testcases passed Time Limit Exceeded | ||
// NOTE: 시간복잡도 O(n^3) | ||
class WrongSolution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
Set<List<Integer>> res = new HashSet<>(); | ||
|
||
List<Integer> temp = new ArrayList<>(); | ||
|
||
temp.remove(3); | ||
|
||
for(int i = 0; i < nums.length; i++) { | ||
for(int j = 0; j < i; j++) { | ||
for(int k = 0; k < j; k++) { | ||
|
||
if((nums[i] + nums[j] + nums[k]) == 0) { | ||
List<Integer> solution = Arrays.asList(nums[i], nums[j], nums[k]); | ||
Collections.sort(solution); | ||
res.add(solution); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
return new ArrayList<>(res); | ||
} | ||
} |
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,21 @@ | ||
// NOTE: 백준 N과 M문제와 거의 동일한 문제 | ||
// memo(n) = memo(n-1)의 경우에 수에서 1칸을 올라가는 경우 + memo(n-2) 의 경우의 수에서 2 칸을 올라가는 경우의 점화식으로 표현됨. | ||
// 시간 복잡도: O(n) | ||
class Solution { | ||
public int climbStairs(int n) { | ||
int[] memo = new int[n + 1]; | ||
|
||
|
||
if(n < 3) { | ||
return n; | ||
} | ||
|
||
memo[1] = 1; | ||
memo[2] = 2; | ||
for(int i = 3; i < memo.length; i++) { | ||
memo[i] = memo[i - 1] + memo[i - 2]; | ||
} | ||
|
||
return memo[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,32 @@ | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
||
// O(nlogn) 시간복잡도. | ||
|
||
class Solution { | ||
public int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> freqMap = new HashMap<>(); | ||
int[] res = new int [k]; | ||
|
||
for(int i = 0; i < nums.length; i++) { | ||
if(freqMap.containsKey(nums[i])) { | ||
freqMap.put(nums[i], freqMap.get(nums[i]) + 1); | ||
} else { | ||
freqMap.put(nums[i], 1); | ||
} | ||
} | ||
|
||
List<Map.Entry<Integer, Integer>> entList = new ArrayList<>(freqMap.entrySet()); | ||
|
||
entList.sort((a, b) -> b.getValue().compareTo(a.getValue())); | ||
|
||
for(int i = 0; i < res.length; i++) { | ||
res[i] = entList.get(i).getKey(); | ||
} | ||
|
||
return res; | ||
} | ||
} |
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,56 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
|
||
// NOTE: anagram 자체를 정확하게 이해하지 못해서 몇 번 틀렸던 문제. | ||
// 두 문자열의 길이가 다르면 anagram이 아니다. | ||
// 같은 알파뱃이 같은 개수가 있는지 확인을 하는게 문제의 핵심. | ||
|
||
// NOTE: 시간복잡도: O(n+m) | ||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
Map<Character, Integer> sMap = new HashMap<>(); | ||
Map<Character, Integer> tMap = new HashMap<>(); | ||
|
||
int tLen = t.toCharArray().length; | ||
int sLen = s.toCharArray().length; | ||
|
||
if(tLen != sLen) { | ||
return false; | ||
} | ||
|
||
for(char c: s.toCharArray()) { | ||
if(sMap.containsKey(c)) { | ||
int cnt = sMap.get(c); | ||
sMap.put(c, cnt + 1); | ||
} else { | ||
sMap.put(c, 1); | ||
} | ||
} | ||
|
||
for(char c: t.toCharArray()) { | ||
if(tMap.containsKey(c)) { | ||
int cnt = tMap.get(c); | ||
tMap.put(c, cnt + 1); | ||
} else { | ||
tMap.put(c, 1); | ||
} | ||
} | ||
|
||
|
||
for(Character c: sMap.keySet()) { | ||
if(!tMap.containsKey(c)) { | ||
return false; | ||
} | ||
|
||
int sMapCnt = sMap.get(c); | ||
int tMapCnt = tMap.get(c); | ||
|
||
if(sMapCnt != tMapCnt) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
YoungSeok-Choi marked this conversation as resolved.
Show resolved
Hide resolved
|
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,88 @@ | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
// NOTE: 답지를 보고 풀었던 문제.. 정확하게 이해하는 과정이 필요하다. | ||
// TODO: 별도 블로그로 풀이과정을 정리할 것. | ||
class Solution { | ||
public boolean isValidBST(TreeNode root) { | ||
return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE); | ||
} | ||
|
||
public boolean dfs(TreeNode cur, long min, long max) { | ||
if(cur == null) return true; | ||
|
||
if(cur.val <= min || cur.val >= max) return false; | ||
|
||
return dfs(cur.left, min, cur.val) && dfs(cur.right, cur.val, max); | ||
} | ||
} | ||
|
||
class WrongSolution { | ||
public boolean isValidBST(TreeNode root) { | ||
return dfs(root, new ArrayList<>(Arrays.asList(root.val))); | ||
} | ||
|
||
public boolean dfs(TreeNode cur, List<Integer> path) { | ||
|
||
if(cur.left != null) { | ||
int lVal = cur.left.val; | ||
for(int i = 0; i < path.size(); i++) { | ||
if(i == path.size() - 1) { | ||
|
||
if(lVal >= path.get(i)) { | ||
return false; | ||
} | ||
|
||
} else { | ||
if(lVal < path.get(i)) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
path.add(cur.left.val); | ||
if(!dfs(cur.left, path)) return false; | ||
path.remove(Integer.valueOf(cur.left.val)); | ||
} | ||
|
||
if(cur.right != null) { | ||
int rVal = cur.right.val; | ||
for(int i = 0; i < path.size(); i++) { | ||
if(i == path.size() - 1) { | ||
|
||
if(rVal <= path.get(i)) { | ||
return false; | ||
} | ||
|
||
|
||
} else { | ||
if(rVal > path.get(i)) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
path.add(cur.right.val); | ||
if(!dfs(cur.right, path)) return false; | ||
path.remove(Integer.valueOf(cur.right.val)); | ||
} | ||
|
||
return true; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.