From e49fe8ee0905d57fe8ec243d0ddc48b2cbfd0439 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 12 Sep 2024 14:13:49 +0800 Subject: [PATCH] 20240912 finished. --- README.md | 16 +++++- justfile | 5 +- src/problem/mod.rs | 3 +- ...nd_the_maximum_number_of_marked_indices.rs | 53 +++++++++++++++++++ 4 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 src/problem/p2576_find_the_maximum_number_of_marked_indices.rs diff --git a/README.md b/README.md index 0a73980..7cb17f0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,17 @@ # LeetCode -运行`cargo run`来初始化题目的模板文件。 +使用Rust完成LeetCode上的题目。 -运行`cargo test test_{id}`来运行指定题目的测试。 +## 使用方法 + +```shell +just pull +``` + +获得指定``的题目。 + +```shell +just commit +``` + +提交目前完成的题目,提交信息的格式为`YYYYMMdd finished.`。 diff --git a/justfile b/justfile index e837ea8..379f5da 100755 --- a/justfile +++ b/justfile @@ -1,6 +1,9 @@ #!/usr/bin/env just --justfile -build: +update: + git pull + +build: update cargo build --release test: diff --git a/src/problem/mod.rs b/src/problem/mod.rs index fab33b1..bc0a92a 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -232,4 +232,5 @@ mod p3177_find_the_maximum_length_of_a_good_subsequence_ii; mod p977_squares_of_a_sorted_array; mod p2181_merge_nodes_in_between_zeros; mod p2552_count_increasing_quadruplets; -mod p2555_maximize_win_from_two_segments; \ No newline at end of file +mod p2555_maximize_win_from_two_segments; +mod p2576_find_the_maximum_number_of_marked_indices; \ No newline at end of file diff --git a/src/problem/p2576_find_the_maximum_number_of_marked_indices.rs b/src/problem/p2576_find_the_maximum_number_of_marked_indices.rs new file mode 100644 index 0000000..9073ae7 --- /dev/null +++ b/src/problem/p2576_find_the_maximum_number_of_marked_indices.rs @@ -0,0 +1,53 @@ +/** + * [2576] Find the Maximum Number of Marked Indices + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn max_num_of_marked_indices(nums: Vec) -> i32 { + let mut nums = nums; + nums.sort(); + let n = nums.len(); + + let check = |k: usize| -> bool { + for i in 0..k { + if nums[i] * 2 > nums[n - k + i] { + return false; + } + } + + true + }; + + let (mut left, mut right) = (0, n / 2); + + while left < right { + let middle = (left + right + 1) / 2; + + if check(middle) { + left = middle; + } else { + right = middle - 1; + } + } + + (left * 2) as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2576() { + assert_eq!(2, Solution::max_num_of_marked_indices(vec![3, 5, 2, 4])); + assert_eq!(4, Solution::max_num_of_marked_indices(vec![9, 2, 5, 4])); + assert_eq!(0, Solution::max_num_of_marked_indices(vec![7, 6, 8])); + } +}