diff --git a/3sum/hyunjung-choi.kt b/3sum/hyunjung-choi.kt new file mode 100644 index 000000000..1aff4131c --- /dev/null +++ b/3sum/hyunjung-choi.kt @@ -0,0 +1,35 @@ +class Solution { + fun threeSum(nums: IntArray): List> { + if (nums.size < 3) return emptyList() + + val result = mutableSetOf>() + + nums.sort() + + for (i in 0 until nums.size - 2) { + + if (i > 0 && nums[i] == nums[i - 1]) continue + + var left = i + 1 + var right = nums.size - 1 + + while (left < right) { + val sum = nums[i] + nums[left] + nums[right] + + when { + sum == 0 -> { + result.add(listOf(nums[i], nums[left], nums[right])) + + left++ + right-- + } + + sum < 0 -> left++ + else -> right-- + } + } + } + + return result.toList() + } +} diff --git a/climbing-stairs/hyunjung-choi.kt b/climbing-stairs/hyunjung-choi.kt new file mode 100644 index 000000000..cadcc083a --- /dev/null +++ b/climbing-stairs/hyunjung-choi.kt @@ -0,0 +1,16 @@ +class Solution { + fun climbStairs(n: Int): Int { + if (n <= 2) return n + + var prev2 = 1 + var prev1 = 2 + + for (i in 3..n) { + val current = prev1 + prev2 + prev2 = prev1 + prev1 = current + } + + return prev1 + } +} diff --git a/product-of-array-except-self/hyunjung-choi.kt b/product-of-array-except-self/hyunjung-choi.kt new file mode 100644 index 000000000..1ac22486c --- /dev/null +++ b/product-of-array-except-self/hyunjung-choi.kt @@ -0,0 +1,16 @@ +class Solution { + fun productExceptSelf(nums: IntArray): IntArray = + IntArray(nums.size) { 1 }.apply { + var leftProduct = 1 + for (i in 1 until nums.size) { + leftProduct *= nums[i - 1] + this[i] = leftProduct + } + + var rightProduct = 1 + for (i in nums.size - 2 downTo 0) { + rightProduct *= nums[i + 1] + this[i] *= rightProduct + } + } +} diff --git a/valid-anagram/hyunjung-choi.kt b/valid-anagram/hyunjung-choi.kt new file mode 100644 index 000000000..4357915c6 --- /dev/null +++ b/valid-anagram/hyunjung-choi.kt @@ -0,0 +1,19 @@ +class Solution { + fun isAnagram(s: String, t: String): Boolean { + if (s.length != t.length) return false + + val charCount = mutableMapOf() + + s.forEach { ch -> + charCount.put(ch, charCount.getOrDefault(ch, 0) + 1) + } + + t.forEach { ch -> + val count = charCount.getOrDefault(ch, 0) + if (count == 0) return false + charCount[ch] = count - 1 + } + + return true + } +} diff --git a/validate-binary-search-tree/hyunjung-choi.kt b/validate-binary-search-tree/hyunjung-choi.kt new file mode 100644 index 000000000..0bce34a62 --- /dev/null +++ b/validate-binary-search-tree/hyunjung-choi.kt @@ -0,0 +1,30 @@ +/** + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ +class Solution { + fun isValidBST(root: TreeNode?): Boolean { + val values = mutableListOf() + inorderTraversal(root, values) + + for (i in 1 until values.size) { + if (values[i] <= values[i - 1]) return false + } + + return true + } + + private fun inorderTraversal(root: TreeNode?, values: MutableList) { + if (root == null) return + + inorderTraversal(root.left, values) + values.add(root.`val`) + inorderTraversal(root.right, values) + } +}