当前位置:网站首页>There is a repeating element iii[pruning with ordered ordering]
There is a repeating element iii[pruning with ordered ordering]
2022-06-25 18:03:00 【REN_ Linsen】
Prune with the order after sorting
Preface
When the time complexity is O(NlongN || N2) when , Without special requirements , Sequencing has no effect on time complexity , You can even prune with the order after the order is arranged , Reduce running time .
Not just DFS Can prune , There are many pruning ideas in many operations .
One 、 There are duplicate elements III
Two 、 Sort + prune
package everyday.medium;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
// There are duplicate elements III
public class ContainsNearbyAlmostDuplicate {
/* target: The limited range of a given subscript , Limited range of element difference , Ask whether there is such Number pair . */
// violence
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;
}
}
// Nonviolence , Ingenious explanation : Prioritize , Then prune by order .( notes : Anyway, for O(NlogN || N2) Time complexity of , Put it in order completely for every effect .)
class ContainsNearbyAlmostDuplicate2 {
/* target: The limited range of a given subscript , Limited range of element difference , Ask whether there is such Number pair . */
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]));
// Explosive search + prune
for (int i = 0; i < arr.size() - 1; i++) {
// notes : Actually j = i + 1 Even if the first pruning .
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;
// Similar to pruning .
if (Math.abs((long) a[1] - b[1]) > t) break;
}
}
return false;
}
}
summary
1) Pruning ideas exist in many operations , not only DFS.
2) When to use sorting boldly ? Order is a good condition .
reference
边栏推荐
- The performance of the server's four channel memory is improved. How about the performance of the four channel memory
- CONDA modifying a mirror source
- 解决nvprof 报错ERR_NVGPUCTRPERM - The user does not have permission to profile on the target device.
- Find the longest substring length satisfying the condition
- 微信小程序报错:request:fail url not in domain list
- Expressing integers by the sum of consecutive natural numbers
- New characteristics of cultural consumption in the era of digital economy
- Precautions for the use of Jerry's wake-up mouth [chapter]
- 一些常用的知识点积累
- Part 5:vs2017 build qt5.9.9 development environment
猜你喜欢
为什么在变频器场合需要安科瑞的电力有源滤波器?
沁恒CH583 USB 自定义HID调试记录
What is an operator?
Chapter 4:win10 installing mingw64
Article 6:clion:toolchains are not configured configure disable profile
Virtual machine class loading mechanism
使用DiskGenius拓展系统盘C盘的容量
SQL Server real time backup library requirements
Utilisation de diskgenius pour augmenter la capacité du disque système C
篇6:CLion:Toolchains are not configured Configure Disable profile
随机推荐
Use of jupyter
What is public chain development? What are the public chain development projects?
new TypeReference用法 fastjson[通俗易懂]
HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报
Bert's summary of me
Acy100 oil fume concentration online monitor for kitchen oil fume emission in catering industry
SDN系统方法 | 10. SDN的未来
解析数仓lazyagg查询重写优化
Precautions for using Jerry's timer [chapter]
Part 5:vs2017 build qt5.9.9 development environment
Find the longest substring length satisfying the condition
使用DiskGenius拓展系統盤C盤的容量
Time series analysis of data mining [easy to understand]
Qt产生指定范围内随机数(随机字符串)
图标丢失,URL附带JESSSIONID的什么来的?
启牛的涨乐财付通如何?安全靠谱吗
微博评论的计算架构
jupyter的使用
篇5:VS2017搭建QT5.9.9开发环境
Swagger implements background interface automation document