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

Given n, find the total number of full staircase rows that can be formed.

n is a non-negative integer and fits within the range of a 32-bit signed integer.

Example 1:

n = 5

The coins can form the following rows:
¤
¤ ¤
¤ ¤ Because the 3rd row is incomplete, we return 2.

Example 2:

n = 8

The coins can form the following rows:
¤
¤ ¤
¤ ¤ ¤
¤ ¤ Because the 4th row is incomplete, we return 3.

思路:

解法1:设一个变量 cur表示当前行需要的硬币数,  初始值为1,从n中减去cur,cur自加1,如果此时剩余的硬币小于cur,返回cur-1即可

解法2:二分搜索法Binary Search,等差数列前m项和: m * (m + 1) / 2, 搜索前i行之和刚好大于n的临界点,这样我们减一个就是能排满的行数

解法3:利用等差数列的公式n = (1 + x) * x / 2,  则x = (-1 + sqrt(8 * n + 1)) / 2,  取整后就是能填满的行数

C++: Time: O(n), Space: O(1)

class Solution {
public:
int arrangeCoins(int n) {
int cur = 1, rem = n - 1;
while (rem >= cur + 1) {
++cur;
rem -= cur;
}
return n == 0 ? 0 : cur;
}
};

C++: Time: O(logn), Space: O(1)

class Solution {
public:
int arrangeCoins(int n) {
if (n <= 1) return n;
long low = 1, high = n;
while (low < high) {
long mid = low + (high - low) / 2;
if (mid * (mid + 1) / 2 <= n) low = mid + 1;
else high = mid;
}
return low - 1;
}
};

C++: Time: O(logn), Space: O(1)

class Solution {
public:
int arrangeCoins(int n) {
return (int)((-1 + sqrt(1 + 8 * (long)n)) / 2); # sqrt is O(logn) time.
}
};

Python: O(n), Space: O(1)

class Solution(object):
def arrangeCoins(self, n):
cur = 1
rem = n
while rem >= cur:
rem -= cur
cur += 1 return 0 if n == 0 else cur - 1

Python: O(logn), Space: O(1)

class Solution(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 1, n
while left <= right:
mid = left + (right - left) / 2
if 2 * n < mid * (mid+1):
right = mid - 1
else:
left = mid + 1
return left - 1

Python: O(logn), Space: O(1)

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

Python: O(logn), Space: O(1)

class Solution(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
return int((math.sqrt(8*n+1)-1) / 2) # sqrt is O(logn) time.

  

  

  

  

  

[LeetCode] 441. Arranging Coins 排列硬币的更多相关文章

  1. 441 Arranging Coins 排列硬币

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

  2. [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 ...

  3. LeetCode 441 Arranging Coins

    Problem: You have a total of n coins that you want to form in a staircase shape, where every k-th ro ...

  4. 【leetcode】441. Arranging Coins

    problem 441. Arranging Coins solution1: class Solution { public: int arrangeCoins(int n) { ; ; while ...

  5. 【LeetCode】441. Arranging Coins 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟计算 二分查找 数学公式 日期 题目地址:htt ...

  6. Leetcode441Arranging Coins排列硬币

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

  7. [LeetCode] 441. Arranging Coins_Easy tag: Math

    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

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. LeetCode_441. Arranging Coins

    441. Arranging Coins Easy You have a total of n coins that you want to form in a staircase shape, wh ...

随机推荐

  1. Python使用pip安装matplotlib模块

    matplotlib是python中强大的画图模块. 首先确保已经安装python,然后用pip来安装matplotlib模块. 进入到cmd窗口下,建议执行python -m pip install ...

  2. test201909027 老Z

    30+100+40=170.数据出锅*2,也是没谁了. 装饰 快要到 Mope 的生日了,Mope 希望举行一场盛大的生日派对. 派对的准备工作中当然有装饰房间啦.Mope 的房间里有按从左到右的顺序 ...

  3. C#编写简单的聊天程序(转)

    这是一篇基于Socket进行网络编程的入门文章,我对于网络编程的学习并不够深入,这篇文章是对于自己知识的一个巩固,同时希望能为初学的朋友提供一点参考.文章大体分为四个部分:程序的分析与设计.C#网络编 ...

  4. modbus-poll和modbus-slave工具的学习使用——modbus协议功能码1的解析

    一.数据解析 上一文介绍了modbus工具的基本使用情况,但是还没用说明modbus中的协议的具体意义, 1.左边是slave,id=1,说明地址是1,f=01说明是功能码01,功能码是一个字节,说明 ...

  5. [Luogu 2386]放苹果

    Description 题库链接 把 \(n\) 个同样的苹果放在 \(m\) 个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分发.多测,数据组数 \(t\). \(1\leq m,n\le ...

  6. JPA注解开发

    JPA注解开发 jpa是sun公司的一个ORM规范,只有接口和注解,没有具体实现. jpa是EJB3中的. 单表常用注解 书写注解,并配置 @Entity @Table(name = "c_ ...

  7. [SPOJ] DIVCNT2 - Counting Divisors (square) (平方的约数个数前缀和 容斥 卡常)

    题目 vjudge URL:Counting Divisors (square) Let σ0(n)\sigma_0(n)σ0​(n) be the number of positive diviso ...

  8. 学习Spring-Data-Jpa(十)---注解式方法查询之@Query、@Modifying与派生delete

    1.@Query 对于少量的查询,使用@NamedQuery在实体上声明查询是一种有效的办法,并且可以很好的工作.由于查询本身绑定到执行它们的java方法,实际上可以通过Spring-Data-Jpa ...

  9. packr 方便的潜入静态资源文件到golang 二进制文件中

    类似的工具以前有介绍过statik,今天使用的工具是packr 也是很方便的golang tools 安装 go get -u github.com/gobuffalo/packr/packr 或者我 ...

  10. 处理kubernetes 一些比较难删除的资源

    kubernetes 提供了force 的命令在我们删除资源的时候,但是很多时候还是不可以的 一般删除资源的处理 命令 kubectl delete <resource> <reso ...