commit 338b4fe562183df1e9b792814872235e85f86b3e
parent 3a9f4581c48f2bc100cce8630201e23ab5f14365
Author: Louis Burda <quent.burda@gmail.com>
Date: Fri, 22 Nov 2024 00:50:08 +0100
Cargo format
Diffstat:
14 files changed, 140 insertions(+), 74 deletions(-)
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
@@ -3,6 +3,6 @@ repos:
hooks:
- id: rustfmt
name: rustfmt
- entry: cargo fmt --all -- --check --color always
+ entry: cargo fmt --all -- --color always
language: system
pass_filenames: false
diff --git a/src/bin/0001-two-sum.rs b/src/bin/0001-two-sum.rs
@@ -60,10 +60,7 @@ impl Solution {
}
pub fn main() {
- println!(
- "{:?}",
- Solution::two_sum(arg_into(1), arg_into(2))
- )
+ println!("{:?}", Solution::two_sum(arg_into(1), arg_into(2)))
}
#[cfg(test)]
diff --git a/src/bin/0004-median-of-two-sorted-arrays.rs b/src/bin/0004-median-of-two-sorted-arrays.rs
@@ -28,7 +28,6 @@
/// 1 <= m + n <= 2000
/// -106 <= nums1[i], nums2[i] <= 106
///
-
use leetcode::arg_into;
struct Solution {}
@@ -109,7 +108,10 @@ impl Solution {
}
pub fn main() {
- println!("{:?}", Solution::find_median_sorted_arrays(arg_into(1), arg_into(2)));
+ println!(
+ "{:?}",
+ Solution::find_median_sorted_arrays(arg_into(1), arg_into(2))
+ );
}
#[cfg(test)]
diff --git a/src/bin/1574-shortest-subarray-to-be-removed-to-make-array-sorted.rs b/src/bin/1574-shortest-subarray-to-be-removed-to-make-array-sorted.rs
@@ -35,7 +35,6 @@
/// * 1 <= arr.length <= 105
/// * 0 <= arr[i] <= 109
///
-
use leetcode::*;
use std::cmp;
@@ -44,13 +43,15 @@ struct Solution {}
impl Solution {
pub fn find_length_of_shortest_subarray(arr: Vec<i32>) -> i32 {
let n = arr.len();
- if n == 0 { return 0; }
+ if n == 0 {
+ return 0;
+ }
let mut left = 1usize;
- while left < n && arr[left] >= arr[left-1] {
+ while left < n && arr[left] >= arr[left - 1] {
left += 1;
}
let mut right = 1usize;
- while right < n && arr[n-1-right] <= arr[n-right] {
+ while right < n && arr[n - 1 - right] <= arr[n - right] {
right += 1;
}
let mut best = cmp::min(n - left, n - right);
@@ -71,7 +72,10 @@ impl Solution {
}
pub fn main() {
- println!("{:?}", Solution::find_length_of_shortest_subarray(arg_into(1)));
+ println!(
+ "{:?}",
+ Solution::find_length_of_shortest_subarray(arg_into(1))
+ );
}
#[cfg(test)]
@@ -80,8 +84,14 @@ mod tests {
#[test]
fn test() {
- assert_eq!(Solution::find_length_of_shortest_subarray(vec![1, 2, 3, 10, 4, 2, 3, 5]), 3);
- assert_eq!(Solution::find_length_of_shortest_subarray(vec![5, 4, 3, 2, 1]), 4);
+ assert_eq!(
+ Solution::find_length_of_shortest_subarray(vec![1, 2, 3, 10, 4, 2, 3, 5]),
+ 3
+ );
+ assert_eq!(
+ Solution::find_length_of_shortest_subarray(vec![5, 4, 3, 2, 1]),
+ 4
+ );
assert_eq!(Solution::find_length_of_shortest_subarray(vec![1, 2, 3]), 0);
}
}
diff --git a/src/bin/1671-minimum-number-of-removals-to-make-mountain-array.rs b/src/bin/1671-minimum-number-of-removals-to-make-mountain-array.rs
@@ -65,10 +65,7 @@ impl Solution {
}
pub fn main() {
- println!(
- "{:?}",
- Solution::minimum_mountain_removals(arg_into(1))
- )
+ println!("{:?}", Solution::minimum_mountain_removals(arg_into(1)))
}
#[cfg(test)]
diff --git a/src/bin/2064-minimized-maximum-of-products-distributed-to-any-store.rs b/src/bin/2064-minimized-maximum-of-products-distributed-to-any-store.rs
@@ -57,7 +57,6 @@
/// * 1 <= m <= n <= 105
/// * 1 <= quantities[i] <= 105
///
-
use leetcode::*;
struct Solution {}
@@ -86,7 +85,10 @@ impl Solution {
}
pub fn main() {
- println!("{:?}", Solution::minimized_maximum(arg_into(1), arg_into(2)));
+ println!(
+ "{:?}",
+ Solution::minimized_maximum(arg_into(1), arg_into(2))
+ );
}
#[cfg(test)]
@@ -98,9 +100,21 @@ mod tests {
assert_eq!(Solution::minimized_maximum(6, vec![11, 6]), 3);
assert_eq!(Solution::minimized_maximum(7, vec![15, 10, 10]), 5);
assert_eq!(Solution::minimized_maximum(1, vec![100000]), 100000);
- assert_eq!(Solution::minimized_maximum(15, vec![16, 24, 18, 26, 18, 28, 11, 8, 22, 26, 21, 23]), 24);
- assert_eq!(Solution::minimized_maximum(22, vec![25, 11, 29, 6, 24, 4, 29, 18, 6, 13, 25, 30]), 13);
- assert_eq!(Solution::minimized_maximum(14, vec![7, 8, 22, 13, 26, 21, 4, 5, 23, 2, 2]), 21);
- assert_eq!(Solution::minimized_maximum(20, vec![8, 7, 13, 27, 3, 2, 29, 2, 17, 2, 3, 13]), 9);
+ assert_eq!(
+ Solution::minimized_maximum(15, vec![16, 24, 18, 26, 18, 28, 11, 8, 22, 26, 21, 23]),
+ 24
+ );
+ assert_eq!(
+ Solution::minimized_maximum(22, vec![25, 11, 29, 6, 24, 4, 29, 18, 6, 13, 25, 30]),
+ 13
+ );
+ assert_eq!(
+ Solution::minimized_maximum(14, vec![7, 8, 22, 13, 26, 21, 4, 5, 23, 2, 2]),
+ 21
+ );
+ assert_eq!(
+ Solution::minimized_maximum(20, vec![8, 7, 13, 27, 3, 2, 29, 2, 17, 2, 3, 13]),
+ 9
+ );
}
}
diff --git a/src/bin/2070-most-beautiful-item-for-each-query.rs b/src/bin/2070-most-beautiful-item-for-each-query.rs
@@ -79,13 +79,7 @@ impl Solution {
pub fn main() {
println!(
"{:?}",
- Solution::maximum_beauty(
- arg(1)
- .split("|")
- .map(parse)
- .collect(),
- arg_into(2)
- )
+ Solution::maximum_beauty(arg(1).split("|").map(parse).collect(), arg_into(2))
)
}
diff --git a/src/bin/2257-count-unguarded-cells-in-the-grid.rs b/src/bin/2257-count-unguarded-cells-in-the-grid.rs
@@ -39,7 +39,6 @@
/// * 0 <= coli, colj < n
/// * All the positions in guards and walls are *unique*.
///
-
use leetcode::*;
#[derive(Default, Clone, Copy, PartialEq, Eq)]
@@ -48,7 +47,7 @@ enum State {
Empty,
Guard,
Wall,
- Counted
+ Counted,
}
struct Solution {}
@@ -110,7 +109,10 @@ impl Solution {
pub fn main() {
let guards = arg(3).split("|").map(leetcode::parse).collect();
let walls = arg(4).split("|").map(leetcode::parse).collect();
- println!("{}", Solution::count_unguarded(arg_into(1), arg_into(2), guards, walls));
+ println!(
+ "{}",
+ Solution::count_unguarded(arg_into(1), arg_into(2), guards, walls)
+ );
}
#[cfg(test)]
@@ -119,7 +121,23 @@ mod tests {
#[test]
fn test() {
- assert_eq!(Solution::count_unguarded(4, 6, vec![vec![0,0], vec![1,1], vec![2,3]], vec![vec![0,1],vec![2,2],vec![1,4]]), 7);
- assert_eq!(Solution::count_unguarded(3, 3, vec![vec![1,1]], vec![vec![0,1],vec![1,0],vec![2,1],vec![1,2]]), 4);
+ assert_eq!(
+ Solution::count_unguarded(
+ 4,
+ 6,
+ vec![vec![0, 0], vec![1, 1], vec![2, 3]],
+ vec![vec![0, 1], vec![2, 2], vec![1, 4]]
+ ),
+ 7
+ );
+ assert_eq!(
+ Solution::count_unguarded(
+ 3,
+ 3,
+ vec![vec![1, 1]],
+ vec![vec![0, 1], vec![1, 0], vec![2, 1], vec![1, 2]]
+ ),
+ 4
+ );
}
}
diff --git a/src/bin/2461-maximum-sum-of-distinct-subarrays-with-length-k.rs b/src/bin/2461-maximum-sum-of-distinct-subarrays-with-length-k.rs
@@ -41,7 +41,6 @@
/// * 1 <= k <= nums.length <= 105
/// * 1 <= nums[i] <= 105
///
-
use leetcode::*;
use std::cmp;
@@ -71,7 +70,10 @@ impl Solution {
}
pub fn main() {
- println!("{}", Solution::maximum_subarray_sum(arg_into(1), arg_into(2)));
+ println!(
+ "{}",
+ Solution::maximum_subarray_sum(arg_into(1), arg_into(2))
+ );
}
#[cfg(test)]
@@ -80,7 +82,10 @@ mod tests {
#[test]
fn test() {
- assert_eq!(Solution::maximum_subarray_sum(vec![1,5,4,2,9,9,9], 3), 15);
- assert_eq!(Solution::maximum_subarray_sum(vec![4,4,4], 3), 0);
+ assert_eq!(
+ Solution::maximum_subarray_sum(vec![1, 5, 4, 2, 9, 9, 9], 3),
+ 15
+ );
+ assert_eq!(Solution::maximum_subarray_sum(vec![4, 4, 4], 3), 0);
}
}
diff --git a/src/bin/2463-minimum-total-distance-traveled.rs b/src/bin/2463-minimum-total-distance-traveled.rs
@@ -72,13 +72,18 @@
/// * The input will be generated such that it is always possible to repair
/// every robot.
///
-
use leetcode::*;
struct Solution {}
impl Solution {
- fn dp(robot: &Vec<i32>, factory: &Vec<Vec<i32>>, cache: &mut [i64], ri: usize, fi: usize) -> i64 {
+ fn dp(
+ robot: &Vec<i32>,
+ factory: &Vec<Vec<i32>>,
+ cache: &mut [i64],
+ ri: usize,
+ fi: usize,
+ ) -> i64 {
if ri >= robot.len() {
return 0;
}
@@ -113,7 +118,7 @@ impl Solution {
}
pub fn minimum_total_distance(mut robot: Vec<i32>, mut factory: Vec<Vec<i32>>) -> i64 {
- factory.sort_by(|a,b| a[0].cmp(&b[0]));
+ factory.sort_by(|a, b| a[0].cmp(&b[0]));
robot.sort();
let mut cache: Box<[i64]> = vec![-1; factory.len() * robot.len()].into_boxed_slice();
@@ -122,7 +127,10 @@ impl Solution {
}
pub fn main() {
- println!("{:?}", Solution::minimum_total_distance(arg_into(1), arg(2).split("|").map(parse).collect()))
+ println!(
+ "{:?}",
+ Solution::minimum_total_distance(arg_into(1), arg(2).split("|").map(parse).collect())
+ )
}
#[cfg(test)]
@@ -131,7 +139,13 @@ mod tests {
#[test]
fn test() {
- assert_eq!(Solution::minimum_total_distance(vec![0,4,6], vec![vec![2,2], vec![6,2]]), 4);
- assert_eq!(Solution::minimum_total_distance(vec![1,-1], vec![vec![-2,1], vec![2,1]]), 2);
+ assert_eq!(
+ Solution::minimum_total_distance(vec![0, 4, 6], vec![vec![2, 2], vec![6, 2]]),
+ 4
+ );
+ assert_eq!(
+ Solution::minimum_total_distance(vec![1, -1], vec![vec![-2, 1], vec![2, 1]]),
+ 2
+ );
}
}
diff --git a/src/bin/2516-take-k-of-each-character-from-left-and-right.rs b/src/bin/2516-take-k-of-each-character-from-left-and-right.rs
@@ -32,7 +32,6 @@
/// * s consists of only the letters 'a', 'b', and 'c'.
/// * 0 <= k <= s.length
///
-
use leetcode::*;
struct Solution {}
@@ -45,9 +44,9 @@ impl Solution {
let (mut left, mut right) = (0, 0);
let mut counts = [0usize; 3];
let mut min_count = 0usize;
- let mut answer = n+1;
+ let mut answer = n + 1;
while left < n && min_count < k {
- counts[(nums[left]-0x61) as usize] += 1;
+ counts[(nums[left] - 0x61) as usize] += 1;
left += 1;
min_count = *counts.iter().min().unwrap();
}
@@ -56,10 +55,10 @@ impl Solution {
}
while left > 0 {
left -= 1;
- counts[(nums[left]-0x61) as usize] -= 1;
+ counts[(nums[left] - 0x61) as usize] -= 1;
min_count = *counts.iter().min().unwrap();
while right < n - left && min_count < k {
- counts[(nums[n-1-right]-0x61) as usize] += 1;
+ counts[(nums[n - 1 - right] - 0x61) as usize] += 1;
right += 1;
min_count = *counts.iter().min().unwrap();
}
diff --git a/src/bin/2563-count-the-number-of-fair-pairs.rs b/src/bin/2563-count-the-number-of-fair-pairs.rs
@@ -28,7 +28,6 @@
/// * -109 <= nums[i] <= 109
/// * -109 <= lower <= upper <= 109
///
-
use leetcode::*;
use std::cmp::{self, Ordering};
@@ -38,7 +37,7 @@ impl Solution {
pub fn count_fair_pairs(mut nums: Vec<i32>, lower: i32, upper: i32) -> i64 {
nums.sort_unstable();
let mut left = 0usize;
- let mut right = nums.len()-1;
+ let mut right = nums.len() - 1;
let mut answer = 0usize;
while left < right {
if nums[left] + nums[right] < lower {
@@ -46,10 +45,12 @@ impl Solution {
} else if nums[left] + nums[right] > upper {
right -= 1;
} else {
- let start = nums[left..right].binary_search_by(|element| match element.cmp(&(lower - nums[left])) {
- Ordering::Equal => Ordering::Greater,
- ord => ord
- }).unwrap_err();
+ let start = nums[left..right]
+ .binary_search_by(|element| match element.cmp(&(lower - nums[left])) {
+ Ordering::Equal => Ordering::Greater,
+ ord => ord,
+ })
+ .unwrap_err();
let start = left + cmp::max(1, start);
answer += right - start + 1;
left += 1;
@@ -60,7 +61,10 @@ impl Solution {
}
pub fn main() {
- println!("{:?}", Solution::count_fair_pairs(arg_into(1), arg_into(2), arg_into(3)))
+ println!(
+ "{:?}",
+ Solution::count_fair_pairs(arg_into(1), arg_into(2), arg_into(3))
+ )
}
#[cfg(test)]
diff --git a/src/bin/3254-find-the-power-of-k-size-subarrays-i.rs b/src/bin/3254-find-the-power-of-k-size-subarrays-i.rs
@@ -48,7 +48,6 @@
/// * 1 <= nums[i] <= 105
/// * 1 <= k <= n
///
-
use leetcode::*;
struct Solution {}
@@ -57,15 +56,15 @@ impl Solution {
pub fn results_array(nums: Vec<i32>, k: i32) -> Vec<i32> {
let k = k as usize;
let mut start = 0usize;
- let mut answer = vec![0; nums.len()-k+1];
+ let mut answer = vec![0; nums.len() - k + 1];
for i in 0..nums.len() {
- if i > 0 && nums[i] != nums[i-1]+1 {
+ if i > 0 && nums[i] != nums[i - 1] + 1 {
start = i;
}
if i + 1 >= start + k {
- answer[i+1-k] = nums[i];
+ answer[i + 1 - k] = nums[i];
} else if i + 1 >= k {
- answer[i+1-k] = -1;
+ answer[i + 1 - k] = -1;
}
}
answer
@@ -82,8 +81,17 @@ mod tests {
#[test]
fn test() {
- assert_eq!(Solution::results_array(vec![1,2,3,4,3,2,5], 3), vec![3,4,-1,-1,-1]);
- assert_eq!(Solution::results_array(vec![2,2,2,2,2], 4), vec![-1,-1]);
- assert_eq!(Solution::results_array(vec![3,2,3,2,3,2], 2), vec![-1,3,-1,3,-1]);
+ assert_eq!(
+ Solution::results_array(vec![1, 2, 3, 4, 3, 2, 5], 3),
+ vec![3, 4, -1, -1, -1]
+ );
+ assert_eq!(
+ Solution::results_array(vec![2, 2, 2, 2, 2], 4),
+ vec![-1, -1]
+ );
+ assert_eq!(
+ Solution::results_array(vec![3, 2, 3, 2, 3, 2], 2),
+ vec![-1, 3, -1, 3, -1]
+ );
}
}
diff --git a/src/lib.rs b/src/lib.rs
@@ -1,19 +1,23 @@
-use std::{env, fmt::Debug, str::{self, FromStr}};
+use std::env;
+use std::fmt::Debug;
+use std::str::{self, FromStr};
pub trait Parsable {
fn parse(s: &str) -> Self;
}
impl<T: str::FromStr> Parsable for Vec<T>
-where
- <T as str::FromStr>::Err: Debug {
+where
+ <T as str::FromStr>::Err: Debug,
+{
fn parse(s: &str) -> Vec<T> {
s.split(",")
- .map(|w| {
- w.trim().parse()
- .unwrap_or_else(|_| panic!("Failed to parse element in argument ({s})"))
- })
- .collect()
+ .map(|w| {
+ w.trim()
+ .parse()
+ .unwrap_or_else(|_| panic!("Failed to parse element in argument ({s})"))
+ })
+ .collect()
}
}