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 ...
随机推荐
- 我的Java后端书架2016年暮春3.0版(转)
书架主要针对Java后端开发. 3.0版把一些后来买的.看的书添补进来,又或删掉或降级一些后来没有再翻开过的书. 更偏爱那些能用简短流畅的话,把少壮不努力的程序员所需的基础补回来的薄书,而有些教课书可 ...
- dedecms 列表页调用自定义字段
在列表附加字段中添加自己定义的字段 如: lwulr调用:{dede:list addfields="lwurl" channelid="1"}[field:l ...
- Core Java Volume I — 4.1. Introduction to Object-Oriented Programming
4.1. Introduction to Object-Oriented ProgrammingObject-oriented programming, or OOP for short, is th ...
- 跟开涛老师学shiro -- shiro简介
1.1 简介 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Securi ...
- ZOJ 1205 Martian Addition
原题链接 题目大意:大数,20进制的加法计算. 解法:convert函数把字符串转换成数组,add函数把两个大数相加. 参考代码: #include<stdio.h> #include&l ...
- 颜色追踪块CamShift---33
原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 颜色追踪块CamShift滤波器. 首先确保你的kinect驱动或者uvc相机驱动能正常启动:(如果你使 ...
- JavaWeb学习记录(十九)——jstl自定义标签库之传统标签
一.传统标签 (1)JSP引擎将遇到自定义标签时,首先创建标签处理器类的实例对象,然后按照JSP规范定义的通信规则依次调用它的方法. public void setPageContext(PageCo ...
- URAL 1320 Graph Decomposition(并查集)
1320. Graph Decomposition Time limit: 0.5 secondMemory limit: 64 MB There is a simple graph with an ...
- P168 实战练习(权限修饰符)
创建Game类,运行代码如下: package org.hanqi.pn0120; public class Game { private String name; private String ca ...
- JsonModelStrategy策略添加
今天在学习 zf2的时候,发现后端如果返回的是JsonModel的话,则在前台异步请求的时候发现取不到数据了,后来经过多次研究,才发现要在view_manager中创建策略,代ma是这样子的,stra ...