LeetCode 1972: First and Last Call On the Same Day Solution

Master LeetCode problem 1972 (First and Last Call On the Same Day), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1972. First and Last Call On the Same Day

Problem Explanation

Explanation:

To solve this problem, we can use binary search to find the first and last call on the same day. We can define a function check that checks if we can achieve the required goal with a given time window size. By iterating through the call times and applying the check function using binary search, we can find the minimum possible time window where the first and last calls occur on the same day.

Here are the steps:

  1. Define a function check that takes the call times, time window size, and current index as parameters.
  2. Initialize two pointers start and end to track the window.
  3. Iterate through the call times:
    • If the difference between the current call time and the start time is less than or equal to the time window, update the end pointer.
    • If the difference between the current call time and the start time is greater than the time window, update the start pointer.
    • Check if the first and last calls occur on the same day by comparing the end time of the window with the last call time.
  4. Use binary search to find the minimum time window size that satisfies the condition of the first and last calls occurring on the same day.

Time complexity: O(n log(max - min)), where n is the number of call times and max-min is the range of call times. Space complexity: O(1)

:

Solution Code

class Solution {
    public int firstAndLastCallOnSameDay(int[] calls) {
        int left = 0, right = 1000000000, result = -1;
        
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (check(calls, mid)) {
                result = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        
        return result;
    }
    
    private boolean check(int[] calls, int window) {
        int start = calls[0], end = calls[0];
        
        for (int i = 1; i < calls.length; i++) {
            if (calls[i] - start <= window) {
                end = calls[i];
            } else {
                start = calls[i];
            }
        }
        
        return end == calls[calls.length - 1];
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1972 (First and Last Call On the Same Day)?

This page provides optimized solutions for LeetCode problem 1972 (First and Last Call On the Same Day) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1972 (First and Last Call On the Same Day)?

The time complexity for LeetCode 1972 (First and Last Call On the Same Day) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1972 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1972 in Java, C++, or Python.

Back to LeetCode Solutions