LintCode "Triangle Count"

Should be "Medium" or even "Easy".. Just with a little Greedy.

class Solution {
public:
    /**
     * @param S: A list of integers
     * @return: An integer
     */
    int triangleCount(vector<int> &S) {
        int n = S.size();
        if(n < 3) return 0;
        
        sort(S.begin(), S.end());
        int ret = 0;
        
        for(int e = n - 1; e > 1; e --)
        {
            int s = 0, m = e - 1;
            while(s < m)
            {
                if( (S[s] + S[m]) <= S[e])
                {
                    s ++;
                }
                else
                {
                    ret += m - s;
                    m --;
                }
            }
        }
        
        return ret;
    }
};
原文地址:https://www.cnblogs.com/tonix/p/4896680.html