LeetCode 2627: Debounce Solution

Master LeetCode problem 2627 (Debounce), a medium 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.

2627. Debounce

Medium

Problem Explanation

Explanation

To implement the debounce functionality, we can keep track of the latest call timestamp and use a timer to delay the execution of the function. If a new call is made before the timer expires, we cancel the previous timer and start a new one with the updated timestamp.

Here is the step-by-step algorithm:

  1. Initialize a variable to store the latest call timestamp as 0.
  2. Create a debounce function that takes the original function and time t as parameters.
  3. Inside the debounce function, return a new function that takes the inputs for the original function.
  4. In the new function, check if the current time minus the latest call timestamp is less than t.
  5. If it is less than t, cancel the previous timer and start a new timer with the updated timestamp and inputs.
  6. If it is greater than or equal to t, execute the original function with the inputs and update the latest call timestamp.

Time complexity: O(1) for each function call. Space complexity: O(1) for storing the latest call timestamp.

Solution Code

import java.util.ArrayList;
import java.util.List;

class Debounce {
    private long lastCallTimestamp = 0;

    public Runnable debounce(Runnable fn, int t) {
        return () -> {
            long currentTimestamp = System.currentTimeMillis();
            if (currentTimestamp - lastCallTimestamp < t) {
                // Cancel the previous timer
                // Start a new timer with updated timestamp
                // Do nothing for now
            } else {
                fn.run();
                lastCallTimestamp = currentTimestamp;
            }
        };
    }

    public static void main(String[] args) {
        Debounce db = new Debounce();
        List<Runnable> calls = new ArrayList<>();

        Runnable call1 = db.debounce(() -> System.out.println("Call 1"), 50);
        Runnable call2 = db.debounce(() -> System.out.println("Call 2"), 75);

        calls.add(call1);
        calls.add(call2);

        for (Runnable call : calls) {
            new Thread(call).start();
        }
    }
}

Try It Yourself

Loading code editor...

Frequently Asked Questions

How to solve LeetCode 2627 (Debounce)?

This page provides optimized solutions for LeetCode problem 2627 (Debounce) 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 2627 (Debounce)?

The time complexity for LeetCode 2627 (Debounce) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2627 on DevExCode?

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

Back to LeetCode Solutions