Skip to content

Commit fb3d605

Browse files
committed
Solve #219 and #220
1 parent 2ee3234 commit fb3d605

5 files changed

+248
-0
lines changed

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,7 @@ mod n0213_house_robber_ii;
184184
mod n0214_shortest_palindrome;
185185
mod n0215_kth_largest_element_in_an_array;
186186
mod n0216_combination_sum_iii;
187+
mod n0217_contains_duplicate;
188+
mod n0218_the_skyline_problem;
189+
mod n0220_contains_duplicate_iii;
190+
mod n0219_contains_duplicate_ii;

src/n0217_contains_duplicate.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* [217] Contains Duplicate
3+
*
4+
* Given an array of integers, find if the array contains any duplicates.
5+
*
6+
* Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
7+
*
8+
* Example 1:
9+
*
10+
*
11+
* Input: [1,2,3,1]
12+
* Output: true
13+
*
14+
* Example 2:
15+
*
16+
*
17+
* Input: [1,2,3,4]
18+
* Output: false
19+
*
20+
* Example 3:
21+
*
22+
*
23+
* Input: [1,1,1,3,3,4,3,2,4,2]
24+
* Output: true
25+
*
26+
*/
27+
pub struct Solution {}
28+
29+
// submission codes start here
30+
31+
impl Solution {
32+
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
33+
if nums.is_empty() { return false }
34+
let mut nums = nums;
35+
nums.sort_unstable();
36+
let mut prev = nums[0];
37+
for i in 1..nums.len() {
38+
if nums[i] == prev {
39+
return true
40+
}
41+
prev = nums[i]
42+
}
43+
false
44+
}
45+
}
46+
47+
// submission codes end
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
52+
53+
#[test]
54+
fn test_217() {
55+
assert_eq!(Solution::contains_duplicate(vec![1]), false);
56+
assert_eq!(Solution::contains_duplicate(vec![]), false);
57+
assert_eq!(Solution::contains_duplicate(vec![1,2,3,4]), false);
58+
assert_eq!(Solution::contains_duplicate(vec![1,2,3,1]), true);
59+
}
60+
}

src/n0218_the_skyline_problem.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* [218] The Skyline Problem
3+
*
4+
* A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
5+
* <a href="/static/images/problemset/skyline1.jpg" target="_blank"><img alt="Buildings" src="https://assets.leetcode.com/uploads/2018/10/22/skyline1.png" style="max-width: 45%; border-width: 0px; border-style: solid;" /> </a> <a href="/static/images/problemset/skyline2.jpg" target="_blank"> <img alt="Skyline Contour" src="https://assets.leetcode.com/uploads/2018/10/22/skyline2.png" style="max-width: 45%; border-width: 0px; border-style: solid;" /> </a>
6+
*
7+
* The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 &le; Li, Ri &le; INT_MAX, 0 < Hi &le; INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
8+
*
9+
* For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .
10+
*
11+
* The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.
12+
*
13+
* For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].
14+
*
15+
* Notes:
16+
*
17+
*
18+
* The number of buildings in any input list is guaranteed to be in the range [0, 10000].
19+
* The input list is already sorted in ascending order by the left x position Li.
20+
* The output list must be sorted by the x position.
21+
* There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]
22+
*
23+
*
24+
*/
25+
pub struct Solution {}
26+
27+
// submission codes start here
28+
29+
impl Solution {
30+
pub fn get_skyline(buildings: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
31+
vec![]
32+
}
33+
}
34+
35+
// submission codes end
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use super::*;
40+
41+
#[test]
42+
fn test_218() {}
43+
}

src/n0219_contains_duplicate_ii.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* [219] Contains Duplicate II
3+
*
4+
* Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
5+
*
6+
* <div>
7+
* Example 1:
8+
*
9+
*
10+
* Input: nums = <span id="example-input-1-1">[1,2,3,1]</span>, k = <span id="example-input-1-2">3</span>
11+
* Output: <span id="example-output-1">true</span>
12+
*
13+
*
14+
* <div>
15+
* Example 2:
16+
*
17+
*
18+
* Input: nums = <span id="example-input-2-1">[1,0,1,1]</span>, k = <span id="example-input-2-2">1</span>
19+
* Output: <span id="example-output-2">true</span>
20+
*
21+
*
22+
* <div>
23+
* Example 3:
24+
*
25+
*
26+
* Input: nums = <span id="example-input-3-1">[1,2,3,1,2,3]</span>, k = <span id="example-input-3-2">2</span>
27+
* Output: <span id="example-output-3">false</span>
28+
*
29+
* </div>
30+
* </div>
31+
* </div>
32+
*
33+
*/
34+
pub struct Solution {}
35+
36+
// submission codes start here
37+
38+
use std::collections::HashMap;
39+
impl Solution {
40+
pub fn contains_nearby_duplicate(nums: Vec<i32>, k: i32) -> bool {
41+
let mut map = HashMap::new();
42+
for (idx, &num) in nums.iter().enumerate() {
43+
match map.get(&num) {
44+
Some(v) => {
45+
if idx - v <= k as usize {
46+
return true;
47+
}
48+
map.insert(num, idx);
49+
},
50+
None => { map.insert(num, idx); },
51+
}
52+
}
53+
false
54+
}
55+
}
56+
57+
// submission codes end
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn test_219() {
65+
assert_eq!(Solution::contains_nearby_duplicate(vec![1,2,3,1,2,3], 2), false);
66+
assert_eq!(Solution::contains_nearby_duplicate(vec![1,2,3,1,2,3], 3), true);
67+
}
68+
}

src/n0220_contains_duplicate_iii.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* [220] Contains Duplicate III
3+
*
4+
* Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
5+
*
6+
* <div>
7+
* Example 1:
8+
*
9+
*
10+
* Input: nums = <span id="example-input-1-1">[1,2,3,1]</span>, k = <span id="example-input-1-2">3</span>, t = <span id="example-input-1-3">0</span>
11+
* Output: <span id="example-output-1">true</span>
12+
*
13+
*
14+
* <div>
15+
* Example 2:
16+
*
17+
*
18+
* Input: nums = <span id="example-input-2-1">[1,0,1,1]</span>, k = <span id="example-input-2-2">1</span>, t = <span id="example-input-2-3">2</span>
19+
* Output: <span id="example-output-2">true</span>
20+
*
21+
*
22+
* <div>
23+
* Example 3:
24+
*
25+
*
26+
* Input: nums = <span id="example-input-3-1">[1,5,9,1,5,9]</span>, k = <span id="example-input-3-2">2</span>, t = <span id="example-input-3-3">3</span>
27+
* Output: <span id="example-output-3">false</span>
28+
*
29+
* </div>
30+
* </div>
31+
* </div>
32+
*
33+
*/
34+
pub struct Solution {}
35+
36+
// submission codes start here
37+
use std::collections::HashMap;
38+
impl Solution {
39+
pub fn contains_nearby_almost_duplicate(nums: Vec<i32>, k: i32, t: i32) -> bool {
40+
if k < 1 || t < 0 { return false }
41+
let mut map = HashMap::new();
42+
for i in 0..nums.len() {
43+
let remap = nums[i] as i64 - i32::min_value() as i64;
44+
let bucket = remap / (t as i64 + 1);
45+
println!("{} {}", remap, bucket);
46+
if map.contains_key(&bucket)
47+
|| map.get(&(bucket-1)).map_or(false, |v| { remap - v <= t as i64})
48+
|| map.get(&(bucket+1)).map_or(false, |v| { v - remap <= t as i64}) {
49+
return true
50+
}
51+
if i >= k as usize {
52+
let last_bucket = (nums[i - k as usize] as i64 - i32::min_value() as i64) / (t as i64 + 1);
53+
map.remove(&last_bucket);
54+
}
55+
map.insert(bucket, remap);
56+
}
57+
false
58+
}
59+
}
60+
61+
// submission codes end
62+
63+
#[cfg(test)]
64+
mod tests {
65+
use super::*;
66+
67+
#[test]
68+
fn test_220() {
69+
// assert_eq!(Solution::contains_nearby_almost_duplicate(vec![1,5,9,1,5,9], 2, 3), false);
70+
// assert_eq!(Solution::contains_nearby_almost_duplicate(vec![1,2,3,1], 3, 0), true);
71+
assert_eq!(Solution::contains_nearby_almost_duplicate(vec![-1,2147483647], 1 ,2147483647), false);
72+
}
73+
}

0 commit comments

Comments
 (0)