Skip to content

Commit a0748b3

Browse files
committed
Solve #275
1 parent e76b651 commit a0748b3

File tree

3 files changed

+122
-31
lines changed

3 files changed

+122
-31
lines changed

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,4 @@ mod n0264_ugly_number_ii;
213213
mod n0268_missing_number;
214214
mod n0273_integer_to_english_words;
215215
mod n0274_h_index;
216+
mod n0275_h_index_ii;

src/n0033_search_in_rotated_sorted_array.rs

+54-31
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,73 @@
22
* [33] Search in Rotated Sorted Array
33
*
44
* Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
5-
*
5+
*
66
* (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
7-
*
7+
*
88
* You are given a target value to search. If found in the array return its index, otherwise return -1.
9-
*
9+
*
1010
* You may assume no duplicate exists in the array.
11-
*
11+
*
1212
* Your algorithm's runtime complexity must be in the order of O(log n).
13-
*
13+
*
1414
* Example 1:
15-
*
16-
*
15+
*
16+
*
1717
* Input: nums = [4,5,6,7,0,1,2], target = 0
1818
* Output: 4
19-
*
20-
*
19+
*
20+
*
2121
* Example 2:
22-
*
23-
*
22+
*
23+
*
2424
* Input: nums = [4,5,6,7,0,1,2], target = 3
2525
* Output: -1
26-
*
26+
*
2727
*/
2828
pub struct Solution {}
2929

3030
/*
31-
\
32-
8
33-
7 9
34-
6 1
35-
5 2
36-
3 \
37-
Consider the given array as ring, each time we split the ring and judge which part is the target belong to, then it's ordinary binary search.
38-
*/
31+
\
32+
8
33+
7 9
34+
6 1
35+
5 2
36+
3 \
37+
Consider the given array as ring, each time we split the ring and judge which part is the target belong to, then it's ordinary binary search.
38+
*/
3939

4040
// submission codes start here
4141

4242
impl Solution {
4343
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
4444
let mut size = nums.len();
45-
if size == 0 { return -1 }
45+
if size == 0 {
46+
return -1;
47+
}
4648
let mut base = 0_usize;
4749
while size > 1 {
4850
let half = size / 2;
4951
let mid = base + half;
50-
if nums[mid] == target { return mid as i32 }
52+
if nums[mid] == target {
53+
return mid as i32;
54+
}
5155
// we split the ring to [base..half] & [half+1..base-1]
5256
// if target not in [base..half] ring, move base to select another ring
53-
if !(((nums[base] < nums[mid]) && (target >= nums[base] && target <= nums[mid])) ||
54-
((nums[base] > nums[mid]) && (target >= nums[base] || target <= nums[mid]))) {
57+
if !(((nums[base] < nums[mid]) && (target >= nums[base] && target <= nums[mid]))
58+
|| ((nums[base] > nums[mid]) && (target >= nums[base] || target <= nums[mid])))
59+
{
5560
base = mid;
5661
}
5762
size -= half;
5863
}
59-
if nums[base] == target { base as i32 } else { -1 }
64+
if nums[base] == target {
65+
base as i32
66+
} else {
67+
-1
68+
}
6069
}
6170
}
6271

63-
64-
6572
// submission codes end
6673

6774
#[cfg(test)]
@@ -70,10 +77,26 @@ mod tests {
7077

7178
#[test]
7279
fn test_33() {
73-
assert_eq!(Solution::search(vec![7,8,1,2,3,4,5,6], 2), 3);
74-
assert_eq!(Solution::search(vec![1004,1005,1006,1007,1008,1009,1010,1011,1012,0,1,2,3,4,5,6,7,8], 0), 9);
75-
assert_eq!(Solution::search(vec![1004,1005,1006,1007,1008,1009,1010,1011,1012,0,1,2,3,4,5,6,7,8], 1006), 2);
76-
assert_eq!(Solution::search(vec![4,5,6,7,0,1,2], 3), -1);
80+
assert_eq!(Solution::search(vec![7, 8, 1, 2, 3, 4, 5, 6], 2), 3);
81+
assert_eq!(
82+
Solution::search(
83+
vec![
84+
1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 0, 1, 2, 3, 4, 5, 6, 7, 8
85+
],
86+
0
87+
),
88+
9
89+
);
90+
assert_eq!(
91+
Solution::search(
92+
vec![
93+
1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 0, 1, 2, 3, 4, 5, 6, 7, 8
94+
],
95+
1006
96+
),
97+
2
98+
);
99+
assert_eq!(Solution::search(vec![4, 5, 6, 7, 0, 1, 2], 3), -1);
77100
assert_eq!(Solution::search(vec![], 3), -1);
78101
}
79102
}

src/n0275_h_index_ii.rs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* [275] H-Index II
3+
*
4+
* Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
5+
*
6+
* According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">definition of h-index on Wikipedia</a>: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N - h papers have no more than h citations each."
7+
*
8+
* Example:
9+
*
10+
*
11+
* Input: citations = [0,1,3,5,6]
12+
* Output: 3
13+
* Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
14+
* received 0, 1, 3, 5, 6 citations respectively.
15+
* Since the researcher has 3 papers with at least 3 citations each and the remaining
16+
* two with no more than 3 citations each, her h-index is 3.
17+
*
18+
* Note:
19+
*
20+
* If there are several possible values for h, the maximum one is taken as the h-index.
21+
*
22+
* Follow up:
23+
*
24+
*
25+
* This is a follow up problem to <a href="/problems/h-index/description/">H-Index</a>, where citations is now guaranteed to be sorted in ascending order.
26+
* Could you solve it in logarithmic time complexity?
27+
*
28+
*
29+
*/
30+
pub struct Solution {}
31+
32+
// submission codes start here
33+
34+
impl Solution {
35+
pub fn h_index(citations: Vec<i32>) -> i32 {
36+
if citations.is_empty() { return 0 }
37+
let len = citations.len();
38+
let (mut low, mut high) = (0_usize, len - 1);
39+
while low <= high {
40+
let mid = low + (high - low) / 2;
41+
if citations[mid] >= (len - mid) as i32 {
42+
if mid == 0 { break }
43+
high = mid - 1;
44+
} else {
45+
low = mid + 1;
46+
}
47+
}
48+
(len - low) as i32
49+
}
50+
}
51+
52+
// submission codes end
53+
54+
#[cfg(test)]
55+
mod tests {
56+
use super::*;
57+
58+
#[test]
59+
fn test_275() {
60+
assert_eq!(Solution::h_index(vec![]), 0);
61+
assert_eq!(Solution::h_index(vec![0]), 0);
62+
assert_eq!(Solution::h_index(vec![11, 15]), 2);
63+
assert_eq!(Solution::h_index(vec![1]), 1);
64+
assert_eq!(Solution::h_index(vec![0,1,3,5,6]), 3);
65+
assert_eq!(Solution::h_index(vec![0,4,4,5,6]), 4);
66+
}
67+
}

0 commit comments

Comments
 (0)