Skip to content

Commit 34cdced

Browse files
authored
Added tasks 2739-2744
1 parent 11566fb commit 34cdced

File tree

15 files changed

+411
-0
lines changed

15 files changed

+411
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g2701_2800.s2739_total_distance_traveled
2+
3+
// #Easy #Math #Simulation #2023_08_05_Time_177_ms_(100.00%)_Space_35.9_MB_(92.11%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun distanceTraveled(mainTank: Int, additionalTank: Int): Int {
9+
val transferableTimes = (mainTank - 1) / 4
10+
val transferredLiters = min(transferableTimes, additionalTank)
11+
return (mainTank + transferredLiters) * 10
12+
}
13+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2739\. Total Distance Traveled
2+
3+
Easy
4+
5+
A truck has two fuel tanks. You are given two integers, `mainTank` representing the fuel present in the main tank in liters and `additionalTank` representing the fuel present in the additional tank in liters.
6+
7+
The truck has a mileage of `10` km per liter. Whenever `5` liters of fuel get used up in the main tank, if the additional tank has at least `1` liters of fuel, `1` liters of fuel will be transferred from the additional tank to the main tank.
8+
9+
Return _the maximum distance which can be traveled._
10+
11+
**Note:** Injection from the additional tank is not continuous. It happens suddenly and immediately for every 5 liters consumed.
12+
13+
**Example 1:**
14+
15+
**Input:** mainTank = 5, additionalTank = 10
16+
17+
**Output:** 60
18+
19+
**Explanation:**
20+
21+
After spending 5 litre of fuel, fuel remaining is (5 - 5 + 1) = 1 litre and distance traveled is 50km.
22+
23+
After spending another 1 litre of fuel, no fuel gets injected in the main tank and the main tank becomes empty.
24+
25+
Total distance traveled is 60km.
26+
27+
**Example 2:**
28+
29+
**Input:** mainTank = 1, additionalTank = 2
30+
31+
**Output:** 10
32+
33+
**Explanation:** After spending 1 litre of fuel, the main tank becomes empty. Total distance traveled is 10km.
34+
35+
**Constraints:**
36+
37+
* `1 <= mainTank, additionalTank <= 100`
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package g2701_2800.s2740_find_the_value_of_the_partition
2+
3+
// #Medium #Array #Sorting #2023_08_05_Time_431_ms_(100.00%)_Space_57.5_MB_(72.73%)
4+
5+
class Solution {
6+
fun findValueOfPartition(nums: IntArray): Int = nums
7+
.sortedDescending()
8+
.zipWithNext(Int::minus)
9+
.min()!!
10+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2740\. Find the Value of the Partition
2+
3+
Medium
4+
5+
You are given a **positive** integer array `nums`.
6+
7+
Partition `nums` into two arrays, `nums1` and `nums2`, such that:
8+
9+
* Each element of the array `nums` belongs to either the array `nums1` or the array `nums2`.
10+
* Both arrays are **non-empty**.
11+
* The value of the partition is **minimized**.
12+
13+
The value of the partition is `|max(nums1) - min(nums2)|`.
14+
15+
Here, `max(nums1)` denotes the maximum element of the array `nums1`, and `min(nums2)` denotes the minimum element of the array `nums2`.
16+
17+
Return _the integer denoting the value of such partition_.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [1,3,2,4]
22+
23+
**Output:** 1
24+
25+
**Explanation:** We can partition the array nums into nums1 = [1,2] and nums2 = [3,4].
26+
- The maximum element of the array nums1 is equal to 2.
27+
- The minimum element of the array nums2 is equal to 3.
28+
29+
The value of the partition is |2 - 3| = 1.
30+
31+
It can be proven that 1 is the minimum value out of all partitions.
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [100,1,10]
36+
37+
**Output:** 9
38+
39+
**Explanation:** We can partition the array nums into nums1 = [10] and nums2 = [100,1].
40+
- The maximum element of the array nums1 is equal to 10.
41+
- The minimum element of the array nums2 is equal to 1.
42+
43+
The value of the partition is |10 - 1| = 9.
44+
45+
It can be proven that 9 is the minimum value out of all partitions.
46+
47+
**Constraints:**
48+
49+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
50+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g2701_2800.s2741_special_permutations
2+
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Bitmask
4+
// #2023_08_07_Time_623_ms_(82.35%)_Space_60.8_MB_(52.94%)
5+
6+
class Solution {
7+
private var dp = HashMap<Pair<Int, Int>, Long>()
8+
private var adj = HashMap<Int, HashSet<Int>>()
9+
private var mod = 1000000007
10+
11+
private fun count(destIdx: Int, set: Int): Long {
12+
if (Integer.bitCount(set) == 1) return 1
13+
val p = destIdx to set
14+
if (dp.containsKey(p)) {
15+
return dp[p]!!
16+
}
17+
var sum = 0L
18+
val newSet = set xor (1 shl destIdx)
19+
for (i in adj[destIdx]!!) {
20+
if ((set and (1 shl i)) == 0) continue
21+
sum += count(i, newSet) % mod
22+
sum %= mod
23+
}
24+
dp[p] = sum
25+
return sum
26+
}
27+
28+
fun specialPerm(nums: IntArray): Int {
29+
for (i in nums.indices) adj[i] = hashSetOf()
30+
for ((i, vI) in nums.withIndex()) {
31+
for ((j, vJ) in nums.withIndex()) {
32+
if (vI != vJ && vI % vJ == 0) {
33+
adj[i]!!.add(j)
34+
adj[j]!!.add(i)
35+
}
36+
}
37+
}
38+
if (adj.all { it.value.size == nums.size - 1 }) {
39+
return (fact(nums.size.toLong()) % mod).toInt()
40+
}
41+
var total = 0
42+
for (i in nums.indices) {
43+
total += (count(i, (1 shl nums.size) - 1) % mod).toInt()
44+
total %= mod
45+
}
46+
return total
47+
}
48+
49+
private fun fact(n: Long): Long {
50+
if (n == 1L) return n
51+
return n * fact(n - 1)
52+
}
53+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2741\. Special Permutations
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums` containing `n` **distinct** positive integers. A permutation of `nums` is called special if:
6+
7+
* For all indexes `0 <= i < n - 1`, either `nums[i] % nums[i+1] == 0` or `nums[i+1] % nums[i] == 0`.
8+
9+
Return _the total number of special permutations. _As the answer could be large, return it **modulo **<code>10<sup>9 </sup>+ 7</code>.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,3,6]
14+
15+
**Output:** 2
16+
17+
**Explanation:** [3,6,2] and [2,6,3] are the two special permutations of nums.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,4,3]
22+
23+
**Output:** 2
24+
25+
**Explanation:** [3,1,4] and [4,1,3] are the two special permutations of nums.
26+
27+
**Constraints:**
28+
29+
* `2 <= nums.length <= 14`
30+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2701_2800.s2742_painting_the_walls
2+
3+
// #Hard #Array #Dynamic_Programming #2023_08_07_Time_268_ms_(100.00%)_Space_41.2_MB_(87.50%)
4+
5+
class Solution {
6+
fun paintWalls(cost: IntArray, time: IntArray): Int {
7+
val n = cost.size
8+
val dp = Array(n + 1) { IntArray(n + 1) }
9+
return solve(n, cost, 0, time, dp)
10+
}
11+
12+
private fun solve(wallsRem: Int, cost: IntArray, idx: Int, time: IntArray, dp: Array<IntArray>): Int {
13+
if (wallsRem <= 0) return 0
14+
if (idx >= cost.size) return 1000000000
15+
if (dp[idx][wallsRem] != 0) {
16+
return dp[idx][wallsRem]
17+
}
18+
val skip = solve(wallsRem, cost, idx + 1, time, dp)
19+
val take = cost[idx] + solve(wallsRem - time[idx] - 1, cost, idx + 1, time, dp)
20+
dp[idx][wallsRem] = skip.coerceAtMost(take)
21+
return dp[idx][wallsRem]
22+
}
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2742\. Painting the Walls
2+
3+
Hard
4+
5+
You are given two **0-indexed** integer arrays, `cost` and `time`, of size `n` representing the costs and the time taken to paint `n` different walls respectively. There are two painters available:
6+
7+
* A** paid painter** that paints the <code>i<sup>th</sup></code> wall in `time[i]` units of time and takes `cost[i]` units of money.
8+
* A** free painter** that paints **any** wall in `1` unit of time at a cost of `0`. But the free painter can only be used if the paid painter is already **occupied**.
9+
10+
Return _the minimum amount of money required to paint the_ `n` _walls._
11+
12+
**Example 1:**
13+
14+
**Input:** cost = [1,2,3,2], time = [1,2,3,2]
15+
16+
**Output:** 3
17+
18+
**Explanation:** The walls at index 0 and 1 will be painted by the paid painter, and it will take 3 units of time; meanwhile, the free painter will paint the walls at index 2 and 3, free of cost in 2 units of time. Thus, the total cost is 1 + 2 = 3.
19+
20+
**Example 2:**
21+
22+
**Input:** cost = [2,3,4,2], time = [1,1,1,1]
23+
24+
**Output:** 4
25+
26+
**Explanation:** The walls at index 0 and 3 will be painted by the paid painter, and it will take 2 units of time; meanwhile, the free painter will paint the walls at index 1 and 2, free of cost in 2 units of time. Thus, the total cost is 2 + 2 = 4.
27+
28+
**Constraints:**
29+
30+
* `1 <= cost.length <= 500`
31+
* `cost.length == time.length`
32+
* <code>1 <= cost[i] <= 10<sup>6</sup></code>
33+
* `1 <= time[i] <= 500`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2701_2800.s2744_find_maximum_number_of_string_pairs
2+
3+
// #Easy #Array #String #Hash_Table #Simulation
4+
// #2023_08_07_Time_162_ms_(96.81%)_Space_36.4_MB_(85.11%)
5+
6+
class Solution {
7+
fun maximumNumberOfStringPairs(words: Array<String>): Int {
8+
val set: MutableSet<String> = HashSet()
9+
var cnt = 0
10+
for (s in words) {
11+
val sb = StringBuilder(s).reverse()
12+
if (set.contains(sb.toString())) {
13+
cnt++
14+
} else {
15+
set.add(s)
16+
}
17+
}
18+
return cnt
19+
}
20+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2744\. Find Maximum Number of String Pairs
2+
3+
Easy
4+
5+
You are given a **0-indexed** array `words` consisting of **distinct** strings.
6+
7+
The string `words[i]` can be paired with the string `words[j]` if:
8+
9+
* The string `words[i]` is equal to the reversed string of `words[j]`.
10+
* `0 <= i < j < words.length`.
11+
12+
Return _the **maximum** number of pairs that can be formed from the array_ `words`_._
13+
14+
Note that each string can belong in **at most one** pair.
15+
16+
**Example 1:**
17+
18+
**Input:** words = ["cd","ac","dc","ca","zz"]
19+
20+
**Output:** 2
21+
22+
**Explanation:** In this example, we can form 2 pair of strings in the following way:
23+
- We pair the 0<sup>th</sup> string with the 2<sup>nd</sup> string, as the reversed string of word[0] is "dc" and is equal to words[2].
24+
- We pair the 1<sup>st</sup> string with the 3<sup>rd</sup> string, as the reversed string of word[1] is "ca" and is equal to words[3].
25+
26+
It can be proven that 2 is the maximum number of pairs that can be formed.
27+
28+
**Example 2:**
29+
30+
**Input:** words = ["ab","ba","cc"]
31+
32+
**Output:** 1
33+
34+
**Explanation:** In this example, we can form 1 pair of strings in the following way:
35+
- We pair the 0<sup>th</sup> string with the 1<sup>st</sup> string, as the reversed string of words[1] is "ab" and is equal to words[0].
36+
37+
It can be proven that 1 is the maximum number of pairs that can be formed.
38+
39+
**Example 3:**
40+
41+
**Input:** words = ["aa","ab"]
42+
43+
**Output:** 0
44+
45+
**Explanation:** In this example, we are unable to form any pair of strings.
46+
47+
**Constraints:**
48+
49+
* `1 <= words.length <= 50`
50+
* `words[i].length == 2`
51+
* `words` consists of distinct strings.
52+
* `words[i]` contains only lowercase English letters.

0 commit comments

Comments
 (0)