Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions linked-list-cycle/yhkee0404.go
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

간단하게 u, v 라고 변수명을 지어도 좋지만 slow, fast처럼 의미를 담아주면 나중에 코드를 다시 볼 때 알고리즘 의도를 더 쉽게 파악할 수 있을 것 같습니다!

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
}
26 changes: 26 additions & 0 deletions maximum-product-subarray/yhkee0404.rs
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
}
}
33 changes: 33 additions & 0 deletions minimum-window-substring/yhkee0404.kt
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)
}
}
37 changes: 37 additions & 0 deletions pacific-atlantic-water-flow/yhkee0404.scala
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)
}
}
}
27 changes: 27 additions & 0 deletions sum-of-two-integers/yhkee0404.dart
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;
}
}