모든 챌린지
쉬움 0명 해결 정답률 0%

Bank Account Rules

Protect an account's balance behind deposit and withdraw rules, rejecting invalid operations.

배우게 될 내용

  • Change state only through methods
  • Reject invalid amounts and overdrafts
  • Keep a count of rejected operations

문제

번역본 보기

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

예시

예시 1
입력:
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].

예시 2
입력:
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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.