Most Expensive Item That Can Not Be Bought — LeetCode 2979 Python Solution

MediumLeetCode PremiumMathDynamic ProgrammingNumber Theory
Problem
#2979
Reading time
2 min

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 - primeTwo

Complexity

MeasureComplexity
TimeO(1)
SpaceO(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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview