Skip to content

Added tasks 3295-3298 #1825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/main/java/g3201_3300/s3295_report_spam_message/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3201_3300.s3295_report_spam_message;

// #Medium #Array #String #Hash_Table #2024_09_24_Time_39_ms_(98.87%)_Space_85.4_MB_(9.13%)

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

public class Solution {
public boolean reportSpam(String[] message, String[] bannedWords) {
Set<String> bannedUnique = new HashSet<>(Arrays.asList(bannedWords));
int bannedCount = 0;
for (String msg : message) {
if (bannedUnique.contains(msg)) {
bannedCount++;
}
if (bannedCount == 2) {
return true;
}
}
return false;
}
}
35 changes: 35 additions & 0 deletions src/main/java/g3201_3300/s3295_report_spam_message/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
3295\. Report Spam Message

Medium

You are given an array of strings `message` and an array of strings `bannedWords`.

An array of words is considered **spam** if there are **at least** two words in it that **exactly** match any word in `bannedWords`.

Return `true` if the array `message` is spam, and `false` otherwise.

**Example 1:**

**Input:** message = ["hello","world","leetcode"], bannedWords = ["world","hello"]

**Output:** true

**Explanation:**

The words `"hello"` and `"world"` from the `message` array both appear in the `bannedWords` array.

**Example 2:**

**Input:** message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]

**Output:** false

**Explanation:**

Only one word from the `message` array (`"programming"`) appears in the `bannedWords` array.

**Constraints:**

* <code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code>
* `1 <= message[i].length, bannedWords[i].length <= 15`
* `message[i]` and `bannedWords[i]` consist only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g3201_3300.s3296_minimum_number_of_seconds_to_make_mountain_height_zero;

// #Medium #Array #Math #Binary_Search #2024_09_24_Time_6_ms_(100.00%)_Space_45.2_MB_(90.23%)

public class Solution {
public long minNumberOfSeconds(int mountainHeight, int[] workerTimes) {
long left = 0;
long right = (long) mountainHeight * (mountainHeight + 1) / 2 * workerTimes[0];
while (left < right) {
long mid = left + (right - left) / 2;
if (canReduceMountain(workerTimes, mountainHeight, mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}

private boolean canReduceMountain(int[] workerTimes, int mountainHeight, long timeLimit) {
long totalHeightReduced = 0;
for (int workerTime : workerTimes) {
long maxHeightThisWorker =
(long) (Math.sqrt(2.0 * timeLimit / workerTime + 0.25) - 0.5);
totalHeightReduced += maxHeightThisWorker;
if (totalHeightReduced >= mountainHeight) {
return true;
}
}
return totalHeightReduced >= mountainHeight;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
3296\. Minimum Number of Seconds to Make Mountain Height Zero

Medium

You are given an integer `mountainHeight` denoting the height of a mountain.

You are also given an integer array `workerTimes` representing the work time of workers in **seconds**.

The workers work **simultaneously** to **reduce** the height of the mountain. For worker `i`:

* To decrease the mountain's height by `x`, it takes `workerTimes[i] + workerTimes[i] * 2 + ... + workerTimes[i] * x` seconds. For example:
* To reduce the height of the mountain by 1, it takes `workerTimes[i]` seconds.
* To reduce the height of the mountain by 2, it takes `workerTimes[i] + workerTimes[i] * 2` seconds, and so on.

Return an integer representing the **minimum** number of seconds required for the workers to make the height of the mountain 0.

**Example 1:**

**Input:** mountainHeight = 4, workerTimes = [2,1,1]

**Output:** 3

**Explanation:**

One way the height of the mountain can be reduced to 0 is:

* Worker 0 reduces the height by 1, taking `workerTimes[0] = 2` seconds.
* Worker 1 reduces the height by 2, taking `workerTimes[1] + workerTimes[1] * 2 = 3` seconds.
* Worker 2 reduces the height by 1, taking `workerTimes[2] = 1` second.

Since they work simultaneously, the minimum time needed is `max(2, 3, 1) = 3` seconds.

**Example 2:**

**Input:** mountainHeight = 10, workerTimes = [3,2,2,4]

**Output:** 12

**Explanation:**

* Worker 0 reduces the height by 2, taking `workerTimes[0] + workerTimes[0] * 2 = 9` seconds.
* Worker 1 reduces the height by 3, taking `workerTimes[1] + workerTimes[1] * 2 + workerTimes[1] * 3 = 12` seconds.
* Worker 2 reduces the height by 3, taking `workerTimes[2] + workerTimes[2] * 2 + workerTimes[2] * 3 = 12` seconds.
* Worker 3 reduces the height by 2, taking `workerTimes[3] + workerTimes[3] * 2 = 12` seconds.

The number of seconds needed is `max(9, 12, 12, 12) = 12` seconds.

**Example 3:**

**Input:** mountainHeight = 5, workerTimes = [1]

**Output:** 15

**Explanation:**

There is only one worker in this example, so the answer is `workerTimes[0] + workerTimes[0] * 2 + workerTimes[0] * 3 + workerTimes[0] * 4 + workerTimes[0] * 5 = 15`.

**Constraints:**

* <code>1 <= mountainHeight <= 10<sup>5</sup></code>
* <code>1 <= workerTimes.length <= 10<sup>4</sup></code>
* <code>1 <= workerTimes[i] <= 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package g3201_3300.s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i;

// #Medium #String #Hash_Table #Sliding_Window #2024_09_24_Time_5_ms_(99.40%)_Space_44.9_MB_(51.04%)

public class Solution {
public long validSubstringCount(String word1, String word2) {
long res = 0;
int keys = 0;
int len = word1.length();
int[] count = new int[26];
boolean[] letters = new boolean[26];
for (char letter : word2.toCharArray()) {
int index = letter - 'a';
if (count[index]++ == 0) {
letters[index] = true;
keys++;
}
}
int start = 0;
int end = 0;
while (end < len) {
int index = word1.charAt(end) - 'a';
if (!letters[index]) {
end++;
continue;
}
if (--count[index] == 0) {
--keys;
}
while (keys == 0) {
res += len - end;
int beginIndex = word1.charAt(start++) - 'a';
if (!letters[beginIndex]) {
continue;
}
if (count[beginIndex]++ == 0) {
keys++;
}
}
end++;
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
3297\. Count Substrings That Can Be Rearranged to Contain a String I

Medium

You are given two strings `word1` and `word2`.

A string `x` is called **valid** if `x` can be rearranged to have `word2` as a prefix.

Return the total number of **valid** substrings of `word1`.

**Example 1:**

**Input:** word1 = "bcca", word2 = "abc"

**Output:** 1

**Explanation:**

The only valid substring is `"bcca"` which can be rearranged to `"abcc"` having `"abc"` as a prefix.

**Example 2:**

**Input:** word1 = "abcabc", word2 = "abc"

**Output:** 10

**Explanation:**

All the substrings except substrings of size 1 and size 2 are valid.

**Example 3:**

**Input:** word1 = "abcabc", word2 = "aaabc"

**Output:** 0

**Constraints:**

* <code>1 <= word1.length <= 10<sup>5</sup></code>
* <code>1 <= word2.length <= 10<sup>4</sup></code>
* `word1` and `word2` consist only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g3201_3300.s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii;

// #Hard #String #Hash_Table #Sliding_Window #2024_09_24_Time_31_ms_(100.00%)_Space_56.1_MB_(68.76%)

public class Solution {
public long validSubstringCount(String word1, String word2) {
char[] ar = word1.toCharArray();
int n = ar.length;
char[] temp = word2.toCharArray();
int[] f = new int[26];
for (char i : temp) {
f[i - 97]++;
}
long ans = 0;
int needed = temp.length;
int beg = 0;
int end = 0;
while (end < n) {
if (f[ar[end] - 97]-- > 0) {
needed--;
}
while (needed == 0) {
// All substrings from [beg, i], where end <= i < n are valid
ans += n - end;
// Shrink
if (f[ar[beg++] - 97]++ == 0) {
needed++;
}
}
end++;
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
3298\. Count Substrings That Can Be Rearranged to Contain a String II

Hard

You are given two strings `word1` and `word2`.

A string `x` is called **valid** if `x` can be rearranged to have `word2` as a prefix.

Return the total number of **valid** substrings of `word1`.

**Note** that the memory limits in this problem are **smaller** than usual, so you **must** implement a solution with a _linear_ runtime complexity.

**Example 1:**

**Input:** word1 = "bcca", word2 = "abc"

**Output:** 1

**Explanation:**

The only valid substring is `"bcca"` which can be rearranged to `"abcc"` having `"abc"` as a prefix.

**Example 2:**

**Input:** word1 = "abcabc", word2 = "abc"

**Output:** 10

**Explanation:**

All the substrings except substrings of size 1 and size 2 are valid.

**Example 3:**

**Input:** word1 = "abcabc", word2 = "aaabc"

**Output:** 0

**Constraints:**

* <code>1 <= word1.length <= 10<sup>6</sup></code>
* <code>1 <= word2.length <= 10<sup>4</sup></code>
* `word1` and `word2` consist only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g3201_3300.s3295_report_spam_message;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void reportSpam() {
assertThat(
new Solution()
.reportSpam(
new String[] {"hello", "world", "leetcode"},
new String[] {"world", "hello"}),
equalTo(true));
}

@Test
void reportSpam2() {
assertThat(
new Solution()
.reportSpam(
new String[] {"hello", "programming", "fun"},
new String[] {"world", "programming", "leetcode"}),
equalTo(false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3201_3300.s3296_minimum_number_of_seconds_to_make_mountain_height_zero;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void minNumberOfSeconds() {
assertThat(new Solution().minNumberOfSeconds(4, new int[] {2, 1, 1}), equalTo(3L));
}

@Test
void minNumberOfSeconds2() {
assertThat(new Solution().minNumberOfSeconds(10, new int[] {3, 2, 2, 4}), equalTo(12L));
}

@Test
void minNumberOfSeconds3() {
assertThat(new Solution().minNumberOfSeconds(5, new int[] {1}), equalTo(15L));
}
}
Loading
Loading