Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Maximum Earnings From Taxi

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2008" 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

There are n points on a road you are driving your taxi on. The n points on the road are labeled from 1 to n in the direction you are going, and you want to drive from point 1 to point n to make money by picking up passengers. You cannot change the direction of the taxi. The passengers are represented by a 0-indexed 2D integer array rides, where rides[i] = [starti, endi, tipi] denotes the ith passenger requesting a ride from point starti to point endi who is willing to give a tipi dollar tip. For each passenger i you pick up, you earn endi - starti + tipi dollars. You may only drive at most one passenger at a time. Given n and rides, return the maximum number of dollars you can earn by picking up the passengers optimally. Note: You may drop off a passenger and pick up a different passenger at the same point. Example 1: Input: n = 5, rides = [[2,5,4],[1,5,1]] Output: 7 Explanation: We can pick up passenger 0 to earn 5 - 2 + 4 = 7 dollars. Example 2: Input: n = 20, rides = [[1,6,1],[3,10,2],[10,12,3],[11,12,2],[12,15,2],[13,18,1]] Output: 20 Explanation: We will pick up the following passengers: - Drive passenger 1 from point 3 to point 10 for a profit of 10 - 3 + 2 = 9 dollars. - Drive passenger 2 from point 10 to point 12 for a profit of 12 - 10 + 3 = 5 dollars. - Drive passenger 5 from point 13 to point 18 for a profit of 18 - 13 + 1 = 6 dollars. We earn 9 + 5 + 6 = 20 dollars in total. Constraints: 1 <= n <= 105 1 <= rides.length <= 3 * 104 rides[i].length == 3 1 <= starti < endi <= n 1 <= tipi <= 105

Explanation

Here's a solution to the problem:

  • Dynamic Programming: The core idea is to use dynamic programming to store the maximum earnings achievable at each point on the road. The DP state dp[i] represents the maximum dollars earned up to point i.
  • Sorting and Binary Search: We sort the rides based on their end points to efficiently process them. For each point i, we iterate through the rides ending at i. To decide whether to take a ride ending at point i, we compare the current maximum profit at point i with the profit of taking a ride ending at i + the best possible profit at point starti. Binary search or bisect module helps to find the profit at point starti in logarithmic time.
  • State Transition: The DP transitions are based on whether we take a ride or not. If we don't take a ride ending at point i, then dp[i] = dp[i-1]. If we do take a ride from start to end=i, then dp[i] = max(dp[i], dp[start] + (end - start + tip)).

  • Runtime Complexity: O(R log R + R log N + N), where R is the number of rides and N is the road length. Sorting rides takes O(R log R), binary search inside DP updates takes O(R log N), and the final DP iteration takes O(N).

  • Storage Complexity: O(N), due to the size of the DP table.

Code

    def max_taxi_earnings(n: int, rides: list[list[int]]) -> int:
    """
    Calculates the maximum dollars earned by picking up passengers optimally.

    Args:
        n: The number of points on the road.
        rides: A 2D integer array where rides[i] = [starti, endi, tipi].

    Returns:
        The maximum number of dollars you can earn.
    """

    rides.sort(key=lambda x: x[1])  # Sort rides by end point
    dp = [0] * (n + 1)  # dp[i] stores the maximum earnings up to point i

    j = 0
    for i in range(1, n + 1):
        dp[i] = dp[i - 1]  # Initialize with the previous maximum

        while j < len(rides) and rides[j][1] == i:
            start, end, tip = rides[j]
            dp[i] = max(dp[i], dp[start] + (end - start + tip))
            j += 1

    return dp[n]

More from this blog

C

Chatmagic blog

2894 posts