-
-
Notifications
You must be signed in to change notification settings - Fork 245
[gmlwls96] Week5 #847
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
[gmlwls96] Week5 #847
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5003c24
[Week5](gmlwls96) Best time to buy an sell stock
gmlwls96 1381be3
[Week5](gmlwls96) Best time to buy an sell stock
gmlwls96 0e7cb61
[Week5](gmlwls96) Group anagrams
gmlwls96 f665acb
[Week5](gmlwls96) Implement Trie prefix Tree
gmlwls96 1e91ae8
[Week5](gmlwls96) Word Break.
gmlwls96 da96545
[Week5](gmlwls96) Encode And Decode String
gmlwls96 bd1b77d
[Week5](gmlwls96)(fix) Encode And Decode String - github action fail 수정
gmlwls96 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,20 @@ | ||
class Solution { | ||
/** | ||
* 시간 : O(n) 공간 : O(1) | ||
* 풀이 | ||
* lastIndex - 1부터 0까지 조회하면서 가장높은값(maxPrice) 에서 현재값(price[i])의 차(currentProfit)를 구한다. | ||
* profit 과 currentProfit중 더 높은값이 profit. | ||
* maxPrice와 prices[i]중 더 높은값이 maxPrice가 된다. | ||
* */ | ||
fun maxProfit(prices: IntArray): Int { | ||
var profit = 0 | ||
var maxPrice = prices[prices.lastIndex] | ||
for (i in prices.lastIndex - 1 downTo 0) { | ||
val currentProfit = maxPrice - prices[i] | ||
profit = max(profit, currentProfit) | ||
maxPrice = max(maxPrice, prices[i]) | ||
} | ||
|
||
return profit | ||
} | ||
} |
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,18 @@ | ||
class Solution { | ||
|
||
fun encode(strs: List<String>): String { | ||
return strs.joinToString(separator = ":;") { | ||
if (it == ":") { | ||
"::" | ||
} else { | ||
it | ||
} | ||
} | ||
} | ||
|
||
fun decode(str: String): List<String> { | ||
return str | ||
.replace("::", ":") | ||
.split(":;") | ||
} | ||
} | ||
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,20 @@ | ||
class Solution { | ||
|
||
/** | ||
* 시간 : O(n*wlogw), 공간 : O(n*w) | ||
* 풀이 | ||
* 1. strs를 한개씩 조회하며 strs[i]를 정렬한 값을 key값, value값은 list(strs[i])형태로 추가한다. | ||
* 2. return 형태에 맞게 map의 value만 뽑아낸다. | ||
* */ | ||
fun groupAnagrams(strs: Array<String>): List<List<String>> { | ||
val map = mutableMapOf<String, MutableList<String>>() | ||
strs.forEach { | ||
val key = it.toCharArray() | ||
.sortedArray() | ||
.joinToString("") | ||
map[key] = map.getOrElse(key) { mutableListOf() } | ||
.apply { add(it) } | ||
} | ||
return map.map { it.value } | ||
} | ||
} |
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,43 @@ | ||
class Node() { | ||
val map = mutableMapOf<Char, Node?>() | ||
var isEnd = false | ||
} | ||
|
||
class Trie() { | ||
/** insert, search, startWith 시간 복잡도 : O(n) * */ | ||
val rootNode = Node() | ||
fun insert(word: String) { | ||
var currentNode = rootNode | ||
word.forEach { char -> | ||
if (currentNode.map[char] == null) { | ||
currentNode.map[char] = Node() | ||
} | ||
currentNode = currentNode.map[char]!! | ||
} | ||
currentNode.isEnd = true | ||
} | ||
|
||
fun search(word: String): Boolean { | ||
var currentNode = rootNode | ||
word.forEach { char -> | ||
if (currentNode.map[char] == null) { | ||
return false | ||
} else { | ||
currentNode = currentNode.map[char]!! | ||
} | ||
} | ||
return currentNode.isEnd | ||
} | ||
|
||
fun startsWith(prefix: String): Boolean { | ||
var currentNode = rootNode | ||
prefix.forEach { char -> | ||
if (currentNode.map[char] == null) { | ||
return false | ||
} else { | ||
currentNode = currentNode.map[char]!! | ||
} | ||
} | ||
return true | ||
} | ||
} |
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,17 @@ | ||
class Solution { | ||
/** | ||
* 시간 : O(s^2*w), 공간 : O(s) | ||
* */ | ||
fun wordBreak(s: String, wordDict: List<String>): Boolean { | ||
val dp = BooleanArray(s.length + 1) | ||
dp[0] = true | ||
for (i in 1..s.length) { | ||
val subS = s.substring(0, i) | ||
val endWord = wordDict.firstOrNull { subS.endsWith(it) } | ||
if (endWord != null) { | ||
dp[i] = dp[i - endWord.length] | ||
} | ||
} | ||
return dp[s.length] | ||
} | ||
} |
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.
번거로우시겠지만 해당 파일 마지막에 줄갱 한줄 추가 부탁드립니다!