Skip to content

Commit 0842a18

Browse files
committed
added 2262-1402
1 parent 3c1de19 commit 0842a18

File tree

2 files changed

+312
-0
lines changed

2 files changed

+312
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
id: reducing-dishes
3+
title: Reducing Dishes
4+
sidebar_label: 1402-Reducing-Dishes
5+
tags:
6+
- Arrays
7+
- Dynamic Programming
8+
- Sorting
9+
- C++
10+
- Java
11+
- Python
12+
description: "This document provides a solution to the Reducing Dishes problem, where we need to maximize the sum of the satisfaction of dishes."
13+
---
14+
15+
## Problem
16+
17+
A chef has collected data on the satisfaction level of his `n` dishes. Chef can cook any dish in 1 unit of time. Each dish takes `1` unit of time to cook.
18+
19+
The chef has a choice to skip or cook the dish. He wants to maximize the sum of the satisfaction he gets from all the dishes he decides to cook. The satisfaction of a dish is defined as the sum of its satisfaction times its cooking time.
20+
21+
Return the maximum sum of satisfaction the chef can obtain.
22+
23+
### Examples
24+
25+
**Example 1:**
26+
27+
Input: satisfaction = [-1,-8,0,5,-9]
28+
Output: 14
29+
Explanation: After removing the negative dishes, the remaining dishes are [0, 5].
30+
The optimal solution is to cook dish 0 at time 1 (0 * 1 = 0) and cook dish 5 at time 2 (5 * 2 = 10).
31+
The total satisfaction is 0 + 10 + 5 = 14.
32+
33+
**Example 2:**
34+
35+
Input: satisfaction = [4,3,2]
36+
Output: 20
37+
Explanation: The optimal solution is to cook all dishes in order.
38+
The total satisfaction is 4 * 1 + 3 * 2 + 2 * 3 = 20.
39+
40+
**Example 3:**
41+
42+
Input: satisfaction = [-1,-4,-5]
43+
Output: 0
44+
Explanation: The chef does not cook any dishes since all the satisfaction levels are negative.
45+
46+
### Constraints
47+
48+
- `n == satisfaction.length`
49+
- `1 <= n <= 500`
50+
- `-10^3 <= satisfaction[i] <= 10^3`
51+
52+
### Approach
53+
54+
To maximize the sum of satisfaction, we can sort the satisfaction array in ascending order. We will then iterate from the end of the sorted list towards the beginning, keeping a running total of the current satisfaction sum. We add to our result only if the current total is positive.
55+
56+
The detailed steps are:
57+
58+
1. Sort the satisfaction array.
59+
2. Initialize `total` and `current` sums to 0.
60+
3. Traverse the array from the end to the beginning, updating `current` sum and checking if adding the current element increases the `total`.
61+
4. Return the maximum `total`.
62+
63+
### Solution
64+
65+
#### Code in Different Languages
66+
67+
### C++ Solution
68+
```cpp
69+
#include <vector>
70+
#include <algorithm>
71+
#include <iostream>
72+
73+
using namespace std;
74+
75+
int maxSatisfaction(vector<int>& satisfaction) {
76+
sort(satisfaction.begin(), satisfaction.end());
77+
int total = 0, current = 0;
78+
for (int i = satisfaction.size() - 1; i >= 0; --i) {
79+
current += satisfaction[i];
80+
if (current > 0) {
81+
total += current;
82+
} else {
83+
break;
84+
}
85+
}
86+
return total;
87+
}
88+
89+
int main() {
90+
vector<int> satisfaction = {-1, -8, 0, 5, -9};
91+
cout << maxSatisfaction(satisfaction) << endl; // Output: 14
92+
}
93+
```
94+
### Java Solution
95+
```java
96+
import java.util.Arrays;
97+
98+
public class ReducingDishes {
99+
public static int maxSatisfaction(int[] satisfaction) {
100+
Arrays.sort(satisfaction);
101+
int total = 0, current = 0;
102+
for (int i = satisfaction.length - 1; i >= 0; --i) {
103+
current += satisfaction[i];
104+
if (current > 0) {
105+
total += current;
106+
} else {
107+
break;
108+
}
109+
}
110+
return total;
111+
}
112+
113+
public static void main(String[] args) {
114+
int[] satisfaction = {-1, -8, 0, 5, -9};
115+
System.out.println(maxSatisfaction(satisfaction)); // Output: 14
116+
}
117+
}
118+
```
119+
### Python Solution
120+
121+
```python
122+
def maxSatisfaction(satisfaction):
123+
satisfaction.sort()
124+
total, current = 0, 0
125+
for i in range(len(satisfaction) - 1, -1, -1):
126+
current += satisfaction[i]
127+
if current > 0:
128+
total += current
129+
else:
130+
break
131+
return total
132+
133+
# Test
134+
satisfaction = [-1, -8, 0, 5, -9]
135+
print(maxSatisfaction(satisfaction)) # Output: 14
136+
```
137+
### Complexity Analysis
138+
**Time Complexity:** O(n log n)
139+
140+
>Reason: Sorting the array takes O(n log n) time, and traversing the array takes O(n) time.
141+
142+
**Space Complexity:** O(1)
143+
144+
>Reason: We use a constant amount of extra space.
145+
146+
This solution sorts the satisfaction array and then iterates through it in reverse to calculate the maximum sum of satisfaction. The time complexity is dominated by the sorting step, and the space complexity is constant.
147+
148+
### References
149+
**LeetCode Problem:** Reducing Dishes
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
id: total-appeal-of-a-string
3+
title: Total Appeal of a String
4+
sidebar_label: 2262-Total-Appeal-of-a-String
5+
tags:
6+
- Strings
7+
- Dynamic Programming
8+
- C++
9+
- Java
10+
- Python
11+
description: "This document provides a solution to the Total Appeal of a String problem, where we need to calculate the total appeal of all substrings of a given string."
12+
---
13+
14+
## Problem
15+
16+
The appeal of a string is the number of distinct characters found in the string. For example, the appeal of "abbca" is 3 because it has 3 distinct characters: 'a', 'b', and 'c'.
17+
18+
Given a string `s`, return the total appeal of all of its substrings.
19+
20+
### Examples
21+
22+
**Example 1:**
23+
24+
Input: s = "abbca"
25+
Output: 28
26+
Explanation: The following are the substrings of "abbca":
27+
- "a" has an appeal of 1.
28+
- "b" has an appeal of 1.
29+
- "b" has an appeal of 1.
30+
- "c" has an appeal of 1.
31+
- "a" has an appeal of 1.
32+
- "ab" has an appeal of 2.
33+
- "bb" has an appeal of 1.
34+
- "bc" has an appeal of 2.
35+
- "ca" has an appeal of 2.
36+
- "abb" has an appeal of 2.
37+
- "bbc" has an appeal of 2.
38+
- "bca" has an appeal of 3.
39+
- "abbc" has an appeal of 3.
40+
- "bbca" has an appeal of 3.
41+
- "abbca" has an appeal of 3.
42+
The total appeal is 1 + 1 + 1 + 1 + 1 + 2 + 1 + 2 + 2 + 2 + 2 + 3 + 3 + 3 + 3 = 28.
43+
44+
**Example 2:**
45+
46+
Input: s = "code"
47+
Output: 20
48+
Explanation: The following are the substrings of "code":
49+
- "c" has an appeal of 1.
50+
- "o" has an appeal of 1.
51+
- "d" has an appeal of 1.
52+
- "e" has an appeal of 1.
53+
- "co" has an appeal of 2.
54+
- "od" has an appeal of 2.
55+
- "de" has an appeal of 2.
56+
- "cod" has an appeal of 3.
57+
- "ode" has an appeal of 3.
58+
- "code" has an appeal of 4.
59+
The total appeal is 1 + 1 + 1 + 1 + 2 + 2 + 2 + 3 + 3 + 4 = 20.
60+
61+
### Constraints
62+
63+
- `1 <= s.length <= 10^5`
64+
- `s` consists of lowercase English letters.
65+
66+
### Approach
67+
68+
To solve this problem, we need to calculate the total appeal of all substrings. The key idea is to use dynamic programming and track the last occurrence of each character. For each character in the string, we compute its contribution to the total appeal based on its position and previous occurrences.
69+
70+
Detailed steps:
71+
1. Initialize an array `last_occurrence` to store the last occurrence index of each character.
72+
2. Traverse the string and for each character, compute its contribution to the total appeal based on its last occurrence.
73+
3. Update the last occurrence of the character to the current position.
74+
75+
### Solution
76+
77+
#### Code in Different Languages
78+
79+
### C++ Solution
80+
```cpp
81+
#include <iostream>
82+
#include <vector>
83+
#include <string>
84+
#include <unordered_map>
85+
86+
using namespace std;
87+
88+
long long appealSum(string s) {
89+
vector<int> last_occurrence(26, -1);
90+
long long total_appeal = 0, current_appeal = 0;
91+
92+
for (int i = 0; i < s.size(); ++i) {
93+
current_appeal += i - last_occurrence[s[i] - 'a'];
94+
total_appeal += current_appeal;
95+
last_occurrence[s[i] - 'a'] = i;
96+
}
97+
98+
return total_appeal;
99+
}
100+
101+
int main() {
102+
string s = "abbca";
103+
cout << appealSum(s) << endl; // Output: 28
104+
}
105+
```
106+
### Java Solution
107+
```java
108+
import java.util.*;
109+
110+
public class TotalAppealOfString {
111+
public long appealSum(String s) {
112+
int[] last_occurrence = new int[26];
113+
Arrays.fill(last_occurrence, -1);
114+
long total_appeal = 0, current_appeal = 0;
115+
116+
for (int i = 0; i < s.length(); ++i) {
117+
current_appeal += i - last_occurrence[s.charAt(i) - 'a'];
118+
total_appeal += current_appeal;
119+
last_occurrence[s.charAt(i) - 'a'] = i;
120+
}
121+
122+
return total_appeal;
123+
}
124+
125+
public static void main(String[] args) {
126+
TotalAppealOfString solution = new TotalAppealOfString();
127+
String s = "abbca";
128+
System.out.println(solution.appealSum(s)); // Output: 28
129+
}
130+
}
131+
```
132+
### Python Solution
133+
134+
```python
135+
def appealSum(s: str) -> int:
136+
last_occurrence = [-1] * 26
137+
total_appeal = 0
138+
current_appeal = 0
139+
140+
for i, char in enumerate(s):
141+
current_appeal += i - last_occurrence[ord(char) - ord('a')]
142+
total_appeal += current_appeal
143+
last_occurrence[ord(char) - ord('a')] = i
144+
145+
return total_appeal
146+
147+
# Test
148+
s = "abbca"
149+
print(appealSum(s)) # Output: 28
150+
```
151+
### Complexity Analysis
152+
**Time Complexity:** O(n)
153+
154+
>Reason: We traverse the string once, and each operation inside the loop takes constant time.
155+
156+
**Space Complexity:** O(1)
157+
158+
>Reason: We use a fixed array of size 26 to store the last occurrences of each character.
159+
160+
This solution calculates the total appeal of all substrings by efficiently tracking the last occurrence of each character and computing their contributions. The time complexity is linear, and the space complexity is constant, making it suitable for large input sizes.
161+
162+
### References
163+
**LeetCode Problem:** Total Appeal of a String

0 commit comments

Comments
 (0)