当前位置:网站首页>存在重复元素III[利用排序后的有序性进行剪枝]
存在重复元素III[利用排序后的有序性进行剪枝]
2022-06-25 18:00:00 【REN_林森】
利用排序后的有序性剪枝
前言
当时间复杂度在O(NlongN || N2)时,没有特别要求的情况下,排个序对时间复杂度是毫无影响的,甚至还能拿排完序后的有序性来剪枝,降低运行时间。
不是只有DFS能剪枝,在很多操作中多存在剪枝思想。
一、存在重复元素III

二、排序+剪枝
package everyday.medium;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
// 存在重复元素III
public class ContainsNearbyAlmostDuplicate {
/* target:给定下标的限定范围,元素差值的限定范围,求是否存在这样的 数对。 */
// 暴力
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (Math.abs((long) i - j) <= k
&& Math.abs((long) nums[i] - nums[j]) <= t)
return true;
}
}
return false;
}
}
// 非暴力,巧解:先排序,然后靠着有序来剪枝。(注:反正对于O(NlogN || N2)的时间复杂度,排个序完全每任何影响。)
class ContainsNearbyAlmostDuplicate2 {
/* target:给定下标的限定范围,元素差值的限定范围,求是否存在这样的 数对。 */
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
List<int[]> arr = new ArrayList<>();
int idx = 0;
for (int num : nums) arr.add(new int[]{
idx++, num});
arr.sort(Comparator.comparingInt(o -> o[1]));
// 爆搜 + 剪枝
for (int i = 0; i < arr.size() - 1; i++) {
// 注:其实 j = i + 1也就算第一个剪枝了。
for (int j = i + 1; j < arr.size(); j++) {
int[] a = arr.get(i), b = arr.get(j);
if (Math.abs((long) a[1] - b[1]) <= t
&& Math.abs((long) a[0] - b[0]) <= k) return true;
// 类似剪枝。
if (Math.abs((long) a[1] - b[1]) > t) break;
}
}
return false;
}
}
总结
1)剪枝思想存在很多操作中,不仅仅是DFS。
2)排序什么时候大胆用?有序性是个好条件。
参考文献
边栏推荐
- 解决nvprof 报错ERR_NVGPUCTRPERM - The user does not have permission to profile on the target device.
- Py3.6 and py3.7 installed by CONDA
- IET出席2022世界科技社团发展与治理论坛 为构建国际科技共同体献言献策
- [daily record] - bug encountered during BigDecimal Division
- QT using SQLite database
- Sentinel sentinel mechanism
- Vscode / * * generate function comments
- 股票开户怎么办理?证券开户哪家好 办理开户安全吗
- 实际开户复杂吗?在线开户安全么?
- Distributed remote management of distribution room environment
猜你喜欢
![[matlab] curve fitting](/img/58/3fdcc4d34e7c7c71b73324517ff69d.png)
[matlab] curve fitting

CentOS7 安装 Redis 7.0.2

Intelligent dialog 01 redis installation

Unity technical manual - interference / noise sub module

Unity technical manual - size over lifetime and size by speed
![Precautions for the use of Jerry's wake-up mouth [chapter]](/img/01/3bfba9a486eb7fa3c0a888bb3ea2d2.png)
Precautions for the use of Jerry's wake-up mouth [chapter]

【日常记录】——对BigDecimal除法运算时遇到的Bug

解析数仓lazyagg查询重写优化

Utilisation de diskgenius pour augmenter la capacité du disque système C

Use diskgenius to expand the capacity of system disk C
随机推荐
Qinheng ch583 USB custom hid debugging record
Bert's summary of me
CentOS7 安装 Redis 7.0.2
Acy100 oil fume concentration online monitor for kitchen oil fume emission in catering industry
MVDR beam MATLAB, MVDR beam forming matlab[easy to understand]
Which is better and safer, GF easy gold rush or compass
CONDA modifying a mirror source
篇6:CLion:Toolchains are not configured Configure Disable profile
ASP. Net supermarket convenience store online shopping mall source code, for the surrounding distribution system
微博评论的计算架构
Agent white paper - jointly build agents and create the wisdom of the whole scene | cloud library No.21 recommendation
Video production material website arrangement
哈希竞猜游戏系统开发如何开发?哈希竞猜游戏系统开发应用详情案例及源码
证券公司排名前十手续费最低 办理开户安全吗
container of()函数简介
jupyter的使用
The icon is missing. What does the URL come from with the jesssionid?
Time series analysis of data mining [easy to understand]
Is it safe for a securities company to open an account with the lowest handling fee among the top ten
About Equilibrium - Simplified bottleneck model