< Summary

Information
Class: LeetCode.Algorithms.DesignAnATMMachine.DesignAnATMMachineGreedy
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignAnATMMachine\DesignAnATMMachineGreedy.cs
Line coverage
100%
Covered lines: 28
Uncovered lines: 0
Coverable lines: 28
Total lines: 68
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor()100%11100%
Deposit(...)100%22100%
Withdraw(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignAnATMMachine\DesignAnATMMachineGreedy.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.DesignAnATMMachine;
 13
 14/// <inheritdoc />
 15public sealed class DesignAnATMMachineGreedy : IDesignAnATMMachine
 16{
 17    private const int BanknotesCount = 5;
 118    private static readonly int[] InvalidOperation = [-1];
 1419    private readonly int[] _banknotes = [20, 50, 100, 200, 500];
 1420    private readonly long[] _banknotesCounts = new long[BanknotesCount];
 21
 22    /// <summary>
 23    ///     Time complexity - O(1)
 24    ///     Space complexity - O(1)
 25    /// </summary>
 26    /// <param name="banknotesCounts"></param>
 27    public void Deposit(int[] banknotesCounts)
 1628    {
 19229        for (var i = 0; i < 5; i++)
 8030        {
 8031            _banknotesCounts[i] += banknotesCounts[i];
 8032        }
 1633    }
 34
 35    /// <summary>
 36    ///     Time complexity - O(1)
 37    ///     Space complexity - O(1)
 38    /// </summary>
 39    /// <param name="amount"></param>
 40    /// <returns></returns>
 41    public int[] Withdraw(int amount)
 1942    {
 1943        var used = new int[BanknotesCount];
 44
 1945        var remaining = amount;
 46
 22847        for (var i = used.Length - 1; i >= 0; i--)
 9548        {
 9549            var canTakeByAmount = remaining / _banknotes[i];
 9550            var canTake = (int)Math.Min(_banknotesCounts[i], canTakeByAmount);
 51
 9552            used[i] = canTake;
 9553            remaining -= used[i] * _banknotes[i];
 9554        }
 55
 1956        if (remaining != 0)
 757        {
 758            return InvalidOperation;
 59        }
 60
 14461        for (var i = 0; i < BanknotesCount; i++)
 6062        {
 6063            _banknotesCounts[i] -= used[i];
 6064        }
 65
 1266        return used;
 1967    }
 68}