Press "Enter" to skip to content

Posts published in “Algorithms”

花花酱 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 189. Rotate Array

Problem

Given an array, rotate the array to the right byĀ kĀ steps, whereĀ kĀ is non-negative.

Example 1:

Input: [1,2,3,4,5,6,7] and k = 3 
Output: [5,6,7,1,2,3,4]
Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] 
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input:[-1,-100,3,99] and k = 2 
Output: [3,99,-1,-100] 
Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100]

Note:

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

Solution 1: Simulate rotation with three reverses.

If k >= n, rotating k times has the same effect as rotating k % n times.

[1,2,3,4,5,6,7], K = 3

[5,6,7,1,2,3,4]

We can simulate the rotation with three reverses.

  1. reverse the whole array O(n) [7,6,5,4,3,2,1]
  2. reverse the left part 0 ~ k – 1 O(k) [5,6,7,4,3,2,1]
  3. reverse the right part k ~ n – 1 O(n-k)Ā [5,6,7,1,2,3,4]

Time complexity: O(n)

Space complexity: O(1) in-place

C++

 

花花酱 LeetCode 881. Random Flip Matrix

Problem

You are given the number of rowsĀ n_rowsĀ and number of columnsĀ n_colsĀ of aĀ 2DĀ binary matrixĀ where all values are initially 0.Ā Write a functionĀ flipĀ which choosesĀ a 0 valueĀ uniformly at random,Ā changes it to 1,Ā and then returns the positionĀ [row.id, col.id]Ā of that value. Also, write a functionĀ resetĀ which sets all values back to 0.Ā Try to minimize the number of calls to system’s Math.random()Ā and optimize the time andĀ space complexity.

Note:

  1. 1 <= n_rows, n_colsĀ <= 10000
  2. 0 <= row.id < n_rowsĀ andĀ 0 <= col.id < n_cols
  3. flipĀ will not be called when the matrix has noĀ 0 values left.
  4. the total number of calls toĀ flipĀ andĀ resetĀ will not exceedĀ 1000.

Example 1:

Input: 
["Solution","flip","flip","flip","flip"]
[[2,3],[],[],[],[]]
Output: [null,[0,1],[1,2],[1,0],[1,1]]

Example 2:

Input: 
["Solution","flip","flip","reset","flip"]
[[1,2],[],[],[],[]]
Output: [null,[0,0],[0,1],null,[0,0]]

Explanation of Input Syntax:

The input is two lists:Ā the subroutines calledĀ and theirĀ arguments.Ā Solution‘s constructorĀ has two arguments,Ā n_rowsĀ andĀ n_cols.Ā flipĀ andĀ resetĀ haveĀ noĀ arguments.Ā ArgumentsĀ areĀ always wrapped with a list, even if there aren’t any.

Solution 1: Hashtable + Resample

Time complexity: O(|flip|) = O(1000) = O(1)

Space complexity: O(|flip|) =Ā O(1000) = O(1)

Solution 2:Ā Fisherā€“Yates shuffle

Generate a random shuffle of 0 to n – 1, one number at a time.

Time complexity: flip: O(1)

Space complexity: O(|flip|) = O(1000) = O(1)

C++

 

花花酱 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)

 

花花酱 LeetCode 739. Daily Temperatures

Problem

Given a list of dailyĀ temperatures, produce a list that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, putĀ 0Ā instead.

For example, given the listĀ temperatures = [73, 74, 75, 71, 69, 72, 76, 73], your output should beĀ [1, 1, 4, 2, 1, 1, 0, 0].

Note:Ā The length ofĀ temperaturesĀ will be in the rangeĀ [1, 30000]. Each temperature will be an integer in the rangeĀ [30, 100].

Solution: Stack

Use a stack to track indices of future warmer days. From top to bottom: recent to far away.

Time complexity: O(n)

Space complexity: O(n)