当前位置:网站首页>存在重复元素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)排序什么时候大胆用?有序性是个好条件。
参考文献
边栏推荐
- Vscode automatically generates ifndef define ENDIF of header file
- Li Kou daily question - day 27 -561 Array splitting I
- Qi v1.2.4协议 之 定频调压方案
- Precautions for the use of Jerry's wake-up mouth [chapter]
- CGI connects to database through ODBC
- Unity technical manual - size over lifetime and size by speed
- container of()函数简介
- 一些常用的知识点积累
- Utilisation de diskgenius pour augmenter la capacité du disque système C
- Precautions for using Jerry's timer [chapter]
猜你喜欢

CentOS7 安装 Redis 7.0.2

SDN系统方法 | 10. SDN的未来
![[matlab] data interpolation](/img/b8/d7e1a5f7c6f56c8312a1fb5d517ac6.png)
[matlab] data interpolation

使用DiskGenius拓展系统盘C盘的容量

【 NLP 】 in this year's English college entrance examination, CMU delivered 134 high scores with reconstruction pre training, significantly surpassing gpt3

Operating steps for installing CUDA in win10 (continuous improvement)

Bilstm and CRF

Using QT to make a beautiful login interface box
![Jerry's ADC_ get_ Incorrect voltage value obtained by voltage function [chapter]](/img/7a/9c4f4f800c3142ffc279b70354a0bc.png)
Jerry's ADC_ get_ Incorrect voltage value obtained by voltage function [chapter]

移动端异构运算技术 - GPU OpenCL 编程(基础篇)
随机推荐
BILSTM和CRF的那些事
Is it convenient to open a stock account? Is online account opening safe?
The performance of the server's four channel memory is improved. How about the performance of the four channel memory
About Equilibrium - Simplified bottleneck model
卷积操作的本质特性+TextCNN文本分类
【 NLP 】 in this year's English college entrance examination, CMU delivered 134 high scores with reconstruction pre training, significantly surpassing gpt3
mysql mysql-8.0.19-winx64 安装与navicat连接
CONDA modifying a mirror source
How Jerry used to output a clock source to the outside world [chapter]
Vscode / * * generate function comments
深度学习网路模型
股票开户找人办比较方便么?在线开户安全么?
VSCode 自动生成头文件的#ifndef #define #endif
启牛的涨乐财付通如何?安全靠谱吗
MVDR beam MATLAB, MVDR beam forming matlab[easy to understand]
Bert's summary of me
How to judge whether you are suitable for software testing
Garbage collector and memory allocation strategy
Use diskgenius to expand the capacity of system disk C
Jerry's addition of encrypted file playback function [chapter]