โจทย์
Encapsulation keeps an object's data private and only changes it through methods that enforce the rules. A bank account's balance can only change through deposit and withdraw.
Start with a balance of 0 and process the operations, each deposit <amount> or withdraw <amount>:
- An amount that is 0 or negative is rejected.
- A withdrawal larger than the current balance is rejected.
- Otherwise the balance changes.
Implement bank_account_rules(operations).
Input
operations: The operations, in order.
Output
Return [final_balance, rejected_count].
เงื่อนไข
0 ≤ number of operations ≤ 100,000
−10^9 ≤ amount ≤ 10^9
ตัวอย่าง
อินพุต:
operations = ["deposit 100", "withdraw 30", "withdraw 200"]
ผลลัพธ์ที่คาดหวัง:
expected = [70, 1]
อินพุต:
std::vector<std::string> operations = {"deposit 100", "withdraw 30", "withdraw 200"};
ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {70, 1};
อินพุต:
string[] operations = new string[] {"deposit 100", "withdraw 30", "withdraw 200"};
ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {70, 1};
คำอธิบาย
100 − 30 leaves 70; withdrawing 200 would overdraw the account, so it is rejected: [70, 1].
อินพุต:
operations = ["deposit -5", "withdraw 0", "deposit 10"]
ผลลัพธ์ที่คาดหวัง:
expected = [10, 2]
อินพุต:
std::vector<std::string> operations = {"deposit -5", "withdraw 0", "deposit 10"};
ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {10, 2};
อินพุต:
string[] operations = new string[] {"deposit -5", "withdraw 0", "deposit 10"};
ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {10, 2};
คำอธิบาย
The negative deposit and the zero withdrawal are both rejected; the last deposit succeeds: [10, 2].
แนวทางการแก้
ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ
คำใบ้ 1
Keep the balance private and change it only in deposit and withdraw.
คำใบ้ 2
Reject amounts of 0 or less before anything else.
คำใบ้ 3
A withdrawal may not take the balance below zero.
ความซับซ้อนที่คาดหวัง
O(number of operations) time, O(1) extra space
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน