Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
22 changes: 22 additions & 0 deletions container-with-most-water/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
/** 시간 : O(n), 공간 : O(1)*/
fun maxArea(height: IntArray): Int {
var maxDiff = 0
var left = 0
var right = height.lastIndex
// left, right값을 순차적으로 조회해서 물높이를 구하고,
// left < right값 보다 작으면 left증가시킨다. 반대는 right 감소
while (left < right) {
maxDiff = max(maxDiff, (right - left) * min(height[left], height[right]))
// 너비 : right - left
// 현재 높이 : min(height[left], height[right])
// 너비 * 현재 높이가 maxDiff 비교하여 더 큰값이 maxDiff가 된다.
if (height[left] < height[right]) {
left++
} else {
right--
}
Comment on lines +14 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

if ~ else 사용 방법도 있고, 조건을 조금 더 추가하여 시간 복잡도는 그대로 가져가되 반복문을 통해 투 포인터를 조정하는 방법도 있을것 같아요 :)

}
return maxDiff
}
}
48 changes: 48 additions & 0 deletions design-add-and-search-words-data-structure/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Node() {
val map = mutableMapOf<Char, Node?>()
Copy link
Contributor

Choose a reason for hiding this comment

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

정해진 캐릭터가 26자라는걸 생각하면 map 대신 다른 자료형을 통해 제한을 줄 수 있을것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

아 그렇네요~! 좋은 리뷰 감사합니다ㅎㅎ

var isEnd = false
}

class WordDictionary() {

val rootNode = Node()

// 시간 : O(n), 공간 : O(n)
fun addWord(word: String) {
var currentNode = rootNode
for (i in word.indices) {
val char = word[i]
if (currentNode.map[char] == null) {
currentNode.map[char] = Node()
}
currentNode = currentNode.map[char]!!
}
currentNode.isEnd = true
}

// 시간 : O(26*n), 공간: O(n)
fun search(word: String): Boolean {
return dfs(
pos = 0,
word = word,
node = rootNode
)
}

fun dfs(pos: Int, word: String, node: Node): Boolean {
var result = false
val char = word[pos]
val isLast = word.lastIndex == pos
node.map.forEach {
if (char == '.' || char == it.key) {
if (isLast) {
result = true
return@forEach
} else {
result = result or dfs(pos + 1, word, it.value!!)
}
}
}
return result
}
}
20 changes: 20 additions & 0 deletions longest-increasing-subsequence/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
// 시간 : O(n), 공간 : O(n)
// nums를 조회하면서 이전값과 비교하여
// 더 증가하였으면 : 이전 카운트 +1
// 같거나 작으면 : 이전 카운트값
fun lengthOfLIS(nums: IntArray): Int {
val count = IntArray(nums.size)
count[0] = 1
var prev = nums[0]
for (i in 1 until nums.size) {
if (prev < nums[i]) {
count[i] += count[i - 1] + 1
} else {
count[i] = count[i - 1]
}
prev = nums[i]
}
return count.last()
}
}
27 changes: 27 additions & 0 deletions valid-parentheses/gmlwls96.kt
Copy link
Contributor

Choose a reason for hiding this comment

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

문제 풀이가 명료하고 가독성이 더 좋은것 같아요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package leetcode_study

class Solution {
/** 시간 : O(n), 공간 : O(n) */
fun isValid(s: String): Boolean {
val stack = Stack<Char>()
val openParentheses = "([{"
s.forEach {
if (openParentheses.contains(it)) {
stack.push(it)
} else {
if (stack.isEmpty()) {
return false
}
val top = stack.pop()
if (
top == openParentheses[0] && it != ')' ||
top == openParentheses[1] && it != ']' ||
top == openParentheses[2] && it != '}'
) {
return false
}
}
}
return stack.isEmpty()
}
}
Loading