博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode#259] 3Sum Smaller
阅读量:5311 次
发布时间:2019-06-14

本文共 6016 字,大约阅读时间需要 20 分钟。

Problem:

Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.

For example, given nums = [-2, 0, 1, 3], and target = 2.

Return 2. Because there are two triplets which sums are less than 2:

[-2, 0, 1][-2, 0, 3]

Follow up:

Could you solve it in O(n2) runtime?

General Analysis:

This problem is easy, but really tricky!It could force you to consider many detail you have not consider before.First, we need to understand it is difference with 3Sum problem.difference 1: the nums may still have duplicates, but we don't need to care about it, since the problme ask us to "find the number of index triplets i, j", which means as long as the elements are different(not value), we could record it.difference 2: it asks us to find all combinations smaller than "target", which is not the same as "equal", the way of couting answer should be different. I have made following mistakes to figure out the solution:

Wrong Solution 1:

public class Solution {    public int threeSumSmaller(int[] nums, int target) {        if (nums == null)            throw new IllegalArgumentException("nums is null");        List
ret = new ArrayList
(); ret.add(0); Arrays.sort(nums); for (int i = 0; i < nums.length - 2; i++) { twoSumSmaller(nums, i + 1, target - nums[i], ret); } return ret.get(0); } private void twoSumSmaller(int[] nums, int i, int target, List
ret) { int front = i; int end = nums.length - 1; while (front < end) { if (nums[front] + nums[end] < target) { ret.set(0, ret.get(0) + 1); end--; } else{ end--; } } }}

Mistakes Analysis:

Error cases:Input:[3,1,0,-2], 4Output:2Expected:3The above solution almost share the same code structure with 3Sum problem, excpet the condition to add "item" into "ret" list. And we don't need to care about duplicates things.However, the above logic of shirking the available range is wrong for this problem.Recall, for 3Sum Problem, we have,---------------------------------------------------if (nums[front] + nums[end] == target) {    ...    front++ (end--); 
}---------------------------------------------------We can move both front and end pointers. Since "nums[front] + nums[end] == target", whether we move front or end pointer, iff nums[front_new] != nums[front] (which is guaranteed), we still need to move end pointer. Thus, we actually need to move both pointers for possible available ranges, no possible answer would be skipped.But for this problem, it has great difference.Problemetic part:if (nums[front] + nums[end] < target) { ... end--;}For above code, the end may still in available range, but we discard it, thus we lose some answers."nums[front+1] + nums[end] < target" may be possible. Fix:if (nums[front] + nums[end] < target) { ... front++;}

Wrong Solution 2:

public class Solution {    public int threeSumSmaller(int[] nums, int target) {        if (nums == null)            throw new IllegalArgumentException("nums is null");        List
ret = new ArrayList
(); ret.add(0); Arrays.sort(nums); for (int i = 0; i < nums.length - 2; i++) { twoSumSmaller(nums, i + 1, target - nums[i], ret); } return ret.get(0); } private void twoSumSmaller(int[] nums, int i, int target, List
ret) { int front = i; int end = nums.length - 1; while (front < end) { if (nums[front] + nums[end] < target) { ret.set(0, ret.get(0) + 1); front++; } else{ end--; } } }}

Mistakes Analysis:

Error cases:Input:[3,1,0,-2], 4Output:2Expected:3Even though we fix the problem for moving pointer of this problem, we still face the other important problem.The problem with above solution is that: use the same counting method of 3Sum.Problemetic part:if (nums[front] + nums[end] < target) {    ret.set(0, ret.get(0) + 1);    front++;} If "nums[front] + nums[end] < target" is meet, all numbers from {nums[front] to nums[end-1]} must also meet the answer, with front fixed. thus we should have following counting method.ret.set(0, ret.get(0) + end - front);You may wander wheather it would incure duplicates in counting.Nope! since after we move front++, for ceratin "nums[i]", the same "nums[front]" would not appear again.

Analysis:

Ask yourself:This problem actually shares a lot of things with 3Sum. Like "binary search problem", this also represents a new genre of problem.  Taking part of a sorting array. With one element was slected, for (int i = 0; i < nums.length - 2; i++) {    twoSumSmaller(nums, i + 1, target - nums[i], ret);}How could you use the invariant "nums[front] + nums[end] meets certain condition" to control the searching in available ranges and couting right answer!!!It's an art!!! Right!

Solution:

public class Solution {    public int threeSumSmaller(int[] nums, int target) {        if (nums == null)            throw new IllegalArgumentException("nums is null");        List
ret = new ArrayList
(); ret.add(0); Arrays.sort(nums); for (int i = 0; i < nums.length - 2; i++) { twoSumSmaller(nums, i + 1, target - nums[i], ret); } return ret.get(0); } private void twoSumSmaller(int[] nums, int i, int target, List
ret) { int front = i; int end = nums.length - 1; while (front < end) { if (nums[front] + nums[end] < target) { ret.set(0, ret.get(0) + end - front); front++; } else{ end--; } } }}

 

转载于:https://www.cnblogs.com/airwindow/p/4802521.html

你可能感兴趣的文章
[Javascript] Classify JSON text data with machine learning in Natural
查看>>
用户十秒离开你网站的15大原因
查看>>
通信之中的原理--噪声做加密
查看>>
java多线程编程题之连续打印abc的几种解法
查看>>
SUSE Linux Enterprise 11 安装 MySQL笔记
查看>>
Java程序性能优化
查看>>
zoj 1842 Prime Distance
查看>>
Linux 文件属性
查看>>
Oracle基础学习笔记(一)
查看>>
iOS 开发笔记-plist使用
查看>>
BZOJ4013 : [HNOI2015]实验比较
查看>>
界面控件DevExpress发布v18.2.5|附下载
查看>>
【重大更新】DevExpress WinForms v18.2新版亮点(七)
查看>>
Jquery实现让滚动条始终保持在最下方
查看>>
java中三种常见内存溢出错误的处理方法
查看>>
从CPU/OS到虚拟机和云计算
查看>>
Luogu 3960 [NOIP2017] 列队 - splay|线段树
查看>>
输出100以内的质数
查看>>
成长——新的开始,一切都是美好的
查看>>
hosts文件配置参数介绍
查看>>