Maximum Points Tourist Can Earn - Complete Solution Guide
Maximum Points Tourist Can Earn is LeetCode problem 3332, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given two integers, n and k , along with two 2D integer arrays, stayScore and travelScore . A tourist is visiting a country with n cities, where each city is directly connected to every other city. The tourist's journey consists of exactly k 0-indexed days, and they can choose any city as their starting point. Each day, the tourist has two choices: Stay in the current city : If the tourist stays in their current city curr during day i , they will earn stayScore[i][curr] points. Move to a
Detailed Explanation
The problem asks us to find the maximum points a tourist can earn while visiting `n` cities over `k` days. The tourist can either stay in the current city or move to another city each day. Staying in a city earns points based on `stayScore`, while moving earns points based on `travelScore`. The goal is to maximize the total points earned over the `k` days, considering all possible starting cities and travel/stay decisions.
Solution Approach
The solution uses dynamic programming to calculate the maximum points achievable at each city for each day. It maintains a 1D array `dp` of size `n`, where `dp[j]` represents the maximum points achievable at city `j` on the current day. The code iterates through the days from 1 to `k-1`, updating the `dp` array in each iteration. For each city `j`, it considers two options: staying in the current city or moving from another city. The `dp[j]` is updated with the maximum of these two options.
Step-by-Step Algorithm
- Step 1: Initialize `dp` array of size `n` with 0s. This array will store the maximum score achievable at each city on the current day.
- Step 2: For the first day (day 0), calculate the initial `dp` values for each city. For each city `j`, consider staying in that city (using `stayScore[0][j]`) and moving from any other city `s` (using `travelScore[s][j]`). The `dp[j]` will be the maximum of the stay score and the maximum travel score.
- Step 3: Iterate from day 1 to `k-1`. For each day `i`, create a new `dp` array called `new_dp`. This is needed to avoid overwriting previous day's values before all calculations for current day are finished.
- Step 4: For each city `j` on day `i`, calculate the score if the tourist stays at city `j` (`dp[j] + stayScore[i][j]`).
- Step 5: Calculate the maximum score if the tourist moves to city `j` from another city `c`. This involves iterating through all other cities `c` and calculating `dp[c] + travelScore[c][j]`, and selecting the maximum value among all these possible moves.
- Step 6: Update `new_dp[j]` with the maximum score achievable between staying at city `j` and moving to city `j` from other cities. In particular, `new_dp[j] = max(score_if_stay, max_score_if_move)`.
- Step 7: After calculating `new_dp` for all cities on day `i`, update `dp` with `new_dp`. This prepares for the next iteration.
- Step 8: After iterating through all `k` days, find the maximum value in the `dp` array, which represents the maximum points the tourist can earn.
Key Insights
- Insight 1: Dynamic programming is suitable for this optimization problem because the optimal solution for `k` days can be built upon the optimal solutions for `k-1` days.
- Insight 2: The state of the dynamic programming solution depends on the current day and the city the tourist is in.
- Insight 3: Since the number of cities (n) and days (k) are relatively small (<= 200), an O(n^2 * k) time complexity solution is acceptable.
Complexity Analysis
Time Complexity: O(n^2*k)
Space Complexity: O(n)
Topics
This problem involves: Array, Dynamic Programming, Matrix.
Companies
Asked at: DE Shaw.