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 < ) return ; sort(S.begin(), S.end());
int ret = ; for(int e = n - ; e > ; e --)
{
int s = , m = e - ;
while(s < m)
{
if( (S[s] + S[m]) <= S[e])
{
s ++;
}
else
{
ret += m - s;
m --;
}
}
} return ret;
}
};
LintCode "Triangle Count"的更多相关文章
- Triangle Count
Given an array of integers, how many three numbers can be found in the array, so that we can build a ...
- LintCode 391: Count Of Airplanes
LintCode 391: Count Of Airplanes 题目描述 给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机? 样例 对于每架飞机的起 ...
- 【Lintcode】382.Triangle Count
题目: Given an array of integers, how many three numbers can be found in the array, so that we can bui ...
- lintcode :Count and Say 报数
题目: 报数 报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数.如下所示: 1, 11, 21, 1211, 111221, ... 1 读作 "one 1" -> ...
- lintcode :Count 1 in Binary 二进制中有多少个1
题目: 二进制中有多少个1 49% 通过 计算在一个 32 位的整数的二进制表式中有多少个 1. 样例 给定 32 (100000),返回 1 给定 5 (101),返回 2 给定 1023 (111 ...
- LintCode: Triangle
C++ 逆推 class Solution { public: /** * @param triangle: a list of lists of integers. * @return: An in ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- [Swift]LeetCode120. 三角形最小路径和 | Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- VMDK镜像迁移到KVM(二)
KVM has the ability to use VMware's .vmdk disk files directly, as long as the disk is wholly contain ...
- 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- javascript判断浏览器的版本
在javascript中直接的使用navigator.userAgent就可以获取当前浏览器的版本等信息,以下是列出来的关于不同浏览器显示的值(Windows.Android.iPhone): IE6 ...
- leetcode 147. Insertion Sort List ----- java
Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...
- leetcode 137. Single Number II ----- java
Given an array of integers, every element appears three times except for one. Find that single one. ...
- Apache脚本路径别名(CGI接口)
CGI:Common Gateway Interface(通用网关接口)使WEB可以跟一个应用程序进行通信,从通信环境中获得结果. CGI是不安全的,需要mod_alias,mod_cgi模块 Scr ...
- Unable to get valid context for root
登陆时报以下错误Unable to get valid context for rootLast login: Wed Jul 24 02:06:01 2013 from 10.64.41.3 单机模 ...
- scala言语基础学习十一
隐式转换 使用隐式转换加强现有的类型的功能-类似于设计模式的装饰模式
- caffe: compile error: Could not open or find file your path~~/resized_data/0 and a total of 2 images .
I0219 14:48:40.965386 31108 net.cpp:76] Memory required for data: 0I0219 14:48:40.965517 31108 layer ...
- java 读取文件的字节数组
/*文件64位编码*/ public static void main(String[] args) { byte[] fileByte = toByteArray(newFile); St ...