Skip to content

Commit 48f5682

Browse files
authored
Added tasks 2801-2824
1 parent d23e195 commit 48f5682

File tree

14 files changed

+1228
-0
lines changed
  • src/main/kotlin/g2801_2900
    • s2806_account_balance_after_rounded_purchase
    • s2807_insert_greatest_common_divisors_in_linked_list
    • s2808_minimum_seconds_to_equalize_a_circular_array
    • s2809_minimum_time_to_make_array_sum_at_most_x
    • s2810_faulty_keyboard
    • s2811_check_if_it_is_possible_to_split_array
    • s2812_find_the_safest_path_in_a_grid
    • s2813_maximum_elegance_of_a_k_length_subsequence
    • s2815_max_pair_sum_in_an_array
    • s2816_double_a_number_represented_as_a_linked_list
    • s2817_minimum_absolute_difference_between_elements_with_constraint
    • s2818_apply_operations_to_maximize_score
    • s2824_count_pairs_whose_sum_is_less_than_target

14 files changed

+1228
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,19 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 2824 |[Count Pairs Whose Sum is Less than Target](src/main/kotlin/g2801_2900/s2824_count_pairs_whose_sum_is_less_than_target)| Easy | Array, Sorting | s | s
1820+
| 2818 |[Apply Operations to Maximize Score](src/main/kotlin/g2801_2900/s2818_apply_operations_to_maximize_score)| Hard | Array, Math, Greedy, Stack, Monotonic_Stack, Number_Theory | 727 | 100.00
1821+
| 2817 |[Minimum Absolute Difference Between Elements With Constraint](src/main/kotlin/g2801_2900/s2817_minimum_absolute_difference_between_elements_with_constraint)| Medium | Array, Binary_Search, Ordered_Set | 759 | 100.00
1822+
| 2816 |[Double a Number Represented as a Linked List](src/main/kotlin/g2801_2900/s2816_double_a_number_represented_as_a_linked_list)| Medium | Math, Stack, Linked_List | 393 | 100.00
1823+
| 2815 |[Max Pair Sum in an Array](src/main/kotlin/g2801_2900/s2815_max_pair_sum_in_an_array)| Easy | Array, Hash_Table | 223 | 82.35
1824+
| 2813 |[Maximum Elegance of a K-Length Subsequence](src/main/kotlin/g2801_2900/s2813_maximum_elegance_of_a_k_length_subsequence)| Hard | Array, Hash_Table, Sorting, Greedy, Heap_Priority_Queue | 853 | 100.00
1825+
| 2812 |[Find the Safest Path in a Grid](src/main/kotlin/g2801_2900/s2812_find_the_safest_path_in_a_grid)| Medium | Array, Breadth_First_Search, Binary_Search, Matrix, Union_Find | 902 | 100.00
1826+
| 2811 |[Check if it is Possible to Split Array](src/main/kotlin/g2801_2900/s2811_check_if_it_is_possible_to_split_array)| Medium | Array, Dynamic_Programming, Greedy | 180 | 100.00
1827+
| 2810 |[Faulty Keyboard](src/main/kotlin/g2801_2900/s2810_faulty_keyboard)| Easy | String, Simulation | 196 | 91.67
1828+
| 2809 |[Minimum Time to Make Array Sum At Most x](src/main/kotlin/g2801_2900/s2809_minimum_time_to_make_array_sum_at_most_x)| Hard | Array, Dynamic_Programming, Sorting | 325 | 100.00
1829+
| 2808 |[Minimum Seconds to Equalize a Circular Array](src/main/kotlin/g2801_2900/s2808_minimum_seconds_to_equalize_a_circular_array)| Medium | Array, Hash_Table, Greedy | 847 | 50.00
1830+
| 2807 |[Insert Greatest Common Divisors in Linked List](src/main/kotlin/g2801_2900/s2807_insert_greatest_common_divisors_in_linked_list)| Medium | Array, Math, Linked_List | 225 | 67.65
1831+
| 2806 |[Account Balance After Rounded Purchase](src/main/kotlin/g2801_2900/s2806_account_balance_after_rounded_purchase)| Easy | Math | 108 | 100.00
18191832
| 2800 |[Shortest String That Contains Three Strings](src/main/kotlin/g2701_2800/s2800_shortest_string_that_contains_three_strings)| Medium | String, Greedy, Enumeration | 259 | 100.00
18201833
| 2799 |[Count Complete Subarrays in an Array](src/main/kotlin/g2701_2800/s2799_count_complete_subarrays_in_an_array)| Medium | Array, Hash_Table, Sliding_Window | 206 | 96.97
18211834
| 2798 |[Number of Employees Who Met the Target](src/main/kotlin/g2701_2800/s2798_number_of_employees_who_met_the_target)| Easy | Array, Enumeration | 153 | 92.50
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 2806\. Account Balance After Rounded Purchase
5+
6+
Easy
7+
8+
Initially, you have a bank account balance of `100` dollars.
9+
10+
You are given an integer `purchaseAmount` representing the amount you will spend on a purchase in dollars.
11+
12+
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**.
13+
14+
If there is more than one nearest multiple of `10`, the **largest multiple** is chosen.
15+
16+
Return _an integer denoting your account balance after making a purchase worth_ `purchaseAmount` _dollars from the store._
17+
18+
**Note:** `0` is considered to be a multiple of `10` in this problem.
19+
20+
**Example 1:**
21+
22+
**Input:** purchaseAmount = 9
23+
24+
**Output:** 90
25+
26+
**Explanation:** In this example, the nearest multiple of 10 to 9 is 10. Hence, your account balance becomes 100 - 10 = 90.
27+
28+
**Example 2:**
29+
30+
**Input:** purchaseAmount = 15
31+
32+
**Output:** 80
33+
34+
**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.
35+
36+
**Constraints:**
37+
38+
* `0 <= purchaseAmount <= 100`
39+
40+
## Solution
41+
42+
```kotlin
43+
class Solution {
44+
fun accountBalanceAfterPurchase(purchaseAmount: Int): Int {
45+
val x = ((purchaseAmount + 5) / 10.0).toInt() * 10
46+
return 100 - x
47+
}
48+
}
49+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 2807\. Insert Greatest Common Divisors in Linked List
5+
6+
Medium
7+
8+
Given the head of a linked list `head`, in which each node contains an integer value.
9+
10+
Between every pair of adjacent nodes, insert a new node with a value equal to the **greatest common divisor** of them.
11+
12+
Return _the linked list after insertion_.
13+
14+
The **greatest common divisor** of two numbers is the largest positive integer that evenly divides both numbers.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2023/07/18/ex1_copy.png)
19+
20+
**Input:** head = [18,6,10,3]
21+
22+
**Output:** [18,6,6,2,10,1,3]
23+
24+
**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).
25+
26+
- We insert the greatest common divisor of 18 and 6 = 6 between the 1<sup>st</sup> and the 2<sup>nd</sup> nodes.
27+
- We insert the greatest common divisor of 6 and 10 = 2 between the 2<sup>nd</sup> and the 3<sup>rd</sup> nodes.
28+
- We insert the greatest common divisor of 10 and 3 = 1 between the 3<sup>rd</sup> and the 4<sup>th</sup> nodes.
29+
30+
There are no more adjacent nodes, so we return the linked list.
31+
32+
**Example 2:**
33+
34+
![](https://assets.leetcode.com/uploads/2023/07/18/ex2_copy1.png)
35+
36+
**Input:** head = [7]
37+
38+
**Output:** [7]
39+
40+
**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.
41+
42+
**Constraints:**
43+
44+
* The number of nodes in the list is in the range `[1, 5000]`.
45+
* `1 <= Node.val <= 1000`
46+
47+
## Solution
48+
49+
```kotlin
50+
import com_github_leetcode.ListNode
51+
52+
/*
53+
* Example:
54+
* var li = ListNode(5)
55+
* var v = li.`val`
56+
* Definition for singly-linked list.
57+
* class ListNode(var `val`: Int) {
58+
* var next: ListNode? = null
59+
* }
60+
*/
61+
class Solution {
62+
fun insertGreatestCommonDivisors(head: ListNode?): ListNode? {
63+
var prevNode: ListNode? = null
64+
var currNode = head
65+
while (currNode != null) {
66+
if (prevNode != null) {
67+
val gcd = greatestCommonDivisor(prevNode.`val`, currNode.`val`)
68+
prevNode.next = ListNode(gcd, currNode)
69+
}
70+
prevNode = currNode
71+
currNode = currNode.next
72+
}
73+
return head
74+
}
75+
76+
private fun greatestCommonDivisor(val1: Int, val2: Int): Int {
77+
return if (val2 == 0) {
78+
val1
79+
} else greatestCommonDivisor(val2, val1 % val2)
80+
}
81+
}
82+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 2808\. Minimum Seconds to Equalize a Circular Array
5+
6+
Medium
7+
8+
You are given a **0-indexed** array `nums` containing `n` integers.
9+
10+
At each second, you perform the following operation on the array:
11+
12+
* 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]`.
13+
14+
**Note** that all the elements get replaced simultaneously.
15+
16+
Return _the **minimum** number of seconds needed to make all elements in the array_ `nums` _equal_.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [1,2,1,2]
21+
22+
**Output:** 1
23+
24+
**Explanation:** We can equalize the array in 1 second in the following way:
25+
- 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.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [2,1,3,3,2]
30+
31+
**Output:** 2
32+
33+
**Explanation:** We can equalize the array in 2 seconds in the following way:
34+
- 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].
35+
- 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].
36+
37+
It can be proven that 2 seconds is the minimum amount of seconds needed for equalizing the array.
38+
39+
**Example 3:**
40+
41+
**Input:** nums = [5,5,5,5]
42+
43+
**Output:** 0
44+
45+
**Explanation:** We don't need to perform any operations as all elements in the initial array are the same.
46+
47+
**Constraints:**
48+
49+
* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
50+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
51+
52+
## Solution
53+
54+
```kotlin
55+
import kotlin.math.max
56+
import kotlin.math.min
57+
58+
class Solution {
59+
fun minimumSeconds(nums: List<Int>): Int {
60+
val n = nums.size
61+
var min = n / 2
62+
val hm = HashMap<Int, MutableList<Int>>()
63+
for (i in 0 until n) {
64+
val v = nums[i]
65+
hm.computeIfAbsent(v) { _: Int? -> ArrayList() }.add(i)
66+
}
67+
for (list in hm.values) {
68+
if (list.size > 1) {
69+
var curr = (list[0] + n - list[list.size - 1]) / 2
70+
for (i in 1 until list.size) {
71+
curr = max(curr.toDouble(), ((list[i] - list[i - 1]) / 2).toDouble()).toInt()
72+
}
73+
min = min(min.toDouble(), curr.toDouble()).toInt()
74+
}
75+
}
76+
return min
77+
}
78+
}
79+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 2809\. Minimum Time to Make Array Sum At Most x
5+
6+
Hard
7+
8+
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:
9+
10+
* Choose an index `0 <= i < nums1.length` and make `nums1[i] = 0`.
11+
12+
You are also given an integer `x`.
13+
14+
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._
15+
16+
**Example 1:**
17+
18+
**Input:** nums1 = [1,2,3], nums2 = [1,2,3], x = 4
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
For the 1st second, we apply the operation on i = 0. Therefore nums1 = [0,2+2,3+3] = [0,4,6].
25+
For the 2nd second, we apply the operation on i = 1. Therefore nums1 = [0+1,0,6+3] = [1,0,9].
26+
For the 3rd second, we apply the operation on i = 2. Therefore nums1 = [1+1,0+2,0] = [2,2,0].
27+
Now sum of nums1 = 4. It can be shown that these operations are optimal, so we return 3.
28+
29+
**Example 2:**
30+
31+
**Input:** nums1 = [1,2,3], nums2 = [3,3,3], x = 4
32+
33+
**Output:** -1
34+
35+
**Explanation:** It can be shown that the sum of nums1 will always be greater than x, no matter which operations are performed.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums1.length <= 10<sup>3</sup></code>
40+
* <code>1 <= nums1[i] <= 10<sup>3</sup></code>
41+
* <code>0 <= nums2[i] <= 10<sup>3</sup></code>
42+
* `nums1.length == nums2.length`
43+
* <code>0 <= x <= 10<sup>6</sup></code>
44+
45+
## Solution
46+
47+
```kotlin
48+
import kotlin.math.max
49+
50+
class Solution {
51+
fun minimumTime(nums1: List<Int?>, nums2: List<Int?>, x: Int): Int {
52+
val n = nums1.size
53+
val nums = Array(n) { IntArray(2) }
54+
for (i in 0 until n) {
55+
nums[i] = intArrayOf(nums1[i]!!, nums2[i]!!)
56+
}
57+
nums.sortWith { a: IntArray, b: IntArray -> a[1] - b[1] }
58+
val dp = IntArray(n + 1)
59+
var sum1: Long = 0
60+
var sum2: Long = 0
61+
for (i in 0 until n) {
62+
sum1 += nums[i][0].toLong()
63+
sum2 += nums[i][1].toLong()
64+
}
65+
if (sum1 <= x) {
66+
return 0
67+
}
68+
for (j in 0 until n) {
69+
for (i in j + 1 downTo 1) {
70+
dp[i] = max(dp[i].toDouble(), (nums[j][0] + nums[j][1] * i + dp[i - 1]).toDouble())
71+
.toInt()
72+
}
73+
}
74+
for (i in 1..n) {
75+
if (sum1 + sum2 * i - dp[i] <= x) {
76+
return i
77+
}
78+
}
79+
return -1
80+
}
81+
}
82+
```

0 commit comments

Comments
 (0)