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,25 @@
package g1601_1700.s1647_minimum_deletions_to_make_character_frequencies_unique;

// #Medium #String #Sorting #Greedy #2022_04_22_Time_8_ms_(100.00%)_Space_43.2_MB_(81.09%)

import java.util.HashSet;
import java.util.Set;

public class Solution {
public int minDeletions(String s) {
int cnt = 0;
int[] freq = new int[26];
Set<Integer> seen = new HashSet<>();
for (char c : s.toCharArray()) {
freq[c - 'a']++;
}
for (int i = 0; i < 26; i++) {
while (freq[i] > 0 && seen.contains(freq[i])) {
freq[i]--;
cnt++;
}
seen.add(freq[i]);
}
return cnt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
1647\. Minimum Deletions to Make Character Frequencies Unique

Medium

A string `s` is called **good** if there are no two different characters in `s` that have the same **frequency**.

Given a string `s`, return _the **minimum** number of characters you need to delete to make_ `s` _**good**._

The **frequency** of a character in a string is the number of times it appears in the string. For example, in the string `"aab"`, the **frequency** of `'a'` is `2`, while the **frequency** of `'b'` is `1`.

**Example 1:**

**Input:** s = "aab"

**Output:** 0

**Explanation:** `s` is already good.

**Example 2:**

**Input:** s = "aaabbbcc"

**Output:** 2

**Explanation:** You can delete two 'b's resulting in the good string "aaabcc". Another way it to delete one 'b' and one 'c' resulting in the good string "aaabbc".

**Example 3:**

**Input:** s = "ceabaacb"

**Output:** 2

**Explanation:** You can delete both 'c's resulting in the good string "eabaab". Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` contains only lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package g1601_1700.s1648_sell_diminishing_valued_colored_balls;

// #Medium #Array #Math #Sorting #Greedy #Binary_Search #Heap_Priority_Queue
// #2022_04_22_Time_27_ms_(80.64%)_Space_56.4_MB_(80.28%)

import java.util.Arrays;

public class Solution {
public int maxProfit(int[] inventory, int orders) {
int n = inventory.length;
long mod = (long) 1e9 + 7;
long totalValue = 0;

Arrays.sort(inventory);

int count = 0;
for (int i = n - 1; i >= 0; i--) {
count++;
if (i == 0 || inventory[i] > inventory[i - 1]) {
long diff = i == 0 ? inventory[i] : inventory[i] - inventory[i - 1];
if (count * diff < orders) {
totalValue += (2L * inventory[i] - diff + 1) * diff * count / 2 % mod;
orders -= count * diff;
} else {
diff = orders / count;
long remainder = orders % count;

totalValue += (2L * inventory[i] - diff + 1) * diff * count / 2 % mod;
totalValue += (inventory[i] - diff) * remainder % mod;
totalValue %= mod;
break;
}
}
}
return (int) totalValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
1648\. Sell Diminishing-Valued Colored Balls

Medium

You have an `inventory` of different colored balls, and there is a customer that wants `orders` balls of **any** color.

The customer weirdly values the colored balls. Each colored ball's value is the number of balls **of that color **you currently have in your `inventory`. For example, if you own `6` yellow balls, the customer would pay `6` for the first yellow ball. After the transaction, there are only `5` yellow balls left, so the next yellow ball is then valued at `5` (i.e., the value of the balls decreases as you sell more to the customer).

You are given an integer array, `inventory`, where `inventory[i]` represents the number of balls of the <code>i<sup>th</sup></code> color that you initially own. You are also given an integer `orders`, which represents the total number of balls that the customer wants. You can sell the balls **in any order**.

Return _the **maximum** total value that you can attain after selling_ `orders` _colored balls_. As the answer may be too large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/05/jj.gif)

**Input:** inventory = [2,5], orders = 4

**Output:** 14

**Explanation:** Sell the 1st color 1 time (2) and the 2nd color 3 times (5 + 4 + 3). The maximum total value is 2 + 5 + 4 + 3 = 14.

**Example 2:**

**Input:** inventory = [3,5], orders = 6

**Output:** 19

**Explanation:** Sell the 1st color 2 times (3 + 2) and the 2nd color 4 times (5 + 4 + 3 + 2). The maximum total value is 3 + 2 + 5 + 4 + 3 + 2 = 19.

**Constraints:**

* <code>1 <= inventory.length <= 10<sup>5</sup></code>
* <code>1 <= inventory[i] <= 10<sup>9</sup></code>
* <code>1 <= orders <= min(sum(inventory[i]), 10<sup>9</sup>)</code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package g1601_1700.s1649_create_sorted_array_through_instructions;

// #Hard #Array #Binary_Search #Ordered_Set #Divide_and_Conquer #Segment_Tree #Binary_Indexed_Tree
// #Merge_Sort #2022_04_22_Time_35_ms_(100.00%)_Space_60.4_MB_(82.68%)

public class Solution {
private static final long MODULO = (long) 1e9 + 7;

public int createSortedArray(int[] instructions) {
int maxValue = 0;
for (int num : instructions) {
maxValue = Math.max(maxValue, num);
}
int[] bit = new int[maxValue + 1];
long cost = 0;
for (int i = 0; i < instructions.length; i++) {
updateBIT(bit, maxValue, instructions[i]);
cost +=
Math.min(
queryBIT(bit, instructions[i] - 1),
1 + i - queryBIT(bit, instructions[i]));
}
return (int) (cost % MODULO);
}

private void updateBIT(int[] bit, int maxValue, int x) {
while (x <= maxValue) {
bit[x] += 1;
x += x & -x;
}
}

private int queryBIT(int[] bit, int x) {
int sum = 0;
while (x > 0) {
sum += bit[x];
x -= x & -x;
}
return sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
1649\. Create Sorted Array through Instructions

Hard

Given an integer array `instructions`, you are asked to create a sorted array from the elements in `instructions`. You start with an empty container `nums`. For each element from **left to right** in `instructions`, insert it into `nums`. The **cost** of each insertion is the **minimum** of the following:

* The number of elements currently in `nums` that are **strictly less than** `instructions[i]`.
* The number of elements currently in `nums` that are **strictly greater than** `instructions[i]`.

For example, if inserting element `3` into `nums = [1,2,3,5]`, the **cost** of insertion is `min(2, 1)` (elements `1` and `2` are less than `3`, element `5` is greater than `3`) and `nums` will become `[1,2,3,3,5]`.

Return _the **total cost** to insert all elements from_ `instructions` _into_ `nums`. Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>

**Example 1:**

**Input:** instructions = [1,5,6,2]

**Output:** 1

**Explanation:** Begin with nums = [].

Insert 1 with cost min(0, 0) = 0, now nums = [1].

Insert 5 with cost min(1, 0) = 0, now nums = [1,5].

Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].

Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].

The total cost is 0 + 0 + 0 + 1 = 1.

**Example 2:**

**Input:** instructions = [1,2,3,6,5,4]

**Output:** 3

**Explanation:** Begin with nums = [].

Insert 1 with cost min(0, 0) = 0, now nums = [1].

Insert 2 with cost min(1, 0) = 0, now nums = [1,2].

Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].

Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].

Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].

Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].

The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.

**Example 3:**

**Input:** instructions = [1,3,3,3,2,4,2,1,2]

**Output:** 4

**Explanation:** Begin with nums = [].

Insert 1 with cost min(0, 0) = 0, now nums = [1].

Insert 3 with cost min(1, 0) = 0, now nums = [1,3].

Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].

Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].

Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].

Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].

Insert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].

Insert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].

Insert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].

The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.

**Constraints:**

* <code>1 <= instructions.length <= 10<sup>5</sup></code>
* <code>1 <= instructions[i] <= 10<sup>5</sup></code>
39 changes: 39 additions & 0 deletions src/main/java/g1601_1700/s1652_defuse_the_bomb/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package g1601_1700.s1652_defuse_the_bomb;

// #Easy #Array #2022_04_22_Time_0_ms_(100.00%)_Space_42.5_MB_(72.00%)

public class Solution {
public int[] decrypt(int[] code, int k) {
int[] result = new int[code.length];
int len = code.length;
if (k == 0) {
for (int i = 0; i < code.length; i++) {
result[i] = 0;
}
} else if (k > 0) {
int kSum = 0;
for (int i = 1; i <= k; i++) {
kSum += code[i];
}
result[0] = kSum;
for (int i = 1; i < len; i++) {
kSum -= code[i];
kSum += code[(i + k) % len];
result[i] = kSum;
}
} else {
int kSum = 0;
int kVal = Math.abs(k);
for (int i = len - 1; i >= len - kVal; i--) {
kSum += code[i];
}
result[0] = kSum;
for (int i = 1; i < len; i++) {
kSum -= code[(len - kVal + i - 1) % len];
kSum += code[i - 1];
result[i] = kSum;
}
}
return result;
}
}
46 changes: 46 additions & 0 deletions src/main/java/g1601_1700/s1652_defuse_the_bomb/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
1652\. Defuse the Bomb

Easy

You have a bomb to defuse, and your time is running out! Your informer will provide you with a **circular** array `code` of length of `n` and a key `k`.

To decrypt the code, you must replace every number. All the numbers are replaced **simultaneously**.

* If `k > 0`, replace the <code>i<sup>th</sup></code> number with the sum of the **next** `k` numbers.
* If `k < 0`, replace the <code>i<sup>th</sup></code> number with the sum of the **previous** `k` numbers.
* If `k == 0`, replace the <code>i<sup>th</sup></code> number with `0`.

As `code` is circular, the next element of `code[n-1]` is `code[0]`, and the previous element of `code[0]` is `code[n-1]`.

Given the **circular** array `code` and an integer key `k`, return _the decrypted code to defuse the bomb_!

**Example 1:**

**Input:** code = [5,7,1,4], k = 3

**Output:** [12,10,16,13]

**Explanation:** Each number is replaced by the sum of the next 3 numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that the numbers wrap around.

**Example 2:**

**Input:** code = [1,2,3,4], k = 0

**Output:** [0,0,0,0]

**Explanation:** When k is zero, the numbers are replaced by 0.

**Example 3:**

**Input:** code = [2,4,9,3], k = -2

**Output:** [12,5,6,13]

**Explanation:** The decrypted code is [3+9, 2+3, 4+2, 9+4]. Notice that the numbers wrap around again. If k is negative, the sum is of the **previous** numbers.

**Constraints:**

* `n == code.length`
* `1 <= n <= 100`
* `1 <= code[i] <= 100`
* `-(n - 1) <= k <= n - 1`
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g1601_1700.s1653_minimum_deletions_to_make_string_balanced;

// #Medium #String #Dynamic_Programming #Stack
// #2022_04_22_Time_26_ms_(90.44%)_Space_64.3_MB_(37.05%)

public class Solution {
public int minimumDeletions(String s) {
int a = 0;
int b = 0;
for (char ch : s.toCharArray()) {
if (ch == 'a') {
a++;
} else {
b = Math.max(a, b) + 1;
}
}

return s.length() - Math.max(a, b);
}
}
Loading