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:

  1. n = 5
  2.  
  3. The coins can form the following rows:
  4. ¤
  5. ¤ ¤
  6. ¤ ¤
  7.  
  8. Because the 3rd row is incomplete, we return 2.

Example 2:

  1. n = 8
  2.  
  3. The coins can form the following rows:
  4. ¤
  5. ¤ ¤
  6. ¤ ¤ ¤
  7. ¤ ¤
  8.  
  9. Because the 4th row is incomplete, we return 3.
  10.  
  11. 利用 x(x+1)/2 <= n 求得x = (sqrt(8*n+1) -1)/2
  12.  
  13. Code O(1)
  1. class Solution:
  2. def arrageCoins(self, n):
  3. return (int(math.sqrt(8*n +1) -1)//2)

[LeetCode] 441. Arranging Coins_Easy tag: Math的更多相关文章

  1. [LeetCode] 258. Add Digits_Easy tag: Math

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

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

  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] 504. Base 7_Easy tag: Math

    Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...

  5. [LeetCode] 292. Nim Game_Easy tag: Math

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  6. [LeetCode] 458. Poor Pigs_Easy tag: Math

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  7. 【leetcode】441. Arranging Coins

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

  8. Leetcode Tags(6)Math

    一.204. Count Primes Count the number of prime numbers less than a non-negative number, n. Input: 10 ...

  9. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

随机推荐

  1. Intersection Observer API 可以让你知道被观察元素何时进入或退出浏览器的视口

    google 文档 https://developers.google.cn/web/updates/2016/04/intersectionobserver MDN 文档 https://devel ...

  2. eclipse下配置Spring环境

    工具: jdk1.8 win10 spring5.0 1.准备工作:下载Spring开发应用的插件,api 1.spring插件包:springsource-tool-suite-3.9.4.RELE ...

  3. ARM Linux Oops使用小结(转)

    出现Oops消息的大部分错误时因为对NULL指针取值或者因为用了其他不正确的指针值. Oops如何产生的解释如下:     由于处理器使用的地址几乎都是虚拟地址,这些地址通过一个被称为“页表”的结构被 ...

  4. PAT甲级1052 Linked List Sorting

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...

  5. mysql日期格式转换,如何保持原日期?CONVERT/Substring 函数截取。replace替换

    http://www.cnblogs.com/stevenjson/p/3729577.html CONVERT(varchar(100), getdate(), 112)这种, 问题就出在getda ...

  6. [No000010F]Git8/9-使用GitHub

    我们一直用GitHub作为免费的远程仓库,如果是个人的开源项目,放到GitHub上是完全没有问题的.其实GitHub还是一个开源协作社区,通过GitHub,既可以让别人参与你的开源项目,也可以参与别人 ...

  7. MyBatis中choose when正确写法

    <choose> <when test="scoreRange!=null and scoreRange eq 1"> AND sc.score <! ...

  8. Mean reversion (finance) 均值回归

    Mean reversion (finance)  均值回归

  9. inotifywait实现目录监控--http://man.linuxde.net/inotifywait

    sudo apt install inotify-tools while inotifywait -q -r -e create,delete,modify,move,attrib --exclude ...

  10. [centos][ntp][administrator] chrony ntp

    以下内容,适用于 CentOS 7 (systemd 体系) 一. 首先,确认你是否启用了 ntp 服务: [root@nlb2-liantiao ~]# timedatectl Local time ...