Skip to content

Commit d64510b

Browse files
committed
Resolve #215
1 parent cc01368 commit d64510b

5 files changed

+104
-3
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ serde_derive = "1.0"
1212

1313
[lib]
1414
doctest = false
15-
test = false
15+
test = true

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,5 @@ mod n0210_course_schedule_ii;
181181
mod n0211_add_and_search_word_data_structure_design;
182182
mod n0212_word_search_ii;
183183
mod n0213_house_robber_ii;
184+
mod n0214_shortest_palindrome;
185+
mod n0215_kth_largest_element_in_an_array;

src/n0213_house_robber_ii.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ mod tests {
6060

6161
#[test]
6262
fn test_213() {
63-
assert_eq!(Solution::rob(vec![2,3,2], 3));
64-
assert_eq!(Solution::rob(vec![1,2,3,1], 4));
63+
assert_eq!(Solution::rob(vec![2,3,2]), 3);
64+
assert_eq!(Solution::rob(vec![1,2,3,1]), 4);
6565
}
6666
}

src/n0214_shortest_palindrome.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* [214] Shortest Palindrome
3+
*
4+
* Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.
5+
*
6+
* Example 1:
7+
*
8+
*
9+
* Input: "aacecaaa"
10+
* Output: "aaacecaaa"
11+
*
12+
*
13+
* Example 2:
14+
*
15+
*
16+
* Input: "abcd"
17+
* Output: "dcbabcd"
18+
*/
19+
pub struct Solution {}
20+
21+
// submission codes start here
22+
23+
impl Solution {
24+
pub fn shortest_palindrome(s: String) -> String {
25+
"".to_owned()
26+
}
27+
}
28+
29+
// submission codes end
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use super::*;
34+
35+
#[test]
36+
fn test_214() {}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* [215] Kth Largest Element in an Array
3+
*
4+
* Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
5+
*
6+
* Example 1:
7+
*
8+
*
9+
* Input: [3,2,1,5,6,4] and k = 2
10+
* Output: 5
11+
*
12+
*
13+
* Example 2:
14+
*
15+
*
16+
* Input: [3,2,3,1,2,4,5,5,6] and k = 4
17+
* Output: 4
18+
*
19+
* Note: <br />
20+
* You may assume k is always valid, 1 &le; k &le; array's length.
21+
*
22+
*/
23+
pub struct Solution {}
24+
25+
// submission codes start here
26+
27+
use std::collections::BinaryHeap;
28+
use std::cmp::Reverse;
29+
impl Solution {
30+
pub fn find_kth_largest(nums: Vec<i32>, k: i32) -> i32 {
31+
let k = k as usize;
32+
let mut heap = BinaryHeap::with_capacity(k);
33+
for &num in nums.iter() {
34+
if heap.len() < k as usize {
35+
heap.push(Reverse(num))
36+
} else if num >= heap.peek().unwrap().0 {
37+
heap.pop();
38+
heap.push(Reverse(num));
39+
}
40+
}
41+
heap.peek().unwrap().0
42+
}
43+
}
44+
45+
// submission codes end
46+
47+
#[cfg(test)]
48+
mod tests {
49+
use super::*;
50+
51+
#[test]
52+
fn test_215() {
53+
assert_eq!(Solution::find_kth_largest(
54+
vec![3, 2, 3, 1, 2, 4, 5, 5, 6],
55+
4
56+
), 4);
57+
assert_eq!(Solution::find_kth_largest(
58+
vec![3,2,1,5,6,4],
59+
2
60+
), 5);
61+
}
62+
}

0 commit comments

Comments
 (0)