समस्या
Encapsulation किसी object के data को private रखती है और उसे केवल methods के माध्यम से बदलती है जो rules को enforce करते हैं। एक bank account का balance केवल deposit और withdraw के माध्यम से बदल सकता है।
0 के balance से शुरू करें और operations को process करें, प्रत्येक deposit <amount> या withdraw <amount>:
- 0 या negative amount अस्वीकार किया जाता है।
- current balance से बड़ी withdrawal अस्वीकार की जाती है।
- अन्यथा balance बदल जाता है।
bank_account_rules(operations) को implement करें।
Input
operations: Operations, order में।
Output
[final_balance, rejected_count] लौटाएँ।
सीमाएँ
0 ≤ ऑपरेशन की संख्या ≤ 100,000
−10^9 ≤ रकम ≤ 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 से 70 बचते हैं; 200 निकालना account को overdraw कर देगा, इसलिए इसे अस्वीकार किया जाता है: [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};
व्याख्या
Negative deposit और zero withdrawal दोनों अस्वीकार किए जाते हैं; last deposit सफल होता है: [10, 2].
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
बैलेंस को प्राइवेट रखें और उसे सिर्फ़ deposit और withdraw में बदलें।
संकेत 2
सबसे पहले 0 या उससे कम रकम ठुकराएँ।
संकेत 3
निकासी से बैलेंस शून्य से नीचे नहीं जाना चाहिए।
अपेक्षित जटिलता
समय O(ऑपरेशन की संख्या), अतिरिक्त स्थान O(1)
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।