diff --git a/house-robber/jdalma.kt b/house-robber/jdalma.kt new file mode 100644 index 000000000..1d083c0f9 --- /dev/null +++ b/house-robber/jdalma.kt @@ -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 + } +} diff --git a/lowest-common-ancestor-of-a-binary-search-tree/jdalma.kt b/lowest-common-ancestor-of-a-binary-search-tree/jdalma.kt new file mode 100644 index 000000000..cc047c53d --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/jdalma.kt @@ -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 + } +} diff --git a/meeting-rooms/jdalma.java b/meeting-rooms/jdalma.java new file mode 100644 index 000000000..1c61555f9 --- /dev/null +++ b/meeting-rooms/jdalma.java @@ -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 intervals) { + return usingBruteForce(intervals); + } + + /** + * TC: O(n^2), SC: O(1) + */ + private boolean usingBruteForce(List 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 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(); + } +}