2844. Minimum Operations to Make a Special Number
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 38% Topics: Math, String, Greedy, Enumeration
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space; O(1)
# math, greedy
class Solution(object):
def minimumOperations(self, num):
"""
:type num: str
:rtype: int
"""
lookup = [0]*10
for i in reversed(xrange(len(num))):
if ((num[i] in "05" and lookup[0]) or
(num[i] in "27" and lookup[5])):
return (len(num)-i)-2
lookup[ord(num[i])-ord('0')] = 1
return len(num)-lookup[0]
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions