Minimum Days to Score Exactly N Points Problem
Description
LeetCode Problem 4050.
You are given an integer n representing a target score.
Your score starts at 0, and each day you either earn points or skip.
Points are earned during a streak. On the first day of a streak you earn 1 point, on the second day 2 points, on the third day 3 points, and so on. Skipping a day earns nothing and resets the streak, so the next time you earn points, you start from 1 again.
Return the minimum number of days, including any skipped days, needed to reach a score of exactly n.
Example 1:
Input: n = 2
Output: 3
Explanation:
- Day 1: earn 1 point. Score is 1.
- Day 2: skip, which resets the streak. Earning here would add 2 points and take the score past n = 2.
- Day 3: the streak has reset, so earning gives 1 point. Score is exactly n = 2 in 3 days.
Example 2:
Input: n = 9
Output: 6
Explanation:
- Days 1 to 3: earn 1, 2, and 3 points. Score is 1 + 2 + 3 = 6.
- Day 4: skip, which resets the streak.
- Days 5 and 6: earn 1 and 2 points. Score is exactly 6 + 1 + 2 = 9 in 6 days.
Example 3:
Input: n = 12
Output: 7
Explanation:
- Days 1 to 3: earn 1, 2, and 3 points. Score is 1 + 2 + 3 = 6.
- Day 4: skip, which resets the streak.
- Days 5 to 7: earn 1, 2, and 3 points. Score is exactly 6 + 1 + 2 + 3 = 12 in 7 days.
Constraints:
1 <= n <= 10^5
Solution
Approach: Dynamic Programming
A streak of j earning days scores the triangular number s = j * (j + 1) / 2. Two streaks must be separated by one skipped day, while the last streak needs no skip after it.
Let f[i] be the minimum number of days to score exactly i. Set f[0] = -1 and everything else to infinity. Try every length j of the last streak, with score s <= i:
f[i] = min(f[i], f[i - s] + j + 1)
The j + 1 counts the j earning days plus one skip. Starting from f[0] = -1 cancels the skip that does not exist after the last streak: a single streak of length j that scores n gives f[n] = -1 + j + 1 = j.
Since j is at most about sqrt(2n), we precompute f up to the limit once and answer each query in O(1).
The Python version runs the same recurrence on bitsets, which is much faster there. Let reach[c] be the set of scores whose streaks cost exactly c days in total, counting j + 1 for a streak of length j, stored as the bits of a big integer. Then reach[0] = {0} and reach[c] is the union of reach[c - j - 1] shifted left by j * (j + 1) / 2. The first c whose set contains n gives the answer c - 1. The answer is about sqrt(2n), so this takes O(n) shifts of n-bit integers, each of them fast.
Complexity: O(n * sqrt(n)) preprocessing and O(n) space; O(1) per query. The Python bitset version does O(n) shifts of n-bit integers and uses O(n * sqrt(n)) bits.
class Solution:
def minDays(self, n: int) -> int:
# reach[c]: bitset of the scores whose streaks cost exactly c, where a streak of
# length j costs j + 1 days (its earning days plus one skip)
mask = (1 << (n + 1)) - 1
reach = [1]
c = 0
while True:
c += 1
cur = 0
j = 1
while j + 1 <= c and j * (j + 1) // 2 <= n:
cur |= reach[c - j - 1] << (j * (j + 1) // 2)
j += 1
cur &= mask
reach.append(cur)
if cur >> n & 1:
return c - 1 # the last streak needs no skip after it