码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode每日一题(2233. Maximum Product After K Increments)


    You are given an array of non-negative integers nums and an integer k. In one operation, you may choose any element from nums and increment it by 1.

    Return the maximum product of nums after at most k operations. Since the answer may be very large, return it modulo 109 + 7. Note that you should maximize the product before taking the modulo.

    Example 1:

    Input: nums = [0,4], k = 5
    Output: 20

    Explanation: Increment the first number 5 times.
    Now nums = [5, 4], with a product of 5 * 4 = 20.
    It can be shown that 20 is maximum product possible, so we return 20.
    Note that there may be other ways to increment nums to have the maximum product.

    Example 2:

    Input: nums = [6,3,3,2], k = 2
    Output: 216

    Explanation: Increment the second number 1 time and increment the fourth number 1 time.
    Now nums = [6, 4, 3, 3], with a product of 6 _ 4 _ 3 * 3 = 216.
    It can be shown that 216 is maximum product possible, so we return 216.
    Note that there may be other ways to increment nums to have the maximum product.

    Constraints:

    • 1 <= nums.length, k <= 105
    • 0 <= nums[i] <= 106

    一句话总结就是每次增加数组中最小的那个数字。之所以要这样做, 我自己的思路是, 每次增加实际对最终答案的贡献是除了当前增加的这个数之外的数组中的其他数的乘积。例如[1, 2, 3, 4, 5], 我增加 1 的话相当于 ans += 2 _ 3 _ 4 _ 5, 增加 2 的话相当与 ans += 1 _ 3 _ 4 _ 5, 增加 3 的话相当与 ans += 1 _ 2 _ 4 * 5, 所以我们要让这个乘积尽可能的大, 自然要把最小的数排除在外, 所以我们只需要每次对数组内最小的数+1 就可以保证最终得到的结果是最大的。


    use std::cmp::Reverse;
    use std::collections::BinaryHeap;
    
    impl Solution {
        pub fn maximum_product(nums: Vec<i32>, mut k: i32) -> i32 {
            let mut heap: BinaryHeap<Reverse<i128>> =
                nums.into_iter().map(|v| Reverse(v as i128)).collect();
            while k > 0 {
                let Reverse(v) = heap.pop().unwrap();
                heap.push(Reverse(v + 1));
                k -= 1;
            }
            let ans = heap.into_iter().fold(1i128, |mut p, Reverse(v)| {
                p *= v;
                p %= 10i128.pow(9) + 7;
                p
            });
            ans as i32
        }
    }
    
  • 相关阅读:
    漏洞扫描工具的编写
    南卡电容笔和欢能哪个更推荐呢?Ipad好用电容笔对比
    IP-guard产品相关端口和服务名称
    自媒体人必看的9个网站,每一个都很实用,值得收藏
    y49.第三章 Kubernetes从入门到精通 -- k8s实战案例(二二)
    MAUI与Blazor共享一套UI,媲美Flutter,实现Windows、macOS、Android、iOS、Web通用UI
    Series (mathematics)
    AMM算法简要理解(Adleman-Mander-Miller Method)
    【LeetCode】【剑指offer】【丑数】
    OpenLayers实战,WebGL图层根据Feature要素的变量动态渲染多种颜色的三角形,适用于大量三角形渲染不同颜色
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126954286
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号