Link: https://leetcode.com/problems/most-expensive-item-that-can-not-be-bought/
Solution:
Intuition
Not a huge fan of these mathy problems, but this one is pretty neat. We start with one key observation: all numbers after primeOne * primeTwo
can be formed. Why is this the case? It’s some number theory proof that I have no interest in getting into.
So now that we know the upper bound is primeOne * primeTwo
, we can use DP on this problem to find the largest number that cannot be formed.
Implementation (DP)
There is also an o(1)
math solution. I suspected this to be the case when I was solving, but I’m not very well versed in number theory. There exists a theorem called the chicken mcnugget theorem. The theorem states that the largest number that can not be formed with two distinct primes can be directly computed: mn - m - n
. Yes, its that simple.
Implementation (math)