Skip to content
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

[gmlwls96] Week 6 #885

Merged
merged 8 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all 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()
}
}
29 changes: 29 additions & 0 deletions spiral-matrix/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
// 시간 : O(y*x), 공간 : O(1)
fun spiralOrder(matrix: Array<IntArray>): List<Int> {
val result = mutableListOf<Int>()
if (matrix.isEmpty() || matrix[0].isEmpty()) return result

var top = 0
var bottom = matrix.size - 1
var left = 0
var right = matrix[0].size - 1

while (top <= bottom && left <= right) {
for (i in left..right) result.add(matrix[top][i])
top++
for (i in top..bottom) result.add(matrix[i][right])
right--
if (top <= bottom) {
for (i in right downTo left) result.add(matrix[bottom][i])
bottom--
}
if (left <= right) {
for (i in bottom downTo top) result.add(matrix[i][left])
left++
}
}

return result
}
}
27 changes: 27 additions & 0 deletions valid-parentheses/gmlwls96.kt
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