Press "Enter" to skip to content

Posts tagged as “greedy”

花花酱 LeetCode 942. DI String Match

Problem

Given a stringĀ SĀ thatĀ onlyĀ contains “I” (increase) or “D” (decrease), letĀ N = S.length.

ReturnĀ anyĀ permutationĀ AĀ ofĀ [0, 1, ..., N]Ā such that for allĀ i = 0,Ā ..., N-1:

  • IfĀ S[i] == "I", thenĀ A[i] < A[i+1]
  • IfĀ S[i] == "D", thenĀ A[i] > A[i+1]

Example 1:

Input: "IDID"
Output: [0,4,1,3,2]

Example 2:

Input: "III"
Output: [0,1,2,3]

Example 3:

Input: "DDI"
Output: [3,2,0,1]

Note:

  1. 1 <= S.length <= 10000
  2. SĀ only contains charactersĀ "I"Ā orĀ "D".

Solution: Greedy

“I” -> use the smallest possible number

“D” -> use the largest possible number

Time complexity: O(n)

Space complexity: O(n)

C++

花花酱 LeetCode 55. Jump Game

Problem

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

Example 1:

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
Ā             jump length is 0, which makes it impossible to reach the last index.

Solution: Greedy

If you can jump to i, then you can jump to at least i + nums[i].

Always jump as far as you can.

Keep tracking the farthest index you can jump to.

Init far = nums[0].

far = max(far, i + nums[i])

check far >= n – 1

ex 1 [2,3,1,1,4]

ex 2Ā [3,2,1,0,4]

C++

花花酱 LeetCode 910. Smallest Range II

Problem

Given an arrayĀ AĀ of integers, for each integerĀ A[i]Ā we need to chooseĀ eitherĀ x = -KĀ orĀ x = K, and addĀ xĀ toĀ A[i]Ā (only once).

After this process, we have some arrayĀ B.

Return the smallest possible difference between the maximum value ofĀ BĀ and the minimum value ofĀ B.

Example 1:

Input: A = [1], K = 0
Output: 0
Explanation: B = [1]

Example 2:

Input: A = [0,10], K = 2
Output: 6
Explanation: B = [2,8]

Example 3:

Input: A = [1,3,6], K = 3
Output: 3
Explanation: B = [4,6,3]

Note:

  1. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 10000
  3. 0 <= K <= 10000

Solution: Greedy

Sort the array and compare adjacent numbers.

Time complexity: O(nlogn)

Space complexity: O(1)

C++

Python3

花花酱 LeetCode 455. Assign Cookies

Problem

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sjĀ >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

Note:
You may assume the greed factor is always positive.
You cannot assign more than one cookie to one child.

Example 1:

Input: [1,2,3], [1,1]

Output: 1

Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. 
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
You need to output 1.

Example 2:

Input: [1,2], [1,2,3]

Output: 2

Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. 
You have 3 cookies and their sizes are big enough to gratify all of the children, 
You need to output 2.

Solution: Greedy + Two Pointers

Time complexity: O(mlogm + nlogn)

Space complexity: O(1)

 

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