Press "Enter" to skip to content

Posts tagged as “medium”

花花酱 LeetCode 638. Shopping Offers

Problem

In LeetCode Store, there are some kinds of items to sell. Each item has a price.

However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.

You are given the each item’s price, a set of special offers, and the number we need to buy for each item. The job is to output the lowest price you have to pay forĀ exactlyĀ certain items as given, where you could make optimal use of the special offers.

Each special offer is represented in the form of an array, the last number represents the price you need to pay for this special offer, other numbers represents how many specific items you could get if you buy this offer.

You could use any of special offers as many times as you want.

Example 1:

Input: [2,5], [[3,0,5],[1,2,10]], [3,2]
Output: 14
Explanation: 
There are two kinds of items, A and B. Their prices are $2 and $5 respectively. 
In special offer 1, you can pay $5 for 3A and 0B
In special offer 2, you can pay $10 for 1A and 2B. 
You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A.

Example 2:

Input: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1]
Output: 11
Explanation: 
The price of A is $2, and $3 for B, $4 for C. 
You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C. 
You need to buy 1A ,2B and 1C, so you may pay $4 for 1A and 1B (special offer #1), and $3 for 1B, $4 for 1C. 
You cannot add more items, though only $9 for 2A ,2B and 1C.

Note:

  1. There are at most 6 kinds of items, 100 special offers.
  2. For each item, you need to buy at most 6 of them.
  3. You areĀ notĀ allowed to buy more items than you want, even if that would lower the overall price.

Solution: Search

Try all combinations.

 

花花酱 LeetCode 592. Fraction Addition and Subtraction

Problem

Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should beĀ irreducible fraction. If your final result is an integer, sayĀ 2, you need to change it to the format of fraction that has denominatorĀ 1. So in this case,Ā 2Ā should be converted toĀ 2/1.

Example 1:

Input:"-1/2+1/2"
Output: "0/1"

Example 2:

Input:"-1/2+1/2+1/3"
Output: "1/3"

Example 3:

Input:"1/3-1/2"
Output: "-1/6"

Example 4:

Input:"5/3+1/3"
Output: "2/1"

Note:

  1. The input string only containsĀ '0'Ā toĀ '9',Ā '/',Ā '+'Ā andĀ '-'. So does the output.
  2. Each fraction (input and output) has formatĀ Ā±numerator/denominator. If the first input fraction or the output is positive, thenĀ '+'Ā will be omitted.
  3. The input only contains validĀ irreducible fractions, where theĀ numeratorĀ andĀ denominatorĀ of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
  4. The number of given fractions will be in the range [1,10].
  5. The numerator and denominator of theĀ final resultĀ are guaranteed to be valid and in the range of 32-bit int.

Solution: Math

a/b+c/d = (a*d + b * c) / (b * d)

Time complexity: O(n)

Space complexity: O(1)

C++

C++/class

 

花花酱 LeetCode 885. Boats to Save People

Problem

TheĀ i-th person has weightĀ people[i], and each boat can carry a maximum weight ofĀ limit.

Each boat carries at most 2 people at the same time, provided the sum of theĀ weight of those people is at mostĀ limit.

Return the minimum number of boats to carry every given person.Ā  (It is guaranteed each person can be carried by a boat.)

Example 1:

Input: people = [1,2], limit = 3
Output: 1
Explanation: 1 boat (1, 2)

Example 2:

Input: people = [3,2,2,1], limit = 3
Output: 3
Explanation: 3 boats (1, 2), (2) and (3)

Example 3:

Input: people = [3,5,3,4], limit = 5
Output: 4
Explanation: 4 boats (3), (3), (4), (5)

Note:

  • 1 <=Ā people.length <= 50000
  • 1 <= people[i] <=Ā limit <= 30000

Solution: Greedy + Two Pointers

Time complexity: O(nlogn)

Space complexity: O(1)

Put one heaviest guy and put the lightest guy if not full.

 

花花酱 LeetCode 808. Soup Servings

Problem

There are two types of soup: type A and type B. Initially we haveĀ NĀ ml of each type of soup. There are four kinds of operations:

  1. ServeĀ 100 ml of soup A and 0 ml of soup B
  2. ServeĀ 75 ml of soup A and 25Ā ml of soup B
  3. Serve 50 ml of soup A and 50 ml of soup B
  4. Serve 25Ā ml of soup A and 75Ā ml of soup B

When we serve some soup, we give it to someone and we no longer have it.Ā  Each turn,Ā we will choose from the four operations with equal probability 0.25. If the remaining volume of soup is not enough to complete the operation, we will serveĀ as much as we can.Ā  We stop once we no longer have some quantity of both types of soup.

Note that we do not have the operation where all 100 ml’s of soup B are used first.

Return the probability that soup A will be emptyĀ first, plus half the probability that A and B become empty at the same time.

Example:
Input: N = 50
Output: 0.625
Explanation: 
If we choose the first two operations, A will become empty first. For the third operation, A and B will become empty at the same time. For the fourth operation, B will become empty first. So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.

Notes:

  • 0 <= N <= 10^9.
  • Answers withinĀ 10^-6Ā of the true value will be accepted as correct.

Solution 1: Recursion with Memorization

Time complexity: O(N^2) N ~ 5000 / 25 = 200

Space complexity: O(N^2)

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++