From cb16f98a12e21f09257b59383afb1df383804e58 Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Sun, 12 Jan 2025 10:56:20 +0900 Subject: [PATCH 1/8] [WEEK6](gmlwls96) valid parentheses --- valid-parentheses/gmlwls96.kt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 valid-parentheses/gmlwls96.kt diff --git a/valid-parentheses/gmlwls96.kt b/valid-parentheses/gmlwls96.kt new file mode 100644 index 000000000..9e5839180 --- /dev/null +++ b/valid-parentheses/gmlwls96.kt @@ -0,0 +1,7 @@ +package leetcode_study + +class Solution { + fun isValid(s: String): Boolean { + + } +} \ No newline at end of file From ceda89cbca3e8b03a9c277fea2778fcf39593dcd Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Mon, 13 Jan 2025 21:00:53 +0900 Subject: [PATCH 2/8] [WEEK6](gmlwls96) valid parentheses --- valid-parentheses/gmlwls96.kt | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/valid-parentheses/gmlwls96.kt b/valid-parentheses/gmlwls96.kt index 9e5839180..775a068aa 100644 --- a/valid-parentheses/gmlwls96.kt +++ b/valid-parentheses/gmlwls96.kt @@ -2,6 +2,25 @@ package leetcode_study class Solution { fun isValid(s: String): Boolean { - + val stack = Stack() + 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() } -} \ No newline at end of file +} From 871575e5c1049aa92be66642253590f0c2069f2b Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Mon, 13 Jan 2025 21:47:51 +0900 Subject: [PATCH 3/8] [WEEK6](gmlwls96) Container With Most Water --- container-with-most-water/gmlwls96.kt | 22 ++++++++++++++++++++++ valid-parentheses/gmlwls96.kt | 1 + 2 files changed, 23 insertions(+) create mode 100644 container-with-most-water/gmlwls96.kt diff --git a/container-with-most-water/gmlwls96.kt b/container-with-most-water/gmlwls96.kt new file mode 100644 index 000000000..81b83ef83 --- /dev/null +++ b/container-with-most-water/gmlwls96.kt @@ -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 + } +} diff --git a/valid-parentheses/gmlwls96.kt b/valid-parentheses/gmlwls96.kt index 775a068aa..6531d9be6 100644 --- a/valid-parentheses/gmlwls96.kt +++ b/valid-parentheses/gmlwls96.kt @@ -1,6 +1,7 @@ package leetcode_study class Solution { + /** 시간 : O(n), 공간 : O(n) */ fun isValid(s: String): Boolean { val stack = Stack() val openParentheses = "([{" From 2826f54500f22c8ee8125150fd7deebb4f030edc Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Tue, 14 Jan 2025 22:00:14 +0900 Subject: [PATCH 4/8] [WEEK6](gmlwls96) Design Add and Search Words Data Structure --- .../gmlwls96.kt | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 design-add-and-search-words-data-structure/gmlwls96.kt diff --git a/design-add-and-search-words-data-structure/gmlwls96.kt b/design-add-and-search-words-data-structure/gmlwls96.kt new file mode 100644 index 000000000..29c7a9ccc --- /dev/null +++ b/design-add-and-search-words-data-structure/gmlwls96.kt @@ -0,0 +1,48 @@ +class Node() { + val map = mutableMapOf() + 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 + } +} From 45c944ef18f9bd50c4d5b1208723b989bdefad56 Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Thu, 16 Jan 2025 22:40:35 +0900 Subject: [PATCH 5/8] [WEEK6](gmlwls96) Longest Increasing Subsequence --- longest-increasing-subsequence/gmlwls.kt | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 longest-increasing-subsequence/gmlwls.kt diff --git a/longest-increasing-subsequence/gmlwls.kt b/longest-increasing-subsequence/gmlwls.kt new file mode 100644 index 000000000..11174aab9 --- /dev/null +++ b/longest-increasing-subsequence/gmlwls.kt @@ -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() + } +} \ No newline at end of file From e6ba55f762a9a8d8974183c2875028795f6dc288 Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Fri, 17 Jan 2025 22:42:27 +0900 Subject: [PATCH 6/8] =?UTF-8?q?[WEEK6](gmlwls96)=20Longest=20Increasing=20?= =?UTF-8?q?Subsequence=20-=20=EC=A4=84=EB=B0=94=EA=BF=88=20lint=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- longest-increasing-subsequence/gmlwls.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longest-increasing-subsequence/gmlwls.kt b/longest-increasing-subsequence/gmlwls.kt index 11174aab9..ed34f5d91 100644 --- a/longest-increasing-subsequence/gmlwls.kt +++ b/longest-increasing-subsequence/gmlwls.kt @@ -17,4 +17,4 @@ class Solution { } return count.last() } -} \ No newline at end of file +} From cad04fa3238929fa235cf044f3ef9765b445f15b Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Fri, 17 Jan 2025 22:46:41 +0900 Subject: [PATCH 7/8] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=EB=AA=85=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{gmlwls.kt => gmlwls96.kt} | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) rename longest-increasing-subsequence/{gmlwls.kt => gmlwls96.kt} (70%) diff --git a/longest-increasing-subsequence/gmlwls.kt b/longest-increasing-subsequence/gmlwls96.kt similarity index 70% rename from longest-increasing-subsequence/gmlwls.kt rename to longest-increasing-subsequence/gmlwls96.kt index ed34f5d91..8bc1b2aee 100644 --- a/longest-increasing-subsequence/gmlwls.kt +++ b/longest-increasing-subsequence/gmlwls96.kt @@ -7,11 +7,11 @@ class Solution { 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] + 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] } From c034d88e9f19252a9408a60b752c4521dfd16d5f Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Sat, 18 Jan 2025 11:29:08 +0900 Subject: [PATCH 8/8] [week6](gmlwls96) Spiral Matrix --- spiral-matrix/gmlwls96.kt | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 spiral-matrix/gmlwls96.kt diff --git a/spiral-matrix/gmlwls96.kt b/spiral-matrix/gmlwls96.kt new file mode 100644 index 000000000..8e94bed2d --- /dev/null +++ b/spiral-matrix/gmlwls96.kt @@ -0,0 +1,29 @@ +class Solution { + // 시간 : O(y*x), 공간 : O(1) + fun spiralOrder(matrix: Array): List { + val result = mutableListOf() + 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 + } +}