From 0842a18e1a419a5d2285281f922122a7cbf707e1 Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Thu, 13 Jun 2024 21:29:29 +0530 Subject: [PATCH] added 2262-1402 --- .../1400-1500/1402-Reducing-Dishes.md | 149 ++++++++++++++++ .../2262-Total-Appeal-of-a-String.md | 163 ++++++++++++++++++ 2 files changed, 312 insertions(+) create mode 100644 dsa-solutions/lc-solutions/1400-1500/1402-Reducing-Dishes.md create mode 100644 dsa-solutions/lc-solutions/2200-2300/2262-Total-Appeal-of-a-String.md diff --git a/dsa-solutions/lc-solutions/1400-1500/1402-Reducing-Dishes.md b/dsa-solutions/lc-solutions/1400-1500/1402-Reducing-Dishes.md new file mode 100644 index 000000000..89415650e --- /dev/null +++ b/dsa-solutions/lc-solutions/1400-1500/1402-Reducing-Dishes.md @@ -0,0 +1,149 @@ +--- +id: reducing-dishes +title: Reducing Dishes +sidebar_label: 1402-Reducing-Dishes +tags: + - Arrays + - Dynamic Programming + - Sorting + - C++ + - Java + - Python +description: "This document provides a solution to the Reducing Dishes problem, where we need to maximize the sum of the satisfaction of dishes." +--- + +## Problem + +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. + +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. + +Return the maximum sum of satisfaction the chef can obtain. + +### Examples + +**Example 1:** + +Input: satisfaction = [-1,-8,0,5,-9] +Output: 14 +Explanation: After removing the negative dishes, the remaining dishes are [0, 5]. + The optimal solution is to cook dish 0 at time 1 (0 * 1 = 0) and cook dish 5 at time 2 (5 * 2 = 10). + The total satisfaction is 0 + 10 + 5 = 14. + +**Example 2:** + +Input: satisfaction = [4,3,2] +Output: 20 +Explanation: The optimal solution is to cook all dishes in order. + The total satisfaction is 4 * 1 + 3 * 2 + 2 * 3 = 20. + +**Example 3:** + +Input: satisfaction = [-1,-4,-5] +Output: 0 +Explanation: The chef does not cook any dishes since all the satisfaction levels are negative. + +### Constraints + +- `n == satisfaction.length` +- `1 <= n <= 500` +- `-10^3 <= satisfaction[i] <= 10^3` + +### Approach + +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. + +The detailed steps are: + +1. Sort the satisfaction array. +2. Initialize `total` and `current` sums to 0. +3. Traverse the array from the end to the beginning, updating `current` sum and checking if adding the current element increases the `total`. +4. Return the maximum `total`. + +### Solution + +#### Code in Different Languages + +### C++ Solution +```cpp +#include +#include +#include + +using namespace std; + +int maxSatisfaction(vector& satisfaction) { + sort(satisfaction.begin(), satisfaction.end()); + int total = 0, current = 0; + for (int i = satisfaction.size() - 1; i >= 0; --i) { + current += satisfaction[i]; + if (current > 0) { + total += current; + } else { + break; + } + } + return total; +} + +int main() { + vector satisfaction = {-1, -8, 0, 5, -9}; + cout << maxSatisfaction(satisfaction) << endl; // Output: 14 +} +``` +### Java Solution +```java +import java.util.Arrays; + +public class ReducingDishes { + public static int maxSatisfaction(int[] satisfaction) { + Arrays.sort(satisfaction); + int total = 0, current = 0; + for (int i = satisfaction.length - 1; i >= 0; --i) { + current += satisfaction[i]; + if (current > 0) { + total += current; + } else { + break; + } + } + return total; + } + + public static void main(String[] args) { + int[] satisfaction = {-1, -8, 0, 5, -9}; + System.out.println(maxSatisfaction(satisfaction)); // Output: 14 + } +} +``` +### Python Solution + +```python +def maxSatisfaction(satisfaction): + satisfaction.sort() + total, current = 0, 0 + for i in range(len(satisfaction) - 1, -1, -1): + current += satisfaction[i] + if current > 0: + total += current + else: + break + return total + +# Test +satisfaction = [-1, -8, 0, 5, -9] +print(maxSatisfaction(satisfaction)) # Output: 14 +``` +### Complexity Analysis +**Time Complexity:** O(n log n) + +>Reason: Sorting the array takes O(n log n) time, and traversing the array takes O(n) time. + +**Space Complexity:** O(1) + +>Reason: We use a constant amount of extra space. + +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. + +### References +**LeetCode Problem:** Reducing Dishes \ No newline at end of file diff --git a/dsa-solutions/lc-solutions/2200-2300/2262-Total-Appeal-of-a-String.md b/dsa-solutions/lc-solutions/2200-2300/2262-Total-Appeal-of-a-String.md new file mode 100644 index 000000000..859ea8eb6 --- /dev/null +++ b/dsa-solutions/lc-solutions/2200-2300/2262-Total-Appeal-of-a-String.md @@ -0,0 +1,163 @@ +--- +id: total-appeal-of-a-string +title: Total Appeal of a String +sidebar_label: 2262-Total-Appeal-of-a-String +tags: + - Strings + - Dynamic Programming + - C++ + - Java + - Python +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." +--- + +## Problem + +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'. + +Given a string `s`, return the total appeal of all of its substrings. + +### Examples + +**Example 1:** + +Input: s = "abbca" +Output: 28 +Explanation: The following are the substrings of "abbca": +- "a" has an appeal of 1. +- "b" has an appeal of 1. +- "b" has an appeal of 1. +- "c" has an appeal of 1. +- "a" has an appeal of 1. +- "ab" has an appeal of 2. +- "bb" has an appeal of 1. +- "bc" has an appeal of 2. +- "ca" has an appeal of 2. +- "abb" has an appeal of 2. +- "bbc" has an appeal of 2. +- "bca" has an appeal of 3. +- "abbc" has an appeal of 3. +- "bbca" has an appeal of 3. +- "abbca" has an appeal of 3. +The total appeal is 1 + 1 + 1 + 1 + 1 + 2 + 1 + 2 + 2 + 2 + 2 + 3 + 3 + 3 + 3 = 28. + +**Example 2:** + +Input: s = "code" +Output: 20 +Explanation: The following are the substrings of "code": +- "c" has an appeal of 1. +- "o" has an appeal of 1. +- "d" has an appeal of 1. +- "e" has an appeal of 1. +- "co" has an appeal of 2. +- "od" has an appeal of 2. +- "de" has an appeal of 2. +- "cod" has an appeal of 3. +- "ode" has an appeal of 3. +- "code" has an appeal of 4. +The total appeal is 1 + 1 + 1 + 1 + 2 + 2 + 2 + 3 + 3 + 4 = 20. + +### Constraints + +- `1 <= s.length <= 10^5` +- `s` consists of lowercase English letters. + +### Approach + +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. + +Detailed steps: +1. Initialize an array `last_occurrence` to store the last occurrence index of each character. +2. Traverse the string and for each character, compute its contribution to the total appeal based on its last occurrence. +3. Update the last occurrence of the character to the current position. + +### Solution + +#### Code in Different Languages + +### C++ Solution +```cpp +#include +#include +#include +#include + +using namespace std; + +long long appealSum(string s) { + vector last_occurrence(26, -1); + long long total_appeal = 0, current_appeal = 0; + + for (int i = 0; i < s.size(); ++i) { + current_appeal += i - last_occurrence[s[i] - 'a']; + total_appeal += current_appeal; + last_occurrence[s[i] - 'a'] = i; + } + + return total_appeal; +} + +int main() { + string s = "abbca"; + cout << appealSum(s) << endl; // Output: 28 +} +``` +### Java Solution +```java +import java.util.*; + +public class TotalAppealOfString { + public long appealSum(String s) { + int[] last_occurrence = new int[26]; + Arrays.fill(last_occurrence, -1); + long total_appeal = 0, current_appeal = 0; + + for (int i = 0; i < s.length(); ++i) { + current_appeal += i - last_occurrence[s.charAt(i) - 'a']; + total_appeal += current_appeal; + last_occurrence[s.charAt(i) - 'a'] = i; + } + + return total_appeal; + } + + public static void main(String[] args) { + TotalAppealOfString solution = new TotalAppealOfString(); + String s = "abbca"; + System.out.println(solution.appealSum(s)); // Output: 28 + } +} +``` +### Python Solution + +```python +def appealSum(s: str) -> int: + last_occurrence = [-1] * 26 + total_appeal = 0 + current_appeal = 0 + + for i, char in enumerate(s): + current_appeal += i - last_occurrence[ord(char) - ord('a')] + total_appeal += current_appeal + last_occurrence[ord(char) - ord('a')] = i + + return total_appeal + +# Test +s = "abbca" +print(appealSum(s)) # Output: 28 +``` +### Complexity Analysis +**Time Complexity:** O(n) + +>Reason: We traverse the string once, and each operation inside the loop takes constant time. + +**Space Complexity:** O(1) + +>Reason: We use a fixed array of size 26 to store the last occurrences of each character. + +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. + +### References +**LeetCode Problem:** Total Appeal of a String \ No newline at end of file