|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +// 2022. Convert 1D Array Into 2D Array, Easy |
| 4 | +// https://leetcode.com/problems/convert-1d-array-into-2d-array/ |
| 5 | +impl Solution1 { |
| 6 | + pub fn construct2_d_array(original: Vec<i32>, m: i32, n: i32) -> Vec<Vec<i32>> { |
| 7 | + // check space |
| 8 | + if original.len() != m as usize * n as usize { |
| 9 | + return vec![]; |
| 10 | + } |
| 11 | + |
| 12 | + let mut ans = vec![vec![0; n as usize]; m as usize]; |
| 13 | + let mut idx = 0; |
| 14 | + for a in original { |
| 15 | + ans[idx / n as usize][idx % n as usize] = a; |
| 16 | + idx += 1; |
| 17 | + } |
| 18 | + |
| 19 | + ans |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// 2023. Number of Pairs of Strings With Concatenation Equal to Target, Medium |
| 24 | +// https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/ |
| 25 | +impl Solution2 { |
| 26 | + pub fn num_of_pairs(nums: Vec<String>, target: String) -> i32 { |
| 27 | + let mut ans = 0; |
| 28 | + for i in 0..nums.len() { |
| 29 | + for j in 0..nums.len() { |
| 30 | + if i != j { |
| 31 | + if [nums[i].clone(), nums[j].clone()].concat() == target { |
| 32 | + ans += 1; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + ans as i32 |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// 2024. Maximize the Confusion of an Exam, Medium |
| 43 | +// https://leetcode.com/problems/maximize-the-confusion-of-an-exam/ |
| 44 | +impl Solution3 { |
| 45 | + pub fn max_consecutive_answers(answer_key: String, k: i32) -> i32 { |
| 46 | + let nums: Vec<i32> = answer_key.chars().map(|c| if c == 'F' { 1 } else { 0 }).collect(); |
| 47 | + |
| 48 | + let [mut l, mut r] = [0, 0]; |
| 49 | + let mut uses = 0; |
| 50 | + let n = nums.len(); |
| 51 | + let mut ans = 0; |
| 52 | + |
| 53 | + while r < n { |
| 54 | + if nums[r] == 0 { |
| 55 | + uses += 1; |
| 56 | + } |
| 57 | + while uses > k { |
| 58 | + if nums[l] == 0 { |
| 59 | + uses -= 1; |
| 60 | + } |
| 61 | + l += 1; |
| 62 | + } |
| 63 | + |
| 64 | + ans = ans.max(r - l); |
| 65 | + r += 1; |
| 66 | + } |
| 67 | + |
| 68 | + r = 0; |
| 69 | + l = 0; |
| 70 | + uses = 0; |
| 71 | + |
| 72 | + while r < n { |
| 73 | + if nums[r] == 1 { |
| 74 | + uses += 1; |
| 75 | + } |
| 76 | + while uses > k { |
| 77 | + if nums[l] == 1 { |
| 78 | + uses -= 1; |
| 79 | + } |
| 80 | + l += 1; |
| 81 | + } |
| 82 | + |
| 83 | + ans = ans.max(r - l); |
| 84 | + r += 1; |
| 85 | + } |
| 86 | + |
| 87 | + ans as i32 + 1 |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// 2025. Maximum Number of Ways to Partition an Array, Hard |
| 92 | +// https://leetcode.com/problems/maximum-number-of-ways-to-partition-an-array/ |
| 93 | +impl Solution4 { |
| 94 | + pub fn ways_to_partition(nums: Vec<i32>, k: i32) -> i32 { |
| 95 | + let mut pre_calcs = Vec::<(i32, i32, usize)>::new(); |
| 96 | + |
| 97 | + let mut part1 = 0; |
| 98 | + let mut part2 = nums.iter().sum::<i32>(); |
| 99 | + for i in 0..nums.len() { |
| 100 | + part1 = part1 + nums[i]; |
| 101 | + part2 = part2 - nums[i]; |
| 102 | + |
| 103 | + pre_calcs.push((part1, part2, i)); |
| 104 | + } |
| 105 | + |
| 106 | + let mut max = 0; |
| 107 | + for i in 0..nums.len() { |
| 108 | + let mut curr = 0; |
| 109 | + for pre_calc in pre_calcs.iter() { |
| 110 | + if i <= pre_calc.2 && pre_calc.0 - nums[i] + k == pre_calc.1 { |
| 111 | + curr += 1; |
| 112 | + } else if i >= pre_calc.2 && pre_calc.0 == pre_calc.1 - nums[i] + k { |
| 113 | + curr += 1; |
| 114 | + } else if pre_calc.0 == pre_calc.1 { |
| 115 | + curr += 1; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + max = max.max(curr - 1); |
| 120 | + } |
| 121 | + |
| 122 | + max |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +struct Solution1 {} |
| 127 | +struct Solution2 {} |
| 128 | +struct Solution3 {} |
| 129 | +struct Solution4 {} |
| 130 | + |
| 131 | +#[cfg(test)] |
| 132 | +mod tests { |
| 133 | + use super::*; |
| 134 | + use crate::{vec_string, vec_vec_i32}; |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn test_construct2_d_array() { |
| 138 | + assert_eq!(Solution1::construct2_d_array(vec![1, 2, 3, 4], 2, 2), vec_vec_i32![[1, 2], [3, 4]]); |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn test_construct2_d_array2() { |
| 143 | + assert_eq!(Solution1::construct2_d_array(vec![1, 2, 3], 1, 3), vec_vec_i32![[1, 2, 3]]); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn test_construct2_d_array3() { |
| 148 | + assert_eq!(Solution1::construct2_d_array(vec![1, 2], 1, 1), vec_vec_i32![]); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn test_construct2_d_array4() { |
| 153 | + assert_eq!(Solution1::construct2_d_array(vec![3], 1, 2), vec_vec_i32![]); |
| 154 | + } |
| 155 | + |
| 156 | + #[test] |
| 157 | + fn test_num_of_pairs() { |
| 158 | + assert_eq!(Solution2::num_of_pairs(vec_string!["777", "7", "77", "77"], "7777".to_string()), 4); |
| 159 | + } |
| 160 | + |
| 161 | + #[test] |
| 162 | + fn test_num_of_pairs2() { |
| 163 | + assert_eq!(Solution2::num_of_pairs(vec_string!["123", "4", "12", "34"], "1234".to_string()), 2); |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn test_num_of_pairs3() { |
| 168 | + assert_eq!(Solution2::num_of_pairs(vec_string!["1", "1", "1"], "11".to_string()), 6); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn test_max_consecutive_answers() { |
| 173 | + assert_eq!(Solution3::max_consecutive_answers("TTFF".to_string(), 2), 4); |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn test_max_consecutive_answers2() { |
| 178 | + assert_eq!(Solution3::max_consecutive_answers("TFFT".to_string(), 1), 3); |
| 179 | + } |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn test_max_consecutive_answers3() { |
| 183 | + assert_eq!(Solution3::max_consecutive_answers("TTFTTFTT".to_string(), 1), 5); |
| 184 | + } |
| 185 | + |
| 186 | + #[test] |
| 187 | + fn test_max_consecutive_answers4() { |
| 188 | + assert_eq!( |
| 189 | + Solution3::max_consecutive_answers( |
| 190 | + "FTFFTFTFTTFTTFTTFFTTFFTTTTTFTTTFTFFTTFFFFFTTTTFTTTTTTTTTFTTFFTTFTFFTTTFFFFFTTTFFTTTTFTFTFFTTFTTTTTTF".to_string(), |
| 191 | + 32 |
| 192 | + ), |
| 193 | + 85 |
| 194 | + ); |
| 195 | + } |
| 196 | + |
| 197 | + #[test] |
| 198 | + fn test_ways_to_partition() { |
| 199 | + assert_eq!(Solution4::ways_to_partition(vec![2, -1, 2], 3), 1); |
| 200 | + } |
| 201 | + |
| 202 | + #[test] |
| 203 | + fn test_ways_to_partition2() { |
| 204 | + assert_eq!(Solution4::ways_to_partition(vec![0, 0, 0], 1), 2); |
| 205 | + } |
| 206 | + |
| 207 | + #[test] |
| 208 | + fn test_ways_to_partition4() { |
| 209 | + assert_eq!( |
| 210 | + Solution4::ways_to_partition( |
| 211 | + vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30827, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 212 | + 0 |
| 213 | + ), |
| 214 | + 33 |
| 215 | + ); |
| 216 | + } |
| 217 | + |
| 218 | + #[test] |
| 219 | + fn test_ways_to_partition5() { |
| 220 | + assert_eq!( |
| 221 | + Solution4::ways_to_partition( |
| 222 | + vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -93166, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 93166, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 223 | + 69428 |
| 224 | + ), |
| 225 | + 37 |
| 226 | + ); |
| 227 | + } |
| 228 | +} |
0 commit comments