-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[gmlwls96] Week4 #807
[gmlwls96] Week4 #807
Changes from all commits
9f7dd2f
2f133cb
2b38afe
f273ad4
824a904
11581cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] | ||
} | ||
} | ||
} | ||
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 | ||
} | ||
Comment on lines
+18
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 왜 |
||
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 | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 풀이만 복잡도 표기를 누락하신 것 같네요. (의도하신 게 아니라면요) |
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 | ||
} | ||
} | ||
Comment on lines
+1
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 nums[i]를 i와 비교하는 방식을 사용했는데, @gmlwls96 님처럼 이전 값과 현재 값의 차이를 비교하는 방식도 있다는 걸 배웠어요. 이 방식으로도 문제를 풀어봐야겠습니다 😊 |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 깔끔한 풀이지만 실제 면접에서는 시간 복잡도에 대해서 챌린지를 받으실 수 있으실 것 같아요. 아직 마감까지 시간이 많으니 더 효율적인 알고리즘도 고민해보시면 좋을 것 같습니다. |
||
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 | ||
} | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 만약에 |
||
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 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아직 이 문제를 풀지 못했는데, 파이썬에서 sys.maxsize // 2를 초기값으로 설정하면, 오버플로우를 방지할 수 있다는 아이디어를 얻었어요. 문제를 풀 때 이 방식을 사용해봐야겠습니다. 이번주도 고생하셨습니다!