Skip to content

Commit 1ffbcc7

Browse files
authored
Merge pull request #566 from jdalma/main
[์ •ํ˜„์ค€] 12์ฃผ์ฐจ
2 parents 735a429 + cfa8bad commit 1ffbcc7

File tree

5 files changed

+297
-0
lines changed

5 files changed

+297
-0
lines changed

โ€Žmaximum-subarray/jdalma.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 `maximum-subarray` {
8+
9+
fun maxSubArray(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).apply {
18+
this[0] = nums[0]
19+
}
20+
21+
for (index in 1 until nums.size) {
22+
dp[index] = max(nums[index], nums[index] + dp[index - 1])
23+
}
24+
25+
return dp.max()
26+
}
27+
28+
/**
29+
* TC: O(n), SC: O(1)
30+
*/
31+
private fun usingKadane(nums: IntArray): Int {
32+
var (current, max) = nums[0] to nums[0]
33+
34+
for (index in 1 until nums.size) {
35+
current = max(nums[index], current + nums[index])
36+
max = max(max, current)
37+
}
38+
39+
return max
40+
}
41+
42+
@Test
43+
fun `์ •์ˆ˜ ๋ฐฐ์—ด์˜ ํ•˜์œ„ ๋ฐฐ์—ด ์ค‘ ๊ฐ€์žฅ ํฐ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค`() {
44+
maxSubArray(intArrayOf(-2,1,-3,4,-1,2,1,-5,4)) shouldBe 6
45+
maxSubArray(intArrayOf(1)) shouldBe 1
46+
maxSubArray(intArrayOf(5,4,-1,7,8)) shouldBe 23
47+
}
48+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import org.assertj.core.api.Assertions;
2+
import org.junit.jupiter.api.DisplayName;
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class CountConnectedComponents {
9+
10+
public int countComponents(int n, int[][] edges) {
11+
final int[] relation = new int[n];
12+
for (int i = 0; i < n ; i++) {
13+
relation[i] = i;
14+
}
15+
16+
int result = n;
17+
for (int[] edge : edges) {
18+
if (union(relation, edge) == 1) {
19+
result--;
20+
}
21+
}
22+
23+
return result;
24+
}
25+
26+
private int union(int[] relation, int[] edge) {
27+
int parent1 = find(relation, edge[0]);
28+
int parent2 = find(relation, edge[1]);
29+
30+
if (parent1 == parent2) {
31+
return 0;
32+
}
33+
34+
if (parent1 < parent2) {
35+
relation[parent2] = parent1;
36+
} else {
37+
relation[parent1] = parent2;
38+
}
39+
return 1;
40+
}
41+
42+
private int find(int[] relation, int node) {
43+
int result = node;
44+
while (relation[result] != result) {
45+
relation[result] = relation[relation[result]];
46+
result = relation[result];
47+
}
48+
return result;
49+
}
50+
51+
@Test
52+
@DisplayName("์ž…๋ ฅ๋ฐ›์€ ๋…ธ๋“œ์™€ ๊ฐ„์„ ์„ ํ†ตํ•ด ๊ทธ๋ž˜ํ”„์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค")
53+
void graphCount() {
54+
int actual = countComponents(3, new int[][]{ {0,1}, {0,2} });
55+
Assertions.assertThat(actual).isEqualTo(1);
56+
57+
int actual1 = countComponents(6, new int[][]{ {0,1}, {1,2}, {2,3}, {4,5} });
58+
Assertions.assertThat(actual1).isEqualTo(2);
59+
60+
int actual2 = countComponents(6, new int[][]{ {0,1}, {2,3}, {4,5}, {1,2}, {3,4} });
61+
Assertions.assertThat(actual2).isEqualTo(1);
62+
}
63+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
6+
class `remove-nth-node-from-end-of-list` {
7+
8+
fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
9+
if (head == null || (head.next == null && n == 1)) {
10+
return null
11+
}
12+
return usingTwoPointers(head, n)
13+
}
14+
15+
/**
16+
* TC: O(n), SC: O(n)
17+
*/
18+
private fun usingExtraList(head: ListNode, n: Int): ListNode {
19+
20+
fun toList(node: ListNode): List<ListNode> {
21+
val list = mutableListOf<ListNode>()
22+
var tmp: ListNode? = node
23+
while (tmp != null) {
24+
list.add(tmp)
25+
tmp = tmp.next
26+
}
27+
return list
28+
}
29+
30+
val list = toList(head)
31+
var root = head
32+
if (list.size == n) {
33+
root = list[1]
34+
} else {
35+
list[list.size - n - 1].next = list[list.size - n].next
36+
}
37+
return root
38+
}
39+
40+
/**
41+
* fast๋ฅผ n๋งŒํผ ๋จผ์ € ์ด๋™ ์‹œํ‚จ ํ›„ fast.next๊ฐ€ null์ผ ๋•Œ๊นŒ์ง€ fast์™€ slow๋ฅผ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™์‹œํ‚จ๋‹ค.
42+
* fast๋ฅผ n๋งŒํผ ์ด๋™์‹œํ‚ค๋ฉด ๊ฒฐ๊ตญ slow๋Š” n์˜ ์ด์ „ ๋…ธ๋“œ์— ๋„๋‹ฌํ•˜๊ฒŒ ๋˜๊ธฐ์— ํ•ด๋‹น slow ๋…ธ๋“œ์˜ next๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.
43+
* TC: O(n), SC: O(1)
44+
*/
45+
private fun usingTwoPointers(head: ListNode, n: Int): ListNode? {
46+
var fast: ListNode? = head
47+
var slow: ListNode? = head
48+
49+
repeat(n) {
50+
fast = fast?.next
51+
}
52+
if (fast == null) return head.next
53+
54+
while (fast?.next != null) {
55+
fast = fast?.next
56+
slow = slow?.next
57+
}
58+
slow?.next = slow?.next?.next
59+
return head
60+
}
61+
62+
@Test
63+
fun `๋งํฌ๋œ ๋ชฉ๋ก์˜ ํ—ค๋“œ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๋ชฉ๋ก์˜ ๋์—์„œ n๋ฒˆ์งธ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๊ทธ ํ—ค๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค`() {
64+
removeNthFromEnd(ListNode.of(1), 1).also {
65+
it shouldBe null
66+
}
67+
68+
removeNthFromEnd(ListNode.of(1, 2), 2).also {
69+
it shouldBe ListNode(2)
70+
}
71+
72+
removeNthFromEnd(ListNode.of(1, 2), 1)!!.also {
73+
it shouldBe ListNode(1)
74+
it.next shouldBe null
75+
}
76+
77+
removeNthFromEnd(ListNode.of(1, 2, 3, 4, 5), 2)!!.also {
78+
it.next!!.next!!.`val` shouldBe 3
79+
it.next!!.next!!.next!!.`val` shouldBe 5
80+
}
81+
}
82+
}

โ€Žsame-tree/jdalma.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
6+
class `same-tree` {
7+
8+
fun isSameTree(p: TreeNode?, q: TreeNode?): Boolean {
9+
return if (p == null && q == null) {
10+
true
11+
} else if (p == null || q == null || p.`val` != q.`val`) {
12+
false
13+
} else {
14+
isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
15+
}
16+
}
17+
18+
@Test
19+
fun `๋‘ ๊ฐœ์˜ ํŠธ๋ฆฌ์˜ ๋™๋“ฑ์„ฑ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค`() {
20+
isSameTree(
21+
TreeNode.of(1,1,2),
22+
TreeNode.of(1,1,2)
23+
) shouldBe true
24+
25+
isSameTree(
26+
TreeNode.of(1,1,2),
27+
TreeNode.of(1,1,2,3)
28+
) shouldBe false
29+
30+
isSameTree(
31+
TreeNode.of(1,1,2),
32+
TreeNode.of(1,1,3)
33+
) shouldBe false
34+
}
35+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import java.util.StringJoiner
6+
7+
/**
8+
* ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ํŠธ๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ์ˆœ์„œ ๊ธฐ์ค€์œผ๋กœ ์ง๋ ฌํ™”๋œ ๋ฌธ์ž์—ด์„ ๋ฐ˜๋Œ€๋กœ ํ’€์–ด๋‚ด๋Š” ๊ฒƒ
9+
* ์ง๋ ฌํ™”๋ฅผ DFS๋กœ ํ•ด๊ฒฐํ•˜๋ฉด ์—ญ์ง๋ ฌํ™”์‹œ ์„ ์ž…์„ ์ถœ ๋ฐฉ์‹์œผ๋กœ ํ•ด๊ฒฐํ•˜์—ฌ์•ผ ํ•˜๊ณ ,
10+
* ์ง๋ ฌํ™”๋ฅผ BFS๋กœ ํ•ด๊ฒฐํ•˜๋ฉด ์—ญ์ง๋ ฌํ™”์‹œ ํž™์˜ ์ธ๋ฑ์Šค ๊ทœ์น™์„ ์ด์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„ ๋“ฏ
11+
*/
12+
class `serialize-and-deserialize-binary-tree` {
13+
14+
private val empty = "X"
15+
private val delimiter = "|"
16+
17+
/**
18+
* DFS๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๋…ธ๋“œ์˜ ๊ฐ’์„ ๋ˆ„์ ํ•œ๋‹ค.
19+
* TC: O(n), SC: O(n)
20+
*/
21+
fun serialize(root: TreeNode?): String {
22+
if (root == null) return ""
23+
24+
val joiner = StringJoiner("|")
25+
nodeToString(root, joiner)
26+
return joiner.toString()
27+
}
28+
29+
private fun nodeToString(node: TreeNode?, joiner: StringJoiner) {
30+
if (node == null) {
31+
joiner.add(empty)
32+
} else {
33+
joiner.add(node.`val`.toString())
34+
nodeToString(node.left, joiner)
35+
nodeToString(node.right, joiner)
36+
}
37+
}
38+
39+
/**
40+
* ๊นŠ์ด ํƒ์ƒ‰์œผ๋กœ ๋ˆ„์ ๋œ ๋ฌธ์ž์—ด์„ ์„ ์ž…์„ ์ถœ๋กœ ๊บผ๋‚ด์–ด ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
41+
* TC: O(n), SC: O(n)
42+
*/
43+
fun deserialize(data: String): TreeNode? {
44+
if (data.isEmpty()) return null
45+
return stringToNode(ArrayDeque(data.split(delimiter)))
46+
}
47+
48+
private fun stringToNode(queue: ArrayDeque<String>): TreeNode? {
49+
val value = queue.removeFirst()
50+
return if (value == empty) {
51+
null
52+
} else {
53+
val node = TreeNode(value.toInt())
54+
node.left = stringToNode(queue)
55+
node.right = stringToNode(queue)
56+
node
57+
}
58+
}
59+
60+
@Test
61+
fun `ํŠธ๋ฆฌ ๋…ธ๋“œ๋ฅผ ์ง๋ ฌํ™”ํ•œ๋‹ค`() {
62+
serialize(TreeNode.of(1,2,3,null,null,4,5)) shouldBe "1|2|X|X|3|4|X|X|5|X|X"
63+
}
64+
65+
@Test
66+
fun `๋ฌธ์ž์—ด์„ ํŠธ๋ฆฌ ๋…ธ๋“œ๋กœ ์—ญ์ง๋ ฌํ™”ํ•œ๋‹ค`() {
67+
deserialize("1|2|X|X|3|4|X|X|5|X|X") shouldBe TreeNode.of(1,2,3,null,null,4,5)
68+
}
69+
}

0 commit comments

Comments
ย (0)