Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package g1401_1500.s1418_display_table_of_food_orders_in_a_restaurant;

// #Medium #Array #String #Hash_Table #Sorting #Ordered_Set
// #2022_03_26_Time_42_ms_(77.01%)_Space_126.7_MB_(59.77%)

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Solution {
public List<List<String>> displayTable(List<List<String>> orders) {
TreeMap<Integer, Map<String, Integer>> map = new TreeMap<>();
Set<String> dishSet = new HashSet<>();
for (List<String> order : orders) {
int tableNumber = Integer.parseInt(order.get(1));
String dishName = order.get(2);
dishSet.add(dishName);
map.putIfAbsent(tableNumber, new HashMap<>());
Map<String, Integer> dishCountMap = map.get(tableNumber);
if (!dishCountMap.containsKey(dishName)) {
dishCountMap.put(dishName, 1);
} else {
dishCountMap.put(dishName, dishCountMap.get(dishName) + 1);
}
}
List<String> dishes = new ArrayList<>(dishSet);
Collections.sort(dishes);
dishes.add(0, "Table");
List<List<String>> result = new ArrayList<>();
result.add(dishes);
for (Map.Entry<Integer, Map<String, Integer>> entry : map.entrySet()) {
List<String> row = new ArrayList<>();
row.add("" + entry.getKey());
for (int i = 1; i < dishes.size(); i++) {
if (map.get(entry.getKey()).containsKey(dishes.get(i))) {
row.add(Integer.toString(map.get(entry.getKey()).get(dishes.get(i))));
} else {
row.add("0");
}
}
result.add(row);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
1418\. Display Table of Food Orders in a Restaurant

Medium

Given the array `orders`, which represents the orders that customers have done in a restaurant. More specifically <code>orders[i]=[customerName<sub>i</sub>,tableNumber<sub>i</sub>,foodItem<sub>i</sub>]</code> where <code>customerName<sub>i</sub></code> is the name of the customer, <code>tableNumber<sub>i</sub></code> is the table customer sit at, and <code>foodItem<sub>i</sub></code> is the item customer orders.

_Return the restaurant's “**display table**”_. The “**display table**” is a table whose row entries denote how many of each food item each table ordered. The first column is the table number and the remaining columns correspond to each food item in alphabetical order. The first row should be a header whose first column is “Table”, followed by the names of the food items. Note that the customer names are not part of the table. Additionally, the rows should be sorted in numerically increasing order.

**Example 1:**

**Input:** orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]

**Output:** [["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]]

**Explanation:** The displaying table looks like:

**Table,Beef Burrito,Ceviche,Fried Chicken,Water**

3 ,0 ,2 ,1 ,0

5 ,0 ,1 ,0 ,1

10 ,1 ,0 ,0 ,0

For the table 3: David orders "Ceviche" and "Fried Chicken", and Rous orders "Ceviche".

For the table 5: Carla orders "Water" and "Ceviche".

For the table 10: Corina orders "Beef Burrito".

**Example 2:**

**Input:** orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"],["Adam","1","Canadian Waffles"],["Brianna","1","Canadian Waffles"]]

**Output:** [["Table","Canadian Waffles","Fried Chicken"],["1","2","0"],["12","0","3"]]

**Explanation:**

For the table 1: Adam and Brianna order "Canadian Waffles".

For the table 12: James, Ratesh and Amadeus order "Fried Chicken".

**Example 3:**

**Input:** orders = [["Laura","2","Bean Burrito"],["Jhon","2","Beef Burrito"],["Melissa","2","Soda"]]

**Output:** [["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]

**Constraints:**

* `1 <= orders.length <= 5 * 10^4`
* `orders[i].length == 3`
* <code>1 <= customerName<sub>i</sub>.length, foodItem<sub>i</sub>.length <= 20</code>
* <code>customerName<sub>i</sub></code> and <code>foodItem<sub>i</sub></code> consist of lowercase and uppercase English letters and the space character.
* <code>tableNumber<sub>i</sub> </code>is a valid integer between `1` and `500`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package g1401_1500.s1419_minimum_number_of_frogs_croaking;

// #Medium #String #Counting #2022_03_26_Time_19_ms_(32.16%)_Space_42.6_MB_(86.27%)

public class Solution {
public int minNumberOfFrogs(String s) {
int ans = 0;
int[] f = new int[26];
for (int i = 0; i < s.length(); i++) {
f[s.charAt(i) - 'a']++;
if (s.charAt(i) == 'k' && checkEnough(f)) {
reduce(f);
}
if (!isValid(f)) {
return -1;
}
ans = Math.max(ans, getMax(f));
}
return isEmpty(f) ? ans : -1;
}

private boolean checkEnough(int[] f) {
return f['c' - 'a'] > 0
&& f['r' - 'a'] > 0
&& f['o' - 'a'] > 0
&& f[0] > 0
&& f['k' - 'a'] > 0;
}

public void reduce(int[] f) {

f['c' - 'a']--;
f['r' - 'a']--;
f['o' - 'a']--;
f[0]--;
f['k' - 'a']--;
}

private int getMax(int[] f) {
int max = 0;
for (int v : f) {
max = Math.max(max, v);
}
return max;
}

private boolean isEmpty(int[] f) {
for (int v : f) {
if (v > 0) {
return false;
}
}
return true;
}

private boolean isValid(int[] f) {
if (f['r' - 'a'] > f['c' - 'a']) {
return false;
}
if (f['o' - 'a'] > f['r' - 'a']) {
return false;
}
if (f[0] > f['o' - 'a']) {
return false;
}
return f['k' - 'a'] <= f[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
1419\. Minimum Number of Frogs Croaking

Medium

You are given the string `croakOfFrogs`, which represents a combination of the string `"croak"` from different frogs, that is, multiple frogs can croak at the same time, so multiple `"croak"` are mixed.

_Return the minimum number of_ different _frogs to finish all the croaks in the given string._

A valid `"croak"` means a frog is printing five letters `'c'`, `'r'`, `'o'`, `'a'`, and `'k'` **sequentially**. The frogs have to print all five letters to finish a croak. If the given string is not a combination of a valid `"croak"` return `-1`.

**Example 1:**

**Input:** croakOfFrogs = "croakcroak"

**Output:** 1

**Explanation:** One frog yelling "croak**"** twice.

**Example 2:**

**Input:** croakOfFrogs = "crcoakroak"

**Output:** 2

**Explanation:** The minimum number of frogs is two. The first frog could yell "**cr**c**oak**roak". The second frog could yell later "cr**c**oak**roak**".

**Example 3:**

**Input:** croakOfFrogs = "croakcrook"

**Output:** -1

**Explanation:** The given string is an invalid combination of "croak**"** from different frogs.

**Constraints:**

* <code>1 <= croakOfFrogs.length <= 10<sup>5</sup></code>
* `croakOfFrogs` is either `'c'`, `'r'`, `'o'`, `'a'`, or `'k'`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package g1401_1500.s1420_build_array_where_you_can_find_the_maximum_exactly_k_comparisons;

// #Hard #Dynamic_Programming #2022_03_28_Time_22_ms_(86.32%)_Space_47_MB_(13.68%)

public class Solution {
private static final int MOD = 1_000_000_007;

public int numOfArrays(int n, int m, int k) {
long[][] ways = new long[m + 1][k + 1];
long[][] sums = new long[m + 1][k + 1];

for (int max = 1; max <= m; max++) {
ways[max][1] = 1;
sums[max][1] = ways[max][1] + sums[max - 1][1];
}

for (int count = 2; count <= n; count++) {
long[][] ways2 = new long[m + 1][k + 1];
long[][] sums2 = new long[m + 1][k + 1];
for (int max = 1; max <= m; max++) {
for (int cost = 1; cost <= k; cost++) {

long noCost = max * ways[max][cost] % MOD;
long newCost = sums[max - 1][cost - 1];

ways2[max][cost] = (noCost + newCost) % MOD;
sums2[max][cost] = (sums2[max - 1][cost] + ways2[max][cost]) % MOD;
}
}
ways = ways2;
sums = sums2;
}

return (int) sums[m][k];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
1420\. Build Array Where You Can Find The Maximum Exactly K Comparisons

Hard

You are given three integers `n`, `m` and `k`. Consider the following algorithm to find the maximum element of an array of positive integers:

![](https://assets.leetcode.com/uploads/2020/04/02/e.png)

You should build the array arr which has the following properties:

* `arr` has exactly `n` integers.
* `1 <= arr[i] <= m` where `(0 <= i < n)`.
* After applying the mentioned algorithm to `arr`, the value `search_cost` is equal to `k`.

Return _the number of ways_ to build the array `arr` under the mentioned conditions. As the answer may grow large, the answer **must be** computed modulo <code>10<sup>9</sup> + 7</code>.

**Example 1:**

**Input:** n = 2, m = 3, k = 1

**Output:** 6

**Explanation:** The possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]

**Example 2:**

**Input:** n = 5, m = 2, k = 3

**Output:** 0

**Explanation:** There are no possible arrays that satisify the mentioned conditions.

**Example 3:**

**Input:** n = 9, m = 1, k = 1

**Output:** 1

**Explanation:** The only possible array is [1, 1, 1, 1, 1, 1, 1, 1, 1]

**Constraints:**

* `1 <= n <= 50`
* `1 <= m <= 100`
* `0 <= k <= n`
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package g1401_1500.s1422_maximum_score_after_splitting_a_string;

// #Easy #String #2022_03_28_Time_1_ms_(96.45%)_Space_42_MB_(57.42%)

public class Solution {
public int maxScore(String s) {
int zeroes = s.charAt(0) == '0' ? 1 : 0;
int ones = 0;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == '1') {
ones++;
}
}
int maxScore = zeroes + ones;
for (int i = 1; i < s.length() - 1; i++) {
if (s.charAt(i) == '0') {
zeroes++;
} else {
ones--;
}
maxScore = Math.max(maxScore, zeroes + ones);
}
return maxScore;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
1422\. Maximum Score After Splitting a String

Easy

Given a string `s` of zeros and ones, _return the maximum score after splitting the string into two **non-empty** substrings_ (i.e. **left** substring and **right** substring).

The score after splitting a string is the number of **zeros** in the **left** substring plus the number of **ones** in the **right** substring.

**Example 1:**

**Input:** s = "011101"

**Output:** 5

**Explanation:** All possible ways of splitting s into two non-empty substrings are:

left = "0" and right = "11101", score = 1 + 4 = 5

left = "01" and right = "1101", score = 1 + 3 = 4

left = "011" and right = "101", score = 1 + 2 = 3

left = "0111" and right = "01", score = 1 + 1 = 2

left = "01110" and right = "1", score = 2 + 1 = 3

**Example 2:**

**Input:** s = "00111"

**Output:** 5

**Explanation:** When left = "00" and right = "111", we get the maximum score = 2 + 3 = 5

**Example 3:**

**Input:** s = "1111"

**Output:** 3

**Constraints:**

* `2 <= s.length <= 500`
* The string `s` consists of characters `'0'` and `'1'` only.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package g1401_1500.s1423_maximum_points_you_can_obtain_from_cards;

// #Medium #Array #Prefix_Sum #Sliding_Window #2022_03_28_Time_4_ms_(31.70%)_Space_65.6_MB_(39.85%)

public class Solution {
public int maxScore(int[] cardPoints, int k) {
int maxScore = 0;
if (cardPoints.length <= k) {
for (int point : cardPoints) {
maxScore += point;
}
return maxScore;
}
for (int i = 0; i < k; i++) {
maxScore += cardPoints[i];
}
int runningSum = maxScore;
for (int i = cardPoints.length - 1, j = 1; i >= 0 && j <= k; i--, j++) {
runningSum = runningSum - cardPoints[k - j] + cardPoints[i];
maxScore = Math.max(maxScore, runningSum);
}
return maxScore;
}
}
Loading