-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #807 from gmlwls96/main
- Loading branch information
Showing
5 changed files
with
183 additions
and
0 deletions.
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,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] | ||
} | ||
} | ||
} |
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,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<Int>() | ||
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 | ||
} | ||
} |
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,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 | ||
} | ||
} |
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,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 | ||
} | ||
} | ||
} |
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,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<CharArray>, 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<CharArray>, | ||
visit: Array<BooleanArray>, | ||
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 | ||
} | ||
} |