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 ...
随机推荐
- LabVIEW串口通信的一个例子-串口"示波器"
1.程序界面:<ignore_js_op> 功能介绍:左边上方串口接收区,下方为串口字符发送区.右方为一个波形图表,在程序内部每次将串口发送过来的数据,以f%格式化,显示在波形图表上.按钮 ...
- python的 map,filter, reduce, enumerate
一, map #基本的map运用都可以用解析去替代,复杂的仍然需要定义函数,利用map去做 map(函数, 序列) 将序列的各项经过函数处理, 然后返回到一个新列表中. #itertools. ...
- python numpy 模块简单介绍
用python自带的list去处理数组效率很低, numpy就诞生了, 它提供了ndarry对象,N-dimensional object, 是存储单一数据类型的多维数组,即所有的元素都是同一种类型. ...
- Single Number II ——位操作
题意: 给定两个32位的整数 N 和 M,以及表示比特位置的 i 与 j .编写一个方法,将 M 插入 N,使得 M 从 N 的第 j 位开始,到第 i 位结束.假定从 j 位到 i 位足以容纳M. ...
- addcontentView之后如何让这个view消失掉
Dia dialog ; public void go(View view){ dialog = (Dia) getLayoutInflater().inflate(R.layout.main, nu ...
- Java-->多线程复制(文件指针)
--> 这里用到两种方法...其实也不算两种,就一点点不一样而已... ---> Test 测试类 package com.dragon.java.multithreadcopy; imp ...
- 装载: Matlab 提取矩阵 某一行 或者 某一列 的方法
比如,从一个6*6矩阵中,提取它的第一行元素,形成一个6维行向量. A(i,:)行 A(:,i)列 方法: A(i,:) 提取矩阵A的第 i行 A(:,i) 提取矩阵A的第 i列 给你个例子: ...
- RelativeLayout用到的一些重要的属性
第一类:属性值为true或false android:layout_centerHrizontal 水平居中 ...
- Chorme dev tools小技巧
1.快速稳健转换:打开DevTools,按下Ctrl+P来快速寻找和打开你工程中的任意文件. 2.在源代码中搜索:打开DevTools,按下Ctrl+Shift+F,即可在所有已加载文件中查找一个特定 ...
- jsp页面 date转化成string
1.先引入fmt标签<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> ...