diff --git a/problems/find-pivot-index.rs b/problems/find-pivot-index.rs index 173fbde..549a0ad 100644 --- a/problems/find-pivot-index.rs +++ b/problems/find-pivot-index.rs @@ -1,20 +1,20 @@ use std::convert::TryInto; -impl Solution { +impl Solution { pub fn pivot_index(nums: Vec) -> i32 { let mut from_left: i32 = 0; let mut from_right: i32 = nums.iter().sum(); - + for (i, e) in nums.iter().enumerate() { from_right -= e; - + if from_left == from_right { return i.try_into().unwrap(); } - + from_left += e; } - + -1 } } diff --git a/problems/max-area-of-island.kt b/problems/max-area-of-island.kt index 64d7edd..4ba34b4 100644 --- a/problems/max-area-of-island.kt +++ b/problems/max-area-of-island.kt @@ -3,7 +3,7 @@ fun product(xs: Sequence, ys: Sequence): Sequence> = fun product(xs: Iterable, ys: Iterable): Sequence> = product(xs.asSequence(), ys.asSequence()) - + class Solution { fun BFS(grid: Array, coords: Pair): Int { val queue = ArrayDeque>() @@ -15,7 +15,7 @@ class Solution { if (grid[y][x] != 1) { continue } - + for ((dy, dx) in sequenceOf(0 to 1, 1 to 0, 0 to -1, -1 to 0)) { if (!(y + dy in grid.indices) || !(x + dx in grid[y + dy].indices)) { continue @@ -25,15 +25,15 @@ class Solution { queue.addLast(y + dy to x + dx) } - + // mark it as done grid[y][x] = 0 size++ } - + return size } - + fun maxAreaOfIsland(grid: Array): Int = product(grid.indices, grid.first().indices).map { BFS(grid, it) }.max() ?: 0 } diff --git a/problems/maximum-number-of-coins-you-can-get.rs b/problems/maximum-number-of-coins-you-can-get.rs index de8a793..64812c3 100644 --- a/problems/maximum-number-of-coins-you-can-get.rs +++ b/problems/maximum-number-of-coins-you-can-get.rs @@ -2,7 +2,7 @@ impl Solution { pub fn max_coins(piles: Vec) -> i32 { let mut sorted_piles = piles.clone(); sorted_piles.sort(); - + sorted_piles .iter() .rev() diff --git a/problems/maximum-units-on-a-truck.kt b/problems/maximum-units-on-a-truck.kt index 4c61be9..7a4e46a 100644 --- a/problems/maximum-units-on-a-truck.kt +++ b/problems/maximum-units-on-a-truck.kt @@ -9,7 +9,7 @@ class Solution { .fold(0 to 0) { acc, boxType -> if (acc.first < truckSize) { val count = minOf(truckSize - acc.first, boxType.boxes) - + (acc.first + count) to (acc.second + count * boxType.units) } else { acc diff --git a/problems/pacific-atlantic-water-flow.rs b/problems/pacific-atlantic-water-flow.rs index 5f82142..fb32602 100644 --- a/problems/pacific-atlantic-water-flow.rs +++ b/problems/pacific-atlantic-water-flow.rs @@ -14,14 +14,11 @@ impl Reachable { let reaches_pacific = y == 0 || x == 0; let reaches_atlantic = y == max_y - 1 || x == max_x - 1; - if reaches_atlantic && reaches_pacific { - Reachable::Both - } else if reaches_atlantic { - Reachable::Atlantic - } else if reaches_pacific { - Reachable::Pacific - } else { - Reachable::None + match (reaches_pacific, reaches_atlantic) { + (true, true) => Reachable::Both, + (true, _) => Reachable::Pacific, + (_, true) => Reachable::Atlantic, + (_, _) => Reachable::None, } } }