当前位置:网站首页>Leetcode689 wrong greedy thinking
Leetcode689 wrong greedy thinking
2022-06-12 09:04:00 【Little wish】
The title is to find 3 A length of k Non overlapping subarrays of , And the biggest .
Wrong thinking :
At first, it was understood as non coincidence , As long as there is one difference, there are different subarrays . So first we use the sliding window to generate all the data with the length of k The sum of subarrays of , Choose the largest one 3 individual .
Later, it was found that there was no overlap , Want to use greed , First select the largest subinterval , Then choose the non overlapping and the largest . Knowing that this does not necessarily guarantee an optimal solution , Try to choose before 10 Large subinterval , Then select the largest non overlapping subinterval from the subinterval smaller than it .
The code is as follows
private boolean chongDie(int b, List<Integer> a, int k) {
for (int t : a) {
if (Math.abs(b - t) < k) {
return true;
}
}
return false;
}
public int[] maxSumOfThreeSubarrays2(int[] nums, int k) {
long cur = 0L;
List<Pair<Long, Integer>> list = new ArrayList<>();
for (int i = 0; i < k; i++) {
cur += nums[i];
}
list.add(new Pair<>(cur, 0));
for (int i = k; i < nums.length; i++) {
cur -= nums[i - k];
cur += nums[i];
list.add(new Pair<>(cur, i - k + 1));
}
System.out.println(list);
list.sort(new Comparator<Pair<Long, Integer>>() {
@Override
public int compare(Pair<Long, Integer> o1, Pair<Long, Integer> o2) {
if (o1.getKey().equals(o2.getKey())) {
if (o1.getValue() < o2.getValue()) {
return -1;
}
} else if (o1.getKey() < o2.getKey()) {
return 1;
} else if (o1.getKey() > o2.getKey()) {
return -1;
}
return 0;
}
});
System.out.println(list);
long sum = 0L;
int[] ans = new int[3];
for (int st = 0; st < Math.min(10, list.size()); st++) {
cur = 0L;
List<Integer> res = new ArrayList<>();
res.add(list.get(st).getValue());
cur += list.get(st).getKey();
for (int i = st + 1; i < list.size(); i++) {
if (!chongDie(list.get(i).getValue(), res, k)) {
cur += list.get(i).getKey();
res.add(list.get(i).getValue());
if (res.size() == 3) {
break;
}
}
}
if (cur > sum) {
Collections.sort(res);
for (int i = 0; i < 3; i++) {
ans[i] = res.get(i);
}
sum = cur;
}
}
return ans;
}
For input
[17,7,19,11,1,19,17,6,13,18,2,7,12,16,16,18,9,3,19,5]
6
My output is
[0,6,12]
The correct output is
[0,6,13]
Look at the , The length is k The subinterval of is as follows :
[74=0, 74=1, 73=2, 67=3, 74=4, 75=5, 63=6, 58=7, 68=8, 71=9, 71=10, 78=11, 74=12, 81=13, 70=14]
[81=13, 78=11, 75=5, 74=0, 74=1, 74=4, 74=12, 73=2, 71=9, 71=10, 70=14, 68=8, 67=3, 63=6, 58=7]
My program didn't choose 13, It chose the first and longest one 12.
Because election 13, The last choice is greed , The final choice is 13、5, The optimal solution is still not guaranteed . So my program chose 13 There is no answer to the question .
边栏推荐
- Filters and listeners
- Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
- 域名映射到指定IP
- 128. 最长连续序列-哈希表
- Detailed explanation of iSCSI (V) -- actual operation of iSCSI client configuration
- 抓取屏幕与毛玻璃效果
- 《MATLAB 神经网络43个案例分析》:第7章 RBF网络的回归--非线性函数回归的实现
- Exists usage in SQL
- Union selector
- (十四)InputField逻辑分析
猜你喜欢
![[character set 7] what are the wide character codes and multi byte codes of Chinese characters](/img/8c/6d375d90234e6094b6930c2cefefa1.png)
[character set 7] what are the wide character codes and multi byte codes of Chinese characters

ip、DNS、域名、URL、hosts

Building a cluster: and replacing with error

利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包

Handling abnormal data

43 cas d'analyse du réseau neuronal MATLAB: chapitre 7 régression du réseau RBF - - réalisation de la régression fonctionnelle non linéaire
![[untitled] task3 multiple recall](/img/84/81ada5dd7afac62c31a52e743547e9.png)
[untitled] task3 multiple recall

最少换乘次数

Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low

Chapter 3 registers (memory access)
随机推荐
最少换乘次数
Cookies and sessions
机器学习笔记 - 循环神经网络备忘清单
Knowledge points of 2022 system integration project management engineer examination: project cost management
Chapter 3 registers (memory access)
MySQL - Import / export operation
CodeCraft-22 and Codeforces Round #795 (Div. 2) 题解
(13) Text rendering text
Background attribute compound writing
MySQL learning records -- III. MySQL query statements
Application method of new version UI of idea + use method of non test qualification and related introduction
(十三)文本渲染Text
[computer use] how to change a computer disk into a mobile disk?
Consumer configuration related
Wechat applet image saving function
2022 low voltage electrician retraining question bank and online simulation examination
(十四)InputField逻辑分析
Description of string
[data storage] storage of floating point data in memory
(js)三位用逗号隔开,保留两位小数(or 四舍五入取整)