1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00

chore: fix formatting

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-09-09 18:56:13 +02:00
parent d7f8f4fd7b
commit e3888415f7
Signed by: mfocko
GPG key ID: 7C47D46246790496
5 changed files with 17 additions and 20 deletions

View file

@ -1,20 +1,20 @@
use std::convert::TryInto; use std::convert::TryInto;
impl Solution { impl Solution {
pub fn pivot_index(nums: Vec<i32>) -> i32 { pub fn pivot_index(nums: Vec<i32>) -> i32 {
let mut from_left: i32 = 0; let mut from_left: i32 = 0;
let mut from_right: i32 = nums.iter().sum(); let mut from_right: i32 = nums.iter().sum();
for (i, e) in nums.iter().enumerate() { for (i, e) in nums.iter().enumerate() {
from_right -= e; from_right -= e;
if from_left == from_right { if from_left == from_right {
return i.try_into().unwrap(); return i.try_into().unwrap();
} }
from_left += e; from_left += e;
} }
-1 -1
} }
} }

View file

@ -3,7 +3,7 @@ fun <A, B> product(xs: Sequence<A>, ys: Sequence<B>): Sequence<Pair<A, B>> =
fun <A, B> product(xs: Iterable<A>, ys: Iterable<B>): Sequence<Pair<A, B>> = fun <A, B> product(xs: Iterable<A>, ys: Iterable<B>): Sequence<Pair<A, B>> =
product(xs.asSequence(), ys.asSequence()) product(xs.asSequence(), ys.asSequence())
class Solution { class Solution {
fun BFS(grid: Array<IntArray>, coords: Pair<Int, Int>): Int { fun BFS(grid: Array<IntArray>, coords: Pair<Int, Int>): Int {
val queue = ArrayDeque<Pair<Int, Int>>() val queue = ArrayDeque<Pair<Int, Int>>()
@ -15,7 +15,7 @@ class Solution {
if (grid[y][x] != 1) { if (grid[y][x] != 1) {
continue continue
} }
for ((dy, dx) in sequenceOf(0 to 1, 1 to 0, 0 to -1, -1 to 0)) { 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)) { if (!(y + dy in grid.indices) || !(x + dx in grid[y + dy].indices)) {
continue continue
@ -25,15 +25,15 @@ class Solution {
queue.addLast(y + dy to x + dx) queue.addLast(y + dy to x + dx)
} }
// mark it as done // mark it as done
grid[y][x] = 0 grid[y][x] = 0
size++ size++
} }
return size return size
} }
fun maxAreaOfIsland(grid: Array<IntArray>): Int = fun maxAreaOfIsland(grid: Array<IntArray>): Int =
product(grid.indices, grid.first().indices).map { BFS(grid, it) }.max() ?: 0 product(grid.indices, grid.first().indices).map { BFS(grid, it) }.max() ?: 0
} }

View file

@ -2,7 +2,7 @@ impl Solution {
pub fn max_coins(piles: Vec<i32>) -> i32 { pub fn max_coins(piles: Vec<i32>) -> i32 {
let mut sorted_piles = piles.clone(); let mut sorted_piles = piles.clone();
sorted_piles.sort(); sorted_piles.sort();
sorted_piles sorted_piles
.iter() .iter()
.rev() .rev()

View file

@ -9,7 +9,7 @@ class Solution {
.fold(0 to 0) { acc, boxType -> .fold(0 to 0) { acc, boxType ->
if (acc.first < truckSize) { if (acc.first < truckSize) {
val count = minOf(truckSize - acc.first, boxType.boxes) val count = minOf(truckSize - acc.first, boxType.boxes)
(acc.first + count) to (acc.second + count * boxType.units) (acc.first + count) to (acc.second + count * boxType.units)
} else { } else {
acc acc

View file

@ -14,14 +14,11 @@ impl Reachable {
let reaches_pacific = y == 0 || x == 0; let reaches_pacific = y == 0 || x == 0;
let reaches_atlantic = y == max_y - 1 || x == max_x - 1; let reaches_atlantic = y == max_y - 1 || x == max_x - 1;
if reaches_atlantic && reaches_pacific { match (reaches_pacific, reaches_atlantic) {
Reachable::Both (true, true) => Reachable::Both,
} else if reaches_atlantic { (true, _) => Reachable::Pacific,
Reachable::Atlantic (_, true) => Reachable::Atlantic,
} else if reaches_pacific { (_, _) => Reachable::None,
Reachable::Pacific
} else {
Reachable::None
} }
} }
} }