Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 44a71e2

Browse files
committed
Add solution #2106
1 parent 19e669a commit 44a71e2

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,749 LeetCode solutions in JavaScript
1+
# 1,750 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1613,6 +1613,7 @@
16131613
2103|[Rings and Rods](./solutions/2103-rings-and-rods.js)|Easy|
16141614
2104|[Sum of Subarray Ranges](./solutions/2104-sum-of-subarray-ranges.js)|Medium|
16151615
2105|[Watering Plants II](./solutions/2105-watering-plants-ii.js)|Medium|
1616+
2106|[Maximum Fruits Harvested After at Most K Steps](./solutions/2106-maximum-fruits-harvested-after-at-most-k-steps.js)|Hard|
16161617
2114|[Maximum Number of Words Found in Sentences](./solutions/2114-maximum-number-of-words-found-in-sentences.js)|Easy|
16171618
2115|[Find All Possible Recipes from Given Supplies](./solutions/2115-find-all-possible-recipes-from-given-supplies.js)|Medium|
16181619
2116|[Check if a Parentheses String Can Be Valid](./solutions/2116-check-if-a-parentheses-string-can-be-valid.js)|Medium|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* 2106. Maximum Fruits Harvested After at Most K Steps
3+
* https://leetcode.com/problems/maximum-fruits-harvested-after-at-most-k-steps/
4+
* Difficulty: Hard
5+
*
6+
* Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array
7+
* fruits where fruits[i] = [positioni, amounti] depicts amounti fruits at the position positioni.
8+
* fruits is already sorted by positioni in ascending order, and each positioni is unique.
9+
*
10+
* You are also given an integer startPos and an integer k. Initially, you are at the position
11+
* startPos. From any position, you can either walk to the left or right. It takes one step to
12+
* move one unit on the x-axis, and you can walk at most k steps in total. For every position
13+
* you reach, you harvest all the fruits at that position, and the fruits will disappear from
14+
* that position.
15+
*
16+
* Return the maximum total number of fruits you can harvest.
17+
*/
18+
19+
/**
20+
* @param {number[][]} fruits
21+
* @param {number} startPos
22+
* @param {number} k
23+
* @return {number}
24+
*/
25+
var maxTotalFruits = function(fruits, startPos, k) {
26+
let result = 0;
27+
let left = 0;
28+
let currentSum = 0;
29+
30+
for (let right = 0; right < fruits.length; right++) {
31+
currentSum += fruits[right][1];
32+
33+
while (left <= right) {
34+
const minPos = fruits[left][0];
35+
const maxPos = fruits[right][0];
36+
const steps = Math.min(
37+
Math.abs(startPos - minPos) + (maxPos - minPos),
38+
Math.abs(startPos - maxPos) + (maxPos - minPos)
39+
);
40+
41+
if (steps <= k) break;
42+
currentSum -= fruits[left][1];
43+
left++;
44+
}
45+
46+
if (left <= right) {
47+
result = Math.max(result, currentSum);
48+
}
49+
}
50+
51+
return result;
52+
};

0 commit comments

Comments
 (0)