leetcode 441. Arranging Coins
来源:互联网 发布:linux工具链 编辑:程序博客网 时间:2024/06/17 05:00
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.
题意很简单,就是一个简单的等比数列求和问题
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <cmath>using namespace std;class Solution {public: /* 1 + 2 + 3 + ... + x = n -> (1 + x)x / 2 = n ->x ^ 2 + x = 2n ->x ^ 2 + x + 1 / 4 = 2n + 1 / 4 -> (x + 1 / 2) ^ 2 = 2n + 1 / 4 -> (x + 0.5) = sqrt(2n + 0.25) ->x = -0.5 + sqrt(2n + 0.25) */ int arrangeCoins(int n) { return floor(-0.5 + sqrt((double)2 * n + 0.25)); }};
阅读全文
0 0
- LeetCode 441. Arranging Coins
- LeetCode 441. Arranging Coins
- leetcode 441. Arranging Coins
- Leetcode: 441. Arranging Coins
- LeetCode 441. Arranging Coins
- 【LeetCode】 441. Arranging Coins
- LeetCode 441. Arranging Coins
- [leetcode]441. Arranging Coins
- LeetCode 441. Arranging Coins
- Leetcode 441. Arranging Coins
- leetcode-441. Arranging Coins
- [LeetCode]441. Arranging Coins
- LeetCode-441. Arranging Coins
- 【LeetCode】441. Arranging Coins
- [LeetCode]441. Arranging Coins
- 【Leetcode】441. Arranging Coins
- [leetcode]: 441. Arranging Coins
- leetcode 441. Arranging Coins
- [知了堂学习笔记]_JavaScript
- ckfinder文件管理
- 计算牛的数量-递归
- Linux忘记 root密码的解决办法
- 百度地图 在用户输入的省市区范围内进行关键字智能提示搜索
- leetcode 441. Arranging Coins
- 阶乘求和
- 刷题笔记:PAT真题在线练习A+B和C
- 求圆柱的体积
- http://blog.csdn.net/jim8757/article/details/76961735
- 享元模式浅析
- tomcat启动窗口一闪而过,cmd启动报错
- x86汇编与C相互调用
- 求1-100的和,偶数和,奇数和