LeetCode 2723: Add Two Promises Solution

Master LeetCode problem 2723 (Add Two Promises), 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.

2723. Add Two Promises

Easy

Problem Explanation

Explanation

We need to create a new promise that resolves with the sum of the values resolved by promise1 and promise2. We can achieve this by creating a new promise that chains the resolutions of promise1 and promise2, then calculates the sum and resolves with the result.

  • Create a new promise that chains the resolutions of promise1 and promise2.
  • Once both promises have resolved, calculate the sum of the two values.
  • Resolve the new promise with the sum.

Time Complexity

The time complexity of this solution is dependent on the time taken by promise1 and promise2 to resolve.

Space Complexity

The space complexity is constant O(1) as we are not using any additional data structures.

Solution Code

import java.util.concurrent.CompletableFuture;

public class Solution {
    public CompletableFuture<Integer> addTwoPromises(CompletableFuture<Integer> promise1, CompletableFuture<Integer> promise2) {
        return promise1.thenCombine(promise2, Integer::sum);
    }
}

Try It Yourself

Loading code editor...

Frequently Asked Questions

How to solve LeetCode 2723 (Add Two Promises)?

This page provides optimized solutions for LeetCode problem 2723 (Add Two Promises) 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 2723 (Add Two Promises)?

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

Can I run code for LeetCode 2723 on DevExCode?

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

Back to LeetCode Solutions