资讯专栏INFORMATION COLUMN

[LeetCode] 441. Arranging Coins

avwu / 2586人阅读

Problem

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.

Solution
class Solution {
    public int arrangeCoins(int n) {
        int i = 0;
        while (n > 0) {
            i += 1;
            n -= i;
        }
        return n == 0 ? i : i-1;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77223.html

相关文章

  • leetcode441. Arranging Coins

    摘要:题目要求用个硬币搭台阶,要求第级台阶必须有个硬币。思路和代码反过来讲,如果要搭级台阶,那么级台阶共包含个硬币。因此我们只需要找到可以搭建的台阶的边界,并采用二分法将边界不断缩小直到达到最大的台阶数。 题目要求 You have a total of n coins that you want to form in a staircase shape, where every k-th ...

    Ali_ 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 题攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0
  • [LeetCode - Dynamic Programming] Coin Change

    摘要:解题思路动态规划,用表示总价为的最小纸币张数,很容易想到状态转移方程当然前提是要大于纸币金额数。表示取一张面额加上合计为的最小纸币数。另题目要求无法合计出的金额,要返回,所以要作特殊处理,否则就会返回元素初始化值代码 Coin ChangeYou are given coins of different denominations and a total amount of money...

    dackel 评论0 收藏0
  • leetcode322. Coin Change

    摘要:传入的参数为手上有的纸币的面额以及希望兑换的面额。这里假设纸币的数量是无穷的。这题本质上考察的是动态规划思想。这里有两种动态规划的方法,分别从递归和非递归的角度解决这个问题。具体的情况还是要看数据的分布情况来确定选择哪种方法。 题目要求 You are given coins of different denominations and a total amount of money ...

    kohoh_ 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<