LeetCode--441--排列硬币
问题描述:
你总共有 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--排列硬币的更多相关文章
- Java实现 LeetCode 441 排列硬币
441. 排列硬币 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整 ...
- LeetCode 441.排列硬币(C++)
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围内. 示例 ...
- leetcode 441.排列硬币(python)
1.题目描述 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范 ...
- Leetcode之二分法专题-441. 排列硬币(Arranging Coins)
Leetcode之二分法专题-441. 排列硬币(Arranging Coins) 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形 ...
- 【LeetCode】排列硬币
[问题]你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币.给定一个数字 n,找出可形成完整阶梯行的总行数.n 是一个非负整数,并且在32位有符号整型的范围内. [ ...
- [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 ...
- [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 ...
- 441 Arranging Coins 排列硬币
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币.给定一个数字 n,找出可形成完整阶梯行的总行数.n 是一个非负整数,并且在32位有符号整型的范围内.示例 1:n ...
- [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 ...
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
随机推荐
- CSS的再深入2(更新中···)
在上一章中,我们又引出了一个知识点: margin的问题 margin:0 auto:(上下为0,左右自适应)会解决元素的居中问题(auto 自适应)前提是给这个元素设置width 同时,我们又要学习 ...
- hdfoo站点开发笔记
为了安全,也要兼顾编辑器切换管理 开发时不必管目录名称的事, 只是在部署的时候,才修改应用目录和tp目录的名字就行了. 为了提高tp的加载效率, 始终给app和tp以绝对路径.就是以 realpath ...
- 排它平方数|2013年蓝桥杯A组题解析第二题-fishers
排它平方数 小明正看着 203879 这个数字发呆. 原来,203879 * 203879 = 41566646641 这有什么神奇呢?仔细观察,203879 是个6位数,并且它的每个数位上的数字都是 ...
- FJUT3574 HOME_W的附加题(带权线段树)题解
题意: 给定n个数a1,a2,a3,……an.和m次操作. 每次操作格式如下 x y k 表示将a[x]替换为y.并求替换后,前k小的数之和 思路:我们用带权线段树维护权值,这里就是维护i的个数n ...
- hotmail 发送邮件 的服务器地址如下
hotmail的服务器地址如下: pop服务器地址:pop-mail.outlook.com smtp服务器地址:smtp-mail.outlook.com imap服务器地址:imap-mail.o ...
- JQuery---高级类选择器
1.ContentFilters 1.1 语法:$('div:contains(edu)').css('backgroundColor','yellow'); 只看div 本身是否包含内容 1.2 语 ...
- Hadoop技术内幕1——源代码环境准备
Hadoop核心 1.HDFS:高容错性.高伸缩性……,允许用户将Hadoop部署在廉价的硬件上,构建分布式系统 2.MapReduce:分布式计算框架,允许用户在不了解分布式系统底层细节的情况下,开 ...
- Latex: 解决 The gutter between columns is x inches wide (on page x), but should be at least 0.2 inches. 问题
参考: Sample_WCCI.tex Latex: 解决 The gutter between columns is x inches wide (on page x), but should be ...
- Python学习笔记3-string
More on Modules and their Namespaces Suppose you've got a module "binky.py" which contains ...
- Virtualbox-CentOS网络配置
1.记下虚拟网卡IP 2. 配置网卡1.网卡2 网卡1 ---对应---eth0----NAT(网络地址转换)用来与外网通信 网卡2 ---对应---eth1----Host-only用来与主机通信 ...