Skip to content

[이유진] Week 5 #874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions best-time-to-buy-and-sell-stock/Real-Reason.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package leetcode_study

import kotlin.math.max
import kotlin.math.min
fun maxProfit(prices: IntArray): Int {
var minPrice = prices[0]
var maxProfit = 0

for (i in 0 until prices.size) {
maxProfit = max(maxProfit, prices[i] - minPrice)
minPrice = min(minPrice, prices[i])
}

return maxProfit
}
13 changes: 13 additions & 0 deletions group-anagrams/Real-Reason.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package leetcode_study

fun groupAnagrams(strs: Array<String>): List<List<String>> {
val anagrams = HashMap<String, MutableList<String>>()
strs.forEach { str ->
val chars = str.toCharArray().sorted().toString()
anagrams.putIfAbsent(chars, mutableListOf())
val words = anagrams[chars]
words?.add(str)
}

return anagrams.values.toList()
}
59 changes: 59 additions & 0 deletions implement-trie-prefix-tree/Real-Reason.kt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2가지 방법 모두 작성하셨군요.
더 나은 로직 고민하시는 부분 멋지십니다 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package leetcode_study

class FirstSolution {
private val document = hashSetOf<String>()

fun insert(word: String) {
document.add(word)
}

fun search(word: String): Boolean {
return document.contains(word)
}

fun startsWith(prefix: String): Boolean {
val word = document.firstOrNull { it.startsWith(prefix) }
return word != null
}
}

class SecondSolution {
class Document(var end: Boolean = true) {
val items = hashMapOf<Char, Document>()
}

private val document = Document(end = true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

질문입니다~
document 노드가 루트 노드인데 end값이 true로 설정된 이유가 있을까요?!
단어의 끝이 아니라서 여쭈어봐요~~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아, emptyString 이 있을거라고 생각해서 true 로 설정했습니다.!
그런데 다시 문제를 보니 length 가 1 이상이네요 😂


fun insert(word: String) {
var node = document
for (char in word) {
if (char !in node.items) {
node.items[char] = Document(end = false)
}
node = node.items[char]!!
}
node.end = true
}

fun search(word: String): Boolean {
var node = document
for (char in word) {
if (char !in node.items) {
return false
}
node = node.items[char]!!
}
return node.end
}

fun startsWith(prefix: String): Boolean {
var node = document
for (char in prefix) {
if (char !in node.items) {
return false
}
node = node.items[char]!!
}
return true
}
}
Loading