-
-
Notifications
You must be signed in to change notification settings - Fork 195
[이유진] 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
[이유진] Week 5 #874
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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 | ||
} |
This file contains hidden or 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,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() | ||
} |
This file contains hidden or 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,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 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
2가지 방법 모두 작성하셨군요.
더 나은 로직 고민하시는 부분 멋지십니다 👍