-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jaejeong1] Week 13 Solutions #583
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,20 @@ | ||
class Solution { | ||
public int rob(int[] nums) { | ||
// 인접한 경우는 제외한 최대 합 | ||
// 풀이: dp로 풀이한다. dp[i] = max(dp[i-1], dp[i-2] + nums[i]) | ||
// TC: O(N), SC: O(N) | ||
if (nums.length == 1) { | ||
return nums[0]; | ||
} | ||
|
||
var dp = new int[nums.length]; | ||
dp[0] = nums[0]; | ||
dp[1] = Math.max(nums[0], nums[1]); | ||
|
||
for (int i=2; i<nums.length; i++) { | ||
dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i]); | ||
} | ||
|
||
return dp[nums.length-1]; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
lowest-common-ancestor-of-a-binary-search-tree/jaejeong1.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,33 @@ | ||
|
||
// Definition for a binary tree node. | ||
class TreeNode { | ||
int val; | ||
TreeNode left; | ||
TreeNode right; | ||
|
||
TreeNode(int x) { | ||
val = x; | ||
} | ||
} | ||
|
||
|
||
class Solution { | ||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | ||
// 플이: p,q 둘다 root 보다 값이 적으면 왼쪽 서브트리를, 둘다 크면 오른쪽 서브트리를 탐색해야한다. | ||
// 그렇지 않으면 해당 root가 최저 공통 조상이 된다. | ||
// TC: O(H), H: 트리의 높이 | ||
// SC: O(1) | ||
var node = root; | ||
while (node != null) { | ||
if (p.val < node.val && q.val < node.val) { | ||
node = node.left; | ||
} else if (p.val > node.val && q.val > node.val) { | ||
node = node.right; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return node; | ||
} | ||
} |
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.List; | ||
|
||
// Definition of Interval: | ||
class Interval { | ||
int start, end; | ||
Interval(int start, int end) { | ||
this.start = start; | ||
this.end = end; | ||
} | ||
} | ||
|
||
|
||
public class Solution { | ||
/** | ||
* @param intervals: an array of meeting time intervals | ||
* @return: if a person could attend all meetings | ||
*/ | ||
public boolean canAttendMeetings(List<Interval> intervals) { | ||
// 풀이: 정렬 후 양옆을 비교해가며 조건에 맞지 않으면 false 를 반환한다. | ||
// TC: O(N) | ||
// SC: O(1) | ||
|
||
var sortedIntervals = intervals.stream().sorted().toList(); | ||
|
||
for (int i=0; i<sortedIntervals.size()-1; i++) { | ||
if (sortedIntervals.get(i).end > sortedIntervals.get(i+1).start) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
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,27 @@ | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
|
||
class Solution { | ||
public int eraseOverlapIntervals(int[][] intervals) { | ||
// 풀이: 정렬 후 각 구간의 끝 값을 저장해가며 양 옆을 비교, 제거 대상일때 카운트를 증가시켜 반환한다. | ||
// TC: O(N) | ||
// SC: O(1) | ||
|
||
// intervals를 각 구간의 끝 값을 기준으로 정렬 | ||
Arrays.sort(intervals, Comparator.comparingInt(a -> a[1])); | ||
|
||
int answer = 0; | ||
int end = intervals[0][1]; // 첫 번째 구간의 끝 값 | ||
|
||
// 두 번째 구간부터 순회하며 겹치는지 확인 | ||
for (int i = 1; i < intervals.length; i++) { | ||
if (intervals[i][0] < end) { // 현재 구간이 이전 구간과 겹치면 | ||
answer++; // 제거 횟수를 증가 | ||
} else { | ||
end = intervals[i][1]; // 겹치지 않으면 현재 구간의 끝 값을 갱신 | ||
} | ||
} | ||
|
||
return answer; | ||
} | ||
} |
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.