[Swift]LeetCode860. 檸檬水找零 | Lemonade Change

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-qyjtmaeh-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

At a lemonade stand, each lemonade costs $5git

Customers are standing in a queue to buy from you, and order one at a time (in the order specified by bills).github

Each customer will only buy one lemonade and pay with either a $5$10, or $20 bill.  You must provide the correct change to each customer, so that the net transaction is that the customer pays $5.微信

Note that you don't have any change in hand at first.ide

Return true if and only if you can provide every customer with correct change.spa

Example 1:code

Input: [5,5,5,10,20]
Output: true Explanation: From the first 3 customers, we collect three $5 bills in order. From the fourth customer, we collect a $10 bill and give back a $5. From the fifth customer, we give a $10 bill and a $5 bill. Since all customers got correct change, we output true. 

Example 2:htm

Input: [5,5,10]
Output: true 

Example 3:blog

Input: [10,10]
Output: false 

Example 4:three

Input: [5,5,10,10,20]
Output: false Explanation: From the first two customers in order, we collect two $5 bills. For the next two customers in order, we collect a $10 bill and give back a $5 bill. For the last customer, we can't give change of $15 back because we only have two $10 bills. Since not every customer received correct change, the answer is false.

Note:

  • 0 <= bills.length <= 10000
  • bills[i] will be either 510, or 20.

在檸檬水攤上,每一杯檸檬水的售價爲 5 美圓。

顧客排隊購買你的產品,(按帳單 bills 支付的順序)一次購買一杯。

每位顧客只買一杯檸檬水,而後向你付 5 美圓、10美圓或 20 美圓。你必須給每一個顧客正確找零,也就是說淨交易是每位顧客向你支付 5 美圓。

注意,一開始你手頭沒有任何零錢。

若是你能給每位顧客正確找零,返回 true ,不然返回 false 。

示例 1:

輸入:[5,5,5,10,20]
輸出:true
解釋:
前 3 位顧客那裏,咱們按順序收取 3 張 5 美圓的鈔票。
第 4 位顧客那裏,咱們收取一張 10 美圓的鈔票,並返還 5 美圓。
第 5 位顧客那裏,咱們找還一張 10 美圓的鈔票和一張 5 美圓的鈔票。
因爲全部客戶都獲得了正確的找零,因此咱們輸出 true。

示例 2:

輸入:[5,5,10]
輸出:true

示例 3:

輸入:[10,10]
輸出:false

示例 4:

輸入:[5,5,10,10,20]
輸出:false
解釋:
前 2 位顧客那裏,咱們按順序收取 2 張 5 美圓的鈔票。
對於接下來的 2 位顧客,咱們收取一張 10 美圓的鈔票,而後返還 5 美圓。
對於最後一位顧客,咱們沒法退回 15 美圓,由於咱們如今只有兩張 10 美圓的鈔票。
因爲不是每位顧客都獲得了正確的找零,因此答案是 false。

提示:

  • 0 <= bills.length <= 10000
  • bills[i] 不是 5 就是 10 或是 20 

Runtime: 104 ms
Memory Usage: 19 MB
 1 class Solution {
 2     func lemonadeChange(_ bills: [Int]) -> Bool {        
 3         var fiveLeft = 0 
 4         var tenLeft = 0
 5         var twentyLeft = 0
 6         for i in 0..<bills.count {
 7             if bills[i] == 5 {
 8                 fiveLeft += 1
 9             } else if bills[i] == 10 {
10                 tenLeft += 1
11                 fiveLeft -= 1
12             } else {
13                 twentyLeft += 1
14                 if tenLeft > 0 {
15                     tenLeft -= 1
16                     fiveLeft -= 1
17                 } else {
18                     fiveLeft -= 3
19                 }                
20             }
21             
22             if fiveLeft < 0 || tenLeft < 0 {
23                 return false
24             }            
25         }
26         
27         return true        
28     }
29 }

104ms

 1 class Solution {
 2     func lemonadeChange(_ bills: [Int]) -> Bool {
 3         if bills.count == 0 {
 4             return true
 5         }
 6         
 7         var fiveDollarBills = 0
 8         var tenDollarBills = 0
 9         
10         for bill in bills {
11             if bill == 5 {
12                 fiveDollarBills += 1
13             } else if bill == 10 {
14                 if fiveDollarBills == 0 {
15                     return false
16                 }
17                 fiveDollarBills -= 1
18                 tenDollarBills += 1
19             } else if bill == 20 {
20                 if tenDollarBills > 0 && fiveDollarBills > 0 {
21                     tenDollarBills -= 1
22                     fiveDollarBills -= 1
23                 } else if fiveDollarBills > 2 {
24                     fiveDollarBills -= 3
25                 } else {
26                     return false
27                 }
28             }
29         }
30         
31         return true
32     }
33 }

108ms

 1 class Solution {
 2     func lemonadeChange(_ bills: [Int]) -> Bool {        
 3         var fiveLeft = 0 
 4         var tenLeft = 0
 5         var twentyLeft = 0
 6         for i in 0..<bills.count {
 7             if bills[i] == 5 {
 8                 fiveLeft += 1
 9             } else if bills[i] == 10 {
10                 tenLeft += 1
11                 fiveLeft -= 1
12             } else {
13                 twentyLeft += 1
14                 if tenLeft > 0 {
15                     tenLeft -= 1
16                     fiveLeft -= 1
17                 } else {
18                     fiveLeft -= 3
19                 }                
20             }            
21             if fiveLeft < 0 {
22                 return false
23             }            
24         }        
25         return true        
26     }
27 }

108ms

 1 class Solution {
 2     func lemonadeChange(_ bills: [Int]) -> Bool {
 3         var c5 = 0
 4         var c10 = 0
 5 
 6         for i in bills {
 7             if i == 5{
 8                 c5 += 1
 9             }
10             else if i == 10{
11                 if c5 > 0{
12                     c5 -= 1
13                     c10 += 1
14                 }else{
15                     return false
16                 }
17             }else {
18                 if c5 > 0 && c10 > 0{
19                     c5 -= 1
20                     c10 -= 1
21                 }else if c5 > 3{
22                     c5 -= 3
23                 }else{
24                     return false
25                 }
26             }
27         }
28         return true
29     }
30 }

110ms

 1 class Solution {
 2    func lemonadeChange(_ bills: [Int]) -> Bool {
 3        
 4         var dic = [Int:Int]()
 5         dic.updateValue(0, forKey: 5)
 6         dic.updateValue(0, forKey: 10)
 7         dic.updateValue(0, forKey: 20)
 8         for i  in 0..<bills.count {
 9             if bills[i] - 5 == 0{
10                 dic.updateValue(dic[5]! + 1, forKey: 5)
11                 continue
12             }
13             let dif = bills[i] - 5
14             let tenCount =  dif/10
15             let fiveCount =  dif/5
16             let sfiveCount =  fiveCount - tenCount * 2
17             var needFive: Int = 0
18             
19             if dic[10]! - tenCount < 0{
20                  needFive = (tenCount - dic[10]!) * 2
21             }
22             if dic[5]! < needFive  + sfiveCount{
23                 return false
24             }else{
25                dic.updateValue(max(0, dic[5]! - needFive - sfiveCount), forKey: 5)
26             }
27             dic.updateValue(max(0, dic[10]! - tenCount), forKey: 10)
28             dic.updateValue(dic[bills[i]]!  + 1, forKey: bills[i])
29         }
30        return true
31     }
32 }

112ms

 1 class Solution {
 2     func lemonadeChange(_ bills: [Int]) -> Bool {
 3         var fiveSum = 0, tenSum = 0, twentySum = 0
 4         let five = 5, ten = 10, twenty = 20
 5         
 6         for bill in bills {
 7             switch bill {
 8             case five:
 9                 fiveSum += bill
10             case ten:
11                 if fiveSum > 0 {
12                     fiveSum -= five
13                     tenSum += bill
14                 } else {
15                     return false
16                 }
17             case twenty:
18                 if tenSum > 0 {
19                     if fiveSum > 0 {
20                         tenSum -= ten
21                         fiveSum -= five
22                     } else {
23                         return false
24                     }
25                 } else if fiveSum >= five * 3 {
26                     fiveSum -= five * 3
27                 } else {
28                     return false
29                 }
30                 
31                 twentySum += bill
32             default: break
33             }
34         }
35         
36         return true
37     }
38 }

120ms

 1 class Solution {
 2     func lemonadeChange(_ bills: [Int]) -> Bool {
 3         var bank = [5:0, 10:0]
 4         for bill in bills {
 5             if bill == 5 {
 6                 bank[5]! += 1
 7             }
 8 
 9             if bill == 10 {
10                 if bank[5]! == 0 {
11                     return false
12                 } else {
13                     bank[5]! -= 1
14                     bank[10]! += 1
15                 }
16             }
17 
18             if bill == 20 {
19                 if bank[10]! > 0 && bank[5]! > 0 {
20                     bank[10]! -= 1
21                     bank[5]! -= 1
22                 } else if bank[5]! >= 3 {
23                     bank[5]! -= 3
24                 } else {
25                     return false
26                 }
27             }
28         }
29         return true
30     }
31 }
相關文章
相關標籤/搜索