rust leetcode 周赛 287

本文分享了参赛者在47分钟竞赛中如何通过优化时间计算、比赛策略调整和代码实现技巧,如二分查找、边界处理和数据结构应用,来提高排名。重点讨论了2224分钟转换、2225赢家筛选和2226糖果分配问题的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模拟竞赛,总时间 47:57,3WA1,4WA1,排名 304/6811,第一名总时间 09:05。

tips:

  1. 二分边界/答案边界需要多考虑一下,懒得考虑就拿特别大的值,last_success 设置对就行。
  2. 调用 unwrap 要当心,option 真的能调用 unwrap 吗?

用处不大的 tips:
3. 定长定格式字符串处理,可以自己快速写处理函数。
4. win/lose 这道题,可以不用注册 win,把 lose 值注册一下就可以,+=0.


2224 转化时间需要的最少操作数

impl Solution {
pub fn convert_time(current: String, correct: String) -> i32 {
    let (h0, m0) = current.split_once(":").unwrap();
    let (h0, m0) = (h0.parse::<i32>().unwrap(), m0.parse::<i32>().unwrap());
    let (h1, m1) = correct.split_once(":").unwrap();
    let (h1, m1) = (h1.parse::<i32>().unwrap(), m1.parse::<i32>().unwrap());

    let mut need = (h1-h0)*60 + (m1-m0);
    let mut ans = 0;
    for unit in [60, 15, 5, 1] {
        ans += need / unit;
        need %= unit;
    }
    ans 
}
}

2225 找出输掉零场或一场比赛的玩家

impl Solution {
pub fn find_winners(matches: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
    use std::collections::BTreeMap;

    let mut win_cnt = BTreeMap::new();
    let mut lose_cnt = BTreeMap::new();
    for mat in matches {
        let (win, lose) = (mat[0], mat[1]);
        win_cnt.entry(win).and_modify(|e| *e += 1).or_insert(1);
        lose_cnt.entry(lose).and_modify(|e| *e += 1).or_insert(1);
    }
    let mut ans = vec![vec![], vec![]];
    for (win, _) in win_cnt {
        if lose_cnt.get(&win).is_none() {
            ans[0].push(win);
        }
    }
    for (lose, cnt) in lose_cnt {
        if cnt == 1 {
            ans[1].push(lose);
        }
    }

    ans
}

}

2226 每个小孩最多能分到多少糖果

impl Solution {
pub fn maximum_candies(candies: Vec<i32>, k: i64) -> i32 {
    let mut left = 1;
    let mut right = *candies.iter().max().unwrap();
    let mut last_success = 0;
    while left <=right {
        let candidate = (left + right)/2;

        let mut cnt = 0;
        for candy in &candies {
            cnt += (candy/candidate) as i64;
        }
        if cnt >= k {
            last_success = candidate;
            left = candidate + 1;
        } else {
            right = candidate - 1;
        }
    }
    last_success
}

}

2227 加密解密字符串

struct Encrypter {
    keys: Vec<char>,
    values: Vec<String>,
    dictionary: Vec<String>,
}


/**
 * `&self` means the method takes an immutable reference.
 * If you need a mutable reference, change it to `&mut self` instead.
 */
impl Encrypter {

    fn new(keys: Vec<char>, values: Vec<String>, dictionary: Vec<String>) -> Self {
        Encrypter { keys, values, dictionary }
    }

    fn encrypt(&self, word1: String) -> String {
        let mut result = String::new();
        for c in word1.chars() {
            // println!("{} {}", c, c as i32);
            let index = self.keys.iter().position(|&x| x == c);
            if index.is_none() {
                return "".to_string();
            }
            result.push_str(&self.values[index.unwrap()]);
        }
        result
    }

    fn decrypt(&self, word2: String) -> i32 {
        let mut res = 0;
        for dic in &self.dictionary {
            if self.encrypt(dic.clone()) == word2 {
                res += 1;
            }
        }
        res
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值