MarsCode 36

MarsCode 36

不要什么都想动态规划!!!
https://www.marscode.cn/practice/8e0x1wey2333y3?problem_id=7424436653624754220
在这里插入图片描述
在这里插入图片描述
思路:先把价格按照升序排列,然后从价格低到高取出来,同时还要看蘑菇数是否小于m

def solution(s: str, a: list, m: int, k: int) -> int:
    # 将价格和是否含有蘑菇的信息结合起来
    dishes = [(a[i], s[i] == '1') for i in range(len(a))]
    
    # 根据价格排序
    dishes.sort(key=lambda x: x[0])
    
    # 初始化变量
    total_price = 0
    mushroom_count = 0
    selected_count = 0
    
    # 选择菜品
    for price, has_mushroom in dishes:
        if selected_count == k:
            break
        if has_mushroom and mushroom_count < m:
            # 选择含有蘑菇的菜
            total_price += price
            mushroom_count += 1
            selected_count += 1
        elif not has_mushroom:
            # 选择不含蘑菇的菜
            total_price += price
            selected_count += 1
    
    # 检查是否选择了足够的菜品
    if selected_count < k:
        return -1
    
    return total_price

if __name__ == '__main__':
    print(solution("001", [10, 20, 30], 1, 2) == 30)
    print(solution("111", [10, 20, 30], 1, 2) == -1)
    print(solution("0101", [5, 15, 10, 20], 2, 3) == 30)

在这里插入图片描述

使用TreeMap会存在问题

在这里插入图片描述

import java.util.*;

public class Main {
    public static long solution(String s, int[] a, int m, int k) {
        // PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
        // write code here
        //使用TreeMap完成价格排序
        Map<Integer,Character> map=new TreeMap<>();
        for(int i=0;i<a.length;i++){
            map.put(a[i], s.charAt(i));
        }
        int count=0;    //选出的菜品总数
        int count_m=0;    //选出的蘑菇总数
        int price=0;
        for(int i:map.keySet()){
            if(count==k){
                break;
            }
            char c=map.get(i);
            if(c=='1' && count_m<m){
                //如果这道菜有蘑菇,并且还没到蘑菇数量的最大值,那么就把这道菜算进来
                count_m++;
                count++;
                price+=i;
            }
            else if(c=='0'){
                count++;
                price+=i;
            }
        }
        if(count<k) return -1;
        return price;
    }

    public static void main(String[] args) {
        System.out.println(solution("001", new int[]{10, 20, 30}, 1, 2) == 30);
        System.out.println(solution("111", new int[]{10, 20, 30}, 1, 2) == -1);
        System.out.println(solution("0101", new int[]{5, 15, 10, 20}, 2, 3) == 30);
    }
}

修改:改用List

import java.util.*;

public class Main {
    public static long solution(String s, int[] a, int m, int k) {
        // PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
        // write code here
        // 使用List存储菜品信息
        List<int[]> dishes = new ArrayList<>();
        for (int i = 0; i < a.length; i++) {
            dishes.add(new int[]{a[i], s.charAt(i) == '1' ? 1 : 0});
        }
        
        // 根据价格排序
        Collections.sort(dishes, (d1, d2) -> d1[0] - d2[0]);
        
        int count = 0;    // 选出的菜品总数
        int count_m = 0;  // 选出的蘑菇总数
        int price = 0;
        
        for (int[] dish : dishes) {
            if (count == k) {
                break;
            }
            if (dish[1] == 1 && count_m < m) {
                // 如果这道菜有蘑菇,并且还没到蘑菇数量的最大值,那么就把这道菜算进来
                count_m++;
                count++;
                price += dish[0];
            } else if (dish[1] == 0) {
                count++;
                price += dish[0];
            }
        }
        
        if (count < k) return -1;
        return price;
    }

    public static void main(String[] args) {
        System.out.println(solution("001", new int[]{10, 20, 30}, 1, 2) == 30);
        System.out.println(solution("111", new int[]{10, 20, 30}, 1, 2) == -1);
        System.out.println(solution("0101", new int[]{5, 15, 10, 20}, 2, 3) == 30);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值