当前位置:网站首页>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
边栏推荐
- 使用DiskGenius拓展系统盘C盘的容量
- Vscode / * * generate function comments
- 使用DiskGenius拓展系統盤C盤的容量
- 微服务介绍
- Sentinel哨兵机制
- [machine learning] case study of college entrance examination prediction based on multiple time series
- Essential characteristics of convolution operation +textcnn text classification
- Article 6:clion:toolchains are not configured configure disable profile
- C语言中%含义
- HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报
猜你喜欢

HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报

Computing architecture of microblog comments

有关均衡----简易版瓶颈模型

Mobile heterogeneous computing technology - GPU OpenCL programming (basic)

Acy100 oil fume concentration online monitor for kitchen oil fume emission in catering industry

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

Article 6:clion:toolchains are not configured configure disable profile
![[compilation principle] overview](/img/af/246750beb72842e83a19e42270c09e.png)
[compilation principle] overview
![[matlab] curve fitting](/img/58/3fdcc4d34e7c7c71b73324517ff69d.png)
[matlab] curve fitting
Qinheng ch583 USB custom hid debugging record
随机推荐
How to judge whether you are suitable for software testing
Accumulation of some common knowledge points
什么是算子?
Vscode / * * generate function comments
158_模型_Power BI 使用 DAX + SVG 打通制作商业图表几乎所有可能
Li Kou daily question - day 27 -561 Array splitting I
中断操作:AbortController学习笔记
Optimization of lazyagg query rewriting in parsing data warehouse
篇5:VS2017搭建QT5.9.9开发环境
QT using SQLite database
Sentinel哨兵机制
Wechat applet reports an error: request:fail URL not in domain list
CONDA modifying a mirror source
What is an operator?
Qt使用SQLITE数据库
Qt产生指定范围内随机数(随机字符串)
BILSTM和CRF的那些事
What is an operator?
How Jerry used to output a clock source to the outside world [chapter]
The performance of the server's four channel memory is improved. How about the performance of the four channel memory