From 2cfdca97068f9cfecbb917cc098a8282fae708d7 Mon Sep 17 00:00:00 2001 From: jeongdalma Date: Wed, 6 Nov 2024 13:45:50 +0900 Subject: [PATCH 1/3] =?UTF-8?q?13=EC=A3=BC=EC=B0=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jdalma.kt | 40 ++++++++++ meeting-rooms/MeetingRooms.java | 76 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/jdalma.kt create mode 100644 meeting-rooms/MeetingRooms.java 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/MeetingRooms.java b/meeting-rooms/MeetingRooms.java new file mode 100644 index 000000000..00539a2c5 --- /dev/null +++ b/meeting-rooms/MeetingRooms.java @@ -0,0 +1,76 @@ +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 MeetingRooms { + + 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(); + } +} From 0dc2794c1a89b1db142fc861843ef1b8f1b56173 Mon Sep 17 00:00:00 2001 From: jeongdalma Date: Wed, 6 Nov 2024 13:47:56 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{MeetingRooms.java => jdalma.java} | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) rename meeting-rooms/{MeetingRooms.java => jdalma.java} (80%) diff --git a/meeting-rooms/MeetingRooms.java b/meeting-rooms/jdalma.java similarity index 80% rename from meeting-rooms/MeetingRooms.java rename to meeting-rooms/jdalma.java index 00539a2c5..1c61555f9 100644 --- a/meeting-rooms/MeetingRooms.java +++ b/meeting-rooms/jdalma.java @@ -8,10 +8,11 @@ import java.util.Comparator; import java.util.List; -public class MeetingRooms { +public class jdalma { public class Interval { public int start, end; + public Interval(int start, int end) { this.start = start; this.end = end; @@ -60,17 +61,19 @@ private boolean usingSort(List intervals) { @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(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(); + Assertions.assertThat(canAttendMeetings(new ArrayList<>() { + { + add(new Interval(5, 8)); + add(new Interval(9, 10)); + } + })).isTrue(); } } From ef90d68e39fcf4545803bdbdbfdfcc1e83c65219 Mon Sep 17 00:00:00 2001 From: jeongdalma Date: Fri, 8 Nov 2024 14:52:09 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- house-robber/jdalma.kt | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 house-robber/jdalma.kt 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 + } +}