Given an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.
Input: nums = [4,3,2,3,5,2,1], k = 4
Output: true
Explanation: It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Determine if it is possible to divide the array into k non-empty subsets with equal sums.
The backtracking approach still has overlapping subproblems, suggesting a dynamic programming solution.
1 << nums.size()
. Each bit of the mask represents whether an element is included in the subset.(i & (1 << j)) == 0
).dp[i ^ (1 << j)]
) is valid, and adding the j-th element does not exceed the target sum.Time Complexity: O(2^n * n), where n is the size of the input array.
Space Complexity: O(2^n).
Given the root of a binary tree, the task is to flatten the tree into a "linked list" using the same TreeNode class. The flattened "linked list" should follow the same order as a pre-order traversal of the binary tree.
Consider the binary tree:
After applying the flatten function, the tree is transformed into:
In a brute force approach, we can perform a pre-order traversal of the binary tree and construct a new linked list using the elements in the order of traversal. This can be done by traversing the tree and creating a new node for each encountered element, linking the nodes in pre-order.
Time complexity: O(n)
The pre-order traversal visits each node once.
Space complexity: O(n)
The space required for the pre-order traversal list is proportional to the number of nodes in the tree.
The strategy is to recursively flatten the right subtree first, and then handle the left subtree. If a node has a left child, it means there is a subtree on the left that needs to be flattened. To preserve the structure, the right child of the current node is linked to the rightmost node of the left subtree before making the left child the new right child. The left child is set to null after this operation.
Time complexity: O(n) - The function visits each node once.
Space complexity: O(h) - The space complexity is dominated by the recursion stack, which is proportional to the height of the binary tree (h). In the worst case, for a skewed tree, it could be O(n), but in balanced trees, it is typically O(log n). The in-place transformation is done without using additional space proportional to the number of nodes in the tree.
Implement the RandomizedSet
class:
RandomizedSet()
Initializes the RandomizedSet object.bool insert(int val)
Inserts an item val
into the set if not present. Returns true
if the item was not present, false
otherwise.bool remove(int val)
Removes an item val
from the set if present. Returns true
if the item was present, false
otherwise.int getRandom()
Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.You must implement the functions of the class such that each function works in average O(1) time complexity.
For implementing the RandomizedSet class, we can use both an unordered map (mymap
) and a vector (list
). This combination allows for constant-time average complexity for the insert, remove, and getRandom operations.
vector (list):
unordered_map (mymap):
Insert Operation:
mymap
.false
.mymap
with its index in list
.list
.true
to indicate a successful insertion.Remove Operation:
mymap
.mymap
.list
.mymap
to reflect the new index of the last value.list
.mymap
.true
to indicate a successful removal.false
.GetRandom Operation:
list
.Insert and Remove
mymap
and list
.GetRandom
list
.Space Complexity
mymap
and list
grow with the size of the set.You are given an array A, where the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit you can achieve by completing at most 2 transactions. Note that you cannot engage in multiple transactions at the same time, meaning you must sell the stock before buying again.
Input: [3, 3, 5, 0, 0, 3, 1, 4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), then buy on day 7 (price = 1) and sell on day 8 (price = 4). Total profit = 3 + 3 = 6.
One way to approach this problem is to consider every possible pair of transactions and calculate the profit for each pair. Keep track of the maximum profit obtained. However, this approach would have a time complexity of O(n^4), making it impractical for large inputs.
To optimize the solution, we can observe that we need to keep track of four variables for each day:
FirstBuy: The minimum price when buying for the first time.
FirstSell: The maximum profit when selling for the first time.
SecondBuy: The minimum price when buying for the second time.
SecondSell: The maximum profit when selling for the second time.
For each day, we simultaneously update these variables based on the current stock price.