-
Notifications
You must be signed in to change notification settings - Fork 126
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
[gmlwls96] Week 6 #885
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cb16f98
[WEEK6](gmlwls96) valid parentheses
gmlwls96 ceda89c
[WEEK6](gmlwls96) valid parentheses
gmlwls96 871575e
[WEEK6](gmlwls96) Container With Most Water
gmlwls96 2826f54
[WEEK6](gmlwls96) Design Add and Search Words Data Structure
gmlwls96 45c944e
[WEEK6](gmlwls96) Longest Increasing Subsequence
gmlwls96 e6ba55f
[WEEK6](gmlwls96) Longest Increasing Subsequence - 줄바꿈 lint 수정
gmlwls96 cad04fa
파일명 에러 수정
gmlwls96 c034d88
[week6](gmlwls96) Spiral Matrix
gmlwls96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 사용 방법도 있고, 조건을 조금 더 추가하여 시간 복잡도는 그대로 가져가되 반복문을 통해 투 포인터를 조정하는 방법도 있을것 같아요 :)