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 ...
随机推荐
- 使用innerHTML生成的script节点不会发出请求与执行text属性
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Qt之QPropertyAnimation
简述 QPropertyAnimation类定义了Qt的属性动画. QPropertyAnimation以Qt属性做差值,作为属性值存储在QVariants中,该类继承自QVariantAnimati ...
- [转载]新手入门:Spring的一些学习方法及意见
原文地址:新手入门:Spring的一些学习方法及意见作者:飞扬飞扬xyz Spring简介: 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您 ...
- crontab 安装 和一些 简单的命令
crontab命 令常见于Unix和Linux的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供 之后读取和执行.通常,crontab ...
- CSS基础知识点(二)——居中
水平居中 (1) 对于块级元素,最常用的自适应水平居中为:margin:0px auto; (与 margin:auto; 效果相同) (2) 对于行内元素(a, img, input等),最常用的水 ...
- WindowsServer问题总结
1.System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志.不可访问的日志: Security.在安装的“回滚”阶段发生异常.将忽略该异常并继续回 ...
- linux 网络协议分析---3
本章节主要介绍linxu网络模型.以及常用的网络协议分析以太网协议.IP协议.TCP协议.UDP协议 一.网络模型 TCP/IP分层模型的四个协议层分别完成以下的功能: 第一层 网络接口层 网络接口层 ...
- 使用 rqt_console 和 roslaunch---8
使用 rqt_console 和 roslaunch Description: 本教程介绍如何使用rqt_console和rqt_logger_level进行调试,以及如何使用roslaunch同时运 ...
- 4-3 yum命令
1.常用yum命令 <1>查询 yum list #查询所有可用软件包列表(以 包名 - 版本 - yum源所在名称 格式显示) yum search 关键字 #搜索服务器上所有和关键字相 ...
- Linux驱动设计—— 驱动调试技术
参考博客与书籍: <Linux设备驱动开发详解> <Linux设备驱动程序> http://blog.chinaunix.net/uid-24219701-id-2884942 ...