diff --git a/src/main/kotlin/g3401_3500/s3407_substring_matching_pattern/Solution.kt b/src/main/kotlin/g3401_3500/s3407_substring_matching_pattern/Solution.kt new file mode 100644 index 000000000..245dab290 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3407_substring_matching_pattern/Solution.kt @@ -0,0 +1,42 @@ +package g3401_3500.s3407_substring_matching_pattern + +// #Easy #String #String_Matching #2025_01_07_Time_2_(100.00%)_Space_38.80_(35.14%) + +class Solution { + fun hasMatch(s: String, p: String): Boolean { + var index = -1 + for (i in 0..>) { + private val tasks: TreeSet + private val taskMap: MutableMap + + init { + this.tasks = + TreeSet( + Comparator { a: IntArray?, b: IntArray? -> + if (b!![2] == a!![2]) b[1] - a[1] else b[2] - a[2] + }, + ) + this.taskMap = HashMap() + for (task in tasks) { + val t = intArrayOf(task[0], task[1], task[2]) + this.tasks.add(t) + this.taskMap.put(task[1], t) + } + } + + fun add(userId: Int, taskId: Int, priority: Int) { + val task = intArrayOf(userId, taskId, priority) + this.tasks.add(task) + this.taskMap.put(taskId, task) + } + + fun edit(taskId: Int, newPriority: Int) { + val task: IntArray = taskMap[taskId]!! + tasks.remove(task) + taskMap.remove(taskId) + val newTask = intArrayOf(task[0], task[1], newPriority) + tasks.add(newTask) + taskMap.put(taskId, newTask) + } + + fun rmv(taskId: Int) { + this.tasks.remove(this.taskMap[taskId]) + this.taskMap.remove(taskId) + } + + fun execTop(): Int { + if (this.tasks.isEmpty()) { + return -1 + } + val task = this.tasks.pollFirst() + this.taskMap.remove(task!![1]) + return task[0] + } +} + +/* + * Your TaskManager object will be instantiated and called as such: + * TaskManager obj = new TaskManager(tasks); + * obj.add(userId,taskId,priority); + * obj.edit(taskId,newPriority); + * obj.rmv(taskId); + * int param_4 = obj.execTop(); + */ diff --git a/src/main/kotlin/g3401_3500/s3408_design_task_manager/readme.md b/src/main/kotlin/g3401_3500/s3408_design_task_manager/readme.md new file mode 100644 index 000000000..9c38851f2 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3408_design_task_manager/readme.md @@ -0,0 +1,48 @@ +3408\. Design Task Manager + +Medium + +There is a task management system that allows users to manage their tasks, each associated with a priority. The system should efficiently handle adding, modifying, executing, and removing tasks. + +Implement the `TaskManager` class: + +* `TaskManager(vector>& tasks)` initializes the task manager with a list of user-task-priority triples. Each element in the input list is of the form `[userId, taskId, priority]`, which adds a task to the specified user with the given priority. + +* `void add(int userId, int taskId, int priority)` adds a task with the specified `taskId` and `priority` to the user with `userId`. It is **guaranteed** that `taskId` does not _exist_ in the system. + +* `void edit(int taskId, int newPriority)` updates the priority of the existing `taskId` to `newPriority`. It is **guaranteed** that `taskId` _exists_ in the system. + +* `void rmv(int taskId)` removes the task identified by `taskId` from the system. It is **guaranteed** that `taskId` _exists_ in the system. + +* `int execTop()` executes the task with the **highest** priority across all users. If there are multiple tasks with the same **highest** priority, execute the one with the highest `taskId`. After executing, the `taskId` is **removed** from the system. Return the `userId` associated with the executed task. If no tasks are available, return -1. + + +**Note** that a user may be assigned multiple tasks. + +**Example 1:** + +**Input:** + ["TaskManager", "add", "edit", "execTop", "rmv", "add", "execTop"] + [[[[1, 101, 10], [2, 102, 20], [3, 103, 15]]], [4, 104, 5], [102, 8], [], [101], [5, 105, 15], []] + +**Output:** + [null, null, null, 3, null, null, 5] + +**Explanation** + +TaskManager taskManager = new TaskManager([[1, 101, 10], [2, 102, 20], [3, 103, 15]]); // Initializes with three tasks for Users 1, 2, and 3. + taskManager.add(4, 104, 5); // Adds task 104 with priority 5 for User 4. + taskManager.edit(102, 8); // Updates priority of task 102 to 8. + taskManager.execTop(); // return 3. Executes task 103 for User 3. + taskManager.rmv(101); // Removes task 101 from the system. + taskManager.add(5, 105, 15); // Adds task 105 with priority 15 for User 5. + taskManager.execTop(); // return 5. Executes task 105 for User 5. + +**Constraints:** + +* 1 <= tasks.length <= 105 +* 0 <= userId <= 105 +* 0 <= taskId <= 105 +* 0 <= priority <= 109 +* 0 <= newPriority <= 109 +* At most 2 * 105 calls will be made in **total** to `add`, `edit`, `rmv`, and `execTop` methods. \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3409_longest_subsequence_with_decreasing_adjacent_difference/Solution.kt b/src/main/kotlin/g3401_3500/s3409_longest_subsequence_with_decreasing_adjacent_difference/Solution.kt new file mode 100644 index 000000000..20c3d9865 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3409_longest_subsequence_with_decreasing_adjacent_difference/Solution.kt @@ -0,0 +1,35 @@ +package g3401_3500.s3409_longest_subsequence_with_decreasing_adjacent_difference + +// #Medium #Array #Dynamic_Programming #2025_01_07_Time_70_(100.00%)_Space_57.87_(85.71%) + +import kotlin.math.max + +class Solution { + fun longestSubsequence(nums: IntArray): Int { + var max = 0 + for (n in nums) { + max = max(n, max) + } + max += 1 + val dp: Array = Array(max) { IntArray(max) } + for (i in nums) { + var v = 1 + for (diff in max - 1 downTo 0) { + if (i + diff < max) { + v = max(v, (dp[i + diff][diff] + 1)) + } + if (i - diff >= 0) { + v = max(v, (dp[i - diff][diff] + 1)) + } + dp[i][diff] = v + } + } + var res = 0 + for (i in dp) { + for (j in i) { + res = max(res, j) + } + } + return res + } +} diff --git a/src/main/kotlin/g3401_3500/s3409_longest_subsequence_with_decreasing_adjacent_difference/readme.md b/src/main/kotlin/g3401_3500/s3409_longest_subsequence_with_decreasing_adjacent_difference/readme.md new file mode 100644 index 000000000..23b570516 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3409_longest_subsequence_with_decreasing_adjacent_difference/readme.md @@ -0,0 +1,46 @@ +3409\. Longest Subsequence With Decreasing Adjacent Difference + +Medium + +You are given an array of integers `nums`. + +Your task is to find the length of the **longest subsequence** `seq` of `nums`, such that the **absolute differences** between _consecutive_ elements form a **non-increasing sequence** of integers. In other words, for a subsequence seq0, seq1, seq2, ..., seqm of `nums`, |seq1 - seq0| >= |seq2 - seq1| >= ... >= |seqm - seqm - 1|. + +Return the length of such a subsequence. + +A **subsequence** is an **non-empty** array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. + +**Example 1:** + +**Input:** nums = [16,6,3] + +**Output:** 3 + +**Explanation:** + +The longest subsequence is `[16, 6, 3]` with the absolute adjacent differences `[10, 3]`. + +**Example 2:** + +**Input:** nums = [6,5,3,4,2,1] + +**Output:** 4 + +**Explanation:** + +The longest subsequence is `[6, 4, 2, 1]` with the absolute adjacent differences `[2, 2, 1]`. + +**Example 3:** + +**Input:** nums = [10,20,10,19,10,20] + +**Output:** 5 + +**Explanation:** + +The longest subsequence is `[10, 20, 10, 19, 10]` with the absolute adjacent differences `[10, 10, 9, 9]`. + +**Constraints:** + +* 2 <= nums.length <= 104 +* `1 <= nums[i] <= 300` \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element/Solution.kt b/src/main/kotlin/g3401_3500/s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element/Solution.kt new file mode 100644 index 000000000..c048dbe1c --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element/Solution.kt @@ -0,0 +1,35 @@ +package g3401_3500.s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element + +// #Hard #Array #Dynamic_Programming #Segment_Tree +// #2025_01_07_Time_80_(100.00%)_Space_68.87_(100.00%) + +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun maxSubarraySum(nums: IntArray): Long { + val prefixMap: MutableMap = HashMap() + var result = nums[0].toLong() + var prefixSum: Long = 0 + var minPrefix: Long = 0 + prefixMap.put(0L, 0L) + for (num in nums) { + prefixSum += num.toLong() + result = max(result, (prefixSum - minPrefix)) + if (num < 0) { + if (prefixMap.containsKey(num.toLong())) { + prefixMap.put( + num.toLong(), + min(prefixMap[num.toLong()]!!, prefixMap[0L]!!) + num, + ) + } else { + prefixMap.put(num.toLong(), prefixMap[0L]!! + num) + } + minPrefix = min(minPrefix, prefixMap[num.toLong()]!!) + } + prefixMap.put(0L, min(prefixMap[0L]!!, prefixSum)) + minPrefix = min(minPrefix, prefixMap[0L]!!) + } + return result + } +} diff --git a/src/main/kotlin/g3401_3500/s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element/readme.md b/src/main/kotlin/g3401_3500/s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element/readme.md new file mode 100644 index 000000000..0d05dd232 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element/readme.md @@ -0,0 +1,47 @@ +3410\. Maximize Subarray Sum After Removing All Occurrences of One Element + +Hard + +You are given an integer array `nums`. + +You can do the following operation on the array **at most** once: + +* Choose **any** integer `x` such that `nums` remains **non-empty** on removing all occurrences of `x`. +* Remove **all** occurrences of `x` from the array. + +Return the **maximum** subarray sum across **all** possible resulting arrays. + +A **subarray** is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [-3,2,-2,-1,3,-2,3] + +**Output:** 7 + +**Explanation:** + +We can have the following arrays after at most one operation: + +* The original array is nums = [-3, 2, -2, -1, **3, -2, 3**]. The maximum subarray sum is `3 + (-2) + 3 = 4`. +* Deleting all occurences of `x = -3` results in nums = [2, -2, -1, **3, -2, 3**]. The maximum subarray sum is `3 + (-2) + 3 = 4`. +* Deleting all occurences of `x = -2` results in nums = [-3, **2, -1, 3, 3**]. The maximum subarray sum is `2 + (-1) + 3 + 3 = 7`. +* Deleting all occurences of `x = -1` results in nums = [-3, 2, -2, **3, -2, 3**]. The maximum subarray sum is `3 + (-2) + 3 = 4`. +* Deleting all occurences of `x = 3` results in nums = [-3, **2**, -2, -1, -2]. The maximum subarray sum is 2. + +The output is `max(4, 4, 7, 4, 2) = 7`. + +**Example 2:** + +**Input:** nums = [1,2,3,4] + +**Output:** 10 + +**Explanation:** + +It is optimal to not perform any operations. + +**Constraints:** + +* 1 <= nums.length <= 105 +* -106 <= nums[i] <= 106 \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3411_maximum_subarray_with_equal_products/Solution.kt b/src/main/kotlin/g3401_3500/s3411_maximum_subarray_with_equal_products/Solution.kt new file mode 100644 index 000000000..e3f3e7f4a --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3411_maximum_subarray_with_equal_products/Solution.kt @@ -0,0 +1,35 @@ +package g3401_3500.s3411_maximum_subarray_with_equal_products + +// #Easy #Array #Math #Sliding_Window #Enumeration #Number_Theory +// #2025_01_07_Time_7_(60.71%)_Space_36.33_(92.86%) + +import kotlin.math.max + +class Solution { + private fun gcd(a: Int, b: Int): Int { + return if (b == 0) a else gcd(b, a % b) + } + + private fun lcm(a: Int, b: Int): Int { + return (a / gcd(a, b)) * b + } + + fun maxLength(nums: IntArray): Int { + val n = nums.size + var maxL = 0 + for (i in 0..> = Array>(26) { ArrayList() } + var r: Long = 0 + for (i in 0..1 <= s.length <= 105 +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3413_maximum_coins_from_k_consecutive_bags/Solution.kt b/src/main/kotlin/g3401_3500/s3413_maximum_coins_from_k_consecutive_bags/Solution.kt new file mode 100644 index 000000000..cd7b60096 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3413_maximum_coins_from_k_consecutive_bags/Solution.kt @@ -0,0 +1,39 @@ +package g3401_3500.s3413_maximum_coins_from_k_consecutive_bags + +// #Medium #Array #Sorting #Greedy #Binary_Search #Prefix_Sum #Sliding_Window +// #2025_01_08_Time_275_(86.67%)_Space_125.92_(80.00%) + +import kotlin.math.max + +class Solution { + fun maximumCoins(coins: Array, k: Int): Long { + coins.sortWith { a: IntArray?, b: IntArray? -> a!![0] - b!![0] } + val n = coins.size + var res: Long = 0 + var cur: Long = 0 + var j = 0 + for (ints in coins) { + while (j < n && coins[j][1] <= ints[0] + k - 1) { + cur += (coins[j][1] - coins[j][0] + 1).toLong() * coins[j][2] + j++ + } + if (j < n) { + val part = max(0.0, (ints[0] + k - 1 - coins[j][0] + 1).toDouble()).toLong() * coins[j][2] + res = max(res.toDouble(), (cur + part).toDouble()).toLong() + } + cur -= (ints[1] - ints[0] + 1).toLong() * ints[2] + } + cur = 0 + j = 0 + for (coin in coins) { + cur += (coin[1] - coin[0] + 1).toLong() * coin[2] + while (coins[j][1] < coin[1] - k + 1) { + cur -= (coins[j][1] - coins[j][0] + 1).toLong() * coins[j][2] + j++ + } + val part = max(0.0, (coin[1] - k - coins[j][0] + 1).toDouble()).toLong() * coins[j][2] + res = max(res, (cur - part)) + } + return res + } +} diff --git a/src/main/kotlin/g3401_3500/s3413_maximum_coins_from_k_consecutive_bags/readme.md b/src/main/kotlin/g3401_3500/s3413_maximum_coins_from_k_consecutive_bags/readme.md new file mode 100644 index 000000000..43eacb690 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3413_maximum_coins_from_k_consecutive_bags/readme.md @@ -0,0 +1,42 @@ +3413\. Maximum Coins From K Consecutive Bags + +Medium + +There are an infinite amount of bags on a number line, one bag for each coordinate. Some of these bags contain coins. + +You are given a 2D array `coins`, where coins[i] = [li, ri, ci] denotes that every bag from li to ri contains ci coins. + +The segments that `coins` contain are non-overlapping. + +You are also given an integer `k`. + +Return the **maximum** amount of coins you can obtain by collecting `k` consecutive bags. + +**Example 1:** + +**Input:** coins = [[8,10,1],[1,3,2],[5,6,4]], k = 4 + +**Output:** 10 + +**Explanation:** + +Selecting bags at positions `[3, 4, 5, 6]` gives the maximum number of coins: `2 + 0 + 4 + 4 = 10`. + +**Example 2:** + +**Input:** coins = [[1,10,3]], k = 2 + +**Output:** 6 + +**Explanation:** + +Selecting bags at positions `[1, 2]` gives the maximum number of coins: `3 + 3 = 6`. + +**Constraints:** + +* 1 <= coins.length <= 105 +* 1 <= k <= 109 +* coins[i] == [li, ri, ci] +* 1 <= li <= ri <= 109 +* 1 <= ci <= 1000 +* The given segments are non-overlapping. \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3414_maximum_score_of_non_overlapping_intervals/Solution.kt b/src/main/kotlin/g3401_3500/s3414_maximum_score_of_non_overlapping_intervals/Solution.kt new file mode 100644 index 000000000..b80f26f22 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3414_maximum_score_of_non_overlapping_intervals/Solution.kt @@ -0,0 +1,67 @@ +package g3401_3500.s3414_maximum_score_of_non_overlapping_intervals + +// #Hard #Array #Dynamic_Programming #Sorting #Binary_Search +// #2025_01_09_Time_892_(100.00%)_Space_91.42_(100.00%) + +class Solution { + fun maximumWeight(intervals: List>): IntArray { + val n = intervals.size + val ns = intervals.mapIndexed { index, li -> intArrayOf(li[0], li[1], li[2], index) }.toTypedArray() + ns.sortBy { it[0] } + var dp1 = Array(n) { IntArray(0) } + var dp = LongArray(n) + repeat(4) { + val dp3 = Array(n) { IntArray(0) } + val dp2 = LongArray(n) + dp3[n - 1] = intArrayOf(ns[n - 1][3]) + dp2[n - 1] = ns[n - 1][2].toLong() + for (i in n - 2 downTo 0) { + var l = i + 1 + var r = n - 1 + while (l <= r) { + val mid = (l + r) shr 1 + if (ns[mid][0] > ns[i][1]) { + r = mid - 1 + } else { + l = mid + 1 + } + } + dp2[i] = ns[i][2] + (if (l < n) dp[l] else 0) + if (i + 1 < n && dp2[i + 1] > dp2[i]) { + dp2[i] = dp2[i + 1] + dp3[i] = dp3[i + 1] + } else { + if (l < n) { + dp3[i] = IntArray(dp1[l].size + 1) + dp3[i][0] = ns[i][3] + for (j in dp1[l].indices) { + dp3[i][j + 1] = dp1[l][j] + } + dp3[i].sort() + } else { + dp3[i] = intArrayOf(ns[i][3]) + } + if (i + 1 < n && dp2[i + 1] == dp2[i] && check(dp3[i], dp3[i + 1]) > 0) { + dp3[i] = dp3[i + 1] + } + } + } + dp = dp2 + dp1 = dp3 + } + return dp1[0] + } + + private fun check(ns1: IntArray, ns2: IntArray): Int { + var i = 0 + while (i < ns1.size && i < ns2.size) { + if (ns1[i] < ns2[i]) { + return -1 + } else if (ns1[i] > ns2[i]) { + return 1 + } + i++ + } + return 0 + } +} diff --git a/src/main/kotlin/g3401_3500/s3414_maximum_score_of_non_overlapping_intervals/readme.md b/src/main/kotlin/g3401_3500/s3414_maximum_score_of_non_overlapping_intervals/readme.md new file mode 100644 index 000000000..6dbd6d388 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3414_maximum_score_of_non_overlapping_intervals/readme.md @@ -0,0 +1,40 @@ +3414\. Maximum Score of Non-overlapping Intervals + +Hard + +You are given a 2D integer array `intervals`, where intervals[i] = [li, ri, weighti]. Interval `i` starts at position li and ends at ri, and has a weight of weighti. You can choose _up to_ 4 **non-overlapping** intervals. The **score** of the chosen intervals is defined as the total sum of their weights. + +Return the **lexicographically smallest** array of at most 4 indices from `intervals` with **maximum** score, representing your choice of non-overlapping intervals. + +Two intervals are said to be **non-overlapping** if they do not share any points. In particular, intervals sharing a left or right boundary are considered overlapping. + +An array `a` is **lexicographically smaller** than an array `b` if in the first position where `a` and `b` differ, array `a` has an element that is less than the corresponding element in `b`. + If the first `min(a.length, b.length)` elements do not differ, then the shorter array is the lexicographically smaller one. + +**Example 1:** + +**Input:** intervals = [[1,3,2],[4,5,2],[1,5,5],[6,9,3],[6,7,1],[8,9,1]] + +**Output:** [2,3] + +**Explanation:** + +You can choose the intervals with indices 2, and 3 with respective weights of 5, and 3. + +**Example 2:** + +**Input:** intervals = [[5,8,1],[6,7,7],[4,7,3],[9,10,6],[7,8,2],[11,14,3],[3,5,5]] + +**Output:** [1,3,5,6] + +**Explanation:** + +You can choose the intervals with indices 1, 3, 5, and 6 with respective weights of 7, 6, 3, and 5. + +**Constraints:** + +* 1 <= intevals.length <= 5 * 104 +* `intervals[i].length == 3` +* intervals[i] = [li, ri, weighti] +* 1 <= li <= ri <= 109 +* 1 <= weighti <= 109 \ No newline at end of file diff --git a/src/test/kotlin/g3401_3500/s3407_substring_matching_pattern/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3407_substring_matching_pattern/SolutionTest.kt new file mode 100644 index 000000000..9b1bd05fb --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3407_substring_matching_pattern/SolutionTest.kt @@ -0,0 +1,25 @@ +package g3401_3500.s3407_substring_matching_pattern + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun hasMatch() { + assertThat( + Solution().hasMatch("leetcode", "ee*e"), + equalTo(true), + ) + } + + @Test + fun hasMatch2() { + assertThat(Solution().hasMatch("car", "c*v"), equalTo(false)) + } + + @Test + fun hasMatch3() { + assertThat(Solution().hasMatch("luck", "u*"), equalTo(true)) + } +} diff --git a/src/test/kotlin/g3401_3500/s3408_design_task_manager/TaskManagerTest.kt b/src/test/kotlin/g3401_3500/s3408_design_task_manager/TaskManagerTest.kt new file mode 100644 index 000000000..f74999a09 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3408_design_task_manager/TaskManagerTest.kt @@ -0,0 +1,32 @@ +package g3401_3500.s3408_design_task_manager + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class TaskManagerTest { + @Test + fun test() { + // Initializes with three tasks for Users 1, 2, and 3. + val taskManager = + TaskManager( + listOf>( + mutableListOf(1, 101, 10), + mutableListOf(2, 102, 20), + mutableListOf(3, 103, 15), + ), + ) + // Adds task 104 with priority 5 for User 4. + taskManager.add(4, 104, 5) + // Updates priority of task 102 to 8. + taskManager.edit(102, 8) + // return 3. Executes task 103 for User 3. + assertThat(taskManager.execTop(), equalTo(3)) + // Removes task 101 from the system. + taskManager.rmv(101) + // Adds task 105 with priority 15 for User 5. + taskManager.add(5, 105, 15) + // return 5. Executes task 105 for User 5. + assertThat(taskManager.execTop(), equalTo(5)) + } +} diff --git a/src/test/kotlin/g3401_3500/s3409_longest_subsequence_with_decreasing_adjacent_difference/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3409_longest_subsequence_with_decreasing_adjacent_difference/SolutionTest.kt new file mode 100644 index 000000000..c42b11011 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3409_longest_subsequence_with_decreasing_adjacent_difference/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3401_3500.s3409_longest_subsequence_with_decreasing_adjacent_difference + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun longestSubsequence() { + assertThat( + Solution().longestSubsequence(intArrayOf(16, 6, 3)), + equalTo(3), + ) + } + + @Test + fun longestSubsequence2() { + assertThat( + Solution().longestSubsequence(intArrayOf(6, 5, 3, 4, 2, 1)), + equalTo(4), + ) + } + + @Test + fun longestSubsequence3() { + assertThat( + Solution().longestSubsequence(intArrayOf(10, 20, 10, 19, 10, 20)), + equalTo(5), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element/SolutionTest.kt new file mode 100644 index 000000000..5d09ac442 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3401_3500.s3410_maximize_subarray_sum_after_removing_all_occurrences_of_one_element + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxSubarraySum() { + assertThat( + Solution().maxSubarraySum(intArrayOf(-3, 2, -2, -1, 3, -2, 3)), + equalTo(7L), + ) + } + + @Test + fun maxSubarraySum2() { + assertThat( + Solution().maxSubarraySum(intArrayOf(1, 2, 3, 4)), + equalTo(10L), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3411_maximum_subarray_with_equal_products/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3411_maximum_subarray_with_equal_products/SolutionTest.kt new file mode 100644 index 000000000..feb16b32e --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3411_maximum_subarray_with_equal_products/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3401_3500.s3411_maximum_subarray_with_equal_products + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxLength() { + assertThat( + Solution().maxLength(intArrayOf(1, 2, 1, 2, 1, 1, 1)), + equalTo(5), + ) + } + + @Test + fun maxLength2() { + assertThat(Solution().maxLength(intArrayOf(2, 3, 4, 5, 6)), equalTo(3)) + } + + @Test + fun maxLength3() { + assertThat( + Solution().maxLength(intArrayOf(1, 2, 3, 1, 4, 5, 1)), + equalTo(5), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3412_find_mirror_score_of_a_string/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3412_find_mirror_score_of_a_string/SolutionTest.kt new file mode 100644 index 000000000..55cde7021 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3412_find_mirror_score_of_a_string/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3401_3500.s3412_find_mirror_score_of_a_string + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun calculateScore() { + assertThat(Solution().calculateScore("aczzx"), equalTo(5L)) + } + + @Test + fun calculateScore2() { + assertThat(Solution().calculateScore("abcdef"), equalTo(0L)) + } +} diff --git a/src/test/kotlin/g3401_3500/s3413_maximum_coins_from_k_consecutive_bags/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3413_maximum_coins_from_k_consecutive_bags/SolutionTest.kt new file mode 100644 index 000000000..2e8c374f2 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3413_maximum_coins_from_k_consecutive_bags/SolutionTest.kt @@ -0,0 +1,26 @@ +package g3401_3500.s3413_maximum_coins_from_k_consecutive_bags + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumCoins() { + assertThat( + Solution().maximumCoins( + arrayOf(intArrayOf(8, 10, 1), intArrayOf(1, 3, 2), intArrayOf(5, 6, 4)), + 4, + ), + equalTo(10L), + ) + } + + @Test + fun maximumCoins2() { + assertThat( + Solution().maximumCoins(arrayOf(intArrayOf(1, 10, 3)), 2), + equalTo(6L), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3414_maximum_score_of_non_overlapping_intervals/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3414_maximum_score_of_non_overlapping_intervals/SolutionTest.kt new file mode 100644 index 000000000..1980b6499 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3414_maximum_score_of_non_overlapping_intervals/SolutionTest.kt @@ -0,0 +1,78 @@ +package g3401_3500.s3414_maximum_score_of_non_overlapping_intervals + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumWeight() { + assertThat( + Solution() + .maximumWeight( + listOf>( + mutableListOf(1, 3, 2), + mutableListOf(4, 5, 2), + mutableListOf(1, 5, 5), + mutableListOf(6, 9, 3), + mutableListOf(6, 7, 1), + mutableListOf(8, 9, 1), + ), + ), + equalTo(intArrayOf(2, 3)), + ) + } + + @Test + fun maximumWeight2() { + assertThat( + Solution() + .maximumWeight( + listOf>( + mutableListOf(5, 8, 1), + mutableListOf(6, 7, 7), + mutableListOf(4, 7, 3), + mutableListOf(9, 10, 6), + mutableListOf(7, 8, 2), + mutableListOf(11, 14, 3), + mutableListOf(3, 5, 5), + ), + ), + equalTo(intArrayOf(1, 3, 5, 6)), + ) + } + + @Test + fun maximumWeight3() { + assertThat( + Solution() + .maximumWeight( + listOf>( + mutableListOf(4, 4, 1), + mutableListOf(2, 5, 3), + mutableListOf(2, 3, 2), + ), + ), + equalTo(intArrayOf(0, 2)), + ) + } + + @Test + fun maximumWeight4() { + assertThat( + Solution() + .maximumWeight( + listOf>( + mutableListOf(19, 23, 23), + mutableListOf(19, 23, 40), + mutableListOf(1, 16, 31), + mutableListOf(16, 18, 31), + mutableListOf(14, 20, 22), + mutableListOf(14, 22, 5), + mutableListOf(23, 24, 23), + ), + ), + equalTo(intArrayOf(1, 2)), + ) + } +}