-
-
Notifications
You must be signed in to change notification settings - Fork 244
[yhkee0404] WEEK 09 solutions #1912
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
6 commits
Select commit
Hold shift + click to select a range
fe9408a
linked list cycle
yhkee0404 54aee8d
pacific atlantic water flow solution
yhkee0404 96dedf8
maximum product subarray solution
yhkee0404 e9e655f
sum of two integers solution
yhkee0404 29ec6d3
minimum window substring solution
yhkee0404 0b49d70
linked list cycle solution with comment
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,23 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* type ListNode struct { | ||
* Val int | ||
* Next *ListNode | ||
* } | ||
*/ | ||
func hasCycle(head *ListNode) bool { | ||
u := head | ||
v := head | ||
for u != nil && v != nil { // T(n) = O(n), S(n) = O(1) | ||
u = u.Next | ||
if (u == nil) { | ||
break | ||
} | ||
u = u.Next // https://hyp.is/gLsBIOHBEe6E_9M9T5MJNw/yhkee0404.github.io/posts/algorithms/leetcode/linked-list-cycle/ | ||
v = v.Next | ||
if u == v { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,26 @@ | ||
impl Solution { | ||
pub fn max_product(nums: Vec<i32>) -> i32 { | ||
let mut neg_abs_min = 1; | ||
let mut total = 1; | ||
let mut ans = *nums.first() | ||
.unwrap(); | ||
for num in nums { | ||
if num == 0 { | ||
neg_abs_min = 1; | ||
total = 1; | ||
ans = ans.max(0); | ||
continue | ||
} | ||
total *= num; | ||
ans = ans.max(total); | ||
if total < 0 { | ||
if neg_abs_min == 1 { | ||
neg_abs_min = total; | ||
} else { | ||
ans = ans.max(total / neg_abs_min) | ||
} | ||
} | ||
} | ||
ans | ||
} | ||
} |
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,33 @@ | ||
class Solution { | ||
fun minWindow(s: String, t: String): String { // T(m, n) = S(m, n) = O(m + n) | ||
val counterT = ( | ||
t | ||
.groupingBy {it} // O(n) | ||
.eachCount() | ||
) | ||
var l = -1 | ||
var r = -1 | ||
val counterS = mutableMapOf<Char, Int>() | ||
var i = -1 | ||
( | ||
s | ||
.withIndex() | ||
.forEach { // O(m) | ||
counterS.compute(it.value) {k, v -> if (v == null) 1 else v + 1} | ||
while (i != it.index && counterS[s[i + 1]]!! > counterT.getOrDefault(s[i + 1], 0)) { // O(m) | ||
counterS.compute(s[++i]) {k, v -> v!! - 1} | ||
} | ||
if ( | ||
counterT | ||
.all {(k, v) -> counterS.getOrDefault(k, 0) >= v} // O(n) | ||
) { | ||
if (r == -1 || r - l > it.index - i) { | ||
l = i | ||
r = it.index | ||
} | ||
} | ||
} | ||
) | ||
return s.substring(l + 1, r + 1) // O(m) | ||
} | ||
} |
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,37 @@ | ||
object Solution { | ||
val _DRCS = Array( | ||
Array(-1, 0), | ||
Array(0, -1), | ||
Array(0, 1), | ||
Array(1, 0), | ||
) | ||
def pacificAtlantic(heights: Array[Array[Int]]): List[List[Int]] = { | ||
val visited = Array.fill[Array[Int]](heights.length)( | ||
Array.fill[Int](heights.head.length)(0) | ||
) | ||
for (i <- heights.indices) { | ||
dfs(heights, visited, i, 0, 1) | ||
dfs(heights, visited, i, heights.head.length - 1, 2) | ||
} | ||
for (i <- heights.head.indices) { | ||
dfs(heights, visited, 0, i, 1) | ||
dfs(heights, visited, heights.length - 1, i, 2) | ||
} | ||
( | ||
for { | ||
r <- visited.indices | ||
c <- visited.head.indices | ||
if visited(r)(c) == 3 | ||
} yield List(r, c) | ||
).toList | ||
} | ||
def dfs(heights: Array[Array[Int]], visited: Array[Array[Int]], r: Int, c: Int, v: Int): Unit = { | ||
visited(r)(c) |= v | ||
_DRCS.map { case Array(dr, dc) => (r + dr, c + dc) } | ||
.filter { case (nr, nc) => nr != -1 && nr != heights.length && nc != -1 && nc != heights.head.length } | ||
.filter { case (nr, nc) => (visited(nr)(nc) & v) == 0 && heights(r)(c) <= heights(nr)(nc) } | ||
.foreach { case (nr, nc) => | ||
dfs(heights, visited, nr, nc, v) | ||
} | ||
} | ||
} |
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,27 @@ | ||
class Solution { | ||
int getSum(int a, int b) { | ||
int ans = 0; | ||
// for (int i = 0, carry = 0, ai, bi, u = 1; i != 64 && (carry | a | b) != 0; i++, a >>>= 1, b >>>= 1, u <<= 1) { // significand 53 bits but u has only 1 bit | ||
for (int i = 0, carry = 0, ai, bi, u = 1; i != 32 && (carry | a | b) != 0; i++, a >>= 1, b >>= 1, u <<= 1) { | ||
ai = a & 1; | ||
bi = b & 1; | ||
if ((ai & bi) == 1) { | ||
if (carry == 1) { | ||
ans |= u; | ||
} | ||
carry = 1; | ||
} else if ((ai ^ bi) == 1) { | ||
if (carry == 0) { | ||
ans |= u; | ||
} | ||
} else { | ||
if (carry == 1) { | ||
ans |= u; | ||
} | ||
carry = 0; | ||
} | ||
} | ||
// return ans; | ||
return (ans & 0x80000000) != 0 ? ans | ~ 0xFFFFFFFF : ans; | ||
} | ||
} |
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.
간단하게 u, v 라고 변수명을 지어도 좋지만 slow, fast처럼 의미를 담아주면 나중에 코드를 다시 볼 때 알고리즘 의도를 더 쉽게 파악할 수 있을 것 같습니다!