**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' ...
随机推荐
- 2015苏州大学ACM-ICPC集训队选拔赛(1) 1006
取金币 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submissio ...
- sqlserver 索引进阶(下)
参考原文 http://www.cnblogs.com/tjy9999/p/4494799.html 第十级, 索引内部结构 建立索引的目的是加快对表中记录的查找或排序.为表设置索引要付出代价的:一是 ...
- spring bean name生成规则
现象: PVService PVServiceImpl ===>名称就是PVServiceImpl, 首字母没有小写 PageViewServiceImpl ==>名称是pageViewS ...
- Redis未授权访问反弹shell
Redis未授权访问反弹shell 目标主机:10.104.11.178 攻击机:kali 攻击步骤: 1.与目标主机连接 root@kali:~# /usr/redis/redis-cli -h 1 ...
- 时间戳date 命令
时间戳date 命令 [chenxiaohao@iZbp1c0q444r0hp2gq8lhmZ server]$ echo $(date +%Y%m%d) ——————>今天 2018032 ...
- 更换 npm 源国内镜像 cnpm
默认镜像源 https://www.npmjs.com/ 临时改变镜像源 方法一:通过config配置指向国内镜像源 npm config set registry http://registry.c ...
- Python + Selenium 练习篇 - 获取页面所有邮箱
代码如下: # coding=utf-8import re #python中利用正则,需要导入re模块from selenium import webdriverdriver = webdriv ...
- 3d Max 2018安装失败怎样卸载3dsmax?错误提示某些产品无法安装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- PlayMaker Play Sound 和 Audio Play
这两个 Action 都可以播放声音 *Play Sound:只要把声音拖进去就可以: *Audio Play:要求游戏对象要有Audio Source组件.
- 【转】浅谈https\ssl\数字证书
转载请注明出处:http://www.cnblogs.com/P_Chou/archive/2010/12/27/https-ssl-certification.html 全球可信的SSL数字证书申请 ...