From 9f7dd2f7778850b87d1737f1776d08f3539e0c65 Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Sun, 29 Dec 2024 11:50:56 +0900 Subject: [PATCH 1/6] [Week4](gmlwls96) Merge two sorted lists. --- merge-two-sorted-lists/gmlwls96.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 merge-two-sorted-lists/gmlwls96.kt diff --git a/merge-two-sorted-lists/gmlwls96.kt b/merge-two-sorted-lists/gmlwls96.kt new file mode 100644 index 000000000..c00e10343 --- /dev/null +++ b/merge-two-sorted-lists/gmlwls96.kt @@ -0,0 +1,14 @@ +/** + * 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 { + fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? { + + } +} From 2f133cb0e2e253b6770938ab22f5382a0df4ce9a Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Sun, 29 Dec 2024 12:28:15 +0900 Subject: [PATCH 2/6] [Week4](gmlwls96) Merge two sorted lists --- merge-two-sorted-lists/gmlwls96.kt | 38 +++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/merge-two-sorted-lists/gmlwls96.kt b/merge-two-sorted-lists/gmlwls96.kt index c00e10343..77be6b5aa 100644 --- a/merge-two-sorted-lists/gmlwls96.kt +++ b/merge-two-sorted-lists/gmlwls96.kt @@ -8,7 +8,43 @@ * } */ 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 } } From 2b38afe487c9116e8777126cbbf0d1ddbcb43773 Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Sun, 29 Dec 2024 20:34:05 +0900 Subject: [PATCH 3/6] [Week4](gmlwls96) Missing number --- missing-number/gmlwls96.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 missing-number/gmlwls96.kt 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 + } +} From f273ad46f25b5a613b20527edf632449d21d687e Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Sun, 29 Dec 2024 21:13:47 +0900 Subject: [PATCH 4/6] [Week4](gmlwls96) word search --- word-search/gmlwls96.kt | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 word-search/gmlwls96.kt 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 + } +} From 824a90466a9cb6ea48f46bc2b35a264e4a3ccc28 Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Tue, 31 Dec 2024 20:27:46 +0900 Subject: [PATCH 5/6] [Week4](gmlwls96) Palindromic-substring --- palindromic-substrings/gmlwls96.kt | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 palindromic-substrings/gmlwls96.kt 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 + } + } +} From 11581cb13d98b48b1d151266a1dda1d16fc2c49c Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Tue, 31 Dec 2024 20:45:43 +0900 Subject: [PATCH 6/6] [Week4](gmlwls96) Coin Change --- coin-change/gmlwls96.kt | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 coin-change/gmlwls96.kt 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] + } + } +}