Skip to content

Commit 5da469b

Browse files
authored
Merge pull request #475 from jdalma/main
[์ •ํ˜„์ค€] 6์ฃผ์ฐจ ๋‹ต์•ˆ ์ œ์ถœ
2 parents c64a6d9 + 8e4a8b1 commit 5da469b

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed
Lines changed: 40 additions & 0 deletions
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.abs
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
typealias Element = Pair<Int,Int>
10+
11+
class `container-with-most-wate` {
12+
13+
/**
14+
* ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋†’์ด๊ฐ€ ๋‚ฎ์€ ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ํ•œ ์นธ์”ฉ ์ด๋™ํ•˜๋ฉฐ ์ตœ๋Œ€ ๋†’์ด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
15+
* TC: O(n), SC: O(1)
16+
*/
17+
fun maxArea(height: IntArray): Int {
18+
var (left, right) = 0 to height.size - 1
19+
var result = 0
20+
21+
while (left < right) {
22+
result = max(result, extent(Element(height[left], left), Element(height[right], right)))
23+
if (height[left] < height[right]) {
24+
left++
25+
} else {
26+
right--
27+
}
28+
}
29+
return result
30+
}
31+
32+
private fun extent(e1: Element, e2: Element): Int {
33+
return min(e1.first, e2.first) * abs(e1.second - e2.second)
34+
}
35+
36+
@Test
37+
fun `์ž…๋ ฅ๋ฐ›์€ ๋†’์ด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด์—์„œ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€์˜ ๋ฌผ์˜ ์–‘์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค`() {
38+
maxArea(intArrayOf(1,8,6,2,5,4,8,3,7)) shouldBe 49
39+
}
40+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import leetcode_study.Type.*
5+
import org.junit.jupiter.api.Test
6+
7+
class `design-add-and-search-words-data-structure` {
8+
9+
class Node {
10+
var isEnd: Boolean = false
11+
val next: MutableMap<Char, Node> = mutableMapOf()
12+
}
13+
14+
class WordDictionary {
15+
private val root = Node()
16+
17+
/**
18+
* TC: O(n), SC: O(n)
19+
*/
20+
fun addWord(word: String) {
21+
var now = root
22+
23+
for (index in word.indices) {
24+
val node =
25+
if (now.next.containsKey(word[index])) { now.next[word[index]] }
26+
else Node().also { now.next[word[index]] = it }
27+
node?.let { now = it }
28+
}
29+
now.isEnd = true
30+
}
31+
32+
/**
33+
* TC: O(26^n), SC: O(n)
34+
*/
35+
fun search(word: String): Boolean {
36+
37+
fun dfs(node: Node, word: String, index: Int): Boolean {
38+
return if (index == word.length) node.isEnd
39+
else if (word[index] == '.') {
40+
node.next.values.any { dfs(it, word, index + 1) }
41+
}
42+
else {
43+
node.next[word[index]]?.let { dfs(it, word, index + 1) } ?: false
44+
}
45+
}
46+
47+
return dfs(this.root, word, 0)
48+
}
49+
}
50+
51+
@Test
52+
fun `๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ`() {
53+
inputCommands(
54+
Command(ADD, "bad"),
55+
Command(ADD, "dad"),
56+
Command(ADD, "mad"),
57+
Command(SEARCH, "pad", false),
58+
Command(SEARCH, "bad", true),
59+
Command(SEARCH, ".ad", true),
60+
Command(SEARCH, "b..", true),
61+
)
62+
inputCommands(
63+
Command(ADD, "at"),
64+
Command(ADD, "and"),
65+
Command(ADD, "an"),
66+
Command(ADD, "add"),
67+
Command(SEARCH, "a", false),
68+
Command(SEARCH, ".at", false),
69+
Command(ADD, "bat"),
70+
Command(SEARCH,".at", true),
71+
Command(SEARCH,"an.", true),
72+
Command(SEARCH,"a.d.", false),
73+
Command(SEARCH,"b.", false),
74+
Command(SEARCH,"a.d", true),
75+
Command(SEARCH,".", false)
76+
)
77+
}
78+
79+
private fun inputCommands(vararg commands: Command) {
80+
val dictionary = WordDictionary()
81+
for (command in commands) {
82+
when(command.type) {
83+
ADD -> dictionary.addWord(command.word)
84+
SEARCH -> dictionary.search(command.word) shouldBe command.expect
85+
}
86+
}
87+
}
88+
}
89+
90+
data class Command(
91+
val type: Type,
92+
val word: String,
93+
val expect : Boolean? = null
94+
)
95+
96+
enum class Type {
97+
ADD, SEARCH;
98+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
6+
class `longest-increasing-subsequence` {
7+
8+
fun lengthOfLIS(nums: IntArray): Int {
9+
return usingBinarySearch(nums)
10+
}
11+
12+
/**
13+
* ์ด์ „ ์ตœ์žฅ ์ˆ˜์—ด ๊ธธ์ด๋ฅผ ์žฌ์‚ฌ์šฉํ•œ๋‹ค.
14+
* TC: O(n^2), SC: O(n)
15+
*/
16+
private fun usingDP(nums: IntArray): Int {
17+
val dp = IntArray(nums.size) { 1 }
18+
19+
for (i in 1 until nums.size) {
20+
for (j in 0 until i) {
21+
if (nums[i] > nums[j] && dp[i] < dp[j] + 1) {
22+
dp[i] = dp[j] + 1
23+
}
24+
}
25+
}
26+
27+
return dp.max()
28+
}
29+
30+
/**
31+
* ์ตœ์žฅ ์ˆ˜์—ด์„ ์ง์ ‘ ๊ฐฑ์‹ ํ•œ๋‹ค.
32+
* TC: O(n * log n), SC: O(n)
33+
*/
34+
private fun usingBinarySearch(nums: IntArray): Int {
35+
36+
fun binarySearch(list: List<Int>, number: Int): Int {
37+
var (low, high) = 0 to list.size - 1
38+
39+
while (low <= high) {
40+
val mid = (low + high) / 2
41+
if (list[mid] == number) {
42+
return mid
43+
} else if (list[mid] < number) {
44+
low = mid + 1
45+
} else {
46+
high = mid - 1
47+
}
48+
}
49+
return low
50+
}
51+
52+
val result = mutableListOf<Int>()
53+
54+
for (num in nums) {
55+
if (result.isEmpty() || result.last() < num) {
56+
result.add(num)
57+
} else {
58+
result[binarySearch(result, num)] = num
59+
}
60+
}
61+
62+
return result.size
63+
}
64+
65+
@Test
66+
fun name() {
67+
// lengthOfLIS(intArrayOf(10,9,2,5,3,7,101,18)) shouldBe 4
68+
// lengthOfLIS(intArrayOf(0,1,0,3,2,3)) shouldBe 4
69+
// lengthOfLIS(intArrayOf(0,1,2,3,4,5,6,7)) shouldBe 8
70+
lengthOfLIS(intArrayOf(4,10,4,3,8,9)) shouldBe 3
71+
}
72+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import java.util.Deque
6+
import java.util.ArrayDeque
7+
8+
class `valid-parentheses` {
9+
10+
/**
11+
* ๊ด„ํ˜ธ์˜ ์Œ์„ Stack์„ ํ™œ์šฉํ•˜์—ฌ ๊ฒ€์ฆํ•œ๋‹ค.
12+
* TC: O(n), SC: O(n)
13+
*/
14+
fun isValid(s: String): Boolean {
15+
if (s.length % 2 != 0) return false
16+
val parentheses = mapOf(
17+
'(' to ')',
18+
'{' to '}',
19+
'[' to ']'
20+
)
21+
22+
val stack: Deque<Char> = ArrayDeque()
23+
for (char in s) {
24+
if (parentheses.containsKey(char)) {
25+
stack.push(char)
26+
} else if (stack.isEmpty() || parentheses[stack.pop()] != char){
27+
return false
28+
}
29+
}
30+
return stack.isEmpty()
31+
}
32+
33+
@Test
34+
fun `์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด์˜ ๊ด„ํ˜ธ์˜ ์—ด๋ฆผ๊ณผ ๋‹ซํž˜์„ ๊ฒ€์ฆํ•œ๋‹ค`() {
35+
isValid("()") shouldBe true
36+
isValid("{()}") shouldBe true
37+
isValid("(){}[]") shouldBe true
38+
39+
isValid("{(}") shouldBe false
40+
isValid("){") shouldBe false
41+
}
42+
}

0 commit comments

Comments
ย (0)