[Leetcode problems]60. Permutation Sequence

LeetCode_logo
The set [1,2,3,...,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123";"132";"213";"231";"312";"321"
Given n and k, return the kth permutation sequence.
Note:
Given n will be between 1 and 9 inclusive.
Given k will be between 1 and n! inclusive.
Example 1:
Input: n = 3, k = 3 Output: "213"
Continue reading “[Leetcode problems]60. Permutation Sequence”

[Leetcode problems]75. Sort Colors

LeetCode_logo
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note: You are not suppose to use the library's sort function for this problem.
Example:
Input: [2,0,2,1,1,0] Output: [0,0,1,1,2,2]
Continue reading “[Leetcode problems]75. Sort Colors”

[Leetcode problems]1482. Minimum Number of Days to Make m Bouquets

LeetCode_logo
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
 
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1 
Output: 3 
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Continue reading “[Leetcode problems]1482. Minimum Number of Days to Make m Bouquets”

[Leetcode problems]1477. Find Two Non-overlapping Sub-arrays Each With Target Sum

LeetCode_logo
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
 
Example 1:
Input: arr = [3,2,2,4,3], target = 3 Output: 2 Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7 Output: 2 Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Continue reading “[Leetcode problems]1477. Find Two Non-overlapping Sub-arrays Each With Target Sum”

[Leetcode Problems]240. Search a 2D Matrix II

LeetCode_logo
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

Example:
Consider the following matrix:
[ [1, 4, 7, 11, 15], 
  [2, 5, 8, 12, 19], 
  [3, 6, 9, 16, 22], 
  [10, 13, 14, 17, 24], 
  [18, 21, 23, 26, 30] ]
Given target = 5, return true.
Given target = 20, return false.
Continue reading “[Leetcode Problems]240. Search a 2D Matrix II”