Press "Enter" to skip to content

Huahua's Tech Road

花花酱 LeetCode 879. Profitable Schemes

Problem

There are G people in a gang, and a list of various crimes they could commit.

TheĀ i-th crime generates aĀ profit[i]Ā and requiresĀ group[i]Ā gang members to participate.

If a gang member participates in one crime, that member can’t participate in another crime.

Let’s call aĀ profitableĀ schemeĀ any subset of these crimes that generates at leastĀ PĀ profit, and the total number of gang members participating in that subset of crimes is at most G.

How many schemes can be chosen?Ā  Since the answer may be veryĀ large,Ā return it moduloĀ 10^9 + 7.

Example 1:

Input: G = 5, P = 3, group = [2,2], profit = [2,3]
Output: 2
Explanation: 
To make a profit of at least 3, the gang could either commit crimes 0 and 1, or just crime 1.
In total, there are 2 schemes.

Example 2:

Input: G = 10, P = 5, group = [2,3,5], profit = [6,7,8]
Output: 7
Explanation: 
To make a profit of at least 5, the gang could commit any crimes, as long as they commit one.
There are 7 possible schemes: (0), (1), (2), (0,1), (0,2), (1,2), and (0,1,2).

Note:

  1. 1 <= G <= 100
  2. 0 <= P <= 100
  3. 1 <= group[i] <= 100
  4. 0 <= profit[i] <= 100
  5. 1 <= group.length = profit.length <= 100

Solution: DP

Time complexity: O(KPG)

Space complexity: O(KPG)

C++

Space complexity: O(PG)

v1: Dimension reduction by copying.

v2: Dimension reduction by using rolling array.

 

花花酱 LeetCode 878. Nth Magical Number

Problem

A positive integerĀ isĀ magicalĀ if it is divisible by eitherĀ AĀ orĀ B.

Return theĀ N-th magical number.Ā  Since the answer may be very large,Ā return it moduloĀ 10^9 + 7.

Example 1:

Input: N = 1, A = 2, B = 3
Output: 2

Example 2:

Input: N = 4, A = 2, B = 3
Output: 6

Example 3:

Input: N = 5, A = 2, B = 4
Output: 10

Example 4:

Input: N = 3, A = 6, B = 4
Output: 8

Note:

  1. 1 <= NĀ <= 10^9
  2. 2 <= AĀ <= 40000
  3. 2 <= BĀ <= 40000

Solution: Math + Binary Search

Let n denote the number of numbers <= X that are divisible by eitherĀ AĀ orĀ B.

n = X / A + X / B – X / lcm(A, B) = X / A + X / B – X / (A * B / gcd(A, B))

Binary search for the smallest X such that n >= N

Time complexity: O(log(1e9*4e5)

Space complexity: O(1)

 

花花酱 LeetCode 877. Stone Game

Problem

Alex and Lee play a game with piles of stones.Ā  There are an even number ofĀ pilesĀ arranged in a row, and each pile has a positive integer number of stonesĀ piles[i].

The objective of the game is to end with the mostĀ stones.Ā  The total number of stones is odd, so there are no ties.

Alex and Lee take turns, with Alex starting first.Ā  Each turn, a playerĀ takes the entire pile of stones from either the beginning or the end of the row.Ā  This continues until there are no more piles left, at which point the person with the most stones wins.

Assuming Alex and Lee play optimally, returnĀ TrueĀ if and only if Alex wins the game.

Example 1:

Input: [5,3,4,5]
Output: true
Explanation: 
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.

Note:

  1. 2 <= piles.length <= 500
  2. piles.lengthĀ is even.
  3. 1 <= piles[i] <= 500
  4. sum(piles)Ā is odd.

Solution 1: min-max (TLE)

Time complexity: O(2^n)

Space complexity: O(n)

Solution 2: min-max + memorization

Time complexity: O(n^2)

Space complexity: O(n^2)

Solution 3:Ā  min-max + DP

Time complexity: O(n^2)

Space complexity: O(n^2)

O(n) Space

Related Problems

花花酱 LeetCode 876. Middle of the Linked List

Problem

Given a non-empty, singlyĀ linked list with head nodeĀ head, returnĀ aĀ middle node of linked list.

If there are two middle nodes, return the second middle node.

Example 1:

Input: [1,2,3,4,5]
Output: Node 3 from this list (Serialization: [3,4,5])
The returned node has value 3.  (The judge's serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.

Example 2:

Input: [1,2,3,4,5,6]
Output: Node 4 from this list (Serialization: [4,5,6])
Since the list has two middle nodes with values 3 and 4, we return the second one.

 

Note:

  • The number of nodes in the given list will be betweenĀ 1Ā andĀ 100.

Solution: Slow + Fast Pointers

Time complexity: O(n)

Space complexity: O(1)

 

花花酱 LeetCode 882. Random Point in Non-overlapping Rectangles

Problem

Given a list ofĀ non-overlappingĀ axis-aligned rectanglesĀ rects, write a functionĀ pickĀ which randomly and uniformily picks anĀ integer pointĀ in the spaceĀ covered by the rectangles.

Note:

  1. AnĀ integer pointĀ is a point that has integer coordinates.
  2. A pointĀ on the perimeterĀ of a rectangle isĀ includedĀ in the space covered by the rectangles.
  3. ith rectangle =Ā rects[i]Ā =Ā [x1,y1,x2,y2], whereĀ [x1, y1]Ā are the integer coordinates of the bottom-left corner, andĀ [x2, y2]Ā are the integer coordinates of the top-right corner.
  4. length and width of each rectangle does not exceedĀ 2000.
  5. 1 <= rects.lengthĀ <= 100
  6. pickĀ return a point as an array of integer coordinatesĀ [p_x, p_y]
  7. pickĀ is called at mostĀ 10000Ā times.

Example 1:

Input: 
["Solution","pick","pick","pick"]
[[[[1,1,5,5]]],[],[],[]]
Output: 
[null,[4,1],[4,1],[3,3]]

Example 2:

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

Explanation of Input Syntax:

The input is two lists:Ā the subroutines calledĀ and theirĀ arguments.Ā Solution‘sĀ constructor has one argument, the array of rectanglesĀ rects.Ā pickĀ has no arguments.Ā ArgumentsĀ areĀ always wrapped with a list, even if there aren’t any.

Solution: Binary Search

Same asĀ LeetCode 880. Random Pick with Weight

Use area of the rectangles as weights.

Time complexity: Init: O(n) Pick: O(logn)

Space complexity: O(n)

Related Problems