20240413 Finished
This commit is contained in:
parent
38eeba1ce7
commit
f0330fdc87
|
@ -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;
|
42
src/problem/p274_h_index.rs
Normal file
42
src/problem/p274_h_index.rs
Normal 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]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user