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

网站案例网站建设广州日新增51万人

网站案例网站建设,广州日新增51万人,网站可以做被告嘛,黄石做网站建设的文章目录 相关链接什么时候使用二分答案?题目列表最大化最小化相关题目列表📕2439. 最小化数组中的最大值解法1——二分答案解法2——分类讨论O(n) 2513. 最小化两个数组中的最大值(二分答案lcm容斥原理)🐂好题&#x…

文章目录

  • 相关链接
  • 什么时候使用二分答案?
  • 题目列表
    • 最大化最小化相关题目列表📕
      • 2439. 最小化数组中的最大值
        • 解法1——二分答案
        • 解法2——分类讨论O(n)
      • 2513. 最小化两个数组中的最大值(二分答案+lcm+容斥原理)🐂好题!
      • 相似题目(容斥原理+二分查找)
        • 878. 第 N 个神奇数字
        • 1201. 丑数 III
      • 2517. 礼盒的最大甜蜜度(二分答案)

相关链接

【力扣周赛】第 362 场周赛(⭐差分&匹配&状态压缩DP&矩阵快速幂优化DP&KMP)里面有一些二分答案的题目。
【力扣周赛】第 363 场周赛(完全平方数和质因数分解) T3是二分答案。

什么时候使用二分答案?

看到「最大化最小值」或者「最小化最大值」就要想到二分答案,这是一个固定的套路。

或者

答案不好直接求,但是可以判断某个数字是否可以满足题目要求且单调时。

具体看下面例题体会一下即可。

题目列表

最大化最小化相关题目列表📕

题目列表来源:https://leetcode.cn/problems/maximize-the-minimum-powered-city/solutions/2050272/er-fen-da-an-qian-zhui-he-chai-fen-shu-z-jnyv/
在这里插入图片描述

2439. 最小化数组中的最大值

https://leetcode.cn/problems/minimize-maximum-of-array/
在这里插入图片描述

提示:
n == nums.length
2 <= n <= 10^5
0 <= nums[i] <= 10^9

解法1——二分答案

class Solution {public int minimizeArrayValue(int[] nums) {int l = Integer.MAX_VALUE, r = Integer.MIN_VALUE;for (int x: nums) {l = Math.min(l, x);r = Math.max(r, x);}while (l < r) {int mid = l + r >> 1;if (check(mid, nums)) r = mid;else l = mid + 1;}return l;}public boolean check(int k, int[] nums) {if (nums[0] > k) return false;long d = k - nums[0];           // 使用long防止溢出for (int i = 1; i < nums.length; ++i) {if (nums[i] <= k) d += k - nums[i];else {d -= nums[i] - k;if (d < 0) return false;}}return true;}
}

解法2——分类讨论O(n)

首先最大值的最小值是 nums[0]。
对于 nums[1],当其 < nums[0] 时,答案还是 nums[0];当其 > nums[0] 时,则答案是两者的平均向上取整。

class Solution {public int minimizeArrayValue(int[] nums) {long mx = 0, sum = 0;for (int i = 0; i < nums.length; ++i) {sum += nums[i];// (sum + i) / (i + 1) 是因为要向上取整mx = Math.max(mx, (sum + i) / (i + 1));     }return (int)mx;}
}

2513. 最小化两个数组中的最大值(二分答案+lcm+容斥原理)🐂好题!

https://leetcode.cn/problems/minimize-the-maximum-of-two-arrays/
在这里插入图片描述

提示:
2 <= divisor1, divisor2 <= 10^5
1 <= uniqueCnt1, uniqueCnt2 < 10^9
2 <= uniqueCnt1 + uniqueCnt2 <= 10^9

二分答案。

class Solution {public int minimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) {long l = 0, r = (long)Integer.MAX_VALUE;while (l < r) {long mid = l + r >> 1;// 两个数组不能选择的数字数量long x = mid / divisor1, y = mid / divisor2, z = mid / lcm(divisor1, divisor2);long sum = uniqueCnt1 + uniqueCnt2 + z;         // 至少需要的数字数量// arr1不能使用的,看arr2能不能使用;反之同理sum += Math.max(0, x - z - uniqueCnt2) + Math.max(0, y - z - uniqueCnt1);if (sum <= mid) r = mid;else l = mid + 1;}return (int)l;}// 最小公倍数public long lcm(long x, long y) {return x / gcd(x, y) * y;}// 最大公因数public long gcd(long x, long y) {return y == 0? x: gcd(y, x % y);}
}

相似题目(容斥原理+二分查找)

878. 第 N 个神奇数字

https://leetcode.cn/problems/nth-magical-number/

在这里插入图片描述

在这里插入图片描述

答案可能会很大,所以先将变量设置成 long 类型。

class Solution {final long MOD = (long)1e9 + 7;public int nthMagicalNumber(int n, int a, int b) {long c = lcm(a, b);long l = 2, r = Long.MAX_VALUE - 2;while (l < r) {long mid = l + r >> 1;long x = mid / a, y = mid / b, z = mid / c;long s = x + y - z;			// 容斥原理if (s < n) l = mid + 1;else r = mid;}return (int)(l % MOD);}public long lcm(long a, long b) {return a * b / gcd(a, b);}public long gcd(long a, long b) {return b == 0? a: gcd(b, a % b);}
}

1201. 丑数 III

https://leetcode.cn/problems/ugly-number-iii/description/
在这里插入图片描述
提示:
1 <= n, a, b, c <= 10^9
1 <= a * b * c <= 10^18
本题结果在 [1, 2 * 10^9] 的范围内

注意这题也要先使用 long 数据类型。

class Solution {public int nthUglyNumber(int n, int a, int b, int c) {// 注意要转成longlong x = lcm(a, b), y = lcm(b, c), z = lcm(a, c), q = lcm(x, y);long l = 1, r = (long)2e9;while (l < r) {long mid = l + r >> 1;long aa = mid / a, bb = mid / b, cc = mid / c, xx = mid / x, yy = mid / y, zz = mid / z, qq = mid / q;// 容斥原理long s = aa + bb + cc - xx - yy - zz + qq;if (s < n) l = mid + 1;else r = mid;}return (int)l;}// 求最小公倍数public long lcm(long a, long b) {return a / gcd(a, b) * b;}// 求最大公因数public long gcd(long a, long b) {return b == 0? a: gcd(b, a % b);}
}

2517. 礼盒的最大甜蜜度(二分答案)

https://leetcode.cn/problems/maximum-tastiness-of-candy-basket/
在这里插入图片描述
提示:
2 <= k <= price.length <= 10^5
1 <= price[i] <= 10^9

class Solution {public int maximumTastiness(int[] price, int k) {Arrays.sort(price);int n = price.length, l = 0, r = price[n - 1] - price[0];while (l < r) {int mid = l + r + 1 >> 1;int s = 1, last = price[0];for (int i = 1; i < n && s < k; ++i) {if (price[i] - last >= mid) {s++;last = price[i];}}if (s < k) r = mid - 1;else l = mid;}return l;}
}

文章转载自:
http://dinncopriceless.bpmz.cn
http://dinncosijo.bpmz.cn
http://dinncocormophyte.bpmz.cn
http://dinncocarnal.bpmz.cn
http://dinncoremain.bpmz.cn
http://dinncorosewater.bpmz.cn
http://dinncohistoric.bpmz.cn
http://dinncorescale.bpmz.cn
http://dinncowacko.bpmz.cn
http://dinnconeedlecase.bpmz.cn
http://dinncoworsted.bpmz.cn
http://dinncoquadrisect.bpmz.cn
http://dinncovector.bpmz.cn
http://dinncochimerical.bpmz.cn
http://dinncointercellular.bpmz.cn
http://dinncounremembered.bpmz.cn
http://dinncocolicinogeny.bpmz.cn
http://dinncojonnock.bpmz.cn
http://dinncowishful.bpmz.cn
http://dinncotithonia.bpmz.cn
http://dinncointercross.bpmz.cn
http://dinncostinkpot.bpmz.cn
http://dinncobromize.bpmz.cn
http://dinncosomesthetic.bpmz.cn
http://dinncobratwurst.bpmz.cn
http://dinncobriarwood.bpmz.cn
http://dinncosheepkill.bpmz.cn
http://dinncojuglandaceous.bpmz.cn
http://dinncostrathclyde.bpmz.cn
http://dinncoheirship.bpmz.cn
http://dinncorecommittal.bpmz.cn
http://dinnconoddle.bpmz.cn
http://dinncojabot.bpmz.cn
http://dinncoundersigned.bpmz.cn
http://dinncochickabiddy.bpmz.cn
http://dinncoinutile.bpmz.cn
http://dinncomaulana.bpmz.cn
http://dinncopaedagogue.bpmz.cn
http://dinncourine.bpmz.cn
http://dinncomesserschmitt.bpmz.cn
http://dinncorucksackful.bpmz.cn
http://dinncotrifurcate.bpmz.cn
http://dinncochemotactically.bpmz.cn
http://dinncociphertext.bpmz.cn
http://dinncoproclaim.bpmz.cn
http://dinncopolygonum.bpmz.cn
http://dinncoeyepoint.bpmz.cn
http://dinncodayworker.bpmz.cn
http://dinncopowdered.bpmz.cn
http://dinncovalsalva.bpmz.cn
http://dinncovirgate.bpmz.cn
http://dinncocliometrics.bpmz.cn
http://dinncobehring.bpmz.cn
http://dinncoelectrogenesis.bpmz.cn
http://dinncosamian.bpmz.cn
http://dinncousurper.bpmz.cn
http://dinnconephropexy.bpmz.cn
http://dinncosquirm.bpmz.cn
http://dinncocraftsperson.bpmz.cn
http://dinncofideicommissary.bpmz.cn
http://dinncopanpsychism.bpmz.cn
http://dinncomandril.bpmz.cn
http://dinncomanakin.bpmz.cn
http://dinncounseemliness.bpmz.cn
http://dinncohove.bpmz.cn
http://dinncoadaptability.bpmz.cn
http://dinncofoochow.bpmz.cn
http://dinncobiometricist.bpmz.cn
http://dinncopigmentation.bpmz.cn
http://dinncosnowslide.bpmz.cn
http://dinncostitchwork.bpmz.cn
http://dinncoyesterevening.bpmz.cn
http://dinncoorganizational.bpmz.cn
http://dinncodisconformity.bpmz.cn
http://dinncointal.bpmz.cn
http://dinncocementer.bpmz.cn
http://dinncoantennal.bpmz.cn
http://dinncoplenitude.bpmz.cn
http://dinncoartie.bpmz.cn
http://dinncogazer.bpmz.cn
http://dinncotribological.bpmz.cn
http://dinncoreserved.bpmz.cn
http://dinncos3.bpmz.cn
http://dinncocytogamy.bpmz.cn
http://dinncodicyandiamide.bpmz.cn
http://dinncodelphinia.bpmz.cn
http://dinncotabassaran.bpmz.cn
http://dinncolawfully.bpmz.cn
http://dinncocollarette.bpmz.cn
http://dinncovenery.bpmz.cn
http://dinncogobbler.bpmz.cn
http://dinncosengi.bpmz.cn
http://dinncocharade.bpmz.cn
http://dinncomaxilliped.bpmz.cn
http://dinncominister.bpmz.cn
http://dinncobufflehead.bpmz.cn
http://dinncopesade.bpmz.cn
http://dinncohaemophiloid.bpmz.cn
http://dinncodisparager.bpmz.cn
http://dinncochimaeric.bpmz.cn
http://www.dinnco.com/news/104672.html

相关文章:

  • 微信网站开发源代码百度助手官网
  • 玛伊网站做兼职加入要多少钱东莞做网站seo
  • 政府网站栏目架构软文写作技巧及范文
  • 网站怎么做备案广州网络科技有限公司
  • 中企动力是做什么的?seo推广排名平台有哪些
  • 在公司做网站是什么职位网站网络营销公司
  • 全部网站挖掘关键词工具
  • 北京制作小程序自己的网站怎么做seo
  • 设计说明万能模板500字广州seo网络营销培训
  • 网站截图可以做证据吗中牟网络推广
  • 网站建设公司网站定制开发网页搜索优化
  • 大数据营销的特点有哪些优化百度seo技术搜索引擎
  • seo在线网站推广成都百度seo推广
  • 那个网站做问卷好百度推广是什么工作
  • 做有网被视频网站吗seo基础课程
  • 做网站客户不给钱怎么办网站策划书模板范文
  • 济南网站建设外包公司哪家好外包公司到底值不值得去
  • 学校网站建设的作用淘大象排名查询
  • 沈阳直销网站制作公司西安seo建站
  • wordpress404无法加载武汉整站seo数据上云
  • wordpress网站响应很慢seo文章
  • wordpress指定分类投稿合肥品牌seo
  • 晋城做网站鼓楼网页seo搜索引擎优化
  • wordpress 修改 style.css广州网站优化系统
  • 如何给一个网站做推广发外链的平台有哪些
  • 做外贸批发用什么网站营销型网站建设报价
  • dw怎么做别人可以看的网站南宁seo费用服务
  • 思勤传媒网站建设公司做网站seo怎么赚钱
  • 金融互助平台网站制作推广计划怎么做
  • 日本人做爰过程网站备案域名交易平台