diff --git a/coin-change/gmlwls96.kt b/coin-change/gmlwls96.kt new file mode 100644 index 000000000..f9414aacf --- /dev/null +++ b/coin-change/gmlwls96.kt @@ -0,0 +1,25 @@ +class Solution { + // 알고리즘 : dp + /** 풀이 + * dp배열에 최소한의 동전의 개수를 저장. + * dp[i] = min(dp[i - 동전값], dp[i]) 중 더 작은값이 최소 동전의 개수. + * */ + // 시간 : O(coins.len*amount), 공간 : O(amount) + fun coinChange(coins: IntArray, amount: Int): Int { + val initValue = Int.MAX_VALUE / 2 + val dp = IntArray(amount + 1) { initValue } + dp[0] = 0 + for (i in 1..amount) { + coins.forEach { c -> + if (c <= i) { + dp[i] = min(dp[i - c] + 1, dp[i]) + } + } + } + return if (dp[amount] == initValue) { + -1 + } else { + dp[amount] + } + } +} diff --git a/merge-two-sorted-lists/gmlwls96.kt b/merge-two-sorted-lists/gmlwls96.kt new file mode 100644 index 000000000..77be6b5aa --- /dev/null +++ b/merge-two-sorted-lists/gmlwls96.kt @@ -0,0 +1,50 @@ +/** + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ +class Solution { + // 시간, 공간 : o(n+m), + fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? { + var currentList1: ListNode? = list1 + var currentList2: ListNode? = list2 + val answerList = LinkedList() + while (currentList1 != null || currentList2 != null) { + when { + currentList1 == null -> { + answerList.offer(currentList2!!.`val`) + currentList2 = currentList2.next + } + currentList2 == null -> { + answerList.offer(currentList1.`val`) + currentList1 = currentList1.next + } + currentList1.`val` <= currentList2.`val` -> { + answerList.offer(currentList1.`val`) + currentList1 = currentList1.next + } + currentList2.`val` < currentList1.`val` -> { + answerList.offer(currentList2.`val`) + currentList2 = currentList2.next + } + } + } + var answer: ListNode? = null + var currentAnswer: ListNode? = null + while (answerList.isNotEmpty()) { + val num = answerList.poll() + if (answer == null) { + answer = ListNode(num) + currentAnswer = answer + } else { + currentAnswer?.next = ListNode(num) + currentAnswer = currentAnswer?.next + } + } + return answer + } +} diff --git a/missing-number/gmlwls96.kt b/missing-number/gmlwls96.kt new file mode 100644 index 000000000..cafe84bbf --- /dev/null +++ b/missing-number/gmlwls96.kt @@ -0,0 +1,13 @@ +class Solution { + fun missingNumber(nums: IntArray): Int { + nums.sort() + var num = nums[0] + for (i in 1 until nums.size) { + if (nums[i] != (num + 1)) { + return num + 1 + } + num = nums[i] + } + return num + 1 + } +} diff --git a/palindromic-substrings/gmlwls96.kt b/palindromic-substrings/gmlwls96.kt new file mode 100644 index 000000000..50432be73 --- /dev/null +++ b/palindromic-substrings/gmlwls96.kt @@ -0,0 +1,28 @@ +class Solution { + // 알고리즘 : brute-force + /** 풀이 + * 1. 모든 substring을 전부 뽑아낸다. + * 2. 해당 substring이 palindrome인지 체크한다. + */ + // 시간 : O(n^3) + fun countSubstrings(s: String): Int { + var count = 0 + for (len in 1..s.length) { // 길이 + for (i in 0..(s.length - len)) { // i : sub string start index. + if (checkPalindrome(s.substring(i, i + len))) { + count++ + } + } + } + return count + } + + private fun checkPalindrome(subStr: String): Boolean { + return if (subStr.length == 1) { + true + } else { + val reverse = subStr.reversed() + subStr == reverse + } + } +} diff --git a/word-search/gmlwls96.kt b/word-search/gmlwls96.kt new file mode 100644 index 000000000..18d13f7c9 --- /dev/null +++ b/word-search/gmlwls96.kt @@ -0,0 +1,67 @@ +class Solution { + + // 풀이 : dfs + // 시간 :O(m * n * 4^w), 공간 :O(m * n + w) + val movePos = arrayOf( + intArrayOf(-1, 0), + intArrayOf(0, -1), + intArrayOf(1, 0), + intArrayOf(0, 1) + ) + + fun exist(board: Array, word: String): Boolean { + for (y in board.indices) { + for (x in board[y].indices) { + if (existDfs( + board, + Array(board.size) { BooleanArray(board[it].size) }, + word, + "", + y, + x + ) + ) { + return true + } + } + } + return false + } + + private fun existDfs( + board: Array, + visit: Array, + findWord: String, + currentWord: String, + y: Int, + x: Int + ): Boolean { + if (findWord == currentWord) return true + val findChar = findWord[currentWord.length] + if (board[y][x] == findChar) { + val newWord = currentWord + board[y][x] + visit[y][x] = true + for (pos in movePos) { + val newY = y + pos[0] + val newX = x + pos[1] + if (newY >= 0 && newX >= 0 + && newY < board.size + && newX < board[newY].size + && !visit[newY][newX] + && existDfs( + board = board, + visit = visit, + findWord = findWord, + currentWord = newWord, + y = newY, + x = newX + ) + ) { + return true + } + } + visit[y][x] = false + } + return false + } +}