**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' ...
随机推荐
- poj1064 Cable master(二分)
Cable master 求电缆的最大长度(二分法) Description Inhabitants of the Wonderland have decided to hold a region ...
- Centos 7.4 配置Tomcat管理员用户
1,进入Tomcat路径下的conf文件夹 ,编辑tomcat-users.xml文件 2,在<tomcat-users>标签中增加user标签,用户名密码随便填写,roles可根据权限需 ...
- 第四章 Appium真机运行测试用例讲解
-----手机自动化之Appium 手机自动化测试用例虽然可以在模拟器上运行,可是模拟器毕竟和真机还是有区别的.在第二章我们讲到了模拟器上运行测试用例后,我又花了两天的时间,研究了一下真机运行测试用例 ...
- classloader 学习
classloader就是把类文件加载到jvm中供虚拟机使用,先看一个magic小例子: 首先,我定义一个alex/vicky包,然后在这个包内定义一个接口: public interfaceISer ...
- java——巨简陋文本编辑器
String :equals()方法是进行内容比较,而不是引用比较. “==”比较两个变量本身的值,即两个对象在内存中的首地址. Scanner :用Scanner实现字符串的输入有两种方法,一种是n ...
- SQL SERVER linked server Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'
昨天创建了一个View, 这个view是一系列的表达式(CTE)组成,封装了好多的业务逻辑,简化下语句如下 ;with CTE AS( ...) SELECT a.company_id ,b.comp ...
- mysql 死锁解决办法
查询表的时候,发现一圈圈转啊转,就是不出来数据,猜测表被锁住 解决办法 : mysql> show processlist ; mysql> kill 4; 说明 : 4为 i ...
- 2019.03.19 读书笔记 string与stringbuilder的性能
1 string与stringbuilder 并不是stringbuilder任何时候都在性能上占优势,在少量(大约个位数)的字符串时,并不比普通string操作快. string慢的原因不是stri ...
- IOS Intro - NSDictionary and NSMutableDictionary
NSDictionary.NSMutableDictionary的基本用法 1.不可变词典NSDictionary 字典初始化 NSNumber *numObj = [NSNumber numberW ...
- iTween Scale缩放
void Start () { //键值对儿的形式保存iTween所用到的参数 Hashtable args = new Hashtable(); //放大的倍数 args.Add(, , )); / ...