Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
20 changes: 20 additions & 0 deletions best-time-to-buy-and-sell-stock/gmlwls96.kt
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
}
}
18 changes: 18 additions & 0 deletions encode-and-decode-strings/gmlwls96.kt
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(":;")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

번거로우시겠지만 해당 파일 마지막에 줄갱 한줄 추가 부탁드립니다!

20 changes: 20 additions & 0 deletions group-anagrams/gmlwls96.kt
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 }
}
}
43 changes: 43 additions & 0 deletions implement-trie-prefix-tree/gmlwls96.kt
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
}
}
17 changes: 17 additions & 0 deletions word-break/gmlwls96.kt
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]
}
}
Loading