Skip to main content

Maximum Points Tourist Can Earn - Solution & Explanation

MediumArrayDynamic ProgrammingMatrix9 min readAsked at: De Shaw
Practice this problem

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 another city: If the tourist moves from their current city curr to city dest, they will earn travelScore[curr][dest] points.

Return the maximum possible points the tourist can earn.

 

Example 1:

Input: n = 2, k = 1, stayScore = [[2,3]], travelScore = [[0,2],[1,0]]

Output: 3

Explanation:

The tourist earns the maximum number of points by starting in city 1 and staying in that city.

Example 2:

Input: n = 3, k = 2, stayScore = [[3,4,2],[2,1,2]], travelScore = [[0,2,1],[2,0,4],[3,2,0]]

Output: 8

Explanation:

The tourist earns the maximum number of points by starting in city 1, staying in that city on day 0, and traveling to city 2 on day 1.

 

Constraints:

  • 1 <= n <= 200
  • 1 <= k <= 200
  • n == travelScore.length == travelScore[i].length == stayScore[i].length
  • k == stayScore.length
  • 1 <= stayScore[i][j] <= 100
  • 0 <= travelScore[i][j] <= 100
  • travelScore[i][i] == 0

Approach Overview

Problem Overview: You are given multiple cities and several travel days. Each day you can either stay in your current city to gain stayScore[day][city] or travel to another city to gain travelScore[from][to]. The goal is to plan movements across days to maximize the total points earned.

Approach 1: Brute Force Recursion (Exponential Time)

The most direct idea is to try every possible decision each day: either stay in the same city or travel to another one. A recursive function explores all combinations of city choices for every day. For each state (day, city), the recursion branches into staying or traveling to every other city.

This approach quickly becomes infeasible because the number of paths grows exponentially with the number of days and cities. Time complexity is O(n^k) in the worst case (where n is the number of cities and k is the number of days). Space complexity is O(k) due to recursion depth. This version mainly helps understand the decision structure before introducing dynamic programming.

Approach 2: Dynamic Programming (O(k * n^2))

Dynamic programming eliminates repeated calculations by storing the best score achievable for each state. Define dp[day][city] as the maximum points obtainable after finishing day days while ending in city. For every new day, evaluate two choices: stay in the same city or travel from any other city.

Staying adds stayScore[day][city] to dp[day-1][city]. Traveling checks all possible previous cities prev and updates the score using dp[day-1][prev] + travelScore[prev][city]. The algorithm iterates through days, then cities, and computes the best transition. Because each city considers all previous cities, the time complexity becomes O(k * n^2). The DP table requires O(k * n) space.

This solution naturally models the problem as a layered decision graph across days. Each layer represents cities for that day, and transitions represent travel or staying. The structure closely resembles problems solved with matrix transitions and state updates common in array-based DP.

Approach 3: Space Optimized Dynamic Programming (O(k * n^2), O(n) space)

The DP relation only depends on the previous day. Instead of storing a full k × n table, maintain two arrays: prev and curr. For each day, compute the best score for every city using values from prev, then swap arrays.

This reduces memory usage from O(k * n) to O(n) while preserving the same transition logic. Time complexity remains O(k * n^2) because each city still checks all possible previous cities when considering travel transitions.

Recommended for interviews: The dynamic programming approach is what interviewers expect. Starting with the brute-force recursion shows you understand the decision tree. Converting it into a DP state dp[day][city] demonstrates optimization skills and familiarity with classic multi-stage DP problems.

Approach 1: Dynamic Programming Approach

The problem can be tackled using dynamic programming by keeping a DP table `dp` where `dp[i][j]` represents the maximum points the tourist can earn on the `i`-th day when staying in city `j`. The key is to decide on each day whether to stay in the current city for the maximum 'stayScore' or travel to another city for added 'travelScore'.

This solution constructs a DP table where each entry dp[i][j] depicts the maximum score after the i-th day in city j. The update on day `i` depends on whether the previous day was spent in the same city for stayScore or coming from another city with travelScore. After initializing days, we loop through all cities, updating the dp[i][j] from each other city's dp[i-1][.] thus considering both staying and traveling.

Code

Python

C++

Java

Complexity

Time Complexity: O(k * n^2) since we iterate over k days and compare scores between every pair of n cities.

Space Complexity: O(k * n) for storing the dp table.

Try this approach in the editor →

Approach 2: Default Approach

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Dynamic Programming Approach

Time Complexity: O(k * n^2) since we iterate over k days and compare scores between every pair of n cities.

Space Complexity: O(k * n) for storing the dp table.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force RecursionO(n^k)O(k)Conceptual understanding of all possible city transitions
Dynamic Programming (DP Table)O(k * n^2)O(k * n)General optimal solution for maximizing points across days and cities
Space Optimized DPO(k * n^2)O(n)Preferred when memory usage matters while keeping the same DP logic

Video Solution

Leetcode Biweekly Contest 142 | 3332. Maximum Points Tourist Can Earn | Codefod • CodeFod • 398 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Maximum Points Tourist Can Earn easy or hard?
Maximum Points Tourist Can Earn is generally considered a medium-level dynamic programming problem. The main challenge is identifying the correct DP state and handling transitions between cities. Once the dp[day][city] definition is clear, the implementation becomes straightforward.
Maximum Points Tourist Can Earn Python/Java solution
Most implementations use a DP table or two rolling arrays. For each day, iterate through all cities and compute the best score by comparing staying in the same city versus traveling from other cities. The same logic works in Python, Java, and C++ with nested loops over days and cities.
How to solve Maximum Points Tourist Can Earn in O(n)?
The full problem cannot be solved in pure O(n) time because each city transition may depend on every other city from the previous day. The best practical solution uses dynamic programming with O(k * n^2) time. However, memory usage can be reduced to O(n) by storing only the previous day's DP values.
What is the best approach for Maximum Points Tourist Can Earn?
Dynamic programming is the most effective approach. Define dp[day][city] as the maximum points achievable after a given day while ending in a specific city. Each state considers staying in the same city or traveling from any other city. This leads to an O(k * n^2) time solution with O(k * n) space.
Is Maximum Points Tourist Can Earn asked at Google/Amazon/Meta?
Dynamic programming problems involving multi-day decisions and transition matrices frequently appear in interviews at companies like Google, Amazon, and Meta. Variations of city travel optimization and stage-based DP are common patterns used to evaluate problem decomposition and state design skills.
What data structure is used in Maximum Points Tourist Can Earn?
The core structure is a dynamic programming table or arrays that store the best score for each (day, city) state. The inputs are typically represented as 2D arrays or matrices for stayScore and travelScore. Iterating through these matrices efficiently is key to implementing the DP transitions.
What is the time complexity of Maximum Points Tourist Can Earn?
The optimal dynamic programming solution runs in O(k * n^2) time, where n is the number of cities and k is the number of days. For each day and destination city, the algorithm checks all possible previous cities to evaluate travel transitions. Space complexity is O(k * n), which can be optimized to O(n).

Ready to solve this problem?

Practice Maximum Points Tourist Can Earn with our built-in code editor and test cases.

Practice on FleetCode