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. ThinkPHP支持的4种路由模式

    下面这个说法和示例,比较具有总结性.

  2. SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

    使用idea创建一个maven项目( 这里演示 的是 web项目) 点击 Finish 然后开始配置 pom.xml文件(添加各种依赖jar包) 先去找 spring 所需的 jar包 jar包中心仓 ...

  3. tensorflow API _ 5 (tensorflow.summary)

    tensorflow的可视化是使用summary和tensorboard合作完成的. 基本用法 首先明确一点,summary也是op. 输出网络结构 with tf.Session() as sess ...

  4. if语句的嵌套:从键盘输入3个实数,求其最大值。

    #include<stdio.h>void main(){ float a,b,c,max; scanf("%f%f%f",&a,&b,&c); ...

  5. LeetCode 457. Circular Array Loop

    原题链接在这里:https://leetcode.com/problems/circular-array-loop/ 题目: You are given a circular array nums o ...

  6. WinDbg常用命令系列---.cmdtree

    .cmdtree 简介 使用形式 .cmdtree cmdfile 参数 cmdfile命令文件,包含多个你需要的命令.必须是一个文本档 使用步骤 1.使用命令创建文本文件test.wl,使用以下示例 ...

  7. ipcs

    用于报告Linux中进程间通信设施的状态,显示的信息包括消息列表.共享内存和信号量的信息

  8. C# DataTable转换为Html

    /// <summary> /// DataTable 转换为 Html /// </summary> /// <param name="dt"> ...

  9. 9-ESP8266 SDK开发基础入门篇--编写串口上位机软件

    https://www.cnblogs.com/yangfengwu/p/11087613.html 页面修改成这样子             现在看串口发送数据 点击点亮 发送0xaa 0x55 0 ...

  10. SQL基础-子查询&EXISTS&UNION

    一.子查询 1.使用子查询作为计算字段 子查询:嵌套在其他查询中的查询 现在有两个表,student表和teacher表 创建teacher表,并插入数据: CREATE TABLE `teacher ...