Home Plus One
Post
Cancel

Plus One

Links

Go to my solution
Go to the question on LeetCode

My Thoughts

What Went Well
Surprisingly, my program was able to rank highly in solution statistics.

What I Learned
I learned multiple unique ways to mutate arrays, strings, and maps.

Comments
I knew this would not be the “optimal” solution, but I wanted to try and find the most Pythonic solution possible.

Solution Statistics

Time Spent Coding
10

Time Complexity
O(n) - Although there are six O(n) operations, we ignore multiples of n, resulting in the O(n) time complexity.

Space Complexity
O(n) - The function creates a map which is a duplicate of each element in the list, resulting in the O(n) space complexity.

Runtime Beats
88.42% of other submissions

Memory Beats
93.81% of other sumbissions

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        # # Convert each element of digits into a string
        # step1 = map(str,digits) # O(n)
        # # Join elements into a string
        # step2 = "".join(step1) # O(n)
        # # Convert string to an int
        # step3 = int(step2) # O(n)
        # # Add one to the int
        # step4 = step3 + 1 # O(1)
        # # Convert int to string
        # step5 = str(step4) # O(n)
        # # Make each character of the string into an element of an array
        # step6 = [*step5] # O(n)
        # # Convert each element in the array into an integer
        # step7 = map(int,step6) # O(n)
        # # Return the "array" (It actually returns a map object)
        # return step7
        return map(int,[*str(int("".join(map(str,digits))) + 1)])
This post is licensed under CC BY 4.0 by the author.

Jewels and Stones

Diameter of Binary Tree