当前位置:网站首页>在D天内送达包裹的能力[抽象类二分--抽象判定方式]
在D天内送达包裹的能力[抽象类二分--抽象判定方式]
2022-06-10 20:51:00 【REN_林森】
抽象类二分--抽象判定方式
前言
个人刷题感觉,从基础 -> (基础抽象/数学/脑筋急转弯/dp/高级数据结构/逆向思维/逻辑分析与整理/等) -> 基础灵活组合成困难题 -> 组合抽象。该题很好的诠释了抽象的魅力。
一、在D天内送达包裹的能力

二、抽象二分判定方式
target:顺序将数组截成days个子数组,求里面最大和即承载重量。
而分成days个子数组,要求子数组分配是非常均匀的,意思就是最大值和尽可能小。
----------------------
// 上面思路是以days为参照物,去寻找承载重量x.但是想不出来。(直观法,自己的初始想法,解不出来。)
// 下面的思路是以x为参照物,去寻找days,让days满足要求。(逆向思维法,借鉴官方答案。)
----------------------
承载重量一定是从max(weights)(不能拆分包裹) - sum(weights)(一下装完)的,所以可二分寻找这个承载重量是多少。
当承载重量x分成的子数组为y组时,
情况1:y如果大于days,那么就说明x小了,需要把寻找更大的x,即left = mid + 1
情况2:y如果小于等于days,那么说明x大了,也可能刚好,需要把上边界定在mid,即right = mid
package everyday;
import java.util.Arrays;
// 在D天内送达包裹的能力。
public class ShipWithinDays {
/* target:顺序将数组截成days个子数组,求里面最大和即承载重量。 而分成days个子数组,要求子数组分配是非常均匀的,意思就是最大值和尽可能小。 ---------------------- // 上面思路是以days为参照物,去寻找承载重量x.但是想不出来。(直观法,自己的初始想法,解不出来。) // 下面的思路是以x为参照物,去寻找days,让days满足要求。(逆向思维法,借鉴官方答案。) ---------------------- 承载重量一定是从max(weights)(不能拆分包裹) - sum(weights)(一下装完)的,所以可二分寻找这个承载重量是多少。 当承载重量x分成的子数组为y组时, y如果大于days,那么就说明x小了,需要把寻找更大的x,即left = mid + 1 y如果小于等于days,那么说明x大了,也可能刚好,需要把上边界定在mid,即right = mid */
// 逆向思维 + 二分法灵活变体(变判定方式,超有意思。)变体也称抽象类二分。
public int shipWithinDays(int[] weights, int days) {
// 取数组最小值、数组和,作为二分夹逼寻找的初始左右边界。
int low = Arrays.stream(weights).max().getAsInt();
int high = Arrays.stream(weights).sum();
// 二分寻找最佳承载x
return binarySearch(weights, low, high, days);
}
/** * 二分寻找最佳承载重量x * * @param weights * @param low * @param high * @param days * @return */
private int binarySearch(int[] weights, int low, int high, int days) {
// 二分夹逼寻找。
while (low < high) {
int mid = low + (high - low >>> 1);
// 获取承载重量x = mid时,需要的天数。
int need = 1, sum = 0;
for (int w : weights) {
if ((sum += w) > mid) {
++need;
sum = w;
}
}
// 通过判断所需天数和实际天数的大小,来推断承载重量x是否过大还是过小。
if (need > days) low = mid + 1;
else high = mid;
}
// 夹逼找到最佳承载重量
return low;
}
}
总结
1)抽象基础的魅力,实践–抽象二分判定方式。
2)正向走不通时,试试逆向思维。
3)刷题感觉,从基础 -> (基础抽象/数学/脑筋急转弯/dp/高级数据结构/逆向思维/逻辑分析与整理/等) -> 基础灵活组合成困难题 -> 组合抽象。
参考文献
边栏推荐
- Before we learn about high-performance computing, let's take a look at its history
- Notes to entry: do I need to know programming for O & M?
- Super detailed tutorial for installing mysql8 in centos7 (no pit!)
- JS anchor positioning can extend many functions
- Cordova Plugin /JPush PhoneGap 极光推送_本地推送_消息推送
- 系统重装以及查询系统性能
- How to realize the marketing purpose of small program integral mall
- 在模仿学习中进步的智能机器人
- 【北大青鸟昌平校区】职教与普教协调发展,今年的中考会容易吗?
- 【MySQL】常见数据类型总结
猜你喜欢

Acl2022 | bert2bert: an efficient pre training method of parameter reuse, which significantly reduces the training cost of oversized models
An analysis of SQL query optimization principle (900w+ data from 17s to 300ms)
PHP pseudo protocol implementation command execution details

Notes to entry: do I need to know programming for O & M?

As a programmer, is it really that important for the underlying principles?

北大青鸟昌平校区:高中学历可以学UI吗?
![[nk] Niuke monthly competition 51 f-average question](/img/b3/c36a0032e606f38fdc2f7c4562713c.png)
[nk] Niuke monthly competition 51 f-average question

Introduction to abbexa bacterial genome DNA Kit
一次SQL查询优化原理分析(900W+数据从17s到300ms)

C language -- 7 operators
随机推荐
Naturalspeech model synthetic speech achieves human speech level for the first time in CMOS test
Principle of gravure overprint and factors affecting overprint
数组 只出现一次的数字
How to realize the marketing purpose of small program integral mall
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated
Self made table
磁盘序列号,磁盘ID,卷序列号的区别
Apple zoom! It's done so well
A WPF developed Print dialog box -printdialogx
oc swift 混编
Detailed steps and actual records of SQL server2019 installation (available for hands-on test)
php的exec函数
AI blessing real-time interaction | analysis of zegoavatar facial expression following technology
目标检测相关概念的理解
C language -- 10 first knowledge of structure
ThinkPHP v6.0.x反序列化漏洞复现
How to view the occupied space of a table in MySQL database
C language ---9 first knowledge of macros and pointers
Before we learn about high-performance computing, let's take a look at its history
C语言qsort()函数的使用(详解)