生活随笔
收集整理的這篇文章主要介紹了
LeetCode 6062. 设计一个 ATM 机器
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
1. 題目
一個(gè) ATM 機(jī)器,存有 5 種面值的鈔票:20 ,50 ,100 ,200 和 500 美元。初始時(shí),ATM 機(jī)是空的。用戶可以用它存或者取任意數(shù)目的錢。
取款時(shí),機(jī)器會(huì)優(yōu)先取 較大 數(shù)額的錢。
比方說(shuō),你想取 $300 ,并且機(jī)器里有 2 張 $50 的鈔票,1 張 $100 的鈔票和1 張 $200 的鈔票,那么機(jī)器會(huì)取出 $100 和 $200 的鈔票。
但是,如果你想取 $600 ,機(jī)器里有 3 張 $200 的鈔票和1 張 $500 的鈔票,那么取款請(qǐng)求會(huì)被拒絕,因?yàn)闄C(jī)器會(huì)先取出 $500 的鈔票,然后無(wú)法取出剩余的 $100 。 注意,因?yàn)橛?$500 鈔票的存在,機(jī)器 不能 取 $200 的鈔票。
請(qǐng)你實(shí)現(xiàn) ATM 類:
ATM() 初始化 ATM 對(duì)象。 void deposit(int[] banknotesCount) 分別存入 $20 ,$50,$100,$200 和 $500 鈔票的數(shù)目。 int[] withdraw(int amount) 返回一個(gè)長(zhǎng)度為 5 的數(shù)組,分別表示 $20 ,$50,$100 ,$200 和 $500 鈔票的數(shù)目,并且更新 ATM 機(jī)里取款后鈔票的剩余數(shù)量。 如果無(wú)法取出指定數(shù)額的錢,請(qǐng)返回 [-1] (這種情況下 不 取出任何鈔票)。
示例
1 :
輸入:
[ "ATM" , "deposit" , "withdraw" , "deposit" , "withdraw" , "withdraw" ]
[ [ ] , [ [ 0 , 0 , 1 , 2 , 1 ] ] , [ 600 ] , [ [ 0 , 1 , 0 , 1 , 1 ] ] , [ 600 ] , [ 550 ] ]
輸出:
[ null
, null
, [ 0 , 0 , 1 , 0 , 1 ] , null
, [ - 1 ] , [ 0 , 1 , 0 , 0 , 1 ] ]
解釋:
ATM atm
= new ATM
( ) ;
atm
. deposit
( [ 0 , 0 , 1 , 2 , 1 ] ) ; // 存入
1 張 $
100 ,
2 張 $
200 和
1 張 $
500 的鈔票。
atm
. withdraw
( 600 ) ; // 返回
[ 0 , 0 , 1 , 0 , 1 ] 。機(jī)器返回
1 張 $
100 和
1 張 $
500 的鈔票。機(jī)器里剩余鈔票的數(shù)量為
[ 0 , 0 , 0 , 2 , 0 ] 。
atm
. deposit
( [ 0 , 1 , 0 , 1 , 1 ] ) ; // 存入
1 張 $
50 ,
1 張 $
200 和
1 張 $
500 的鈔票。
// 機(jī)器中剩余鈔票數(shù)量為
[ 0 , 1 , 0 , 3 , 1 ] 。
atm
. withdraw
( 600 ) ; // 返回
[ - 1 ] 。機(jī)器會(huì)嘗試取出 $
500 的鈔票,然后無(wú)法得到剩余的 $
100 ,所以取款請(qǐng)求會(huì)被拒絕。
// 由于請(qǐng)求被拒絕,機(jī)器中鈔票的數(shù)量不會(huì)發(fā)生改變。
atm
. withdraw
( 550 ) ; // 返回
[ 0 , 1 , 0 , 0 , 1 ] ,機(jī)器會(huì)返回
1 張 $
50 的鈔票和
1 張 $
500 的鈔票。提示:
banknotesCount
. length
== 5
0 <= banknotesCount
[ i
] <= 10 ^ 9
1 <= amount
<= 10 ^ 9
總共 最多有
5000 次 withdraw 和 deposit 的調(diào)用。
函數(shù) withdraw 和 deposit 至少各有 一次 調(diào)用。
來(lái)源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/design-an-atm-machine 著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
2. 解題
按題意模擬,注意 int 數(shù)據(jù)溢出
class ATM { vector
< long long > ct
; vector
< int > num
;
public : ATM ( ) { num
= { 20 , 50 , 100 , 200 , 500 } ; ct
= { 0 , 0 , 0 , 0 , 0 } ; } void deposit ( vector
< int > banknotesCount
) { for ( int i
= 0 ; i
< 5 ; ++ i
) { ct
[ i
] += banknotesCount
[ i
] ; } } vector
< int > withdraw ( int amount
) { vector
< int > need ( 5 , 0 ) ; for ( int i
= 4 ; i
>= 0 ; -- i
) { int n
= amount
/ num
[ i
] ; need
[ i
] = min ( ct
[ i
] , 1LL * n
) ; amount
-= need
[ i
] * num
[ i
] ; } if ( amount
== 0 ) { for ( int i
= 0 ; i
< 5 ; ++ i
) ct
[ i
] -= need
[ i
] ; return need
; } else return { - 1 } ; }
} ;
460 ms 105.1 MB C++
我的CSDN博客地址 https://michael.blog.csdn.net/
長(zhǎng)按或掃碼關(guān)注我的公眾號(hào)(Michael阿明),一起加油、一起學(xué)習(xí)進(jìn)步!
總結(jié)
以上是生活随笔 為你收集整理的LeetCode 6062. 设计一个 ATM 机器 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。