Most Expensive Item That Can Not Be Bought — LeetCode 2979 Python Solution
MediumLeetCode PremiumMathDynamic ProgrammingNumber Theory
- Problem
- #2979
- Pattern
- Dynamic Programming
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given two distinct prime numbers primeOne and primeTwo. Alice and Bob are visiting a market.
Example
- Input
- primeOne = 2, primeTwo = 5
- Output
- 3
- Explanation
- The prices of items which cannot be bought are [1,3]. It can be shown that all items with a price greater than 3 can be bought using a combination of coins of denominations 2 and 5.
Python solution
Python
class Solution:
def mostExpensiveItem(self, primeOne: int, primeTwo: int) -> int:
return primeOne * primeTwo - primeOne - primeTwoComplexity
| Measure | Complexity |
|---|---|
| Time | O(1) |
| Space | O(1) auxiliary |
Pattern: Dynamic Programming
Define a state, write the transition, and stop recomputing the same subproblem. LeetCode 2979. Most Expensive Item That Can Not Be Bought is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Dynamic Programming.
The dynamic programming guide has the Python template for the pattern and the 481 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2979. Most Expensive Item That Can Not Be Bought?
- LeetCode 2979. Most Expensive Item That Can Not Be Bought is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2979. Most Expensive Item That Can Not Be Bought?
- The Python solution on this page runs in O(1).
- What is the space complexity of LeetCode 2979. Most Expensive Item That Can Not Be Bought?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2979. Most Expensive Item That Can Not Be Bought cover?
- LeetCode 2979. Most Expensive Item That Can Not Be Bought is tagged Math, Dynamic Programming and Number Theory on LeetCode.
- Is LeetCode 2979. Most Expensive Item That Can Not Be Bought a premium problem?
- Yes. LeetCode 2979. Most Expensive Item That Can Not Be Bought is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.