-
-
Notifications
You must be signed in to change notification settings - Fork 247
[yhkee0404] WEEK 04 solutions #1823
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
65f77b6
merge two sorted lists solution
yhkee0404 8c9f7df
maximum depth of binary tree solution
yhkee0404 d33110d
find minimum in rotated sorted array solution
yhkee0404 c1769eb
word search solution
yhkee0404 4f5c769
coin change solution
yhkee0404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,14 @@ | ||
class Solution { | ||
int coinChange(List<int> coins, int amount) { | ||
List<int> dp = List.filled(amount + 1, amount + 1); | ||
// S(n) = O(amount) | ||
dp[0] = 0; | ||
for (int i = 1; i <= amount; i++) { | ||
// T(n) = O(amount * coins.length) | ||
dp[i] = 1 + coins.where((x) => x <= i) | ||
.map((x) => dp[i - x]) | ||
.fold(amount, (a, b) => min(a, b)); | ||
} | ||
return dp[amount] <= amount ? dp[amount] : -1; | ||
} | ||
} |
This file contains hidden or 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,16 @@ | ||
object Solution { | ||
def findMin(nums: Array[Int]): Int = { | ||
var l = 0 | ||
var r = nums.size | ||
while (l != r) { | ||
val m = l + ((r - l) >> 1) | ||
// T(n) = lg(n) | ||
if (nums(m) <= nums.last) { | ||
r = m | ||
} else { | ||
l = m + 1 | ||
} | ||
} | ||
nums(l) | ||
} | ||
} |
This file contains hidden or 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,31 @@ | ||
// Definition for a binary tree node. | ||
// #[derive(Debug, PartialEq, Eq)] | ||
// pub struct TreeNode { | ||
// pub val: i32, | ||
// pub left: Option<Rc<RefCell<TreeNode>>>, | ||
// pub right: Option<Rc<RefCell<TreeNode>>>, | ||
// } | ||
// | ||
// impl TreeNode { | ||
// #[inline] | ||
// pub fn new(val: i32) -> Self { | ||
// TreeNode { | ||
// val, | ||
// left: None, | ||
// right: None | ||
// } | ||
// } | ||
// } | ||
use std::rc::Rc; | ||
use std::cell::RefCell; | ||
impl Solution { | ||
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { | ||
match root { | ||
None => 0, | ||
Some(u) => { | ||
let u = u.borrow(); | ||
1 + Self::max_depth(u.left.clone()).max(Self::max_depth(u.right.clone())) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,24 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* type ListNode struct { | ||
* Val int | ||
* Next *ListNode | ||
* } | ||
*/ | ||
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode { | ||
ans := &ListNode{} | ||
l1 := list1 | ||
l2 := list2 | ||
l3 := ans | ||
for l1 != nil || l2 != nil { | ||
if l2 == nil || l1 != nil && l1.Val < l2.Val { | ||
l3.Next = l1 | ||
l1 = l1.Next | ||
} else { | ||
l3.Next = l2 | ||
l2 = l2.Next | ||
} | ||
l3 = l3.Next | ||
} | ||
return ans.Next | ||
} |
This file contains hidden or 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,58 @@ | ||
val _DRCS = listOf( | ||
listOf(0, -1), | ||
listOf(0, 1), | ||
listOf(-1, 0), | ||
listOf(1, 0), | ||
) | ||
|
||
class Solution { | ||
fun exist(board: Array<CharArray>, word: String): Boolean { | ||
val offset = 'A'.code | ||
val invertedIndex = List('z'.code - offset + 1) {mutableListOf<List<Int>>()} | ||
board.withIndex() | ||
.forEach { row -> | ||
row.value | ||
.withIndex() | ||
.forEach { | ||
invertedIndex[it.value.toInt() - offset].add(listOf(row.index, it.index)) | ||
} | ||
} | ||
val freq = MutableList(invertedIndex.size) {0} | ||
word.map {it.toInt() - offset} | ||
.forEach { | ||
freq[it]++ | ||
} | ||
if ((0 until freq.size).any {freq[it] > invertedIndex[it].size}) { | ||
return false | ||
} | ||
val target = if (invertedIndex[word.first().toInt() - offset].size <= invertedIndex[word.last().toInt() - offset].size) word | ||
else word.reversed() | ||
val stack = invertedIndex[target.first().toInt() - offset].map { | ||
mutableListOf(it.first(), it.last(), 0, 1) | ||
}.toMutableList() | ||
val visited = MutableList(board.size) {MutableList(board.first().size) {false}} | ||
while (! stack.isEmpty()) { | ||
val u = stack.last() | ||
if (u[2] == 4) { | ||
visited[u[0]][u[1]] = false | ||
stack.removeLast() | ||
continue | ||
} | ||
if (u[2] == 0) { | ||
if (u[3] == target.length) { | ||
return true | ||
} | ||
visited[u[0]][u[1]] = true | ||
} | ||
val drc = _DRCS[u[2]] | ||
u[2]++ | ||
val v = mutableListOf(u[0] + drc[0], u[1] + drc[1], 0, u[3] + 1) | ||
if (v[0] == -1 || v[0] == board.size || v[1] == -1 || v[1] == board.first().size | ||
|| visited[v[0]][v[1]] || board[v[0]][v[1]] != target[u[3]]) { | ||
continue | ||
} | ||
stack.add(v) | ||
} | ||
return false | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
스택을 사용해서 푸는건 생각도 못했는데 대단하시네요!
게다가 주력 언어가 파이썬이라고 하셨죠?
문제들을 모두 멋지게 풀어주셔서 많이 배웁니다 :)