Skip to content

Added tasks 3295-3298 #694

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 3 commits into from
Sep 24, 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
19 changes: 19 additions & 0 deletions src/main/kotlin/g3201_3300/s3295_report_spam_message/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3201_3300.s3295_report_spam_message

// #Medium #Array #String #Hash_Table #2024_09_24_Time_782_ms_(74.19%)_Space_109.6_MB_(38.71%)

class Solution {
fun reportSpam(message: Array<String>, bannedWords: Array<String>): Boolean {
val bannedUnique: MutableSet<String?> = mutableSetOf(*bannedWords)
var bannedCount = 0
for (msg in message) {
if (bannedUnique.contains(msg)) {
bannedCount++
}
if (bannedCount == 2) {
return true
}
}
return false
}
}
35 changes: 35 additions & 0 deletions src/main/kotlin/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,33 @@
package g3201_3300.s3296_minimum_number_of_seconds_to_make_mountain_height_zero

// #Medium #Array #Math #Binary_Search #2024_09_24_Time_228_ms_(87.50%)_Space_38.6_MB_(81.25%)

import kotlin.math.sqrt

class Solution {
fun minNumberOfSeconds(mountainHeight: Int, workerTimes: IntArray): Long {
var left: Long = 0
var right = mountainHeight.toLong() * (mountainHeight + 1) / 2 * workerTimes[0]
while (left < right) {
val mid = left + (right - left) / 2
if (canReduceMountain(workerTimes, mountainHeight, mid)) {
right = mid
} else {
left = mid + 1
}
}
return left
}

private fun canReduceMountain(workerTimes: IntArray, mountainHeight: Int, timeLimit: Long): Boolean {
var totalHeightReduced: Long = 0
for (workerTime in workerTimes) {
val maxHeightThisWorker = (sqrt(2.0 * timeLimit / workerTime + 0.25) - 0.5).toLong()
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,45 @@
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_215_ms_(93.33%)_Space_38.2_MB_(93.33%)

class Solution {
fun validSubstringCount(word1: String, word2: String): Long {
var res: Long = 0
var keys = 0
val len = word1.length
val count = IntArray(26)
val letters = BooleanArray(26)
for (letter in word2.toCharArray()) {
val index = letter.code - 'a'.code
if (count[index]++ == 0) {
letters[index] = true
keys++
}
}
var start = 0
var end = 0
while (end < len) {
val index = word1[end].code - 'a'.code
if (!letters[index]) {
end++
continue
}
if (--count[index] == 0) {
--keys
}
while (keys == 0) {
res += (len - end).toLong()
val beginIndex = word1[start++].code - 'a'.code
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_433_ms_(78.57%)_Space_51.9_MB_(42.86%)

class Solution {
fun validSubstringCount(word1: String, word2: String): Long {
val ar = word1.toCharArray()
val n = ar.size
val temp = word2.toCharArray()
val f = IntArray(26)
for (i in temp) {
f[i.code - 97]++
}
var ans: Long = 0
var needed = temp.size
var beg = 0
var end = 0
while (end < n) {
if (f[ar[end].code - 97]-- > 0) {
needed--
}
while (needed == 0) {
// All substrings from [beg, i], where end <= i < n are valid
ans += (n - end).toLong()
// Shrink
if (f[ar[beg++].code - 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,31 @@
package g3201_3300.s3295_report_spam_message

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Test

internal class SolutionTest {
@Test
fun reportSpam() {
assertThat<Boolean>(
Solution()
.reportSpam(
arrayOf<String>("hello", "world", "leetcode"),
arrayOf<String>("world", "hello")
),
equalTo<Boolean>(true)
)
}

@Test
fun reportSpam2() {
assertThat<Boolean>(
Solution()
.reportSpam(
arrayOf<String>("hello", "programming", "fun"),
arrayOf<String>("world", "programming", "leetcode")
),
equalTo<Boolean>(false)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g3201_3300.s3296_minimum_number_of_seconds_to_make_mountain_height_zero

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Test

internal class SolutionTest {
@Test
fun minNumberOfSeconds() {
assertThat<Long>(
Solution().minNumberOfSeconds(4, intArrayOf(2, 1, 1)),
equalTo<Long>(3L)
)
}

@Test
fun minNumberOfSeconds2() {
assertThat<Long>(
Solution().minNumberOfSeconds(10, intArrayOf(3, 2, 2, 4)),
equalTo<Long>(12L)
)
}

@Test
fun minNumberOfSeconds3() {
assertThat<Long>(
Solution().minNumberOfSeconds(5, intArrayOf(1)),
equalTo<Long>(15L)
)
}
}
Loading