作者: 负雪明烛
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. 创建一个vue实例

    创建一个vue实例 每个 Vue 应用都是通过用 Vue 函数创建一个新的 Vue 实例开始的: var vm = new Vue({ // 选项 }) 虽然没有完全遵循 MVVM 模型,但是 Vue ...

  2. matplotlib以对象方式绘制子图

    matplotlib有两种绘图方式,一种是基于脚本的方式,另一种是面向对象的方式 面向脚本的方式类似于matlab,面向对象的方式使用起来更为简便 创建子图的方式也很简单 fig,ax = plt.s ...

  3. A Child's History of England.6

    It was a British Prince named Vortigern who took this resolution, and who made a treaty of friendshi ...

  4. JavaScript小数、百分数的转换

    百分数转化为小数 function toPoint(percent){ var str=percent.replace("%",""); str= str/10 ...

  5. rust常用技巧

    允许未使用的方法,写在文件开头,可过滤过掉该项提示 #![allow(unused)]

  6. APP调用系统相册,使用3DTouch重压,崩溃

    崩溃:app调用系统相册,使用3DTouch重压,崩溃 问题描述 app调用系统相册,使用3DTouch重压,一般的app都会崩溃. 解决方法 写个分类即可 @implementation UICol ...

  7. OpenStack之七: compute服务(端口8774)

    注意此处的bug,参考o版 官网地址 https://docs.openstack.org/nova/stein/install/controller-install-rdo.html 控制端配置 # ...

  8. 锁对象-条件对象-synchronized关键字

    1 import java.util.concurrent.locks.Condition; 2 import java.util.concurrent.locks.Lock; 3 import ja ...

  9. Google Guava 常用集合方法

    /** * Author: momo * Date: 2018/6/7 * Description: */ public class ListTest { public static void mai ...

  10. Synchronized深度解析

    概览: 简介:作用.地位.不控制并发的影响 用法:对象锁和类锁 多线程访问同步方法的7种情况 性质:可重入.不可中断 原理:加解锁原理.可重入原理.可见性原理 缺陷:效率低.不够灵活.无法预判是否成功 ...