作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/arranging-coins/#/description

题目描述

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.

题目大意

给了n个硬币,要求第k层有k个硬币,问能摆出多少层,如果最后一层不满足的话,是不算的。

解题方法

模拟计算

如果模拟这个安排硬币的过程的话,可以这么做:

class Solution(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
level = 0
count = 0
while count + level + 1 <= n:
level += 1
count += level
return level

二分查找

上面做法的效率不高。因为前k层的硬币个数可以直接通过(k + 1) * k / 2求出来,所以很直接的想法就可以使用二分查找。

即目的是找到(k + 1) * k / 2<=sum的最大数字,套用二分查找的模板,很容易写出来。

class Solution(object):
def arrangeCoins(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 0, n + 1 #[left, right)
while left < right:
mid = left + (right - left) / 2
if mid * (mid + 1) / 2 <= n:
left = mid + 1
else:
right = mid
return left - 1

数学公式

刷题的时候第一次遇到纯数学的问题,其实就是求解sum = (x + 1) * x / 2这个方程,很简单的就能得到x = (-1 + sqrt(8 * n + 1)) / 2,向下取整就能得到结果了。

下面的代码的括号也要重视一下。

public class Solution {
public int arrangeCoins(int n) {
return (int)((-1 + Math.sqrt(1 + 8 * (long) n)) / 2);
}
}

日期

2017 年 5 月 6 日
2018 年 11 月 24 日 —— 周六快乐

【LeetCode】441. Arranging Coins 解题报告(Python)的更多相关文章

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

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

  3. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  4. 【leetcode】441. Arranging Coins

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

  5. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  6. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  7. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  8. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  9. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

随机推荐

  1. linux下面升级 Python版本并修改yum属性信息

    最近需要在linux下使用python,故需要升级一下python版本,上网查询了一下相关资料,更新了一下linux下面的python环境,记录如下: linux下面升级 Python版本并修改yum ...

  2. chown & chmod用法

    chown & chmod 1. chown更改文件的属主&属组 NAME chown - 改变文件的属主和属组(change file owner and group) 用法 cho ...

  3. 学习java的第十四天

    一.今日收获 1.完成了手册第二章没有验证完成的例题 2.预习了第三章的算法以及for语句与if语句的用法 二.今日难题 1.验证上出现问题,没有那么仔细. 2.第二章还有没有完全理解的问题 三.明日 ...

  4. CentOS7 搭建maven私服Nexus

    下载解压 官网https://www.sonatype.com/download-oss-sonatype 下载页面 https://help.sonatype.com/repomanager2/do ...

  5. CSS基础语法(一)

    目录 CSS基础语法(一) 一.CSS简介 1.CSS语法规范 2.CSS代码风格 二.CSS基础选择器 1.标签选择器 2.类选择器 3.id选择器 4.通配符选择器 5.总结 三.CSS字体属性 ...

  6. Postman 中 Pre-request Script 常用 js 脚本

    1. 生成一个MD5或SHA1加密的字符串str_md5,str_sha1 string1 = "123456"; var str_md5= CryptoJS.MD5(string ...

  7. OC-基础数据类型

    七 字符串与基本数据类型转换 获取字符串的每个字符/字符串和其他数据类型转换 八 NSMutableString 基本概念/常用方法 九 NSArray NSArray基本概念/创建方式/注意事项/常 ...

  8. docker配置国内阿里云镜像源

    使用docker默认镜像源下载镜像会很慢,因此很多情况下,我们在安装完docker以后都会修改为国内的镜像,这样在下载镜像的时候就不用等那么长时间了. 配置docker的镜像为阿里云镜像 方法一 $ ...

  9. docker之镜像制作

    #:下载镜像并初始化系统 root@ubuntu:~# docker pull centos #:创建目录 root@ubuntu:/opt# mkdir dockerfile/{web/{nginx ...

  10. linux 配置本地yum

    1.挂载光盘 #挂载光盘 mount /dev/cdrom /mnt/cdrom 2.修改yum.conf, 运行 vi /etc/yum.conf,文件替换成如下内容 [main] cachedir ...