# Posts published in “Hard”

Problem:

Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits. You should try to optimize your time and space complexity.

Example 1:

nums1 = [3, 4, 6, 5]
nums2 = [9, 1, 2, 5, 8, 3]
k = 5
return [9, 8, 6, 5, 3]

Example 2:

nums1 = [6, 7]
nums2 = [6, 0, 4]
k = 5
return [6, 7, 6, 0, 4]

Example 3:

nums1 = [3, 9]
nums2 = [8, 9]
k = 3
return [9, 8, 9]

# Solution:

Time complexity: O(k * (n1+n2)^2)

Space complexity: O(n1+n2)

## Java

Problem:

There are two sorted arrays nums1 and nums2 of size m and n respectively.

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

Example 1:

Example 2:

Idea:

Binary Search

Time complexity: O(log(min(n1,n2)))

Space complexity: O(1)

# Related Problem:

Problem:

Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.

Example 1:

Note:

1. 2 <= len(nums) <= 10000.
2. 0 <= nums[i] < 1000000.
3. 1 <= k <= len(nums) * (len(nums) - 1) / 2.

Idea

Bucket sort

Binary search / dp

Solution

C++ / binary search

C++ / bucket sort w/ vector O(n^2)

Related Problems:

Problem:

On an infinite number line (x-axis), we drop given squares in the order they are given.

The i-th square dropped (positions[i] = (left, side_length)) is a square with the left-most point being positions[i][0] and sidelength positions[i][1].

The square is dropped with the bottom edge parallel to the number line, and from a higher height than all currently landed squares. We wait for each square to stick before dropping the next.

The squares are infinitely sticky on their bottom edge, and will remain fixed to any positive length surface they touch (either the number line or another square). Squares dropped adjacent to each other will not stick together prematurely.

Return a list ans of heights. Each height ans[i] represents the current highest height of any square we have dropped, after dropping squares represented by positions[0], positions[1], ..., positions[i].

Example 1:

Example 2:

Note:

• 1 <= positions.length <= 1000.
• 1 <= positions[i][0] <= 10^8.
• 1 <= positions[i][1] <= 10^6.

Idea:

Range query with map

Solution:

C++ map

C++ vector without merge

C++ / vector with merge (slower)

Related Problems:

Problem:

A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the following interfaces in an efficient manner.

• addRange(int left, int right) Adds the half-open interval [left, right), tracking every real number in that interval. Adding an interval that partially overlaps with currently tracked numbers should add any numbers in the interval [left, right) that are not already tracked.
• queryRange(int left, int right) Returns true if and only if every real number in the interval [left, right) is currently being tracked.
• removeRange(int left, int right) Stops tracking every real number currently being tracked in the interval [left, right).

Example 1:

Note:

• A half open interval [left, right) denotes all real numbers left <= x < right.
• 0 < left < right < 10^9 in all calls to addRange, queryRange, removeRange.
• The total number of calls to addRange in a single test case is at most 1000.
• The total number of calls to queryRange in a single test case is at most 5000.
• The total number of calls to removeRange in a single test case is at most 1000.

Idea:

map / ordered ranges

Solution:

C++ / vector

C++ / map

Related Problems: