Determining Profitability of Excursions - codepath/compsci_guides GitHub Wiki

Unit 7 Session 2 (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 25 mins
  • 🛠️ Topics: Binary Search, Arrays

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q: Can the list be empty?
    • A: No, the problem assumes the list will always have at least one element.
  • Q: What should be returned if no such value x exists?
    • A: Return -1 if the list is not profitable.
  • Q: Are all values in the list distinct?
    • A: The problem statement does not specify, so we assume there could be duplicate values.
HAPPY CASE
Input: excursion_counts = [3, 5]
Output: 2
Explanation: There are 2 values (3 and 5) that are greater than or equal to 2.

Input: excursion_counts = [1, 2, 2, 3, 5, 5]
Output: 3
Explanation: There are 3 values (3, 5, 5) that are greater than or equal to 3.

EDGE CASE
Input: excursion_counts = [0, 0]
Output: -1
Explanation: No numbers fit the criteria for x.

Input: excursion_counts = [0, 1, 3, 6]
Output: 2
Explanation: There are 2 values (3 and 6) that are greater than or equal to 2.

2: M-atch

Match what this problem looks like to known categories of problems, e.g., Linked List or Dynamic Programming, and strategies or patterns in those categories.

For Binary Search problems, we want to consider the following approaches:

  • Binary Search: Since the list is sorted, binary search can be used to efficiently determine if there exists a value x that satisfies the condition of profitability.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Use binary search to find the value of x where there are exactly x excursions with at least x passengers signed up.

1) Calculate the length of the list `n`.
2) Perform a binary search:
    a) Set `left` to 0 and `right` to `n-1`.
    b) While `left` is less than or equal to `right`, calculate `mid`.
    c) Calculate `x` as `n - mid`.
    d) If `excursion_counts[mid]` is greater than or equal to `x`, check if `mid` is 0 or the element before `mid` is less than `x`. If so, return `x`.
    e) If `excursion_counts[mid]` is less than `x`, adjust `left` to `mid + 1`.
    f) If `excursion_counts[mid]` is greater than or equal to `x`, adjust `right` to `mid - 1`.
3) If no such `x` is found, return `-1`.

⚠️ Common Mistakes

  • Failing to correctly handle cases where the number of excursions does not match any x.
  • Incorrectly setting the boundaries in the binary search.

4: I-mplement

Implement the code to solve the algorithm.

def is_profitable(excursion_counts):
    n = len(excursion_counts)
    
    # Binary search to find the special x
    left, right = 0, n - 1
    
    while left <= right:
        mid = left + (right + left) // 2
        x = n - mid
        
        if excursion_counts[mid] >= x:
            if mid == 0 or excursion_counts[mid - 1] < x:
                return x
            right = mid - 1
        else:
            left = mid + 1
    
    return -1

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Trace through your code with the input [3, 5]:
    • The binary search identifies x = 2 where there are exactly 2 excursions with at least 2 passengers.

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Assume N represents the length of the excursion_counts list.

  • Time Complexity: O(log N) because we are performing binary search.
  • Space Complexity: O(1) because we are using a constant amount of extra space.