[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 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 排列硬币的更多相关文章
- 441 Arranging Coins 排列硬币
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币.给定一个数字 n,找出可形成完整阶梯行的总行数.n 是一个非负整数,并且在32位有符号整型的范围内.示例 1:n ...
- [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 ...
- 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 ...
- 【leetcode】441. Arranging Coins
problem 441. Arranging Coins solution1: class Solution { public: int arrangeCoins(int n) { ; ; while ...
- 【LeetCode】441. Arranging Coins 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟计算 二分查找 数学公式 日期 题目地址:htt ...
- Leetcode441Arranging Coins排列硬币
你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围内. 示例 ...
- [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 ...
- 441. Arranging Coins
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 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 ...
随机推荐
- Windows环境下安装和使用nginx1.16.0
nginx是一款开源的HTTP服务器和反向代理服务器,nginx可以作为Web服务器提供HTTP访问功能,类似于Apache.IIS等.目前nginx已经在国内外很多网站作为Web服务器或反向代理服务 ...
- C# 验证控件组
C# 验证控件允许使用ValidationGroup给验证控件分组,分组后的两组验证控件可以独立使用,互不相干.比如一个页面有登录和注册两个部分,假如使用验证控件组,提交的时候会对所有的验证控件进行验 ...
- P5431 【【模板】乘法逆元2】
卡常毒瘤题.交了一页的我. 首先容易想出暴力的做法,直接逆元累加,复杂度\(O(nlogn)\). for(register int i=1;i<=n;++i){ ll a=read(); an ...
- get_template_part()调用自定义模板|wordpress函数
我们在用wordpress开发主题的时候,可能需要调用一段固定的代码比如左侧菜单,这段代码会在主页.栏目页.文章页用到,如果直接写入每个页面模板是没有问题,但是如果要改左侧菜单一个链接那就要改三次,很 ...
- modbus-poll和modbus-slave工具的学习使用——环境搭建
在modbus的学习工具中,非modbus-poll和modbus-slave莫属了,在电脑上模拟的过程中,两者缺一不可 ,当然还需要虚拟串口工具:Configure Virtual Serial P ...
- if语句的嵌套:从键盘输入3个实数,求其最大值。
#include<stdio.h>void main(){ float a,b,c,max; scanf("%f%f%f",&a,&b,&c); ...
- This content should also be served over HTTPS
HTTPS 是 HTTP over Secure Socket Layer,以安全为目标的 HTTP 通道,所以在 HTTPS 承载的页面上不允许出现 http 请求,一旦出现就是提示或报错: jqu ...
- 02-Flutter移动电商实战-建立项目和编写入口文件
环境搭建请参考之前写的一篇文章:Flutter_初体验_创建第一个应用 1.创建项目 采用AndroidStudio构建本项目,FIle>New>New Flutter Project… ...
- tensorflow2.0 学习(一)
虽说是按<TensorFlow深度学习>这本书来学习的,但是总会碰到新的问题!记录下这些问题,有利于巩固知新. 之前学过一些tensorflow1.0的知识,到RNN这章节,后面没有再继续 ...
- learning java FileWriter
import java.io.FileWriter; import java.io.IOException; public class FileWriterTest { public static v ...