Skip to content

Commit a742ab5

Browse files
committed
Add more solutions
1 parent 74c0ca9 commit a742ab5

File tree

5 files changed

+358
-3
lines changed

5 files changed

+358
-3
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
LeetCode is a website that has programming-related questions that are designed to be solved in a limited amount of time. This repository is a collection of some of my solutions written in [Rust](https://www.rust-lang.org/).
88

9-
## Solutions (84)
9+
## Solutions (88)
1010
| No. | Title | Solution | Problem | Difficulty |
1111
|:---:|:------|:--------:|:-------:|:----------:|
1212
| 1 | Two Sum | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/two_sum.rs) | [Leetcode](https://leetcode.com/problems/two-sum/) | Easy |
@@ -67,6 +67,7 @@ LeetCode is a website that has programming-related questions that are designed t
6767
| 1013 | Partition Array Into Three Parts With Equal Sum | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/can_three_parts_equal_sum.rs) | [Leetcode](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | Easy |
6868
| 1035 | Uncrossed Lines | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/max_uncrossed_lines.rs) | [Leetcode](https://leetcode.com/problems/uncrossed-lines/) | Medium |
6969
| 1041 | Robot Bounded In Circle | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/amazon/is_robot_bounded.rs) | [Leetcode](https://leetcode.com/problems/robot-bounded-in-circle/submissions/) | Medium |
70+
| 1086 | High Five | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/amazon/high_five.rs) | [Leetcode](https://leetcode.com/problems/high-five/) | Easy |
7071
| 1134 | Armstrong Number | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/is_good_array.rs) | [Leetcode](https://leetcode.com/problems/armstrong-number/) | Easy |
7172
| 1135 | Connecting Cities With Minimum Cost | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/amazon/minimum_cost.rs) | [Leetcode](https://leetcode.com/problems/connecting-cities-with-minimum-cost/) | Medium |
7273
| 1137 | N-th Tribonacci Number | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/tribonacci.rs) | [Leetcode](https://leetcode.com/problems/n-th-tribonacci-number/) | Easy |
@@ -92,4 +93,7 @@ LeetCode is a website that has programming-related questions that are designed t
9293
| 2024 | Maximize the Confusion of an Exam | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/biweekly_62.rs) | [Leetcode](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | Medium |
9394
| 2025 | Maximum Number of Ways to Partition an Array | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/biweekly_62.rs) | [Leetcode](https://leetcode.com/problems/maximum-number-of-ways-to-partition-an-array/) | Hard |
9495
| 2027 | Minimum Moves to Convert String | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/weekly_261.rs) | [Leetcode](https://leetcode.com/problems/minimum-moves-to-convert-string/) | Easy |
95-
| 2028 | Find Missing Observations | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/weekly_261.rs) | [Leetcode](https://leetcode.com/problems/find-missing-observations/) | Medium |
96+
| 2028 | Find Missing Observations | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/weekly_261.rs) | [Leetcode](https://leetcode.com/problems/find-missing-observations/) | Medium |
97+
| 2032 | Two Out of Three | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/weekly_262.rs) | [Leetcode](https://leetcode.com/problems/two-out-of-three/) | Easy |
98+
| 2033 | Minimum Operations to Make a Uni-Value Grid | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/weekly_262.rs) | [Leetcode](https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/) | Medium |
99+
| 2034 | Stock Price Fluctuation | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/contest/weekly_262.rs) | [Leetcode](https://leetcode.com/problems/stock-price-fluctuation/) | Medium |

src/leetcode/contest/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mod biweekly_62;
2-
mod weekly_261;
2+
mod weekly_261;
3+
mod weekly_262;

src/leetcode/contest/weekly_262.rs

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
use std::collections::BinaryHeap;
2+
use std::{
3+
collections::{HashMap, HashSet},
4+
iter::FromIterator,
5+
};
6+
7+
// 2032. Two Out of Three, Easy
8+
// https://leetcode.com/problems/two-out-of-three/
9+
impl Solution1 {
10+
pub fn two_out_of_three(nums1: Vec<i32>, nums2: Vec<i32>, nums3: Vec<i32>) -> Vec<i32> {
11+
let mut count_map = HashMap::<i32, i32>::new();
12+
13+
let set1 = nums1.iter().collect::<HashSet<&i32>>();
14+
let set2 = nums2.iter().collect::<HashSet<&i32>>();
15+
let set3 = nums3.iter().collect::<HashSet<&i32>>();
16+
17+
for num1 in set1.iter() {
18+
count_map.entry(**num1).and_modify(|n| *n += 1).or_insert(1);
19+
}
20+
for num2 in set2.iter() {
21+
count_map.entry(**num2).and_modify(|n| *n += 1).or_insert(1);
22+
}
23+
for num3 in set3.iter() {
24+
count_map.entry(**num3).and_modify(|n| *n += 1).or_insert(1);
25+
}
26+
27+
let mut ans = vec![];
28+
for (k, v) in count_map.iter() {
29+
if *v >= 2 {
30+
ans.push(*k);
31+
}
32+
}
33+
34+
ans
35+
}
36+
}
37+
38+
// 2033. Minimum Operations to Make a Uni-Value Grid, Medium
39+
// https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/
40+
impl Solution2 {
41+
pub fn min_operations(grid: Vec<Vec<i32>>, x: i32) -> i32 {
42+
let mut nums = grid.iter().flatten().map(|n| *n).collect::<Vec<i32>>();
43+
44+
let min = nums.iter().min().unwrap();
45+
for i in 0..nums.len() {
46+
if (nums[i] - min) % x != 0 {
47+
return -1;
48+
}
49+
}
50+
51+
nums.sort_unstable();
52+
let median = nums[nums.len() / 2];
53+
54+
let mut ans = 0;
55+
for i in 0..nums.len() {
56+
ans += i32::abs(nums[i] - median) / x;
57+
}
58+
ans
59+
}
60+
}
61+
62+
struct StockPrice {
63+
prices: Vec<i32>,
64+
last: (i32, i32), // (time, price)
65+
prices_map: HashMap<i32, i32>,
66+
}
67+
68+
// 2034. Stock Price Fluctuation, Medium
69+
// https://leetcode.com/problems/stock-price-fluctuation/
70+
impl StockPrice {
71+
fn new() -> Self {
72+
StockPrice {
73+
prices: Vec::<i32>::new(),
74+
last: (0, 0),
75+
prices_map: HashMap::new(),
76+
}
77+
}
78+
79+
fn update(&mut self, timestamp: i32, price: i32) {
80+
if timestamp >= self.last.0 {
81+
self.last.0 = timestamp;
82+
self.last.1 = price;
83+
}
84+
85+
let old_price = self.prices_map.get(&timestamp);
86+
if let Some(old_price) = old_price {
87+
let pos = self.prices.binary_search(old_price).unwrap_or_else(|e| e);
88+
self.prices.remove(pos);
89+
}
90+
91+
let pos = self.prices.binary_search(&price).unwrap_or_else(|e| e);
92+
self.prices.insert(pos, price);
93+
94+
self.prices_map.entry(timestamp).and_modify(|n| *n = price).or_insert(price);
95+
}
96+
97+
fn current(&self) -> i32 {
98+
self.last.1
99+
}
100+
101+
fn maximum(&mut self) -> i32 {
102+
*self.prices.last().unwrap_or(&0)
103+
}
104+
105+
fn minimum(&mut self) -> i32 {
106+
*self.prices.first().unwrap_or(&0)
107+
}
108+
}
109+
110+
struct Solution1 {}
111+
struct Solution2 {}
112+
113+
#[cfg(test)]
114+
115+
mod tests {
116+
use super::*;
117+
use crate::{vec_string, vec_vec_i32};
118+
119+
#[test]
120+
fn test_two_out_of_three() {
121+
assert_eq!(Solution1::two_out_of_three(vec![1, 1, 3, 2], vec![2, 3], vec![3]).len(), 2);
122+
}
123+
124+
#[test]
125+
fn test_min_operations() {
126+
assert_eq!(Solution2::min_operations(vec_vec_i32![[2, 4], [6, 8]], 2), 4);
127+
}
128+
129+
#[test]
130+
fn test_min_operations2() {
131+
assert_eq!(Solution2::min_operations(vec_vec_i32![[1, 5], [2, 3]], 1), 5);
132+
}
133+
134+
#[test]
135+
fn test_min_operations3() {
136+
assert_eq!(Solution2::min_operations(vec_vec_i32![[1, 2], [3, 4]], 2), -1);
137+
}
138+
139+
#[test]
140+
fn test_min_operations4() {
141+
assert_eq!(Solution2::min_operations(vec_vec_i32![[146]], 86), 0);
142+
}
143+
144+
#[test]
145+
fn test_min_operations5() {
146+
assert_eq!(Solution2::min_operations(vec_vec_i32![[931, 128], [639, 712]], 73), 12);
147+
}
148+
149+
#[test]
150+
fn test_min_operations6() {
151+
assert_eq!(
152+
Solution2::min_operations(vec_vec_i32![[980, 476, 644, 56], [644, 140, 812, 308], [812, 812, 896, 560], [728, 476, 56, 812]], 84),
153+
45
154+
);
155+
}
156+
157+
#[test]
158+
fn test_min_operations7() {
159+
assert_eq!(
160+
Solution2::min_operations(
161+
vec_vec_i32![
162+
[454, 328, 160, 286, 664],
163+
[496, 538, 748, 244, 286],
164+
[34, 244, 454, 706, 790],
165+
[496, 538, 832, 958, 328],
166+
[370, 874, 370, 874, 286]
167+
],
168+
42
169+
),
170+
122
171+
);
172+
}
173+
174+
#[test]
175+
fn test_min_operations8() {
176+
assert_eq!(
177+
Solution2::min_operations(
178+
vec_vec_i32![
179+
[596, 904, 960, 232, 120, 932, 176],
180+
[372, 792, 288, 848, 960, 960, 764],
181+
[652, 92, 904, 120, 680, 904, 120],
182+
[372, 960, 92, 680, 876, 624, 904],
183+
[176, 652, 64, 344, 316, 764, 316],
184+
[820, 624, 848, 596, 960, 960, 372],
185+
[708, 120, 456, 92, 484, 932, 540]
186+
],
187+
28
188+
),
189+
473
190+
);
191+
}
192+
193+
#[test]
194+
fn test_stock_price() {
195+
let mut obj = StockPrice::new();
196+
obj.update(1, 1);
197+
let ret_2: i32 = obj.current();
198+
let ret_3: i32 = obj.maximum();
199+
let ret_4: i32 = obj.minimum();
200+
assert_eq!(ret_2, 1);
201+
assert_eq!(ret_3, 1);
202+
assert_eq!(ret_4, 1);
203+
}
204+
}
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use std::collections::{BinaryHeap, HashMap};
2+
3+
// 1086. High Five, Easy
4+
// https://leetcode.com/problems/high-five/
5+
impl Solution {
6+
pub fn high_five(items: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
7+
// items[i] = [IDi, scorei]
8+
let mut five_map = HashMap::<i32, BinaryHeap<i32>>::new();
9+
10+
let mut ans: Vec<Vec<i32>> = vec![];
11+
for item in items.iter() {
12+
let [id, score] = [item[0], item[1]];
13+
if five_map.contains_key(&id) {
14+
five_map.entry(id).and_modify(|h| h.push(-score));
15+
} else {
16+
five_map.insert(id, BinaryHeap::from(vec![-score]));
17+
}
18+
19+
if five_map.get_mut(&id).unwrap().len() > 5 {
20+
five_map.get_mut(&id).unwrap().pop();
21+
}
22+
}
23+
24+
five_map.iter().for_each(|(id, heap)| {
25+
let sum = -heap.iter().sum::<i32>();
26+
ans.push(vec![*id, sum / heap.len() as i32])
27+
});
28+
29+
ans.sort_unstable_by_key(|f| f[0]);
30+
31+
ans
32+
}
33+
}
34+
35+
struct Solution {}
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use super::*;
40+
use crate::vec_vec_i32;
41+
42+
#[test]
43+
fn test_high_five() {
44+
assert_eq!(
45+
Solution::high_five(vec_vec_i32![
46+
[1, 91],
47+
[1, 92],
48+
[2, 93],
49+
[2, 97],
50+
[1, 60],
51+
[2, 77],
52+
[1, 65],
53+
[1, 87],
54+
[1, 100],
55+
[2, 100],
56+
[2, 76]
57+
]),
58+
vec_vec_i32![[1, 87], [2, 88]]
59+
);
60+
}
61+
62+
#[test]
63+
fn test_high_five2() {
64+
assert_eq!(
65+
Solution::high_five(vec_vec_i32![
66+
[1, 100],
67+
[7, 100],
68+
[1, 100],
69+
[7, 100],
70+
[1, 100],
71+
[7, 100],
72+
[1, 100],
73+
[7, 100],
74+
[1, 100],
75+
[7, 100]
76+
]),
77+
vec_vec_i32![[1, 100], [7, 100]]
78+
);
79+
}
80+
}
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// 201. Bitwise AND of Numbers Range, Medium
2+
// https://leetcode.com/problems/bitwise-and-of-numbers-range/
3+
impl Solution {
4+
pub fn range_bitwise_and(left: i32, right: i32) -> i32 {
5+
let [mut left, mut right] = [left, right];
6+
let mut shifts = 0;
7+
8+
while left < right {
9+
left >>= 1;
10+
right >>= 1;
11+
shifts += 1;
12+
}
13+
14+
left << shifts
15+
}
16+
17+
pub fn range_bitwise_and_slow(left: i32, right: i32) -> i32 {
18+
let mut ans = left;
19+
20+
if left < right {
21+
((left + 1)..=right).for_each(|num| {
22+
println!("{:?}", num);
23+
ans &= num;
24+
});
25+
}
26+
27+
ans
28+
}
29+
}
30+
31+
struct Solution {}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
use super::*;
36+
37+
#[test]
38+
fn test_range_bitwise_and() {
39+
assert_eq!(Solution::range_bitwise_and(5, 7), 4);
40+
}
41+
42+
#[test]
43+
fn test_range_bitwise_and2() {
44+
assert_eq!(Solution::range_bitwise_and(2147483647, 2147483647), 2147483647);
45+
}
46+
47+
#[test]
48+
fn test_range_bitwise_and3() {
49+
assert_eq!(Solution::range_bitwise_and(1, 2), 0);
50+
}
51+
52+
#[test]
53+
fn test_range_bitwise_and_slow() {
54+
assert_eq!(Solution::range_bitwise_and(5, 7), 4);
55+
}
56+
57+
#[test]
58+
fn test_range_bitwise_and_slow2() {
59+
assert_eq!(Solution::range_bitwise_and(2147483647, 2147483647), 2147483647);
60+
}
61+
62+
#[test]
63+
fn test_range_bitwise_and_slow3() {
64+
assert_eq!(Solution::range_bitwise_and(1, 2), 0);
65+
}
66+
}

0 commit comments

Comments
 (0)