Home Account Balance After Rounded Purchase
Post
Cancel

Account Balance After Rounded Purchase

View Account Balance After Rounded Purchase on LeetCode

Statistics

Time Spent Coding
5 minutes

Time Complexity
O(1) - Only simple machine instructions are ran, and no operation iterates through the input, resulting in the O(1) time complexity.

Space Complexity
O(1) - Only one variable is created, resulting in the O(1) space complexity.

Runtime Beats
99.53% of other submissions

Memory Beats
99.01% of other sumbissions

Explanation

The comments explain the program and provide examples.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution:
    def accountBalanceAfterPurchase(self, cost: int) -> int:
        # Calculate and store remainder
        r = cost % 10

        # If the remainder is >= to 5, then round up by adding 10,
        # since the remainder is always added to the account
        if r >= 5:
            cost += 10
        
        # Return the account balance after purchase with adjusted price
        return 100 - cost + r

        ''' Examples
        Cost = 9
        Remainder = 9
        Account = 100 + 10 - 9 + 9

        Cost = 17
        Remainder = 7
        Account = 100 + 10 - 17 + 7

        Cost = 14
        Remainder = 4
        Account = 100 - 14 + 4
        '''
This post is licensed under CC BY 4.0 by the author.

Minimum Time Difference

Unique Number of Occurrences