-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #583 from jaejeong1/main
[jaejeong1] Week 13 Solutions
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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; | ||
} | ||
} |