作者: 负雪明烛
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. python-django-查询详解

    倒数第二条性质可以实现链式的调用,通过第一次的过滤还可以再过滤倒数第一条就是结果集从数据库中查询出来之后不会再进行数据库的查询的我们使用的object就是模型管理器manager的一个对象 obj = ...

  2. 【Python小试】计算蛋白序列中指定氨基酸所占的比例

    编码 from __future__ import division def get_aa_percentage(protein, aa_list=['A','I','L','M','F','W',' ...

  3. 毕业设计之LVS+keealive 负载均衡高可用实现

    环境介绍 centos6.9最小化安装 主机名 IPADDR lvsA.load.quan.bbs 192.168.111.131 lvsB.load.quan.bbs 192.168.111.132 ...

  4. Vector总结及部分底层源码分析

    Vector总结及部分底层源码分析 1. Vector继承的抽象类和实现的接口 Vector类实现的接口 List接口:里面定义了List集合的基本接口,Vector进行了实现 RandomAcces ...

  5. django中的filter(), all(), get()

    1. 类名.objects中的get(), filter(), all() 的区别 结论: (1)all()返回的是QuerySet对象,程序并没有真的在数据库中执行SQL语句查询数据,但支持迭代,使 ...

  6. Linux之sftp服务

    Linux之sftp服务 一.sftp介绍转自:[1]Linux如何开启SFTP https://www.cnblogs.com/xuliangxing/p/7120205.htmlSFTP是Secu ...

  7. Output of C++ Program | Set 17

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  8. Linux基础命令---sendmail发送邮件

    sendmail sendmail是postfix中的一个发送邮件的代理程序,它负责发送邮件到远程服务器,并且可以接收邮件.sendmail在发送邮件的时候,默认从标砖输入读取内容,以".& ...

  9. Spring AOP通过注解的方式设置切面和切入点

    切面相当于一个功能的某一个类,切入点是这个类的某部分和需要额外执行的其他代码块,这两者是多对多的关系,在代码块处指定执行的条件. Aspect1.java package com.yh.aop.sch ...

  10. 9.Vue.js 监听属性

    本章节,我们将为大家介绍 Vue.js 监听属性 watch,我们可以通过 watch 来响应数据的变化. 以下实例通过使用 watch 实现计数器: <div id = "app&q ...