Skip to content

Commit 346b352

Browse files
authored
Merge pull request #847 from gmlwls96/main
[gmlwls96] Week5
2 parents f244f24 + bd1b77d commit 346b352

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
/**
3+
* 시간 : O(n) 공간 : O(1)
4+
* 풀이
5+
* lastIndex - 1부터 0까지 조회하면서 가장높은값(maxPrice) 에서 현재값(price[i])의 차(currentProfit)를 구한다.
6+
* profit 과 currentProfit중 더 높은값이 profit.
7+
* maxPrice와 prices[i]중 더 높은값이 maxPrice가 된다.
8+
* */
9+
fun maxProfit(prices: IntArray): Int {
10+
var profit = 0
11+
var maxPrice = prices[prices.lastIndex]
12+
for (i in prices.lastIndex - 1 downTo 0) {
13+
val currentProfit = maxPrice - prices[i]
14+
profit = max(profit, currentProfit)
15+
maxPrice = max(maxPrice, prices[i])
16+
}
17+
18+
return profit
19+
}
20+
}

encode-and-decode-strings/gmlwls96.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
3+
fun encode(strs: List<String>): String {
4+
return strs.joinToString(separator = ":;") {
5+
if (it == ":") {
6+
"::"
7+
} else {
8+
it
9+
}
10+
}
11+
}
12+
13+
fun decode(str: String): List<String> {
14+
return str
15+
.replace("::", ":")
16+
.split(":;")
17+
}
18+
}

group-anagrams/gmlwls96.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
3+
/**
4+
* 시간 : O(n*wlogw), 공간 : O(n*w)
5+
* 풀이
6+
* 1. strs를 한개씩 조회하며 strs[i]를 정렬한 값을 key값, value값은 list(strs[i])형태로 추가한다.
7+
* 2. return 형태에 맞게 map의 value만 뽑아낸다.
8+
* */
9+
fun groupAnagrams(strs: Array<String>): List<List<String>> {
10+
val map = mutableMapOf<String, MutableList<String>>()
11+
strs.forEach {
12+
val key = it.toCharArray()
13+
.sortedArray()
14+
.joinToString("")
15+
map[key] = map.getOrElse(key) { mutableListOf() }
16+
.apply { add(it) }
17+
}
18+
return map.map { it.value }
19+
}
20+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Node() {
2+
val map = mutableMapOf<Char, Node?>()
3+
var isEnd = false
4+
}
5+
6+
class Trie() {
7+
/** insert, search, startWith 시간 복잡도 : O(n) * */
8+
val rootNode = Node()
9+
fun insert(word: String) {
10+
var currentNode = rootNode
11+
word.forEach { char ->
12+
if (currentNode.map[char] == null) {
13+
currentNode.map[char] = Node()
14+
}
15+
currentNode = currentNode.map[char]!!
16+
}
17+
currentNode.isEnd = true
18+
}
19+
20+
fun search(word: String): Boolean {
21+
var currentNode = rootNode
22+
word.forEach { char ->
23+
if (currentNode.map[char] == null) {
24+
return false
25+
} else {
26+
currentNode = currentNode.map[char]!!
27+
}
28+
}
29+
return currentNode.isEnd
30+
}
31+
32+
fun startsWith(prefix: String): Boolean {
33+
var currentNode = rootNode
34+
prefix.forEach { char ->
35+
if (currentNode.map[char] == null) {
36+
return false
37+
} else {
38+
currentNode = currentNode.map[char]!!
39+
}
40+
}
41+
return true
42+
}
43+
}

word-break/gmlwls96.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/**
3+
* 시간 : O(s^2*w), 공간 : O(s)
4+
* */
5+
fun wordBreak(s: String, wordDict: List<String>): Boolean {
6+
val dp = BooleanArray(s.length + 1)
7+
dp[0] = true
8+
for (i in 1..s.length) {
9+
val subS = s.substring(0, i)
10+
val endWord = wordDict.firstOrNull { subS.endsWith(it) }
11+
if (endWord != null) {
12+
dp[i] = dp[i - endWord.length]
13+
}
14+
}
15+
return dp[s.length]
16+
}
17+
}

0 commit comments

Comments
 (0)