Press "Enter" to skip to content

Posts published in “Binary Search”

花花酱 LeetCode 911. Online Election

Problem

n an election, theĀ i-thĀ vote was cast forĀ persons[i]Ā at timeĀ times[i].

Now, we would like to implement the following query function:Ā TopVotedCandidate.q(int t)Ā will return the number of the person that was leading the election at timeĀ t.

Votes cast at timeĀ tĀ will count towards our query.Ā  In the case of a tie, the most recent vote (among tied candidates) wins.

Example 1:

Input: ["TopVotedCandidate","q","q","q","q","q","q"], [[[0,1,1,0,0,1,0],[0,5,10,15,20,25,30]],[3],[12],[25],[15],[24],[8]]
Output: [null,0,1,1,0,0,1]
Explanation: 
At time 3, the votes are [0], and 0 is leading.
At time 12, the votes are [0,1,1], and 1 is leading.
At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.)
This continues for 3 more queries at time 15, 24, and 8.

Note:

  1. 1 <= persons.length = times.length <= 5000
  2. 0 <= persons[i] <= persons.length
  3. timesĀ is a strictly increasing array with all elements inĀ [0, 10^9].
  4. TopVotedCandidate.qĀ is called at mostĀ 10000Ā times per test case.
  5. TopVotedCandidate.q(int t)Ā is always called withĀ t >= times[0].

Solution: HashTable + Binary Search

Compute the leads for each t in times using a hash table.

binary search the upper bound of t, and return the lead of previous entry.

Time complexity: Constructor O(n), Query: O(logn)

Space complexity: O(n)

C++

花花酱 LeetCode 18. 4Sum

Problem

Given an arrayĀ numsĀ ofĀ nĀ integers and an integerĀ target, are there elementsĀ a,Ā b,Ā c, andĀ dĀ inĀ numsĀ such thatĀ aĀ +Ā bĀ +Ā cĀ +Ā dĀ =Ā target? Find all unique quadruplets in the array which gives the sum ofĀ target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]

Solution 1: Sorting + Binary Search

Time complexity: O(n^3 log n + klogk)

Space complexity: O(k)

C++

C++ opt

Solution 2: Sorting + HashTable

Time complexity: O(n^3 + klogk)

Space complexity: O(n + k)

C++

Related Problems

花花酱 LeetCode 668. Kth Smallest Number in Multiplication Table

Problem

Nearly every one have used theĀ Multiplication Table. But could you find out theĀ k-thĀ smallest number quickly from the multiplication table?

Given the heightĀ mĀ and the lengthĀ nĀ of aĀ m * nĀ Multiplication Table, and a positive integerĀ k, you need to return theĀ k-thĀ smallest number in this table.

Example 1:

Input: m = 3, n = 3, k = 5
Output: 
Explanation: 
The Multiplication Table:
1	2	3
2	4	6
3	6	9

The 5-th smallest number is 3 (1, 2, 2, 3, 3).

Example 2:

Input: m = 2, n = 3, k = 6
Output: 
Explanation: 
The Multiplication Table:
1	2	3
2	4	6

The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6).

Note:

  1. TheĀ mĀ andĀ nĀ will be in the range [1, 30000].
  2. TheĀ kĀ will be in the range [1, m * n]

Ā 

Solution 1: Nth element (MLE)

Time complexity: O(mn)

Space complexity: O(mn)

C++

Solution2 : Binary Search

Find first x such that there are k elements less or equal to x in the table.

Time complexity: O(m*log(m*n))

Space complexity: O(1)

C++

Java

Python3

RelatedĀ Problems

花花酱 SP5 Binary Search

Template:

Time complexity: O(log(r-l)) * O(f(m) + g(m))

Space complexity: O(1)

 

Slides:

Lower Bound / Upper Bound

Mentioned Problems

花花酱 LeetCode 875. Koko Eating Bananas

Problem

Koko loves to eat bananas.Ā  There areĀ NĀ piles of bananas, theĀ i-thĀ pile hasĀ piles[i]Ā bananas.Ā  The guards have gone and will come back inĀ HĀ hours.

Koko can decide her bananas-per-hour eating speed ofĀ K.Ā  Each hour, she chooses some pile of bananas, and eats K bananas from that pile.Ā  If the pile has less thanĀ KĀ bananas, she eats all of them instead, and won’t eat any more bananas during this hour.

Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.

Return the minimum integerĀ KĀ such that she can eat all the bananas withinĀ HĀ hours.

Example 1:

Input: piles = [3,6,7,11], H = 8
Output: 4

Example 2:

Input: piles = [30,11,23,4,20], H = 5
Output: 30

Example 3:

Input: piles = [30,11,23,4,20], H = 6
Output: 23

Note:

  • 1 <= piles.length <= 10^4
  • piles.length <= H <= 10^9
  • 1 <= piles[i] <= 10^9

Solution: Binary Search

search for the smallest k [1, max_pile_height] such that eating time h <= H.

Time complexity: O(nlogh)

Space complexity: O(1)