Sum of Numbers With Units Digit K — LeetCode 2310 Python Solution
MediumGreedyMathDynamic ProgrammingEnumeration
- Problem
- #2310
- Pattern
- Greedy
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given two integers num and k, consider a set of positive integers with the following properties: The units digit of each integer is k. The sum of the integers is num.
Example
- Input
- num = 58, k = 9
- Output
- 2
- Explanation
- One valid set is [9,49], as the sum is 58 and each integer has a units digit of 9.
Python solution
Python
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if num == 0:
return 0
for i in range(1, num + 1):
if (t := num - k * i) >= 0 and t % 10 == 0:
return i
return -1Complexity
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the size of \textit{num} |
| Space | O(1) auxiliary |
Pattern: Greedy
Take the locally best option every time — when you can prove that never costs you later. LeetCode 2310. Sum of Numbers With Units Digit K is filed here because LeetCode tags it Greedy, which is the vocabulary this hub collects.
The greedy guide has the Python template for the pattern and the 346 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2310. Sum of Numbers With Units Digit K?
- LeetCode 2310. Sum of Numbers With Units Digit K is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2310. Sum of Numbers With Units Digit K?
- The Python solution on this page runs in O(n), where n is the size of \textit{num}.
- What is the space complexity of LeetCode 2310. Sum of Numbers With Units Digit K?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2310. Sum of Numbers With Units Digit K cover?
- LeetCode 2310. Sum of Numbers With Units Digit K is tagged Greedy, Math, Dynamic Programming and Enumeration on LeetCode.