Skip to content

Commit 68a19f1

Browse files
committed
Apr-27
1 parent 59533ed commit 68a19f1

12 files changed

+454
-66
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5555
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5656
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5757
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 15/128 | 3 vips |
58-
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 93/2210 | 4 vips |
58+
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 96/2210 | 4 vips |
5959

60-
**Solved**: 678 problems
60+
**Solved**: 681 problems
6161

6262
## 类型/Category
6363

@@ -216,7 +216,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
216216

217217
## Simulation
218218

219-
| Link | Problem(26) | Solution | Tag | Time | Space | Ref |
219+
| Link | Problem(28) | Solution | Tag | Time | Space | Ref |
220220
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
221221
| [Leetcode-67](https://leetcode.com/problems/add-binary/) | Add Binary | [c++](./leetcode/67.add-binary.cpp), [python3](./leetcode/67.add-binary.py) | Simulation | O\(M\+N\) | O\(1\) | - |
222222
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Simulation | O\(N\) | O\(1\) | - |
@@ -231,6 +231,8 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
231231
| [Leetcode-1910](https://leetcode.com/problems/remove-all-occurrences-of-a-substring/) | Remove All Occurrences Of A Substring | [c++](./leetcode/1910.remove-all-occurrences-of-a-substring.cpp), [python3](./leetcode/1910.remove-all-occurrences-of-a-substring.py) | Simulation | O\(NM\) | O\(1\) | - |
232232
| [Leetcode-566](https://leetcode.com/problems/reshape-the-matrix/) | Reshape The Matrix | [c++](./leetcode/566.reshape-the-matrix.cpp), [python3](./leetcode/566.reshape-the-matrix.py) | Simulation | O\(MN\) | O\(1\) | - |
233233
| [Leetcode-874](https://leetcode.com/problems/walking-robot-simulation/) | Walking Robot Simulation | [c++](./leetcode/874.walking-robot-simulation.cpp), [python3](./leetcode/874.walking-robot-simulation.py) | Simulation | O\(N\) | O\(K\) | - |
234+
| [Leetcode-2079](https://leetcode.com/problems/watering-plants/) | Watering Plants | [c++](./leetcode/2079.watering-plants.cpp), [python3](./leetcode/2079.watering-plants.py) | Simulation | O\(N\) | O\(1\) | - |
235+
| [Leetcode-2105](https://leetcode.com/problems/watering-plants-ii/) | Watering Plants II | [c++](./leetcode/2105.watering-plants-ii.cpp), [python3](./leetcode/2105.watering-plants-ii.py) | Simulation | O\(N\) | O\(1\) | - |
234236
| [Lintcode-849](https://www.lintcode.com/problem/basic-calculator-iii/) | Basic Calculator III | [c++](./lintcode/849.basic-calculator-iii.cpp), [python3](./lintcode/849.basic-calculator-iii.py) | Simulation | O\(N\) | O\(1\) | Leetcode-772 |
235237
| [Lintcode-553](https://www.lintcode.com/problem/bomb-enemy/) | Bomb Enemy | [c++](./lintcode/553.bomb-enemy.cpp), [python3](./lintcode/553.bomb-enemy.py) | Simulation | O\(NM\) | O\(NM\) | Leetcode-361 |
236238
| [Lintcode-3662](https://www.lintcode.com/problem/design-hit-counter/) | Design Hit Counter | [c++](./lintcode/3662.design-hit-counter.cpp), [python3](./lintcode/3662.design-hit-counter.py) | Simulation | O\(N\) | O\(N\) | Leetcode-362 |
@@ -846,7 +848,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
846848

847849
## Two Pointers
848850

849-
| Link | Problem(46) | Solution | Tag | Time | Space | Ref |
851+
| Link | Problem(47) | Solution | Tag | Time | Space | Ref |
850852
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
851853
| [Leetcode-15](https://leetcode.com/problems/3sum/) | 3Sum | [c++](./leetcode/15.3sum.cpp), [python3](./leetcode/15.3sum.py) | Two Pointers | O\(N^2\) | O\(1\) | - |
852854
| [Leetcode-18](https://leetcode.com/problems/4sum/) | 4Sum | [c++](./leetcode/18.4sum.cpp), [python3](./leetcode/18.4sum.py) | Two Pointers | O\(N^3\) | O\(1\) | - |
@@ -887,6 +889,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
887889
| [Leetcode-653](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | Two Sum Iv Input Is A Bst | [c++](./leetcode/653.two-sum-iv-input-is-a-bst.cpp), [python3](./leetcode/653.two-sum-iv-input-is-a-bst.py) | Two Pointers | O\(NH\) | O\(H\) | - |
888890
| [Leetcode-125](https://leetcode.com/problems/valid-palindrome/) | Valid Palindrome | [c++](./leetcode/125.valid-palindrome.cpp), [python3](./leetcode/125.valid-palindrome.py) | Two Pointers | O\(N\) | O\(1\) | - |
889891
| [Leetcode-680](https://leetcode.com/problems/valid-palindrome-ii/) | Valid Palindrome II | [c++](./leetcode/680.valid-palindrome-ii.cpp), [python3](./leetcode/680.valid-palindrome-ii.py) | Two Pointers | O\(N\) | O\(1\) | - |
892+
| [Leetcode-2105](https://leetcode.com/problems/watering-plants-ii/) | Watering Plants II | [c++](./leetcode/2105.watering-plants-ii.cpp), [python3](./leetcode/2105.watering-plants-ii.py) | Two Pointers | O\(N\) | O\(1\) | - |
890893
| [Leetcode-1712](https://leetcode.com/problems/ways-to-split-array-into-three-subarrays/) | Ways To Split Array Into Three Subarrays | [c++](./leetcode/1712.ways-to-split-array-into-three-subarrays.cpp), [python3](./leetcode/1712.ways-to-split-array-into-three-subarrays.py) | Two Pointers | O\(N\) | O\(N\) | - |
891894
| [Lintcode-861](https://www.lintcode.com/problem/k-empty-slots/) | K Empty Slots | [c++](./lintcode/861.k-empty-slots.cpp), [python3](./lintcode/861.k-empty-slots.py) | Two Pointers | O\(N\) | O\(N\) | Leetcode-683 |
892895
| [Lintcode-3736](https://www.lintcode.com/problem/kth-smallest-subarray-sum/) | Kth Smallest Subarray Sum | [c++](./lintcode/3736.kth-smallest-subarray-sum.cpp), [python3](./lintcode/3736.kth-smallest-subarray-sum.py) | Two Pointers | O\(NlogA\) | O\(1\) | Leetcode-1918 |
@@ -1435,11 +1438,12 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
14351438

14361439
## Array
14371440

1438-
| Link | Problem(7) | Solution | Tag | Time | Space | Ref |
1441+
| Link | Problem(8) | Solution | Tag | Time | Space | Ref |
14391442
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
14401443
| [Leetcode-3386](https://leetcode.com/problems/button-with-longest-push-time/) | Button With Longest Push Time | [c++](./leetcode/3386.button-with-longest-push-time.cpp), [python3](./leetcode/3386.button-with-longest-push-time.py) | Array | O\(N\) | O\(1\) | - |
14411444
| [Leetcode-2176](https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/) | Count Equal And Divisible Pairs In An Array | [c++](./leetcode/2176.count-equal-and-divisible-pairs-in-an-array.cpp), [python3](./leetcode/2176.count-equal-and-divisible-pairs-in-an-array.py) | Array | O\(N^2\) | O\(1\) | - |
14421445
| [Leetcode-2210](https://leetcode.com/problems/count-hills-and-valleys-in-an-array/) | Count Hills And Valleys In An Array | [c++](./leetcode/2210.count-hills-and-valleys-in-an-array.cpp), [python3](./leetcode/2210.count-hills-and-valleys-in-an-array.py) | Array | O\(N\) | O\(1\) | - |
1446+
| [Leetcode-3392](https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/) | Count Subarrays Of Length Three With A Condition | [c++](./leetcode/3392.count-subarrays-of-length-three-with-a-condition.cpp), [python3](./leetcode/3392.count-subarrays-of-length-three-with-a-condition.py) | Array | O\(N\) | O\(1\) | - |
14431447
| [Leetcode-3285](https://leetcode.com/problems/find-indices-of-stable-mountains/) | Find Indices Of Stable Mountains | [c++](./leetcode/3285.find-indices-of-stable-mountains.cpp), [python3](./leetcode/3285.find-indices-of-stable-mountains.py) | Array | O\(N\) | O\(1\) | - |
14441448
| [Leetcode-674](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | Longest Continuous Increasing Subsequence | [c++](./leetcode/674.longest-continuous-increasing-subsequence.cpp), [python3](./leetcode/674.longest-continuous-increasing-subsequence.py) | Array | O\(N\) | O\(1\) | - |
14451449
| [Leetcode-665](https://leetcode.com/problems/non-decreasing-array/) | Non Decreasing Array | [c++](./leetcode/665.non-decreasing-array.cpp), [python3](./leetcode/665.non-decreasing-array.py) | Array | O\(N\) | O\(1\) | - |

leetcode/2079.watering-plants.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Tag: Array, Simulation
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// You want to water n plants in your garden with a watering can. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i. There is a river at x = -1 that you can refill your watering can at.
8+
// Each plant needs a specific amount of water. You will water the plants in the following way:
9+
//
10+
// Water the plants in order from left to right.
11+
// After watering the current plant, if you do not have enough water to completely water the next plant, return to the river to fully refill the watering can.
12+
// You cannot refill the watering can early.
13+
//
14+
// You are initially at the river (i.e., x = -1). It takes one step to move one unit on the x-axis.
15+
// Given a 0-indexed integer array plants of n integers, where plants[i] is the amount of water the ith plant needs, and an integer capacity representing the watering can capacity, return the number of steps needed to water all the plants.
16+
//  
17+
// Example 1:
18+
//
19+
// Input: plants = [2,2,3,3], capacity = 5
20+
// Output: 14
21+
// Explanation: Start at the river with a full watering can:
22+
// - Walk to plant 0 (1 step) and water it. Watering can has 3 units of water.
23+
// - Walk to plant 1 (1 step) and water it. Watering can has 1 unit of water.
24+
// - Since you cannot completely water plant 2, walk back to the river to refill (2 steps).
25+
// - Walk to plant 2 (3 steps) and water it. Watering can has 2 units of water.
26+
// - Since you cannot completely water plant 3, walk back to the river to refill (3 steps).
27+
// - Walk to plant 3 (4 steps) and water it.
28+
// Steps needed = 1 + 1 + 2 + 3 + 3 + 4 = 14.
29+
//
30+
// Example 2:
31+
//
32+
// Input: plants = [1,1,1,4,2,3], capacity = 4
33+
// Output: 30
34+
// Explanation: Start at the river with a full watering can:
35+
// - Water plants 0, 1, and 2 (3 steps). Return to river (3 steps).
36+
// - Water plant 3 (4 steps). Return to river (4 steps).
37+
// - Water plant 4 (5 steps). Return to river (5 steps).
38+
// - Water plant 5 (6 steps).
39+
// Steps needed = 3 + 3 + 4 + 4 + 5 + 5 + 6 = 30.
40+
//
41+
// Example 3:
42+
//
43+
// Input: plants = [7,7,7,7,7,7,7], capacity = 8
44+
// Output: 49
45+
// Explanation: You have to refill before watering each plant.
46+
// Steps needed = 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6 + 6 + 7 = 49.
47+
//
48+
//  
49+
// Constraints:
50+
//
51+
// n == plants.length
52+
// 1 <= n <= 1000
53+
// 1 <= plants[i] <= 106
54+
// max(plants[i]) <= capacity <= 109
55+
//
56+
//
57+
58+
class Solution {
59+
public:
60+
int wateringPlants(vector<int>& plants, int capacity) {
61+
int n = plants.size();
62+
int step = 0;
63+
int bucket = capacity;
64+
for (int i = 0; i < n; i++) {
65+
if (bucket >= plants[i]) {
66+
step += 1;
67+
bucket -= plants[i];
68+
} else {
69+
step += 2 * i + 1;
70+
bucket = capacity - plants[i];
71+
}
72+
}
73+
return step;
74+
}
75+
};

leetcode/2079.watering-plants.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Tag: Array, Simulation
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# You want to water n plants in your garden with a watering can. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i. There is a river at x = -1 that you can refill your watering can at.
8+
# Each plant needs a specific amount of water. You will water the plants in the following way:
9+
#
10+
# Water the plants in order from left to right.
11+
# After watering the current plant, if you do not have enough water to completely water the next plant, return to the river to fully refill the watering can.
12+
# You cannot refill the watering can early.
13+
#
14+
# You are initially at the river (i.e., x = -1). It takes one step to move one unit on the x-axis.
15+
# Given a 0-indexed integer array plants of n integers, where plants[i] is the amount of water the ith plant needs, and an integer capacity representing the watering can capacity, return the number of steps needed to water all the plants.
16+
#  
17+
# Example 1:
18+
#
19+
# Input: plants = [2,2,3,3], capacity = 5
20+
# Output: 14
21+
# Explanation: Start at the river with a full watering can:
22+
# - Walk to plant 0 (1 step) and water it. Watering can has 3 units of water.
23+
# - Walk to plant 1 (1 step) and water it. Watering can has 1 unit of water.
24+
# - Since you cannot completely water plant 2, walk back to the river to refill (2 steps).
25+
# - Walk to plant 2 (3 steps) and water it. Watering can has 2 units of water.
26+
# - Since you cannot completely water plant 3, walk back to the river to refill (3 steps).
27+
# - Walk to plant 3 (4 steps) and water it.
28+
# Steps needed = 1 + 1 + 2 + 3 + 3 + 4 = 14.
29+
#
30+
# Example 2:
31+
#
32+
# Input: plants = [1,1,1,4,2,3], capacity = 4
33+
# Output: 30
34+
# Explanation: Start at the river with a full watering can:
35+
# - Water plants 0, 1, and 2 (3 steps). Return to river (3 steps).
36+
# - Water plant 3 (4 steps). Return to river (4 steps).
37+
# - Water plant 4 (5 steps). Return to river (5 steps).
38+
# - Water plant 5 (6 steps).
39+
# Steps needed = 3 + 3 + 4 + 4 + 5 + 5 + 6 = 30.
40+
#
41+
# Example 3:
42+
#
43+
# Input: plants = [7,7,7,7,7,7,7], capacity = 8
44+
# Output: 49
45+
# Explanation: You have to refill before watering each plant.
46+
# Steps needed = 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6 + 6 + 7 = 49.
47+
#
48+
#  
49+
# Constraints:
50+
#
51+
# n == plants.length
52+
# 1 <= n <= 1000
53+
# 1 <= plants[i] <= 106
54+
# max(plants[i]) <= capacity <= 109
55+
#
56+
#
57+
58+
class Solution:
59+
def wateringPlants(self, plants: List[int], capacity: int) -> int:
60+
n = len(plants)
61+
step = 0
62+
bucket = capacity
63+
for i in range(n):
64+
if bucket >= plants[i]:
65+
step += 1
66+
bucket -= plants[i]
67+
else:
68+
step += 2 * i + 1
69+
bucket = capacity - plants[i]
70+
71+
return step

leetcode/2105.watering-plants-ii.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Tag: Array, Two Pointers, Simulation
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Alice and Bob want to water n plants in their garden. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i.
8+
// Each plant needs a specific amount of water. Alice and Bob have a watering can each, initially full. They water the plants in the following way:
9+
//
10+
// Alice waters the plants in order from left to right, starting from the 0th plant. Bob waters the plants in order from right to left, starting from the (n - 1)th plant. They begin watering the plants simultaneously.
11+
// It takes the same amount of time to water each plant regardless of how much water it needs.
12+
// Alice/Bob must water the plant if they have enough in their can to fully water it. Otherwise, they first refill their can (instantaneously) then water the plant.
13+
// In case both Alice and Bob reach the same plant, the one with more water currently in his/her watering can should water this plant. If they have the same amount of water, then Alice should water this plant.
14+
//
15+
// Given a 0-indexed integer array plants of n integers, where plants[i] is the amount of water the ith plant needs, and two integers capacityA and capacityB representing the capacities of Alice's and Bob's watering cans respectively, return the number of times they have to refill to water all the plants.
16+
//  
17+
// Example 1:
18+
//
19+
// Input: plants = [2,2,3,3], capacityA = 5, capacityB = 5
20+
// Output: 1
21+
// Explanation:
22+
// - Initially, Alice and Bob have 5 units of water each in their watering cans.
23+
// - Alice waters plant 0, Bob waters plant 3.
24+
// - Alice and Bob now have 3 units and 2 units of water respectively.
25+
// - Alice has enough water for plant 1, so she waters it. Bob does not have enough water for plant 2, so he refills his can then waters it.
26+
// So, the total number of times they have to refill to water all the plants is 0 + 0 + 1 + 0 = 1.
27+
//
28+
// Example 2:
29+
//
30+
// Input: plants = [2,2,3,3], capacityA = 3, capacityB = 4
31+
// Output: 2
32+
// Explanation:
33+
// - Initially, Alice and Bob have 3 units and 4 units of water in their watering cans respectively.
34+
// - Alice waters plant 0, Bob waters plant 3.
35+
// - Alice and Bob now have 1 unit of water each, and need to water plants 1 and 2 respectively.
36+
// - Since neither of them have enough water for their current plants, they refill their cans and then water the plants.
37+
// So, the total number of times they have to refill to water all the plants is 0 + 1 + 1 + 0 = 2.
38+
//
39+
// Example 3:
40+
//
41+
// Input: plants = [5], capacityA = 10, capacityB = 8
42+
// Output: 0
43+
// Explanation:
44+
// - There is only one plant.
45+
// - Alice's watering can has 10 units of water, whereas Bob's can has 8 units. Since Alice has more water in her can, she waters this plant.
46+
// So, the total number of times they have to refill is 0.
47+
//
48+
//  
49+
// Constraints:
50+
//
51+
// n == plants.length
52+
// 1 <= n <= 105
53+
// 1 <= plants[i] <= 106
54+
// max(plants[i]) <= capacityA, capacityB <= 109
55+
//
56+
//
57+
58+
class Solution {
59+
public:
60+
int minimumRefill(vector<int>& plants, int capacityA, int capacityB) {
61+
int l = 0;
62+
int r = plants.size() - 1;
63+
int alice = capacityA;
64+
int bob = capacityB;
65+
int res = 0;
66+
while (l < r) {
67+
if (alice < plants[l]) {
68+
res += 1;
69+
alice = capacityA - plants[l];
70+
} else {
71+
alice -= plants[l];
72+
}
73+
if (bob < plants[r]) {
74+
res += 1;
75+
bob = capacityB - plants[r];
76+
} else {
77+
bob -= plants[r];
78+
}
79+
l += 1;
80+
r -= 1;
81+
}
82+
return res + (l == r && max(alice, bob) < plants[l]);
83+
}
84+
};

0 commit comments

Comments
 (0)