|
| 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(×tamp); |
| 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 | +} |
0 commit comments