-
-
Notifications
You must be signed in to change notification settings - Fork 245
[이유진] 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
[이유진] Week 5 #874
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
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()] | ||
words?.add(str) | ||
} | ||
|
||
return anagrams.values.toList() | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 질문입니다~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아, emptyString 이 있을거라고 생각해서 true 로 설정했습니다.! |
||
|
||
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 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chars.toString()
이 반복적으로 사용되고 있어서chars
변수에 바로 string값을 저장하는 것도 괜찮을 것 같아요 ~There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코틀린 언어는 처음인데
putIfAbsent
메서드를 지원하나봐요! 조건문 처리할 때 엄청 편할 것 같아요 👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오, 그렇네요~
리뷰 반영했습니다 :) c5e173a
맞습니다~ map 에 대해서 다양한 메서드를 제공하더라구요 :)