-
-
Notifications
You must be signed in to change notification settings - Fork 195
[bky373] 10th week solutions #166
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
5 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,42 @@ | ||
/* | ||
time: O(n + m), where n is the number of nodes and m is the number of edges in the graph. | ||
space: O(n + m) | ||
*/ | ||
class Solution { | ||
|
||
public boolean validTree(int n, int[][] edges) { | ||
|
||
if (edges.length != n - 1) { | ||
return false; | ||
} | ||
|
||
List<List<Integer>> adjList = new ArrayList<>(); | ||
for (int i = 0; i < n; i++) { | ||
adjList.add(new ArrayList<>()); | ||
} | ||
for (int[] edge : edges) { | ||
adjList.get(edge[0]) | ||
.add(edge[1]); | ||
adjList.get(edge[1]) | ||
.add(edge[0]); | ||
} | ||
|
||
Stack<Integer> stack = new Stack<>(); | ||
Set<Integer> visited = new HashSet<>(); | ||
stack.push(0); | ||
visited.add(0); | ||
|
||
while (!stack.isEmpty()) { | ||
int curr = stack.pop(); | ||
for (int adj : adjList.get(curr)) { | ||
if (visited.contains(adj)) { | ||
continue; | ||
} | ||
visited.add(adj); | ||
stack.push(adj); | ||
} | ||
} | ||
|
||
return visited.size() == 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,33 @@ | ||
/* | ||
time: O(N) | ||
time: O(1) | ||
*/ | ||
class Solution { | ||
|
||
public int rob(int[] nums) { | ||
if (nums.length == 0) { | ||
return 0; | ||
} | ||
if (nums.length == 1) { | ||
return nums[0]; | ||
} | ||
|
||
return Math.max( | ||
dp(nums, 0, nums.length - 2), | ||
dp(nums, 1, nums.length - 1) | ||
); | ||
} | ||
|
||
private static int dp(int[] nums, int start, int end) { | ||
int maxOfOneStepAhead = nums[end]; | ||
int maxOfTwoStepsAhead = 0; | ||
|
||
for (int i = end - 1; i >= start; --i) { | ||
int curr = Math.max(maxOfOneStepAhead, maxOfTwoStepsAhead + nums[i]); | ||
|
||
maxOfTwoStepsAhead = maxOfOneStepAhead; | ||
maxOfOneStepAhead = curr; | ||
} | ||
return maxOfOneStepAhead; | ||
} | ||
} |
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,23 @@ | ||
/* | ||
time: O(N) | ||
space: O(1) | ||
*/ | ||
class Solution { | ||
|
||
public int rob(int[] nums) { | ||
if (nums.length == 0) { | ||
return 0; | ||
} | ||
|
||
int maxOfTwoStepsAhead = 0; | ||
int maxOfOneStepAhead = nums[nums.length - 1]; | ||
|
||
for (int i = nums.length - 2; i >= 0; --i) { | ||
int curr = Math.max(maxOfOneStepAhead, maxOfTwoStepsAhead + nums[i]); | ||
|
||
maxOfTwoStepsAhead = maxOfOneStepAhead; | ||
maxOfOneStepAhead = curr; | ||
} | ||
return maxOfOneStepAhead; | ||
} | ||
} |
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,104 @@ | ||
class Solution { | ||
|
||
/* | ||
* Approach 1. | ||
* time: O(n^3) | ||
* space: O(1) | ||
*/ | ||
public String longestPalindrome1(String s) { | ||
String longest = ""; | ||
for (int i = 0; i < s.length(); i++) { | ||
for (int j = i; j < s.length(); j++) { | ||
String ss = s.substring(i, j + 1); | ||
if (isPalindrome(ss)) { | ||
if (ss.length() > longest.length()) { | ||
longest = ss; | ||
} | ||
} | ||
} | ||
} | ||
return longest; | ||
} | ||
|
||
// Approach 1. | ||
private boolean isPalindrome(String s) { | ||
int left = 0; | ||
int right = s.length() - 1; | ||
while (left < right) { | ||
if (s.charAt(left) != s.charAt(right)) { | ||
return false; | ||
} | ||
left++; | ||
right--; | ||
} | ||
return true; | ||
} | ||
|
||
/* | ||
* Approach 2-1. | ||
* time: 시간 복잡도는 평균적으로 O(n)이고, palindrome 의 길이가 n 에 가까워지면 시간 복잡도 역시 O(n^2) 에 가까워 진다. | ||
* space: O(1) | ||
*/ | ||
class Solution { | ||
|
||
public String longestPalindrome(String s) { | ||
int maxStart = 0; | ||
int maxEnd = 0; | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
int start = i; | ||
int end = i; | ||
while (0 <= start && end < s.length() && s.charAt(start) == s.charAt(end)) { | ||
if (maxEnd - maxStart < end - start) { | ||
maxStart = start; | ||
maxEnd = end; | ||
} | ||
start--; | ||
end++; | ||
} | ||
|
||
start = i; | ||
end = i + 1; | ||
while (0 <= start && end < s.length() && s.charAt(start) == s.charAt(end)) { | ||
if (maxEnd - maxStart < end - start) { | ||
maxStart = start; | ||
maxEnd = end; | ||
} | ||
start--; | ||
end++; | ||
} | ||
} | ||
return s.substring(maxStart, maxEnd + 1); | ||
} | ||
} | ||
|
||
/* | ||
* Approach 2-2. | ||
* time: 시간 복잡도는 평균적으로 O(n)이고, palindrome 의 길이가 n 에 가까워지면 시간 복잡도 역시 O(n^2) 에 가까워 진다. | ||
* space: O(1) | ||
*/ | ||
class Solution { | ||
Comment on lines
+75
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dev-jonghoonpark |
||
int maxStart = 0; | ||
int maxEnd = 0; | ||
|
||
public String longestPalindrome(String s) { | ||
for (int i = 0; i < s.length(); i++) { | ||
calculateMaxLength(i, i, s); | ||
calculateMaxLength(i, i+1, s); | ||
} | ||
return s.substring(maxStart, maxEnd + 1); | ||
} | ||
|
||
public void calculateMaxLength(int start, int end, String s){ | ||
while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) { | ||
if (this.maxEnd - this.maxStart < end - start) { | ||
this.maxStart = start; | ||
this.maxEnd = end; | ||
} | ||
start--; | ||
end++; | ||
} | ||
} | ||
|
||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
number-of-connected-components-in-an-undirected-graph/bky373.java
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,42 @@ | ||
/* | ||
time: O(n+m), where n is the number of nodes and m is the number of edges in the graph. | ||
space: O(n) | ||
*/ | ||
class Solution { | ||
|
||
public int countComponents(int n, int[][] edges) { | ||
boolean[] visited = new boolean[n]; | ||
|
||
Map<Integer, List<Integer>> map = new HashMap<>(); | ||
for (int[] edge : edges) { | ||
map.computeIfAbsent(edge[0], k -> new ArrayList<>()) | ||
.add(edge[1]); | ||
map.computeIfAbsent(edge[1], k -> new ArrayList<>()) | ||
.add(edge[0]); | ||
} | ||
|
||
int ans = 0; | ||
for (int i = 0; i < n; i++) { | ||
if (visited[i]) { | ||
continue; | ||
} | ||
dfs(i, map, visited); | ||
ans++; | ||
} | ||
return ans; | ||
} | ||
|
||
public void dfs(int k, Map<Integer, List<Integer>> map, boolean[] visited) { | ||
if (visited[k]) { | ||
return; | ||
} | ||
visited[k] = true; | ||
if (!map.containsKey(k)) { | ||
return; | ||
} | ||
List<Integer> values = map.get(k); | ||
for (int v : values) { | ||
dfs(v, map, visited); | ||
} | ||
} | ||
} |
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.
이 while 블록이 위에 있는 while 블록과 완전히 동일한것 같은데
맞다면 중복을 제거해보면 어떠실까요?
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.
네 맞습니다 동일한 블록입니다 중복을 제거해봐도 좋겠네요 감사합니다!