Maximum Points Tourist Can Earn - Problem
Maximum Points Tourist Can Earn

Imagine you're planning the ultimate sightseeing adventure! 🏙️ You have n cities to explore over exactly k days, and you want to maximize your experience points.

Each day, you face an exciting decision:
Stay in your current city and earn stayScore[day][city] points
Travel to a different city and earn travelScore[currentCity][destinationCity] points

All cities are directly connected, so you can travel anywhere! Your mission is to find the maximum possible points you can earn during your k-day journey.

Goal: Return the maximum points achievable starting from any city.
Input: Two integers n, k and two 2D arrays stayScore, travelScore
Output: Maximum points possible

Input & Output

example_1.py — Basic Case
$ Input: n = 2, k = 1 stayScore = [[2, 3]] travelScore = [[0, 2], [1, 0]]
Output: 3
💡 Note: With only 1 day, we can either start in city 0 and stay (2 points) or start in city 1 and stay (3 points). Starting in city 1 and staying gives maximum 3 points.
example_2.py — Travel vs Stay
$ Input: n = 2, k = 2 stayScore = [[2, 3], [1, 4]] travelScore = [[0, 5], [3, 0]]
Output: 9
💡 Note: Optimal path: Start in city 0, travel to city 1 (5 points), then stay in city 1 (4 points). Total: 5 + 4 = 9 points.
example_3.py — Multiple Cities
$ Input: n = 3, k = 2 stayScore = [[1, 2, 3], [4, 5, 6]] travelScore = [[0, 2, 1], [3, 0, 2], [1, 4, 0]]
Output: 9
💡 Note: Start in city 2, stay day 0 (3 points), stay day 1 (6 points). Total: 3 + 6 = 9 points. This is optimal among all possible starting cities and action sequences.

Constraints

  • 1 ≤ n ≤ 200
  • 1 ≤ k ≤ 200
  • n × k ≤ 1000
  • 0 ≤ stayScore[i][j] ≤ 106
  • 0 ≤ travelScore[i][j] ≤ 106
  • travelScore[i][i] = 0 (no points for staying via travel)

Visualization

Tap to expand
Maximum Points Tourist Can Earn INPUT n = 2 (cities), k = 1 (days) stayScore[day][city]: day0: 2 3 C0 C1 travelScore[from][to]: C0 C1 2 1 self: 0 self: 0 [[0,2],[1,0]] ALGORITHM (DP) 1 Define State dp[day][city] = max pts 2 Initialize dp[0][c] = 0 for all c 3 Transition Stay: +stayScore Travel: +travelScore 4 Find Maximum max(dp[k][all cities]) DP Table (k=1): City C0 C1 d=0 0 0 d=1 2 3 C0: max(0+2, 0+1) = 2 C1: max(0+3, 0+2) = 3 FINAL RESULT Optimal Strategy: Start C1 Day 1: Stay in C1 +3 points Maximum Points: 3 OK - Verified Stay C1 (3) beats Travel (2) Stay C0 (2) is lower Key Insight: Dynamic Programming builds optimal solutions day-by-day. For each city on each day, we compute the maximum points by considering ALL previous cities - either staying (same city) or traveling (different city). Time: O(k * n^2) | Space: O(n) with rolling array optimization. TutorialsPoint - Maximum Points Tourist Can Earn | Dynamic Programming Approach
Asked in
Google 45 Amazon 38 Meta 29 Microsoft 22 Apple 18
43.7K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen