Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Maximum Points Tourist Can Earn

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3332" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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

Explanation

Here's the solution:

  • Dynamic Programming: We use dynamic programming to store the maximum score achievable at each city on each day. dp[i][j] represents the maximum score at city j on day i.
  • Transitions: For each day and each city, we calculate the maximum score by either staying in the current city or traveling from another city.
  • Initialization: We initialize the DP table based on the starting city.

  • Runtime Complexity: O(n2k), Storage Complexity: O(nk)

Code

    def max_score(n: int, k: int, stayScore: list[list[int]], travelScore: list[list[int]]) -> int:
    """
    Calculates the maximum possible points a tourist can earn in a country with n cities over k days.

    Args:
        n: The number of cities.
        k: The number of days.
        stayScore: A 2D array where stayScore[i][j] is the score for staying in city j on day i.
        travelScore: A 2D array where travelScore[i][j] is the score for traveling from city i to city j.

    Returns:
        The maximum possible points the tourist can earn.
    """

    dp = [[0] * n for _ in range(k)]

    # Initialize the first day's scores
    for city in range(n):
        dp[0][city] = stayScore[0][city]

    # Iterate over the remaining days
    for day in range(1, k):
        for curr_city in range(n):
            # Option 1: Stay in the current city
            dp[day][curr_city] = dp[day - 1][curr_city] + stayScore[day][curr_city]

            # Option 2: Travel from another city
            for prev_city in range(n):
                if prev_city != curr_city:
                    dp[day][curr_city] = max(dp[day][curr_city], dp[day - 1][prev_city] + travelScore[prev_city][curr_city])

    # Find the maximum score on the last day
    max_total_score = 0
    for city in range(n):
        max_total_score = max(max_total_score, dp[k - 1][city])

    return max_total_score

More from this blog

C

Chatmagic blog

2894 posts