-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #475 from jdalma/main
[์ ํ์ค] 6์ฃผ์ฐจ ๋ต์ ์ ์ถ
- Loading branch information
Showing
4 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.abs | ||
import kotlin.math.max | ||
import kotlin.math.min | ||
|
||
typealias Element = Pair<Int,Int> | ||
|
||
class `container-with-most-wate` { | ||
|
||
/** | ||
* ํฌ ํฌ์ธํฐ๋ฅผ ํ์ฉํ์ฌ ๋์ด๊ฐ ๋ฎ์ ์ธ๋ฑ์ค์ ๊ฐ์ ํ ์นธ์ฉ ์ด๋ํ๋ฉฐ ์ต๋ ๋์ด๋ฅผ ๋ฐํํ๋ค. | ||
* TC: O(n), SC: O(1) | ||
*/ | ||
fun maxArea(height: IntArray): Int { | ||
var (left, right) = 0 to height.size - 1 | ||
var result = 0 | ||
|
||
while (left < right) { | ||
result = max(result, extent(Element(height[left], left), Element(height[right], right))) | ||
if (height[left] < height[right]) { | ||
left++ | ||
} else { | ||
right-- | ||
} | ||
} | ||
return result | ||
} | ||
|
||
private fun extent(e1: Element, e2: Element): Int { | ||
return min(e1.first, e2.first) * abs(e1.second - e2.second) | ||
} | ||
|
||
@Test | ||
fun `์ ๋ ฅ๋ฐ์ ๋์ด๋ฅผ ํํํ๋ ์ ์ ๋ฐฐ์ด์์ ๋ฐ์ ์ ์๋ ์ต๋์ ๋ฌผ์ ์์ ๋ฐํํ๋ค`() { | ||
maxArea(intArrayOf(1,8,6,2,5,4,8,3,7)) shouldBe 49 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import leetcode_study.Type.* | ||
import org.junit.jupiter.api.Test | ||
|
||
class `design-add-and-search-words-data-structure` { | ||
|
||
class Node { | ||
var isEnd: Boolean = false | ||
val next: MutableMap<Char, Node> = mutableMapOf() | ||
} | ||
|
||
class WordDictionary { | ||
private val root = Node() | ||
|
||
/** | ||
* TC: O(n), SC: O(n) | ||
*/ | ||
fun addWord(word: String) { | ||
var now = root | ||
|
||
for (index in word.indices) { | ||
val node = | ||
if (now.next.containsKey(word[index])) { now.next[word[index]] } | ||
else Node().also { now.next[word[index]] = it } | ||
node?.let { now = it } | ||
} | ||
now.isEnd = true | ||
} | ||
|
||
/** | ||
* TC: O(26^n), SC: O(n) | ||
*/ | ||
fun search(word: String): Boolean { | ||
|
||
fun dfs(node: Node, word: String, index: Int): Boolean { | ||
return if (index == word.length) node.isEnd | ||
else if (word[index] == '.') { | ||
node.next.values.any { dfs(it, word, index + 1) } | ||
} | ||
else { | ||
node.next[word[index]]?.let { dfs(it, word, index + 1) } ?: false | ||
} | ||
} | ||
|
||
return dfs(this.root, word, 0) | ||
} | ||
} | ||
|
||
@Test | ||
fun `๋ฌธ์์ด์ ์ ์ฅํ๊ณ ๊ฒ์ํ๋ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๊ตฌํํ๋ผ`() { | ||
inputCommands( | ||
Command(ADD, "bad"), | ||
Command(ADD, "dad"), | ||
Command(ADD, "mad"), | ||
Command(SEARCH, "pad", false), | ||
Command(SEARCH, "bad", true), | ||
Command(SEARCH, ".ad", true), | ||
Command(SEARCH, "b..", true), | ||
) | ||
inputCommands( | ||
Command(ADD, "at"), | ||
Command(ADD, "and"), | ||
Command(ADD, "an"), | ||
Command(ADD, "add"), | ||
Command(SEARCH, "a", false), | ||
Command(SEARCH, ".at", false), | ||
Command(ADD, "bat"), | ||
Command(SEARCH,".at", true), | ||
Command(SEARCH,"an.", true), | ||
Command(SEARCH,"a.d.", false), | ||
Command(SEARCH,"b.", false), | ||
Command(SEARCH,"a.d", true), | ||
Command(SEARCH,".", false) | ||
) | ||
} | ||
|
||
private fun inputCommands(vararg commands: Command) { | ||
val dictionary = WordDictionary() | ||
for (command in commands) { | ||
when(command.type) { | ||
ADD -> dictionary.addWord(command.word) | ||
SEARCH -> dictionary.search(command.word) shouldBe command.expect | ||
} | ||
} | ||
} | ||
} | ||
|
||
data class Command( | ||
val type: Type, | ||
val word: String, | ||
val expect : Boolean? = null | ||
) | ||
|
||
enum class Type { | ||
ADD, SEARCH; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class `longest-increasing-subsequence` { | ||
|
||
fun lengthOfLIS(nums: IntArray): Int { | ||
return usingBinarySearch(nums) | ||
} | ||
|
||
/** | ||
* ์ด์ ์ต์ฅ ์์ด ๊ธธ์ด๋ฅผ ์ฌ์ฌ์ฉํ๋ค. | ||
* TC: O(n^2), SC: O(n) | ||
*/ | ||
private fun usingDP(nums: IntArray): Int { | ||
val dp = IntArray(nums.size) { 1 } | ||
|
||
for (i in 1 until nums.size) { | ||
for (j in 0 until i) { | ||
if (nums[i] > nums[j] && dp[i] < dp[j] + 1) { | ||
dp[i] = dp[j] + 1 | ||
} | ||
} | ||
} | ||
|
||
return dp.max() | ||
} | ||
|
||
/** | ||
* ์ต์ฅ ์์ด์ ์ง์ ๊ฐฑ์ ํ๋ค. | ||
* TC: O(n * log n), SC: O(n) | ||
*/ | ||
private fun usingBinarySearch(nums: IntArray): Int { | ||
|
||
fun binarySearch(list: List<Int>, number: Int): Int { | ||
var (low, high) = 0 to list.size - 1 | ||
|
||
while (low <= high) { | ||
val mid = (low + high) / 2 | ||
if (list[mid] == number) { | ||
return mid | ||
} else if (list[mid] < number) { | ||
low = mid + 1 | ||
} else { | ||
high = mid - 1 | ||
} | ||
} | ||
return low | ||
} | ||
|
||
val result = mutableListOf<Int>() | ||
|
||
for (num in nums) { | ||
if (result.isEmpty() || result.last() < num) { | ||
result.add(num) | ||
} else { | ||
result[binarySearch(result, num)] = num | ||
} | ||
} | ||
|
||
return result.size | ||
} | ||
|
||
@Test | ||
fun name() { | ||
// lengthOfLIS(intArrayOf(10,9,2,5,3,7,101,18)) shouldBe 4 | ||
// lengthOfLIS(intArrayOf(0,1,0,3,2,3)) shouldBe 4 | ||
// lengthOfLIS(intArrayOf(0,1,2,3,4,5,6,7)) shouldBe 8 | ||
lengthOfLIS(intArrayOf(4,10,4,3,8,9)) shouldBe 3 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
import java.util.Deque | ||
import java.util.ArrayDeque | ||
|
||
class `valid-parentheses` { | ||
|
||
/** | ||
* ๊ดํธ์ ์์ Stack์ ํ์ฉํ์ฌ ๊ฒ์ฆํ๋ค. | ||
* TC: O(n), SC: O(n) | ||
*/ | ||
fun isValid(s: String): Boolean { | ||
if (s.length % 2 != 0) return false | ||
val parentheses = mapOf( | ||
'(' to ')', | ||
'{' to '}', | ||
'[' to ']' | ||
) | ||
|
||
val stack: Deque<Char> = ArrayDeque() | ||
for (char in s) { | ||
if (parentheses.containsKey(char)) { | ||
stack.push(char) | ||
} else if (stack.isEmpty() || parentheses[stack.pop()] != char){ | ||
return false | ||
} | ||
} | ||
return stack.isEmpty() | ||
} | ||
|
||
@Test | ||
fun `์ ๋ ฅํ ๋ฌธ์์ด์ ๊ดํธ์ ์ด๋ฆผ๊ณผ ๋ซํ์ ๊ฒ์ฆํ๋ค`() { | ||
isValid("()") shouldBe true | ||
isValid("{()}") shouldBe true | ||
isValid("(){}[]") shouldBe true | ||
|
||
isValid("{(}") shouldBe false | ||
isValid("){") shouldBe false | ||
} | ||
} |