Solve the following three algorithmic tasks:
-
Unique triplet sum: Given an integer array nums and an integer target (default
0), return all unique triplets [a, b, c] such that a + b + c = target with no duplicate triplets. Explain an O(n^
-
approach using sorting and two pointers, and detail how you handle duplicates and edge cases.
-
K most common values: Given an integer array nums and an integer k, return the k values with highest frequency. If two values share the same frequency, order them by value descending in the output. Implement an O(n log k) solution using a min-heap of size k (e.g., Python’s heapq) and explain how you produce final descending order.
-
Equalize root-to-leaf path sums with minimal increments: You are given a complete binary tree represented as an array costs where costs[i] is the nonnegative cost at node i. In one operation you may increment any node’s cost by 1. Compute the minimum total increments required so that every root-to-leaf path has the same sum. Provide an algorithm and analyze its time and space complexity.