Skip to content

Added tasks 3300-3307 #698

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
Oct 1, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package g3201_3300.s3300_minimum_element_after_replacement_with_digit_sum

// #Easy #Array #Math #2024_10_01_Time_153_ms_(100.00%)_Space_36.5_MB_(95.24%)

import kotlin.math.min

class Solution {
fun minElement(nums: IntArray): Int {
var min = Int.Companion.MAX_VALUE
for (x in nums) {
min = min(min, solve(x))
}
return min
}

private fun solve(x: Int): Int {
var x = x
var sum = 0
while (x != 0) {
sum += x % 10
x /= 10
}
return sum
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
3300\. Minimum Element After Replacement With Digit Sum

Easy

You are given an integer array `nums`.

You replace each element in `nums` with the **sum** of its digits.

Return the **minimum** element in `nums` after all replacements.

**Example 1:**

**Input:** nums = [10,12,13,14]

**Output:** 1

**Explanation:**

`nums` becomes `[1, 3, 4, 5]` after all replacements, with minimum element 1.

**Example 2:**

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

**Output:** 1

**Explanation:**

`nums` becomes `[1, 2, 3, 4]` after all replacements, with minimum element 1.

**Example 3:**

**Input:** nums = [999,19,199]

**Output:** 10

**Explanation:**

`nums` becomes `[27, 10, 19]` after all replacements, with minimum element 10.

**Constraints:**

* `1 <= nums.length <= 100`
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package g3301_3400.s3301_maximize_the_total_height_of_unique_towers

// #Medium #Array #Sorting #Greedy #2024_10_01_Time_761_ms_(87.50%)_Space_68.1_MB_(77.50%)

class Solution {
fun maximumTotalSum(maximumHeight: IntArray): Long {
maximumHeight.sort()
var result = maximumHeight[maximumHeight.size - 1].toLong()
var previousHeight = maximumHeight[maximumHeight.size - 1].toLong()
for (i in maximumHeight.size - 2 downTo 0) {
if (previousHeight == 1L) {
return -1
}
val height = maximumHeight[i].toLong()
if (height >= previousHeight) {
result = result + previousHeight - 1
previousHeight = previousHeight - 1
} else {
result = result + height
previousHeight = height
}
}
return result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3301\. Maximize the Total Height of Unique Towers

Medium

You are given an array `maximumHeight`, where `maximumHeight[i]` denotes the **maximum** height the <code>i<sup>th</sup></code> tower can be assigned.

Your task is to assign a height to each tower so that:

1. The height of the <code>i<sup>th</sup></code> tower is a positive integer and does not exceed `maximumHeight[i]`.
2. No two towers have the same height.

Return the **maximum** possible total sum of the tower heights. If it's not possible to assign heights, return `-1`.

**Example 1:**

**Input:** maximumHeight = [2,3,4,3]

**Output:** 10

**Explanation:**

We can assign heights in the following way: `[1, 2, 4, 3]`.

**Example 2:**

**Input:** maximumHeight = [15,10]

**Output:** 25

**Explanation:**

We can assign heights in the following way: `[15, 10]`.

**Example 3:**

**Input:** maximumHeight = [2,2,1]

**Output:** \-1

**Explanation:**

It's impossible to assign positive heights to each index so that no two towers have the same height.

**Constraints:**

* <code>1 <= maximumHeight.length <= 10<sup>5</sup></code>
* <code>1 <= maximumHeight[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package g3301_3400.s3302_find_the_lexicographically_smallest_valid_sequence

// #Medium #String #Dynamic_Programming #Greedy #Two_Pointers
// #2024_10_01_Time_705_ms_(100.00%)_Space_65.9_MB_(100.00%)

class Solution {
fun validSequence(word1: String, word2: String): IntArray? {
val c1 = word1.toCharArray()
val c2 = word2.toCharArray()
val dp = IntArray(c1.size + 1)
var j = c2.size - 1
for (i in c1.indices.reversed()) {
if (j >= 0 && c1[i] == c2[j]) {
dp[i] = dp[i + 1] + 1
j--
} else {
dp[i] = dp[i + 1]
}
}
val ans = IntArray(c2.size)
var i = 0
j = 0
while (i < c1.size && j < c2.size) {
if (c1[i] == c2[j]) {
ans[j] = i
j++
} else {
if (dp[i + 1] >= c2.size - 1 - j) {
ans[j] = i
j++
i++
break
}
}
i++
}
if (j < c2.size && i == c1.size) {
return IntArray(0)
}
while (j < c2.size && i < c1.size) {
if (c2[j] == c1[i]) {
ans[j] = i
j++
}
i++
}
return ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
3302\. Find the Lexicographically Smallest Valid Sequence

Medium

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

A string `x` is called **almost equal** to `y` if you can change **at most** one character in `x` to make it _identical_ to `y`.

A sequence of indices `seq` is called **valid** if:

* The indices are sorted in **ascending** order.
* _Concatenating_ the characters at these indices in `word1` in **the same** order results in a string that is **almost equal** to `word2`.

Return an array of size `word2.length` representing the lexicographically smallest **valid** sequence of indices. If no such sequence of indices exists, return an **empty** array.

**Note** that the answer must represent the _lexicographically smallest array_, **not** the corresponding string formed by those indices.

**Example 1:**

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

**Output:** [0,1,2]

**Explanation:**

The lexicographically smallest valid sequence of indices is `[0, 1, 2]`:

* Change `word1[0]` to `'a'`.
* `word1[1]` is already `'b'`.
* `word1[2]` is already `'c'`.

**Example 2:**

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

**Output:** [1,2,4]

**Explanation:**

The lexicographically smallest valid sequence of indices is `[1, 2, 4]`:

* `word1[1]` is already `'a'`.
* Change `word1[2]` to `'b'`.
* `word1[4]` is already `'c'`.

**Example 3:**

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

**Output:** []

**Explanation:**

There is no valid sequence of indices.

**Example 4:**

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

**Output:** [0,1]

**Constraints:**

* <code>1 <= word2.length < word1.length <= 3 * 10<sup>5</sup></code>
* `word1` and `word2` consist only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package g3301_3400.s3303_find_the_occurrence_of_first_almost_equal_substring

// #Hard #String #String_Matching #2024_10_01_Time_364_ms_(100.00%)_Space_40.8_MB_(100.00%)

import kotlin.math.abs

class Solution {
fun minStartingIndex(s: String, pattern: String): Int {
val n = s.length
var left = 0
var right = 0
val f1 = IntArray(26)
val f2 = IntArray(26)
for (ch in pattern.toCharArray()) {
f2[ch.code - 'a'.code]++
}
while (right < n) {
val ch = s[right]
f1[ch.code - 'a'.code]++
if (right - left + 1 == pattern.length + 1) {
f1[s[left].code - 'a'.code]--
left += 1
}
if (right - left + 1 == pattern.length && check(f1, f2, left, s, pattern)) {
return left
}
right += 1
}
return -1
}

private fun check(f1: IntArray, f2: IntArray, left: Int, s: String, pattern: String): Boolean {
var cnt = 0
for (i in 0..25) {
if (f1[i] != f2[i]) {
if ((abs((f1[i] - f2[i])) > 1) || (abs(f1[i] - f2[i]) != 1 && cnt == 2)) {
return false
}
cnt += 1
}
}
cnt = 0
var start = 0
var end = pattern.length - 1
while (start <= end) {
if (s[start + left] != pattern[start]) {
cnt += 1
}
if (start + left != left + end && s[left + end] != pattern[end]) {
cnt += 1
}
if (cnt >= 2) {
return false
}
start++
end--
}
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
3303\. Find the Occurrence of First Almost Equal Substring

Hard

You are given two strings `s` and `pattern`.

A string `x` is called **almost equal** to `y` if you can change **at most** one character in `x` to make it _identical_ to `y`.

Return the **smallest** _starting index_ of a substring in `s` that is **almost equal** to `pattern`. If no such index exists, return `-1`.

A **substring** is a contiguous **non-empty** sequence of characters within a string.

**Example 1:**

**Input:** s = "abcdefg", pattern = "bcdffg"

**Output:** 1

**Explanation:**

The substring `s[1..6] == "bcdefg"` can be converted to `"bcdffg"` by changing `s[4]` to `"f"`.

**Example 2:**

**Input:** s = "ababbababa", pattern = "bacaba"

**Output:** 4

**Explanation:**

The substring `s[4..9] == "bababa"` can be converted to `"bacaba"` by changing `s[6]` to `"c"`.

**Example 3:**

**Input:** s = "abcd", pattern = "dba"

**Output:** \-1

**Example 4:**

**Input:** s = "dde", pattern = "d"

**Output:** 0

**Constraints:**

* <code>1 <= pattern.length < s.length <= 3 * 10<sup>5</sup></code>
* `s` and `pattern` consist only of lowercase English letters.

**Follow-up:** Could you solve the problem if **at most** `k` **consecutive** characters can be changed?
Loading