Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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()
anagrams.putIfAbsent(chars.toString(), mutableListOf())
val words = anagrams[chars.toString()]
Copy link
Contributor

Choose a reason for hiding this comment

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

chars.toString()이 반복적으로 사용되고 있어서 chars 변수에 바로 string값을 저장하는 것도 괜찮을 것 같아요 ~

Copy link
Contributor

Choose a reason for hiding this comment

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

코틀린 언어는 처음인데 putIfAbsent 메서드를 지원하나봐요! 조건문 처리할 때 엄청 편할 것 같아요 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

chars.toString()이 반복적으로 사용되고 있어서 chars 변수에 바로 string값을 저장하는 것도 괜찮을 것 같아요 ~

오, 그렇네요~
리뷰 반영했습니다 :) c5e173a

코틀린 언어는 처음인데 putIfAbsent 메서드를 지원하나봐요! 조건문 처리할 때 엄청 편할 것 같아요 👍

맞습니다~ map 에 대해서 다양한 메서드를 제공하더라구요 :)

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