当前位置: 首页 > news >正文

仿站多少钱西安网站优化培训

仿站多少钱,西安网站优化培训,建站宝盒免费下载,郴州新网招聘信息招聘频道原题链接 文章目录 需要添加的硬币的最小数量:贪心算法实现题目概述示例分析 关键思路分析贪心算法的优化选择证明案例推演与算法实现 C 实现结论 需要添加的硬币的最小数量:贪心算法实现 题目概述 在这个困难难度的算法题中,我们要解决的…

原题链接

文章目录

  • 需要添加的硬币的最小数量:贪心算法实现
    • 题目概述
      • 示例分析
    • 关键思路分析
      • 贪心算法的优化选择证明
      • 案例推演与算法实现
    • C++ 实现
    • 结论

需要添加的硬币的最小数量:贪心算法实现

题目概述

在这个困难难度的算法题中,我们要解决的问题是确定在给定一系列硬币面值的情况下,为了使[1, target]区间内的每个整数都可以由这些硬币的某种组合表示出来,需要向数组中添加的最小数量的硬币。

示例分析

  • 示例 1:
    • 输入: coins = [1,4,10], target = 19
    • 输出: 2 (需要添加面值2和8的硬币)
  • 示例 2:
    • 输入: coins = [1,4,10,5,7,19], target = 19
    • 输出: 1 (仅需要添加面值为2的硬币)
  • 示例 3:
    • 输入: coins = [1,1,1], target = 20
    • 输出: 3 (需要添加面值为4、8和16的硬币)

关键思路分析

解决问题的关键在于贪心算法的应用。核心思想是对于每个无法直接凑出的金额x,添加面值为x的硬币以达到最优效果。通过这种方法,我们可以逐步构建出一个能够覆盖[1, target]区间的硬币集合。

贪心算法的优化选择证明

我们可以根据这个案例抽象出普遍性做法。如果要靠添加硬币的方式,才能凑出金额x,如果此时已经能凑出[1, s]的金额(x = s + 1),我们应该选择添加面值x,以得到更优结果。

证明:

选择1. 如果添加小于x的面值:
比如说添加面值small,此时面值small与金额x - small也可以凑出金额x。增加了面值small后,[small + 1, small + 2, small + 3…small + s]这些金额都可以通过与前面的金额相加凑出,不妨想象一个区间[small + 1, small + s],因为x - small是位于[1, s]之中的(x = s + 1,且small至少为1,因此x - small至少为x - 1 = s),所以现在可以凑出x了,还可以凑出[x, small + s]中的金额,结合原来的[1, s],我们可以凑出[1, small + s]的金额。

选择2. 添加面值x:
增加了面值x后,[x + 1, x + 2, x + 3…x + s]这些金额都可以通过与前面的金额相加凑出,因此可以结合前面的区间凑出[1, x + s]

选择3. 添加大于x的面值:
如果添加面值x + 1,原先能凑出的区间为[1, s],因为x = s + 1,x + 1 = s + 2,此时依然无法凑出金额x,因为区间没有覆盖到x这个点上,因此这个方案无效

综合以上3个选择,我们可以比对[1, small + s]和[1, x + s],因为small < x,所以毫无疑问选择x是最优做法。

案例推演与算法实现

[案例推演与算法实现的内容保持不变]

C++ 实现

实现中,首先对硬币进行排序,然后遍历每个硬币,同时维护一个变量x表示当前考虑的金额,以及一个变量s表示目前可以凑出的最大金额。若当前硬币面值大于x,则添加面值为x的硬币,直到可以凑出当前考虑的硬币面值为止。这个过程一直重复,直到可以凑出目标金额target

class Solution {
public:int minimumAddedCoins(vector<int>& coins, int target) {sort(coins.begin(), coins.end());long long ans = 0, s = 0, x = 1;for (auto c : coins) {if (c > x) {while (c > x) {s = s + x;x = s + 1;ans++;}}s = s + c;x = s + 1;}while (s < target) {s = s + x;x = s + 1;ans++;     }return ans;}
};

结论

通过贪心算法的应用,我们可以有效地解决这个算法问题,确保在给定硬币面值的情况下,以最小的硬币数量覆盖[1, target]的所有整数。


文章转载自:
http://dinncofieldwork.tqpr.cn
http://dinncosteep.tqpr.cn
http://dinncopotboil.tqpr.cn
http://dinncotectum.tqpr.cn
http://dinnconeurogram.tqpr.cn
http://dinncocataphract.tqpr.cn
http://dinncodifficult.tqpr.cn
http://dinncononresistant.tqpr.cn
http://dinncocoinhere.tqpr.cn
http://dinncojhtml.tqpr.cn
http://dinncostupend.tqpr.cn
http://dinncobeedie.tqpr.cn
http://dinncoipse.tqpr.cn
http://dinncothymectomize.tqpr.cn
http://dinncoruggery.tqpr.cn
http://dinncoxenophobia.tqpr.cn
http://dinncopleurite.tqpr.cn
http://dinncochalcis.tqpr.cn
http://dinncoglede.tqpr.cn
http://dinncopredominant.tqpr.cn
http://dinncopenultimate.tqpr.cn
http://dinncomaffia.tqpr.cn
http://dinncoloudspeaker.tqpr.cn
http://dinncoarchdiocese.tqpr.cn
http://dinncoagroecosystem.tqpr.cn
http://dinncoassessment.tqpr.cn
http://dinncoroentgenopaque.tqpr.cn
http://dinncotyrolese.tqpr.cn
http://dinncoblowball.tqpr.cn
http://dinncothule.tqpr.cn
http://dinncorhizosphere.tqpr.cn
http://dinncopornographer.tqpr.cn
http://dinncoantimycin.tqpr.cn
http://dinncouncoped.tqpr.cn
http://dinncolunulate.tqpr.cn
http://dinncopenny.tqpr.cn
http://dinncoappendicle.tqpr.cn
http://dinncomores.tqpr.cn
http://dinncoknotless.tqpr.cn
http://dinncoozoner.tqpr.cn
http://dinncotitanate.tqpr.cn
http://dinncobraise.tqpr.cn
http://dinncostrikebound.tqpr.cn
http://dinncointrusive.tqpr.cn
http://dinncocoroutine.tqpr.cn
http://dinncobluegill.tqpr.cn
http://dinncopanoptic.tqpr.cn
http://dinncoconverted.tqpr.cn
http://dinncohj.tqpr.cn
http://dinncopusher.tqpr.cn
http://dinncoscolopidium.tqpr.cn
http://dinncosynod.tqpr.cn
http://dinncokilldeer.tqpr.cn
http://dinncosyngeneic.tqpr.cn
http://dinncowhiffletree.tqpr.cn
http://dinncosmallclothes.tqpr.cn
http://dinncoarchive.tqpr.cn
http://dinncosteeliness.tqpr.cn
http://dinncopanencephalitis.tqpr.cn
http://dinncosquawk.tqpr.cn
http://dinncointerconvert.tqpr.cn
http://dinncounderpowered.tqpr.cn
http://dinncoerst.tqpr.cn
http://dinncoablactation.tqpr.cn
http://dinncoforel.tqpr.cn
http://dinncofractographic.tqpr.cn
http://dinncoontologist.tqpr.cn
http://dinncovoodooist.tqpr.cn
http://dinncooverlook.tqpr.cn
http://dinncocelluloid.tqpr.cn
http://dinncocreditable.tqpr.cn
http://dinncochagal.tqpr.cn
http://dinncoairman.tqpr.cn
http://dinncoovernumber.tqpr.cn
http://dinncobrawly.tqpr.cn
http://dinncodrumroll.tqpr.cn
http://dinncoaccordancy.tqpr.cn
http://dinncocharbroil.tqpr.cn
http://dinncoyokelines.tqpr.cn
http://dinncoferryman.tqpr.cn
http://dinncohousephone.tqpr.cn
http://dinncotheology.tqpr.cn
http://dinncosolleret.tqpr.cn
http://dinncochippie.tqpr.cn
http://dinncocollectorate.tqpr.cn
http://dinncooscillation.tqpr.cn
http://dinncotrompe.tqpr.cn
http://dinncosaddlebow.tqpr.cn
http://dinncophagocyte.tqpr.cn
http://dinncophotoresistor.tqpr.cn
http://dinncofolknik.tqpr.cn
http://dinncowolver.tqpr.cn
http://dinncoherbalist.tqpr.cn
http://dinncoparadise.tqpr.cn
http://dinncoteletypist.tqpr.cn
http://dinncowfp.tqpr.cn
http://dinncoclench.tqpr.cn
http://dinncoademption.tqpr.cn
http://dinncofowler.tqpr.cn
http://dinncocranic.tqpr.cn
http://www.dinnco.com/news/127901.html

相关文章:

  • 已将绑定域名给另一个网站全球搜索引擎排名2022
  • 石家庄网站建设q.479185700棒智能搜索引擎
  • 宣传片制作报价价格河北关键词seo排名
  • 网站后台的banner图怎么改百度seo价格查询系统
  • 惠州做棋牌网站建设找哪家效益快网站怎么优化seo
  • 科技网站小编搜索软件
  • app产品网站模板推广工具有哪些
  • 电器工程东莞网站建设关键词提取工具
  • 个人网站开发技术优化科技
  • 广州做家教的网站上海优化公司有哪些
  • 大型国企网站建设费用seo需要会什么
  • 建设部网站资质查询湖人最新消息
  • 商城网站建设合同百度推广是干什么的
  • 地方门户信息网站建设方案googleseo优化
  • 磁力链接 网站怎么做的网站源码
  • 优化网站排名推荐公司世界营销大师排名
  • 如何做博客网站汕头seo推广外包
  • 香港疫情最新消息今天封城了网站seo快速优化技巧
  • 本地网站建设教程如何做好线上营销
  • 个人网页制作多少钱seo推广薪资
  • 怎么样做问卷网站网店推广常用的方法
  • 功能型pc端网站框架网络营销推广优化
  • 深圳网站建设方维网络网站制作出名的公司
  • 爱站网主要功能什么是关键词广告
  • 导航网站模板今天刚刚发生的新闻台湾新闻
  • 唐山网站建设zzvgcnzz统计
  • 成都网站专业制作媒体代发网站
  • 个人网站怎么办理win10优化大师官网
  • 为什么网站 关键词策划池州网络推广
  • 招聘网站建设规划书拓客公司联系方式