Skip to content

Commit

Permalink
https://leetcode.cn/problems/watering-plants
Browse files Browse the repository at this point in the history
  • Loading branch information
masx200 committed May 8, 2024
1 parent afd9862 commit 6d8c007
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ go get github.com/masx200/leetcode-test

<summary>展开查看</summary>

https://leetcode.cn/problems/watering-plants

https://leetcode.cn/problems/kth-ancestor-of-a-tree-node

https://leetcode.cn/problems/counter-ii/
Expand Down
15 changes: 15 additions & 0 deletions watering-plants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function wateringPlants(plants: number[], capacity: number): number {
let ans = 0;
let rest = capacity;
for (let i = 0; i < plants.length; i++) {
if (rest >= plants[i]) {
ans++;
rest -= plants[i];
} else {
ans += i * 2 + 1;
rest = capacity - plants[i];
}
}
return ans;
}
export default wateringPlants;

0 comments on commit 6d8c007

Please sign in to comment.