20240413 Finished

This commit is contained in:
jackfiled 2024-04-13 11:15:25 +08:00
parent 38eeba1ce7
commit f0330fdc87
2 changed files with 44 additions and 1 deletions

View File

@ -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;
mod p121_best_time_to_buy_and_sell_stock;
mod p274_h_index;

View File

@ -0,0 +1,42 @@
/**
* [274] H-Index
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn h_index(citations: Vec<i32>) -> 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]));
}
}