Press "Enter" to skip to content

Posts published in “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 915. Partition Array into Disjoint Intervals

Problem

Given an arrayĀ A, partition itĀ into two (contiguous) subarraysĀ leftĀ andĀ rightĀ so that:

  • Every element inĀ leftĀ is less than or equal to every element inĀ right.
  • leftĀ andĀ rightĀ are non-empty.
  • leftĀ has the smallest possible size.

Return theĀ lengthĀ ofĀ leftĀ after such a partitioning.Ā  It is guaranteed that such a partitioning exists.

Example 1:

Input: [5,0,3,8,6]
Output: 3
Explanation: left = [5,0,3], right = [8,6]

Example 2:

Input: [1,1,1,0,6,12]
Output: 4
Explanation: left = [1,1,1,0], right = [6,12]

Note:

  1. 2 <= A.lengthĀ <= 30000
  2. 0 <= A[i] <= 10^6
  3. It is guaranteed there is at least one way to partitionĀ AĀ as described.

Solution 1: BST

Time complexity: O(nlogn)

Space complexity: O(n)

C++

Solution 2: Greedy

Time complexity: O(n)

Space complexity: O(1)

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)