Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1709. Biggest Window Between Visits - Leetcode Solution

Code Implementation

from typing import List

class Solution:
    def biggestWindow(self, visits: List[int]) -> int:
        if not visits or len(visits) == 1:
            return 0
        max_gap = 0
        for i in range(1, len(visits)):
            gap = visits[i] - visits[i-1]
            max_gap = max(max_gap, gap)
        return max_gap
    
#include <vector>
#include <algorithm>

class Solution {
public:
    int biggestWindow(std::vector<int>& visits) {
        if (visits.size() <= 1) return 0;
        int max_gap = 0;
        for (size_t i = 1; i < visits.size(); ++i) {
            int gap = visits[i] - visits[i-1];
            max_gap = std::max(max_gap, gap);
        }
        return max_gap;
    }
};
    
class Solution {
    public int biggestWindow(int[] visits) {
        if (visits == null || visits.length <= 1) return 0;
        int maxGap = 0;
        for (int i = 1; i < visits.length; i++) {
            int gap = visits[i] - visits[i-1];
            if (gap > maxGap) {
                maxGap = gap;
            }
        }
        return maxGap;
    }
}
    
/**
 * @param {number[]} visits
 * @return {number}
 */
var biggestWindow = function(visits) {
    if (!visits || visits.length <= 1) return 0;
    let maxGap = 0;
    for (let i = 1; i < visits.length; i++) {
        let gap = visits[i] - visits[i - 1];
        if (gap > maxGap) {
            maxGap = gap;
        }
    }
    return maxGap;
};
    

Problem Description

Given an array visits representing the times (as integers) at which a user visited a website, your task is to find the largest time window (gap) between two consecutive visits. The array visits is sorted in ascending order and contains at least one integer. The result should be the maximum difference between any two consecutive elements in visits. If there is only one visit, return 0.

  • Each element in visits is unique and sorted in ascending order.
  • Do not reuse elements; only consider consecutive pairs.
  • There is always one valid solution.

Thought Process

The problem is essentially about finding the largest gap between two consecutive numbers in a sorted list. The brute-force approach would be to check all possible pairs, but since the array is sorted and only consecutive pairs matter, we can optimize by just checking each adjacent pair.

This is similar to looking at a timeline and asking: "Where is the longest period without a visit?" Since the visits are sorted, the biggest gap must be between two consecutive entries.

Solution Approach

  • Step 1: Check if the visits array has less than two elements. If so, return 0, as there are no consecutive pairs to compare.
  • Step 2: Initialize a variable (e.g., max_gap) to keep track of the largest gap found so far.
  • Step 3: Loop through the array, starting from the second element. For each index i, compute the gap as visits[i] - visits[i-1].
  • Step 4: If the current gap is greater than max_gap, update max_gap.
  • Step 5: After the loop, return max_gap.

This approach is efficient because we only need to look at each pair once, and we store only the maximum gap found.

Example Walkthrough

Suppose visits = [2, 5, 9, 15].

  • Compare 5 - 2 = 3
  • Compare 9 - 5 = 4
  • Compare 15 - 9 = 6

The gaps are 3, 4, and 6. The biggest gap is 6 (between 9 and 15). Thus, the function should return 6.

Time and Space Complexity

  • Brute-force approach: Checking all pairs would be O(n^2), but since only consecutive pairs matter, this is unnecessary.
  • Optimized approach: The loop runs once through the array, so the time complexity is O(n), where n is the length of visits.
  • Space complexity: Only a constant amount of extra space is used (for the max_gap variable), so space complexity is O(1).

Summary

The key insight is that since the visits array is sorted, the biggest window between visits will always be between two consecutive elements. By iterating once through the array and checking each adjacent pair, we can efficiently find the answer in linear time. This makes the solution both simple and elegant.