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: 8
๐Ÿ’ก 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. Wait, let me recalculate: Start city 1, stay (3 points), stay again (4 points) = 7. Or start city 0, travel to city 1 (5 points), stay (4 points) = 9. But we need to check all starting positions properly.
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
City ACity BCity CDay 0Stay: +10 ptsTravel: +5 ptsDynamic Programmingmemo[day][city] = max_pointsBuild solution incrementally๐ŸŽฏ Find the Maximum Score Path!
Understanding the Visualization
1
Choose Starting City
Pick any city to begin your k-day adventure
2
Daily Decision
Each day: stay and get local experience points, or travel for journey points
3
Build Optimal Path
Use DP to remember best score reachable at each (day, city) combination
4
Find Maximum
Return the highest score achievable across all starting cities
Key Takeaway
๐ŸŽฏ Key Insight: This is a classic dynamic programming problem where we build the optimal solution by making the best local decisions at each step, using memoization to avoid redundant calculations.
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