[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]1475. Final Prices With a Special Discount in a Shop

LeetCode_logo
Given the array prices where prices[i] is the price of the ith item in a shop. There is a special discount for items in the shop, if you buy the ith item, then you will receive a discount equivalent to prices[j] where j is the minimum index such that j > i and prices[j] <= prices[i], otherwise, you will not receive any discount at all.

Return an array where the ith element is the final price you will pay for the ith item of the shop considering the special discount.

Examples:
Input: prices = [8,4,6,2,3] Output: [4,2,4,2,3]
Input: prices = [1,2,3,4,5] Output: [1,2,3,4,5]
Continue reading “[Leetcode problems]1475. Final Prices With a Special Discount in a Shop”

[Leetcode problems] 45 Jump Game II

LeetCode_logo
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.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4] 
Output: 2 
Explanation: The minimum number of jumps to reach the last index is 2. 
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
Continue reading “[Leetcode problems] 45 Jump Game II”