Skip to content

Commit

Permalink
Merge pull request #581 from jdalma/main
Browse files Browse the repository at this point in the history
[정현준] 13주차
  • Loading branch information
jdalma authored Nov 9, 2024
2 parents c8d452b + ef90d68 commit 6748722
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
65 changes: 65 additions & 0 deletions house-robber/jdalma.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package leetcode_study

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import kotlin.math.max

class `house-robber` {

fun rob(nums: IntArray): Int {
return usingDP(nums)
}

/**
* TC: O(n), SC: O(n)
*/
private fun usingDP(nums: IntArray): Int {
val dp = IntArray(nums.size + 1).apply {
this[0] = 0
this[1] = nums[0]
}
for (index in 1 until nums.size) {
dp[index + 1] = max(nums[index] + dp[index - 1], dp[index])
}

return dp[nums.size]
}

/**
* TC: O(n), SC: O(n)
*/
private fun usingMemoization(nums: IntArray): Int {
val memo = IntArray(nums.size) { -1 }
fun recursive(index: Int): Int {
return if (index > nums.size - 1) 0
else if (memo[index] != -1) memo[index]
else {
memo[index] = max(nums[index] + recursive(index + 2), recursive(index + 1))
memo[index]
}
}

return recursive(0)
}

/**
* 시간초과
* TC: O(2^n), SC: O(n)
*/
private fun usingRecursive(nums:IntArray): Int {
fun recursive(index: Int, depth: Int): Int {
if (index > nums.size - 1) return 0
println("${"-".repeat(depth)} : max($index + ${index + 2}, ${index + 1})")
return max(nums[index] + recursive(index + 2, depth + 1), recursive(index + 1, depth + 1))
}

return recursive(0, 0)
}

@Test
fun `인접하지 않은 원소를 선택하여 최대의 합을 반환한다`() {
rob(intArrayOf(1,2,3,1)) shouldBe 4
rob(intArrayOf(2,7,9,3,1)) shouldBe 12
rob(intArrayOf(8,7,9,11,1)) shouldBe 19
}
}
40 changes: 40 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/jdalma.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package leetcode_study

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import kotlin.math.max
import kotlin.math.min

class `lowest-common-ancestor-of-a-binary-search-tree` {

/**
* TC: O(log n), SC: O(1)
*/
fun lowestCommonAncestor(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode? {
if (p == null || q == null) return null

var node = root
val max = max(p.`val`, q.`val`)
val min = min(p.`val`, q.`val`)

while (node != null) {
node = if (node.`val` > max) {
node.left
} else if (node.`val` < min) {
node.right
} else {
return node
}
}
return null
}

@Test
fun `가장 낮은 값의 공통 조상을 반환한다`() {
lowestCommonAncestor(
TreeNode.of(6,2,8,0,4,7,9,null,null,3,5),
TreeNode(2),
TreeNode(8)
)!!.`val` shouldBe 6
}
}
79 changes: 79 additions & 0 deletions meeting-rooms/jdalma.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package leetcode;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class jdalma {

public class Interval {
public int start, end;

public Interval(int start, int end) {
this.start = start;
this.end = end;
}
}

public boolean canAttendMeetings(List<Interval> intervals) {
return usingBruteForce(intervals);
}

/**
* TC: O(n^2), SC: O(1)
*/
private boolean usingBruteForce(List<Interval> intervals) {
final int size = intervals.size();
for (int i = 0; i < size; i++) {
Interval A = intervals.get(i);
for (int j = i + 1; j < size; j++) {
Interval B = intervals.get(j);
if (Math.min(A.end, B.end) > Math.max(A.start, B.start)) {
return false;
}
}
}
return true;
}

/**
* TC: O(n log n), SC: O(1)
*/
private boolean usingSort(List<Interval> intervals) {
intervals.sort(Comparator.comparingInt(i -> i.start));

for (int i = 1; i < intervals.size(); i++) {
Interval first = intervals.get(i - 1);
Interval second = intervals.get(i);

if (first.end > second.start) {
return false;
}
}

return true;
}

@Test
@DisplayName("입력받은 간격들의 충돌 여부를 반환한다.")
void name() {
Assertions.assertThat(canAttendMeetings(new ArrayList<>() {
{
add(new Interval(0, 30));
add(new Interval(5, 10));
add(new Interval(15, 20));
}
})).isFalse();

Assertions.assertThat(canAttendMeetings(new ArrayList<>() {
{
add(new Interval(5, 8));
add(new Interval(9, 10));
}
})).isTrue();
}
}

0 comments on commit 6748722

Please sign in to comment.