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:

  1. The length of the given array won't exceed 1000.
  2. 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)的更多相关文章

  1. leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)

    这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...

  2. LeetCode 611. Valid Triangle Number有效三角形的个数 (C++)

    题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...

  3. 【LeetCode】611. Valid Triangle Number 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...

  4. 611. Valid Triangle Number

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  5. 611. Valid Triangle Number三角形计数

    [抄题]: 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? [暴力解法]: 全部都用for循环 时间分析: 空间分析: [思维问题 ...

  6. LeetCode 611. 有效三角形的个数(Valid Triangle Number)

    611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...

  7. Leetcode 之 Valid Triangle Number

    611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...

  8. [LeetCode] Valid Triangle Number 合法的三角形个数

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  9. 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' ...

随机推荐

  1. Java8 流的使用示例

    foreach遍历处理 dataList.stream().forEach(index -> sb.append(dataList.get(index) + "',")); ...

  2. spring boot http2

    项目已启动的情况下,启动单元测试报端口has binded 可以使用server.port=-1 禁用端口绑定来启动单元测试 https://docs.spring.io/spring-boot/do ...

  3. gulp不压缩打包layui

    从网上下载的layui都是压缩包,如何打包在一个文件且不压缩呢?如下方法: 1.https://gitee.com/sentsin/layui下载源码(本文的为2.4.5版本) 2.安装nodejs( ...

  4. Gone Fishing

    原题网址 代码已经写出来了,自己测试的时候没有问题,提交上去之后反馈了我一个Runtime error  一口老血啊! 找了半天还是没找到可能越界啊啥的地方 import java.util.Scan ...

  5. 基础 —— ip地址与子网掩码的认识

    目录: 1.IP地址的作用 2.IP地址如何表示 3.IP地址的结构 4.子网掩码 5.IP地址的分类 6.私有IP地址 7.二进制与十进制的转换 8.练习题 IP地址的作用: 在一定范围内,唯一的标 ...

  6. ctrip-apollo

    云端多网卡问题: 参考:https://blog.csdn.net/buyaore_wo/article/details/79847404

  7. 文献综述一:基于UML技术的商品管理系统设计与实现

    一.基本信息 标题:基于UML技术的商品管理系统设计与实现 时间:2018 出版源:福建电脑 文件分类:uml技术的研究 二.研究背景 使用 UML 技术对商品管理系统进行了分析与研究,使用户对商品信 ...

  8. JAVA HTTP请求和HTTPS请求

    HTTP与HTTPS区别:http://blog.csdn.net/lyhjava/article/details/51860215 URL发送 HTTP.HTTPS:http://blog.csdn ...

  9. Django-5.1 模型层 单表操作

    7.1 ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开 ...

  10. bootstrap-datetimepicker:基于twitter bootstrap的日期/时间选择控件

    bootstrap-datetimepicker是一个基于twitter bootstrap的简单日期/时间选择控件. <!DOCTYPE HTML> <html> <h ...