Skip to content

Commit 0e79631

Browse files
authored
Added tasks 2801-2824
1 parent 4d5a6d3 commit 0e79631

File tree

39 files changed

+1519
-0
lines changed

39 files changed

+1519
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package g2801_2900.s2806_account_balance_after_rounded_purchase
2+
3+
// #Easy #Math #2023_12_06_Time_108_ms_(100.00%)_Space_32.7_MB_(100.00%)
4+
5+
class Solution {
6+
fun accountBalanceAfterPurchase(purchaseAmount: Int): Int {
7+
val x = ((purchaseAmount + 5) / 10.0).toInt() * 10
8+
return 100 - x
9+
}
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2806\. Account Balance After Rounded Purchase
2+
3+
Easy
4+
5+
Initially, you have a bank account balance of `100` dollars.
6+
7+
You are given an integer `purchaseAmount` representing the amount you will spend on a purchase in dollars.
8+
9+
At the store where you will make the purchase, the purchase amount is rounded to the **nearest multiple** of `10`. In other words, you pay a **non-negative** amount, `roundedAmount`, such that `roundedAmount` is a multiple of `10` and `abs(roundedAmount - purchaseAmount)` is **minimized**.
10+
11+
If there is more than one nearest multiple of `10`, the **largest multiple** is chosen.
12+
13+
Return _an integer denoting your account balance after making a purchase worth_ `purchaseAmount` _dollars from the store._
14+
15+
**Note:** `0` is considered to be a multiple of `10` in this problem.
16+
17+
**Example 1:**
18+
19+
**Input:** purchaseAmount = 9
20+
21+
**Output:** 90
22+
23+
**Explanation:** In this example, the nearest multiple of 10 to 9 is 10. Hence, your account balance becomes 100 - 10 = 90.
24+
25+
**Example 2:**
26+
27+
**Input:** purchaseAmount = 15
28+
29+
**Output:** 80
30+
31+
**Explanation:** In this example, there are two nearest multiples of 10 to 15: 10 and 20. So, the larger multiple, 20, is chosen. Hence, your account balance becomes 100 - 20 = 80.
32+
33+
**Constraints:**
34+
35+
* `0 <= purchaseAmount <= 100`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g2801_2900.s2807_insert_greatest_common_divisors_in_linked_list
2+
3+
// #Medium #Array #Math #Linked_List #2023_12_06_Time_225_ms_(67.65%)_Space_37.6_MB_(97.06%)
4+
5+
import com_github_leetcode.ListNode
6+
7+
/*
8+
* Example:
9+
* var li = ListNode(5)
10+
* var v = li.`val`
11+
* Definition for singly-linked list.
12+
* class ListNode(var `val`: Int) {
13+
* var next: ListNode? = null
14+
* }
15+
*/
16+
class Solution {
17+
fun insertGreatestCommonDivisors(head: ListNode?): ListNode? {
18+
var prevNode: ListNode? = null
19+
var currNode = head
20+
while (currNode != null) {
21+
if (prevNode != null) {
22+
val gcd = greatestCommonDivisor(prevNode.`val`, currNode.`val`)
23+
prevNode.next = ListNode(gcd, currNode)
24+
}
25+
prevNode = currNode
26+
currNode = currNode.next
27+
}
28+
return head
29+
}
30+
31+
private fun greatestCommonDivisor(val1: Int, val2: Int): Int {
32+
return if (val2 == 0) {
33+
val1
34+
} else greatestCommonDivisor(val2, val1 % val2)
35+
}
36+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2807\. Insert Greatest Common Divisors in Linked List
2+
3+
Medium
4+
5+
Given the head of a linked list `head`, in which each node contains an integer value.
6+
7+
Between every pair of adjacent nodes, insert a new node with a value equal to the **greatest common divisor** of them.
8+
9+
Return _the linked list after insertion_.
10+
11+
The **greatest common divisor** of two numbers is the largest positive integer that evenly divides both numbers.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2023/07/18/ex1_copy.png)
16+
17+
**Input:** head = [18,6,10,3]
18+
19+
**Output:** [18,6,6,2,10,1,3]
20+
21+
**Explanation:** The 1<sup>st</sup> diagram denotes the initial linked list and the 2<sup>nd</sup> diagram denotes the linked list after inserting the new nodes (nodes in blue are the inserted nodes).
22+
23+
- We insert the greatest common divisor of 18 and 6 = 6 between the 1<sup>st</sup> and the 2<sup>nd</sup> nodes.
24+
- We insert the greatest common divisor of 6 and 10 = 2 between the 2<sup>nd</sup> and the 3<sup>rd</sup> nodes.
25+
- We insert the greatest common divisor of 10 and 3 = 1 between the 3<sup>rd</sup> and the 4<sup>th</sup> nodes.
26+
27+
There are no more adjacent nodes, so we return the linked list.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2023/07/18/ex2_copy1.png)
32+
33+
**Input:** head = [7]
34+
35+
**Output:** [7]
36+
37+
**Explanation:** The 1<sup>st</sup> diagram denotes the initial linked list and the 2<sup>nd</sup> diagram denotes the linked list after inserting the new nodes. There are no pairs of adjacent nodes, so we return the initial linked list.
38+
39+
**Constraints:**
40+
41+
* The number of nodes in the list is in the range `[1, 5000]`.
42+
* `1 <= Node.val <= 1000`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2801_2900.s2808_minimum_seconds_to_equalize_a_circular_array
2+
3+
// #Medium #Array #Hash_Table #Greedy #2023_12_06_Time_847_ms_(50.00%)_Space_78.3_MB_(50.00%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minimumSeconds(nums: List<Int>): Int {
10+
val n = nums.size
11+
var min = n / 2
12+
val hm = HashMap<Int, MutableList<Int>>()
13+
for (i in 0 until n) {
14+
val v = nums[i]
15+
hm.computeIfAbsent(v) { k: Int? -> ArrayList() }.add(i)
16+
}
17+
for (list in hm.values) {
18+
if (list.size > 1) {
19+
var curr = (list[0] + n - list[list.size - 1]) / 2
20+
for (i in 1 until list.size) {
21+
curr = max(curr.toDouble(), ((list[i] - list[i - 1]) / 2).toDouble()).toInt()
22+
}
23+
min = min(min.toDouble(), curr.toDouble()).toInt()
24+
}
25+
}
26+
return min
27+
}
28+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2808\. Minimum Seconds to Equalize a Circular Array
2+
3+
Medium
4+
5+
You are given a **0-indexed** array `nums` containing `n` integers.
6+
7+
At each second, you perform the following operation on the array:
8+
9+
* For every index `i` in the range `[0, n - 1]`, replace `nums[i]` with either `nums[i]`, `nums[(i - 1 + n) % n]`, or `nums[(i + 1) % n]`.
10+
11+
**Note** that all the elements get replaced simultaneously.
12+
13+
Return _the **minimum** number of seconds needed to make all elements in the array_ `nums` _equal_.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,2,1,2]
18+
19+
**Output:** 1
20+
21+
**Explanation:** We can equalize the array in 1 second in the following way:
22+
- At 1<sup>st</sup> second, replace values at each index with [nums[3],nums[1],nums[3],nums[3]]. After replacement, nums = [2,2,2,2]. It can be proven that 1 second is the minimum amount of seconds needed for equalizing the array.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [2,1,3,3,2]
27+
28+
**Output:** 2
29+
30+
**Explanation:** We can equalize the array in 2 seconds in the following way:
31+
- At 1<sup>st</sup> second, replace values at each index with [nums[0],nums[2],nums[2],nums[2],nums[3]]. After replacement, nums = [2,3,3,3,3].
32+
- At 2<sup>nd</sup> second, replace values at each index with [nums[1],nums[1],nums[2],nums[3],nums[4]]. After replacement, nums = [3,3,3,3,3].
33+
34+
It can be proven that 2 seconds is the minimum amount of seconds needed for equalizing the array.
35+
36+
**Example 3:**
37+
38+
**Input:** nums = [5,5,5,5]
39+
40+
**Output:** 0
41+
42+
**Explanation:** We don't need to perform any operations as all elements in the initial array are the same.
43+
44+
**Constraints:**
45+
46+
* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
47+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g2801_2900.s2809_minimum_time_to_make_array_sum_at_most_x
2+
3+
// #Hard #Array #Dynamic_Programming #Sorting
4+
// #2023_12_06_Time_325_ms_(100.00%)_Space_42.6_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun minimumTime(nums1: List<Int?>, nums2: List<Int?>, x: Int): Int {
10+
val n = nums1.size
11+
val nums = Array(n) { IntArray(2) }
12+
for (i in 0 until n) {
13+
nums[i] = intArrayOf(nums1[i]!!, nums2[i]!!)
14+
}
15+
nums.sortWith { a: IntArray, b: IntArray -> a[1] - b[1] }
16+
val dp = IntArray(n + 1)
17+
var sum1: Long = 0
18+
var sum2: Long = 0
19+
for (i in 0 until n) {
20+
sum1 += nums[i][0].toLong()
21+
sum2 += nums[i][1].toLong()
22+
}
23+
if (sum1 <= x) {
24+
return 0
25+
}
26+
for (j in 0 until n) {
27+
for (i in j + 1 downTo 1) {
28+
dp[i] = max(dp[i].toDouble(), (nums[j][0] + nums[j][1] * i + dp[i - 1]).toDouble())
29+
.toInt()
30+
}
31+
}
32+
for (i in 1..n) {
33+
if (sum1 + sum2 * i - dp[i] <= x) {
34+
return i
35+
}
36+
}
37+
return -1
38+
}
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2809\. Minimum Time to Make Array Sum At Most x
2+
3+
Hard
4+
5+
You are given two **0-indexed** integer arrays `nums1` and `nums2` of equal length. Every second, for all indices `0 <= i < nums1.length`, value of `nums1[i]` is incremented by `nums2[i]`. **After** this is done, you can do the following operation:
6+
7+
* Choose an index `0 <= i < nums1.length` and make `nums1[i] = 0`.
8+
9+
You are also given an integer `x`.
10+
11+
Return _the **minimum** time in which you can make the sum of all elements of_ `nums1` _to be **less than or equal** to_ `x`, _or_ `-1` _if this is not possible._
12+
13+
**Example 1:**
14+
15+
**Input:** nums1 = [1,2,3], nums2 = [1,2,3], x = 4
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
For the 1st second, we apply the operation on i = 0. Therefore nums1 = [0,2+2,3+3] = [0,4,6].
22+
For the 2nd second, we apply the operation on i = 1. Therefore nums1 = [0+1,0,6+3] = [1,0,9].
23+
For the 3rd second, we apply the operation on i = 2. Therefore nums1 = [1+1,0+2,0] = [2,2,0].
24+
Now sum of nums1 = 4. It can be shown that these operations are optimal, so we return 3.
25+
26+
**Example 2:**
27+
28+
**Input:** nums1 = [1,2,3], nums2 = [3,3,3], x = 4
29+
30+
**Output:** -1
31+
32+
**Explanation:** It can be shown that the sum of nums1 will always be greater than x, no matter which operations are performed.
33+
34+
**Constraints:**
35+
36+
* <code>1 <= nums1.length <= 10<sup>3</sup></code>
37+
* <code>1 <= nums1[i] <= 10<sup>3</sup></code>
38+
* <code>0 <= nums2[i] <= 10<sup>3</sup></code>
39+
* `nums1.length == nums2.length`
40+
* <code>0 <= x <= 10<sup>6</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2801_2900.s2810_faulty_keyboard
2+
3+
// #Easy #String #Simulation #2023_12_06_Time_196_ms_(91.67%)_Space_36.9_MB_(91.67%)
4+
5+
class Solution {
6+
fun finalString(s: String): String {
7+
val stringBuilder = StringBuilder()
8+
for (ch in s.toCharArray()) {
9+
if (ch == 'i') {
10+
stringBuilder.reverse()
11+
continue
12+
}
13+
stringBuilder.append(ch)
14+
}
15+
return stringBuilder.toString()
16+
}
17+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2810\. Faulty Keyboard
2+
3+
Easy
4+
5+
Your laptop keyboard is faulty, and whenever you type a character `'i'` on it, it reverses the string that you have written. Typing other characters works as expected.
6+
7+
You are given a **0-indexed** string `s`, and you type each character of `s` using your faulty keyboard.
8+
9+
Return _the final string that will be present on your laptop screen._
10+
11+
**Example 1:**
12+
13+
**Input:** s = "string"
14+
15+
**Output:** "rtsng"
16+
17+
**Explanation:**
18+
19+
After typing first character, the text on the screen is "s".
20+
21+
After the second character, the text is "st".
22+
23+
After the third character, the text is "str".
24+
25+
Since the fourth character is an 'i', the text gets reversed and becomes "rts".
26+
27+
After the fifth character, the text is "rtsn".
28+
29+
After the sixth character, the text is "rtsng".
30+
31+
Therefore, we return "rtsng".
32+
33+
**Example 2:**
34+
35+
**Input:** s = "poiinter"
36+
37+
**Output:** "ponter"
38+
39+
**Explanation:**
40+
41+
After the first character, the text on the screen is "p".
42+
43+
After the second character, the text is "po".
44+
45+
Since the third character you type is an 'i', the text gets reversed and becomes "op".
46+
47+
Since the fourth character you type is an 'i', the text gets reversed and becomes "po".
48+
49+
After the fifth character, the text is "pon". After the sixth character, the text is "pont".
50+
51+
After the seventh character, the text is "ponte". After the eighth character, the text is "ponter".
52+
53+
Therefore, we return "ponter".
54+
55+
**Constraints:**
56+
57+
* `1 <= s.length <= 100`
58+
* `s` consists of lowercase English letters.
59+
* `s[0] != 'i'`

0 commit comments

Comments
 (0)