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.
Solutionclass Solution { public int arrangeCoins(int n) { int i = 0; while (n > 0) { i += 1; n -= i; } return n == 0 ? i : i-1; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/77223.html
摘要:題目要求用個硬幣搭臺階,要求第級臺階必須有個硬幣。思路和代碼反過來講,如果要搭級臺階,那么級臺階共包含個硬幣。因此我們只需要找到可以搭建的臺階的邊界,并采用二分法將邊界不斷縮小直到達(dá)到最大的臺階數(shù)。 題目要求 You have a total of n coins that you want to form in a staircase shape, where every k-th ...
摘要:月下半旬攻略道題,目前已攻略題。目前簡單難度攻略已經(jīng)到題,所以后面會調(diào)整自己,在刷算法與數(shù)據(jù)結(jié)構(gòu)的同時,攻略中等難度的題目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道題,目前已攻略 100 題。 一 目錄 不折騰的前端,和咸魚有什么區(qū)別...
摘要:在線網(wǎng)站地址我的微信公眾號完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個題。這是項(xiàng)目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號: showImg(htt...
摘要:解題思路動態(tài)規(guī)劃,用表示總價為的最小紙幣張數(shù),很容易想到狀態(tài)轉(zhuǎn)移方程當(dāng)然前提是要大于紙幣金額數(shù)。表示取一張面額加上合計(jì)為的最小紙幣數(shù)。另題目要求無法合計(jì)出的金額,要返回,所以要作特殊處理,否則就會返回元素初始化值代碼 Coin ChangeYou are given coins of different denominations and a total amount of money...
摘要:傳入的參數(shù)為手上有的紙幣的面額以及希望兌換的面額。這里假設(shè)紙幣的數(shù)量是無窮的。這題本質(zhì)上考察的是動態(tài)規(guī)劃思想。這里有兩種動態(tài)規(guī)劃的方法,分別從遞歸和非遞歸的角度解決這個問題。具體的情況還是要看數(shù)據(jù)的分布情況來確定選擇哪種方法。 題目要求 You are given coins of different denominations and a total amount of money ...
閱讀 3463·2023-04-25 18:14
閱讀 1568·2021-11-24 09:38
閱讀 3282·2021-09-22 14:59
閱讀 3095·2021-08-09 13:43
閱讀 2600·2019-08-30 15:54
閱讀 595·2019-08-30 13:06
閱讀 1580·2019-08-30 12:52
閱讀 2750·2019-08-30 11:13