Skip to content

Commit

Permalink
leetcode: Add 1534
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 15, 2024
1 parent de18096 commit 7aa18f4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/leetcode/1534.count-good-triplets/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-1534-count-good-triplets"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
4 changes: 4 additions & 0 deletions src/leetcode/1534.count-good-triplets/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
76 changes: 76 additions & 0 deletions src/leetcode/1534.count-good-triplets/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

// Bruteforce
pub fn count_good_triplets1(arr: Vec<i32>, a: i32, b: i32, c: i32) -> i32 {
let len: usize = arr.len();
let mut count: i32 = 0;

// 三层嵌套循环
for i in 0..(len - 2) {
for j in (i + 1)..(len - 1) {
for k in (j + 1)..len {
if (arr[i] - arr[j]).abs() <= a
&& (arr[j] - arr[k]).abs() <= b
&& (arr[i] - arr[k]).abs() <= c
{
count += 1;
}
}
}
}
count
}

// 优化循环
pub fn count_good_triplets2(arr: Vec<i32>, a: i32, b: i32, c: i32) -> i32 {
let len: usize = arr.len();
let mut count: i32 = 0;

// 三层嵌套循环
for i in 0..(len - 2) {
for j in (i + 1)..(len - 1) {
if (arr[i] - arr[j]).abs() > a {
continue;
}

for k in (j + 1)..len {
if (arr[j] - arr[k]).abs() <= b && (arr[i] - arr[k]).abs() <= c {
count += 1;
}
}
}
}
count
}

pub type SolutionFn = fn(Vec<i32>, i32, i32, i32) -> i32;

fn check_solution(func: SolutionFn) {
let arr = vec![3, 0, 1, 1, 9, 7];
assert_eq!(func(arr, 7, 2, 3), 4);

let arr = vec![1, 1, 2, 2, 3];
assert_eq!(func(arr, 0, 0, 1), 0);
}

fn main() {
check_solution(count_good_triplets1);
check_solution(count_good_triplets2);
}

#[cfg(test)]
mod tests {
use super::{check_solution, count_good_triplets1, count_good_triplets2};

#[test]
fn test_count_good_triplets1() {
check_solution(count_good_triplets1);
}

#[test]
fn test_count_good_triplets2() {
check_solution(count_good_triplets2);
}
}

0 comments on commit 7aa18f4

Please sign in to comment.