Skip to content

Commit 6748722

Browse files
authored
Merge pull request #581 from jdalma/main
[정현준] 13주차
2 parents c8d452b + ef90d68 commit 6748722

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

house-robber/jdalma.kt

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import kotlin.math.max
6+
7+
class `house-robber` {
8+
9+
fun rob(nums: IntArray): Int {
10+
return usingDP(nums)
11+
}
12+
13+
/**
14+
* TC: O(n), SC: O(n)
15+
*/
16+
private fun usingDP(nums: IntArray): Int {
17+
val dp = IntArray(nums.size + 1).apply {
18+
this[0] = 0
19+
this[1] = nums[0]
20+
}
21+
for (index in 1 until nums.size) {
22+
dp[index + 1] = max(nums[index] + dp[index - 1], dp[index])
23+
}
24+
25+
return dp[nums.size]
26+
}
27+
28+
/**
29+
* TC: O(n), SC: O(n)
30+
*/
31+
private fun usingMemoization(nums: IntArray): Int {
32+
val memo = IntArray(nums.size) { -1 }
33+
fun recursive(index: Int): Int {
34+
return if (index > nums.size - 1) 0
35+
else if (memo[index] != -1) memo[index]
36+
else {
37+
memo[index] = max(nums[index] + recursive(index + 2), recursive(index + 1))
38+
memo[index]
39+
}
40+
}
41+
42+
return recursive(0)
43+
}
44+
45+
/**
46+
* 시간초과
47+
* TC: O(2^n), SC: O(n)
48+
*/
49+
private fun usingRecursive(nums:IntArray): Int {
50+
fun recursive(index: Int, depth: Int): Int {
51+
if (index > nums.size - 1) return 0
52+
println("${"-".repeat(depth)} : max($index + ${index + 2}, ${index + 1})")
53+
return max(nums[index] + recursive(index + 2, depth + 1), recursive(index + 1, depth + 1))
54+
}
55+
56+
return recursive(0, 0)
57+
}
58+
59+
@Test
60+
fun `인접하지 않은 원소를 선택하여 최대의 합을 반환한다`() {
61+
rob(intArrayOf(1,2,3,1)) shouldBe 4
62+
rob(intArrayOf(2,7,9,3,1)) shouldBe 12
63+
rob(intArrayOf(8,7,9,11,1)) shouldBe 19
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class `lowest-common-ancestor-of-a-binary-search-tree` {
9+
10+
/**
11+
* TC: O(log n), SC: O(1)
12+
*/
13+
fun lowestCommonAncestor(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode? {
14+
if (p == null || q == null) return null
15+
16+
var node = root
17+
val max = max(p.`val`, q.`val`)
18+
val min = min(p.`val`, q.`val`)
19+
20+
while (node != null) {
21+
node = if (node.`val` > max) {
22+
node.left
23+
} else if (node.`val` < min) {
24+
node.right
25+
} else {
26+
return node
27+
}
28+
}
29+
return null
30+
}
31+
32+
@Test
33+
fun `가장 낮은 값의 공통 조상을 반환한다`() {
34+
lowestCommonAncestor(
35+
TreeNode.of(6,2,8,0,4,7,9,null,null,3,5),
36+
TreeNode(2),
37+
TreeNode(8)
38+
)!!.`val` shouldBe 6
39+
}
40+
}

meeting-rooms/jdalma.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package leetcode;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.Comparator;
9+
import java.util.List;
10+
11+
public class jdalma {
12+
13+
public class Interval {
14+
public int start, end;
15+
16+
public Interval(int start, int end) {
17+
this.start = start;
18+
this.end = end;
19+
}
20+
}
21+
22+
public boolean canAttendMeetings(List<Interval> intervals) {
23+
return usingBruteForce(intervals);
24+
}
25+
26+
/**
27+
* TC: O(n^2), SC: O(1)
28+
*/
29+
private boolean usingBruteForce(List<Interval> intervals) {
30+
final int size = intervals.size();
31+
for (int i = 0; i < size; i++) {
32+
Interval A = intervals.get(i);
33+
for (int j = i + 1; j < size; j++) {
34+
Interval B = intervals.get(j);
35+
if (Math.min(A.end, B.end) > Math.max(A.start, B.start)) {
36+
return false;
37+
}
38+
}
39+
}
40+
return true;
41+
}
42+
43+
/**
44+
* TC: O(n log n), SC: O(1)
45+
*/
46+
private boolean usingSort(List<Interval> intervals) {
47+
intervals.sort(Comparator.comparingInt(i -> i.start));
48+
49+
for (int i = 1; i < intervals.size(); i++) {
50+
Interval first = intervals.get(i - 1);
51+
Interval second = intervals.get(i);
52+
53+
if (first.end > second.start) {
54+
return false;
55+
}
56+
}
57+
58+
return true;
59+
}
60+
61+
@Test
62+
@DisplayName("입력받은 간격들의 충돌 여부를 반환한다.")
63+
void name() {
64+
Assertions.assertThat(canAttendMeetings(new ArrayList<>() {
65+
{
66+
add(new Interval(0, 30));
67+
add(new Interval(5, 10));
68+
add(new Interval(15, 20));
69+
}
70+
})).isFalse();
71+
72+
Assertions.assertThat(canAttendMeetings(new ArrayList<>() {
73+
{
74+
add(new Interval(5, 8));
75+
add(new Interval(9, 10));
76+
}
77+
})).isTrue();
78+
}
79+
}

0 commit comments

Comments
 (0)