问题描述:

你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。

给定一个数字 n,找出可形成完整阶梯行的总行数。

n 是一个非负整数,并且在32位有符号整型的范围内。

示例 1:

n = 5

硬币可排列成以下几行:
¤
¤ ¤
¤ ¤ 因为第三行不完整,所以返回2.

示例 2:

n = 8

硬币可排列成以下几行:
¤
¤ ¤
¤ ¤ ¤
¤ ¤ 因为第四行不完整,所以返回3.

方法1:

 class Solution(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
low = 0
high = n while low<=high:
mid = int((low+high)//2) if mid*(mid+1)/2 <= n < (mid+1)*(mid+2)/2:
return mid
elif mid*(mid+1)/2 > n:
high = mid-1
elif (mid+1)*(mid+2)/2 <= n:
low = mid + 1

AMAZING:

 class Solution(object):
def arrangeCoins(self, n):
return int(((8*n + 1)**0.5 - 1)/2)

同上:

class Solution(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
k = int((2*n+0.25)**0.5-0.5)
return k

2018-10-03 21:33:05

LeetCode--441--排列硬币的更多相关文章

  1. Java实现 LeetCode 441 排列硬币

    441. 排列硬币 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整 ...

  2. LeetCode 441.排列硬币(C++)

    你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围内. 示例 ...

  3. leetcode 441.排列硬币(python)

    1.题目描述 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范 ...

  4. Leetcode之二分法专题-441. 排列硬币(Arranging Coins)

    Leetcode之二分法专题-441. 排列硬币(Arranging Coins) 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形 ...

  5. 【LeetCode】排列硬币

    [问题]你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币.给定一个数字 n,找出可形成完整阶梯行的总行数.n 是一个非负整数,并且在32位有符号整型的范围内. [ ...

  6. [LeetCode] 441. Arranging Coins 排列硬币

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  7. [LeetCode] Arranging Coins 排列硬币

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  8. 441 Arranging Coins 排列硬币

    你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币.给定一个数字 n,找出可形成完整阶梯行的总行数.n 是一个非负整数,并且在32位有符号整型的范围内.示例 1:n ...

  9. [Swift]LeetCode441. 排列硬币 | Arranging Coins

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  10. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

随机推荐

  1. selenium css定位方式

  2. HDU 2647 Reward 【拓扑排序反向建图+队列】

    题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...

  3. FancyBox的使用技巧 (汇总)

    http://note.youdao.com/share/?id=1c8373249f523529a6b6dcde60777400&type=note#/

  4. oracle 之 连接查询

    where 连接 select * from a,b //使用的是笛卡尔乘积  显示 a.count*b.count 条数 select * from a,b where a.id=b.id 其实只是 ...

  5. LuoguP5221 Product

    题目地址 题目链接 题解 注,下方\((i,j)\)均指\(gcd(i,j)\),以及证明过程有一定的跳步,请确保自己会莫比乌斯反演的基本套路. 介绍本题的\(O(n)\)和\(O(n\sqrt{n} ...

  6. Docker:Stacks

    Prerequisites Install Docker version 1.13 or higher. Get Docker Compose as described in Part 3 prere ...

  7. .NET 介绍

    In order to continue our effort of being modular and well factored we don’t just provide the entire ...

  8. 理解ffmpeg中的pts,dts,time_base

    首先介绍下概念: PTS:Presentation Time Stamp.PTS主要用于度量解码后的视频帧什么时候被显示出来 DTS:Decode Time Stamp.DTS主要是标识读入内存中的b ...

  9. [从零开始搭网站二]服务器环境配置:Mac电脑连接CentOS不用每次都输入密码

    上一篇讲了如何购买服务器,并且***.看这里的第一篇文章: 从零开始搭网站 从这里开始的文章,我会默认大家都是最起码是入门级的程序员,如果你完全不懂我在说什么,那就退出好了. 作为开发人员,接下来为了 ...

  10. python 操作记事本

    需事先打开记事本,再运行下面脚本 # encoding: utf- import win32api import win32gui import win32con print("Hello, ...