当前位置:网站首页>存在重复元素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)排序什么时候大胆用?有序性是个好条件。
参考文献
边栏推荐
- [matlab] numerical calculus and equation solving
- Bilstm and CRF
- The icon is missing. What does the URL come from with the jesssionid?
- SDN系统方法 | 10. SDN的未来
- Swagger implements background interface automation document
- 深度学习网路模型
- 158_模型_Power BI 使用 DAX + SVG 打通制作商业图表几乎所有可能
- SDN系统方法 | 9. 接入网
- 有关均衡----简易版瓶颈模型
- About queryinterface functions
猜你喜欢
![How Jerry used to output a clock source to the outside world [chapter]](/img/ea/161be6416726bcd80bb2823a5f6389.png)
How Jerry used to output a clock source to the outside world [chapter]

Deeply understand and grasp the basic characteristics of digital economy

ASP. Net supermarket convenience store online shopping mall source code, for the surrounding distribution system

Deep understanding of ELF files

SQL Server real time backup library requirements

Unity technical manual - size over lifetime and size by speed

深度学习网路模型

【工作小技巧】刚入职的软件测试工程师怎么快速上手新岗位

Unity technical manual - interference / noise sub module

New characteristics of cultural consumption in the era of digital economy
随机推荐
The performance of the server's four channel memory is improved. How about the performance of the four channel memory
SDN system method | 10 The future of SDN
Uncover ges super large scale graph computing engine hyg: Graph Segmentation
mvdr波束 matlab,mvdr波束形成matlab[通俗易懂]
揭秘GES超大规模图计算引擎HyG:图切分
CONDA modifying a mirror source
Idea global search for Chinese characters [easy to understand]
Are the top ten leading securities companies safe to open accounts
Essential characteristics of convolution operation +textcnn text classification
New characteristics of cultural consumption in the era of digital economy
Is the actual account opening complicated? Is online account opening safe?
什么是算子?
【 NLP 】 in this year's English college entrance examination, CMU delivered 134 high scores with reconstruction pre training, significantly surpassing gpt3
[compilation principle] overview
SQL Server real time backup library requirements
Distributed remote management of distribution room environment
Use of jupyter
力扣每日一题-第27天-561.数组拆分Ⅰ
A simple and easy-to-use graph visualization tool developed recently
[matlab] curve fitting