**611. Valid Triangle Number three pointer O(n^3) -> square(binary search larget number smaller than target)
Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.
Example 1:
Input: [2,2,3,4]
Output: 3
Explanation:
Valid combinations are:
2,3,4 (using the first 2)
2,3,4 (using the second 2)
2,2,3
Note:
- The length of the given array won't exceed 1000.
- The integers in the given array are in the range of [0, 1000].
reference -- https://leetcode.com/problems/valid-triangle-number/solution/
Idea: it is not like permutation problem(for each position, there are many cases to fill in), because we choose some elements from the array.
sort: to check only one case a+b >c instead of three of them
a +b > c and sorting the array
accepted solution 1: binary search
class Solution {
int res = 0;
public int binarySearch(int[] nums, int left, int right, int target){//[]
while(right >= left && right < nums.length ){//stop comdition is right >= left: there is onl one element
int mid = (right - left)/2 + left;
if(nums[mid] >= target){
right = mid -1;
}else {
left = mid + 1;
}
}
return left;// it is on the right of the number we want: real target, left(index)
}
public int triangleNumber(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
for(int i = 0 ; i <= n-3 ; i++){
int a = nums[i];
for(int j = i+1 ; j <= n-2 ; j++){
int b = nums[j];
//System.out.println(j+1);
int index = binarySearch(nums,j+1,n-1, a+b ); //[]// find the largest element smaller than a+b
//System.out.println(index);
//System.out.println(nums[index]);
res = res + index - j-1; //why - 1??
}
}
return res;
}
}
Accepted solution 2 -- O(n^2)
class Solution {
public int triangleNumber(int[] nums) {
int res = 0;
//int n = nums.length;
Arrays.sort(nums);
for(int i = 0; i < nums.length-2; i++){
if(nums[i] == 0) continue;
int k = i + 2; // why here
if(nums[k] == 0) continue;
for(int j = i+1 ; j<nums.length-1; j++){
if(nums[j] == 0) continue;
while(k < nums.length && nums[k]< nums[i]+nums[j] && nums[k]!=0){
k++;// move k
}
res = res + k - j -1;
}
}
return res;
}
}
traverse k and j only n^2 -> square time complexixity
**611. Valid Triangle Number three pointer O(n^3) -> square(binary search larget number smaller than target)的更多相关文章
- leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- LeetCode 611. Valid Triangle Number有效三角形的个数 (C++)
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- 【LeetCode】611. Valid Triangle Number 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...
- 611. Valid Triangle Number
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- 611. Valid Triangle Number三角形计数
[抄题]: 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? [暴力解法]: 全部都用for循环 时间分析: 空间分析: [思维问题 ...
- LeetCode 611. 有效三角形的个数(Valid Triangle Number)
611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...
- Leetcode 之 Valid Triangle Number
611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...
- [LeetCode] Valid Triangle Number 合法的三角形个数
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number
一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...
随机推荐
- Java String 字符串操作小结
// 转载加编辑 -- 21 Apr 2014 1. Java字符串中子串的查找 Java中字符串中子串的查找共有四种方法,如下: 1.int indexOf(String str) :返回第一次出现 ...
- asp.net模板页实现类似jquery中document.ready
模板页先判断是否有方法DocumentReady,有的话就调用 1.模板页 <script type="text/javascript" language="jav ...
- 通过JS,按照原比例控制图片尺寸
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con ...
- Java——socket
Server: import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public cla ...
- AVplayer搭建ftp共享PC端
1.安装FTP服务 2.关闭防火墙 3.添加FTP站点 设置ip时,需要查询本机的ip 本机测试 4.iphone安装AVPlayer,并设置
- Easy Touch 摇感控制人物移动
Easy Touch 摇感控制人物移动 public class joystick : MonoBehaviour { public float Speed; //定义速度 p ...
- 小萝卜控机大师录制脚本(手机app自动化)
手机自动化测试 之前发布过小萝贝控机大师与按键精灵结合实现手机自动化测试的功能,小萝贝控机大师升级了实现了更多手机自动化测试的功能,如下: l 手机功能自动化测试:录制脚本,检查点时点击小萝贝控机大师 ...
- 查看服务器配置信息prtdiag与systeminfo实用命令
UNIX(SUN服务器)bash-2.05# prtdiag -v系统配置: Sun Microsystems sun4u Sun Fire V890系统时钟频率:150 MHz内存大小:3276 ...
- pat1016. Phone Bills (25)
1016. Phone Bills (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A long-di ...
- 项目中使用的spring 注解说明
以前在项目中spring 的依赖注入使用 xml 配置,现在使用 注解(Annotation) 来实现配置. 1声明bean 1.1实例 有类: public class MyBean{ //do s ...