3289. 数字小镇中的捣蛋鬼

This commit is contained in:
li-chx 2025-10-31 16:52:13 +08:00
parent 8a581be8b5
commit 292f597d2b
1 changed files with 24 additions and 3 deletions

View File

@ -2,11 +2,32 @@ mod arr;
struct Solution {}
impl Solution {
pub fn min_number_operations(target: Vec<i32>) -> i32 {
target[0] + target.windows(2).map(|x| (x[1] - x[0]).max(0)).sum::<i32>()
pub fn get_sneaky_numbers(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len() - 2;
let xor_all: i32 = nums.iter().enumerate().fold((n ^ (n+1)) as i32, |acc, (i ,&x)| acc ^ x ^ i as i32);
let mut idx = 0;
let mut calc_idx = xor_all;
while true {
if calc_idx % 2 == 1 {
break;
}
calc_idx >>= 1;
idx += 1;
}
let mut ans = vec![0;2];
for (i,&x) in nums.iter().enumerate() {
if i < n{
ans[i >> idx &1 as usize] ^= i as i32;
}
ans[(x >> idx &1) as usize] ^= x;
}
ans
}
}
fn main() {
println!("{:?}", Solution::min_number_operations(vec![3,1,5,4,2]));
println!(
"{:?}",
Solution::get_sneaky_numbers(vec![2,0,2,1,0])
);
}