Skip to content

Commit a49e1d1

Browse files
author
Leyang Zou
authored
Add #1402 in C++ (#113)
* Create _1402.cpp * Update README.md * Update README.md
1 parent 466334e commit a49e1d1

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ _If you like this project, please leave me a star._ ★
8686
|1409|[Queries on a Permutation With Key](https://leetcode.com/problems/queries-on-a-permutation-with-key/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1409.java) | |Medium|Array|
8787
|1408|[String Matching in an Array](https://leetcode.com/problems/string-matching-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1408.java) | |Easy|String|
8888
|1403|[Minimum Subsequence in Non-Increasing Order](https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1403.java) | |Easy|Greedy, Sort|
89+
|1402|[Reducing Dishes](https://leetcode.com/problems/reducing-dishes/)|[Solution](../master/cpp/_1402.cpp) | |Hard|Dynamic Programming|
8990
|1401|[Circle and Rectangle Overlapping](https://leetcode.com/problems/circle-and-rectangle-overlapping/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1401.java) | |Medium|Geometry|
9091
|1400|[Construct K Palindrome Strings](https://leetcode.com/problems/construct-k-palindrome-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1400.java) | |Medium|Greedy|
9192
|1399|[Count Largest Group](https://leetcode.com/problems/count-largest-group/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1399.java) | |Easy|Array|

Diff for: cpp/_1402.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int maxSatisfaction(vector<int>& sat) {
4+
// we'll greedily only consider a suffix of the sorted array
5+
sort(sat.begin(), sat.end());
6+
int cmax = 0;
7+
int sum = 0;
8+
int csum = 0;
9+
int i = sat.size(); // iterate from n - 1 to 0
10+
while(i--) {
11+
// calulate current satisfaction
12+
csum += sat[i];
13+
sum += csum;
14+
// compare with cmax
15+
cmax = max(cmax, sum);
16+
}
17+
return cmax;
18+
}
19+
};

0 commit comments

Comments
 (0)