LeetCode 2725: Interval Cancellation Solution
Master LeetCode problem 2725 (Interval Cancellation), a easy 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.
2725. Interval Cancellation
Problem Explanation
Explanation
To solve this problem, we need to implement a function cancellable that takes in a function fn, an array of arguments args, and a time interval t. The function should call fn with args immediately, and then call it repeatedly every t milliseconds until a cancel function cancelFn is invoked after a specified delay.
The main idea is to use a timer to schedule the repeated calls to fn with the given arguments. We can achieve this by using setTimeout to set up a timer for each iteration, and clearing the timer when the cancel function is invoked.
Algorithm:
- Create a closure function
cancellablethat takesfn,args, andtas input and returns the cancel function. - Inside the closure function, schedule repeated calls to
fnwithargseverytmilliseconds until the cancel function is invoked. - When the cancel function is invoked, clear the timer to stop further executions of
fn.
Time Complexity:
- The time complexity of this algorithm is O(n), where n is the number of iterations before cancellation.
Space Complexity:
- The space complexity is O(1) as we are not using any additional data structures that grow with the input size.
Solution Code
import java.util.ArrayList;
import java.util.List;
public class IntervalCancellation {
public static Runnable cancellable(Function fn, Object[] args, int t) {
List<Integer> result = new ArrayList<>();
long startTime = System.currentTimeMillis();
Runnable cancelFn = () -> {
long cancelTimeMs = System.currentTimeMillis();
System.out.println("Cancelled at " + cancelTimeMs);
};
Runnable task = () -> {
long currentTime = System.currentTimeMillis();
result.add((int) fn.apply(args));
System.out.println("time: " + (currentTime - startTime) + ", returned: " + result.get(result.size() - 1));
if (currentTime - startTime < cancelTimeMs) {
new Thread(() -> cancellable(fn, args, t)).start();
}
};
new Thread(task).start();
return cancelFn;
}
public static void main(String[] args) {
Function fn = (Object[] x) -> (int) x[0] * 2;
Object[] arguments = {4};
int t = 35;
Runnable cancelFn = cancellable(fn, arguments, t);
}
@FunctionalInterface
interface Function {
int apply(Object[] args);
}
}Try It Yourself
Loading code editor...
Frequently Asked Questions
How to solve LeetCode 2725 (Interval Cancellation)?
This page provides optimized solutions for LeetCode problem 2725 (Interval Cancellation) 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 2725 (Interval Cancellation)?
The time complexity for LeetCode 2725 (Interval Cancellation) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2725 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2725 in Java, C++, or Python.