From f0330fdc87b920a096b21aa9012aafcb1d2043a3 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 13 Apr 2024 11:15:25 +0800 Subject: [PATCH] 20240413 Finished --- src/problem/mod.rs | 3 ++- src/problem/p274_h_index.rs | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/problem/p274_h_index.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 0c44635..359b4e1 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -97,4 +97,5 @@ mod p27_remove_element; mod p80_remove_duplicates_from_sorted_array_ii; mod p169_majority_element; mod p189_rotate_array; -mod p121_best_time_to_buy_and_sell_stock; \ No newline at end of file +mod p121_best_time_to_buy_and_sell_stock; +mod p274_h_index; \ No newline at end of file diff --git a/src/problem/p274_h_index.rs b/src/problem/p274_h_index.rs new file mode 100644 index 0000000..5e5740b --- /dev/null +++ b/src/problem/p274_h_index.rs @@ -0,0 +1,42 @@ +/** + * [274] H-Index + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn h_index(citations: Vec) -> i32 { + let mut h_array = vec![0; 1001]; + + for i in citations { + let i = i as usize; + + h_array[i] += 1; + } + + let mut count = 0; + for i in (0..=1000).rev() { + count += h_array[i]; + + if count >= i { + return i as i32; + } + } + + 0 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_274() { + assert_eq!(3, Solution::h_index(vec![3, 0, 6, 1, 5])); + assert_eq!(1, Solution::h_index(vec![1,3,1])); + } +}