-
-
Notifications
You must be signed in to change notification settings - Fork 245
[gmlwls96] Week 6 #885
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
Changes from 7 commits
cb16f98
ceda89c
871575e
2826f54
45c944e
e6ba55f
cad04fa
c034d88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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-- | ||
} | ||
} | ||
return maxDiff | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class Node() { | ||
val map = mutableMapOf<Char, Node?>() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정해진 캐릭터가 26자라는걸 생각하면 map 대신 다른 자료형을 통해 제한을 줄 수 있을것 같아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} |
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() | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
} | ||
} |
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.
if ~ else 사용 방법도 있고, 조건을 조금 더 추가하여 시간 복잡도는 그대로 가져가되 반복문을 통해 투 포인터를 조정하는 방법도 있을것 같아요 :)