Arranging Coins
来源:互联网 发布:算法设计与分析pdf下载 编辑:程序博客网 时间:2024/06/07 06:34
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: 模拟相加
class Solution {public: int arrangeCoins(int n) { int level = 1; for(long sum = 0 ; sum <=n ; ++level){ sum+=level; } return max(level-2,0); }};
方法2:数学公式
class Solution {public: int arrangeCoins(int n) { return sqrt(2*static_cast<long>(n)+1/4.0)-1/2.0; }};
0 0
- Arranging Coins
- Arranging Coins
- Arranging Coins
- Arranging Coins
- Arranging Coins
- Arranging Coins
- Arranging Coins
- LeetCode 441. Arranging Coins
- 441. Arranging Coins
- 441. Arranging Coins
- 441. Arranging Coins
- LeetCode Arranging Coins
- 441. Arranging Coins
- LeetCode 441. Arranging Coins
- 441. Arranging Coins
- [leetcode 441]Arranging Coins
- 441. Arranging Coins
- leetcode 441. Arranging Coins
- 区别pointer和references
- openssl堆栈
- openssl内存分配
- 10分钟理解Android数据库的创建与使用(附详解和示例代码)
- 数字签名图文并茂
- Arranging Coins
- 游戏开发从需求出发,注重新趋势变化发展
- MySQL NULL 值处理
- markdown数学公式
- JESD79-4 第4章 SDRAM命令描述与操作(4.25)
- Delphi容器类之---Tlist,TStringlist,THashedStringlist的效率比较
- POJ 2828 Buy Tickets(线段树 )
- Python 自定义异常类,主动捕获异常,主动抛出异常
- MySQL 正则表达式