611. Valid Triangle Number

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

My solution:

class Solution {
public:
    int triangleNumber(vector<int>& nums) {
    int pile[2000],count=0;//pile初始化空间要大一些,1005不行
    memset(pile,0,sizeof(pile));
    int x,y,tmp;
    for(int i=0; i<nums.size(); i++)
    {
        tmp=nums[i];
        pile[tmp]++;
    }
    for(int a=0; a<=1000; a++)
    {
        for(int b=a; b<=1000; b++)
        {
            if(pile[a]!=0 && pile[b]!=0)
            {
                for(int c=max(b-a+1,b); c<a+b; c++)
                {
                    if(pile[c]!=0)
                    {
                        if(a==b && a!=c && pile[a]>=2)
                            count+=pile[a]*(pile[a]-1)*pile[c]/2;
                        else if(a!=b && b==c && pile[b]>=2)
                            count+=pile[b]*(pile[b]-1)*pile[a]/2;
                        else if(a==b && a==c && pile[a]>=3)
                            count+=pile[a]*(pile[a]-1)*(pile[a]-2)/6;
                        else if((b-a)*(b-c)*(a-c)!=0)
                            count+=pile[a]*pile[b]*pile[c];
                    }
                }   
            }
        }
    }
    return count;     
    }
};

clean solution:Pointers O(N2)

class Solution {
public:
    int triangleNumber(vector<int>& a) {
        int res = 0;
        sort(a.begin(), a.end());
        reverse(a.begin(), a.end());    // a is decreasing
        for (int i = 0; i + 2 < a.size(); i++) {
            for (int j = i + 1, k = a.size() - 1; j < k; j++) {
                while (j < k && a[j] + a[k] <= a[i]) {
                    k--;
                }
                res += k - j;
            }
        }
        return res;
    }
};

O(N2):

class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        vector<int> snums(nums);
        sort(snums.begin(), snums.end());
        int count = 0;
        for ( int n = nums.size(), k = n - 1; k > 1; --k ) {
            int i = 0, j = k - 1;
            while ( i < j ) {
                // any value x between i...j will satisfy snums[x] + snums[j] > snums[k]
                // and because snums[k] > snums[j] > snums[x] >= 0, they will always satisfy
                // snums[k] + snums[x] > snums[j] and snums[k] + snums[j] > snums[x]
                if ( snums[i] + snums[j] > snums[k] )
                    count += --j - i + 1;
                else
                    ++i;
            }
        }
        return count;
    }
};

// 243 / 243 test cases passed.
// Status: Accepted
// Runtime: 59 ms

基本上好的题解就是,先用sort()排序,然后固定k为三边最大遍历一次,嵌套i从最小值开始遍历,i<j<k,从k-1开始找符合条件的第一个j(也是最大的那个),找到第一个j之和,j到i之间的数都是符合要求的。

i<j<k,三角形三边成立的条件(i+k>j, j+k>i都是显然成立的,所以只需要判断i+j<k即可)

而使用while可以减少一个循环,因为j是从大到小递减, snums[i] + snums[j] > snums[k],如果不满足这个条件,说明i不够大,所以i++,如果满足条件,就要找有没有更小的j也满足,所以j–。(这里很妙,需要理解)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值