Skip to content
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
40 changes: 35 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1816,9 +1816,39 @@

| # | Title | Difficulty | Tag | Time, ms | Time, %
|------|----------------|-------------|-------------|----------|--------
| 2706 |[Buy Two Chocolates](src/main/kotlin/g2701_2800/s2706_buy_two_chocolates)| Easy | Array, Sorting | 187 | 96.43
| 2705 |[Compact Object](src/main/kotlin/g2701_2800/s2705_compact_object)| Medium || 78 | 99.38
| 2704 |[To Be Or Not To Be](src/main/kotlin/g2701_2800/s2704_to_be_or_not_to_be)| Easy || 49 | 97.88
| 2703 |[Return Length of Arguments Passed](src/main/kotlin/g2701_2800/s2703_return_length_of_arguments_passed)| Easy || 47 | 98.59
| 2699 |[Modify Graph Edge Weights](src/main/kotlin/g2601_2700/s2699_modify_graph_edge_weights)| Hard | Heap_Priority_Queue, Graph, Shortest_Path | 1592 | 40.00
| 2698 |[Find the Punishment Number of an Integer](src/main/kotlin/g2601_2700/s2698_find_the_punishment_number_of_an_integer)| Medium | Math, Backtracking | 133 | 100.00
| 2697 |[Lexicographically Smallest Palindrome](src/main/kotlin/g2601_2700/s2697_lexicographically_smallest_palindrome)| Easy | String, Two_Pointers | 267 | 100.00
| 2696 |[Minimum String Length After Removing Substrings](src/main/kotlin/g2601_2700/s2696_minimum_string_length_after_removing_substrings)| Easy | String, Stack, Simulation | 185 | 100.00
| 2695 |[Array Wrapper](src/main/kotlin/g2601_2700/s2695_array_wrapper)| Easy || 40 | 100.00
| 2694 |[Event Emitter](src/main/kotlin/g2601_2700/s2694_event_emitter)| Medium || 45 | 99.58
| 2693 |[Call Function with Custom Context](src/main/kotlin/g2601_2700/s2693_call_function_with_custom_context)| Medium | Array, Dynamic_Programming, Matrix | 51 | 97.92
| 2685 |[Count the Number of Complete Components](src/main/kotlin/g2601_2700/s2685_count_the_number_of_complete_components)| Medium | Array, Dynamic_Programming, Depth_First_Search, Breadth_First_Search, Matrix, Graph | 436 | 100.00
| 2684 |[Maximum Number of Moves in a Grid](src/main/kotlin/g2601_2700/s2684_maximum_number_of_moves_in_a_grid)| Medium | Array, Dynamic_Programming, Matrix | 542 | 100.00
| 2683 |[Neighboring Bitwise XOR](src/main/kotlin/g2601_2700/s2683_neighboring_bitwise_xor)| Medium | Array, Bit_Manipulation | 988 | 100.00
| 2682 |[Find the Losers of the Circular Game](src/main/kotlin/g2601_2700/s2682_find_the_losers_of_the_circular_game)| Easy | Array, Hash_Table, Simulation | 186 | 100.00
| 2681 |[Power of Heroes](src/main/kotlin/g2601_2700/s2681_power_of_heroes)| Hard | Array, Math, Sorting, Prefix_Sum | 454 | 85.71
| 2680 |[Maximum OR](src/main/kotlin/g2601_2700/s2680_maximum_or)| Medium | Array, Greedy, Bit_Manipulation, Prefix_Sum | 592 | 90.91
| 2679 |[Sum in a Matrix](src/main/kotlin/g2601_2700/s2679_sum_in_a_matrix)| Medium | Array, Sorting, Matrix, Heap_Priority_Queue, Simulation | 581 | 100.00
| 2678 |[Number of Senior Citizens](src/main/kotlin/g2601_2700/s2678_number_of_senior_citizens)| Easy | Array, String | 163 | 96.30
| 2677 |[Chunk Array](src/main/kotlin/g2601_2700/s2677_chunk_array)| Easy || 55 | 96.89
| 2673 |[Make Costs of Paths Equal in a Binary Tree](src/main/kotlin/g2601_2700/s2673_make_costs_of_paths_equal_in_a_binary_tree)| Medium | Array, Dynamic_Programming, Greedy, Tree, Binary_Tree | 645 | 75.00
| 2672 |[Number of Adjacent Elements With the Same Color](src/main/kotlin/g2601_2700/s2672_number_of_adjacent_elements_with_the_same_color)| Medium | Array | 1208 | 100.00
| 2671 |[Frequency Tracker](src/main/kotlin/g2601_2700/s2671_frequency_tracker)| Medium | Hash_Table, Design | 1109 | 80.00
| 2670 |[Find the Distinct Difference Array](src/main/kotlin/g2601_2700/s2670_find_the_distinct_difference_array)| Easy | Array, Hash_Table | 320 | 94.74
| 2667 |[Create Hello World Function](src/main/kotlin/g2601_2700/s2667_create_hello_world_function)| Easy || 52 | 92.72
| 2666 |[Allow One Function Call](src/main/kotlin/g2601_2700/s2666_allow_one_function_call)| Easy || 53 | 93.29
| 2665 |[Counter II](src/main/kotlin/g2601_2700/s2665_counter_ii)| Easy || 65 | 86.59
| 2663 |[Lexicographically Smallest Beautiful String](src/main/kotlin/g2601_2700/s2663_lexicographically_smallest_beautiful_string)| Hard | String, Greedy | 324 | 100.00
| 2662 |[Minimum Cost of a Path With Special Roads](src/main/kotlin/g2601_2700/s2662_minimum_cost_of_a_path_with_special_roads)| Medium | Array, Heap_Priority_Queue, Graph, Shortest_Path | 690 | 100.00
| 2661 |[First Completely Painted Row or Column](src/main/kotlin/g2601_2700/s2661_first_completely_painted_row_or_column)| Medium | Array, Hash_Table, Matrix | 901 | 100.00
| 2660 |[Determine the Winner of a Bowling Game](src/main/kotlin/g2601_2700/s2660_determine_the_winner_of_a_bowling_game)| Easy | Array, Simulation | 263 | 85.71
| 2659 |[Make Array Empty](src/main/kotlin/g2601_2700/s2659_make_array_empty)| Hard | Array, Sorting, Greedy, Binary_Search, Ordered_Set, Segment_Tree, Binary_Indexed_Tree | 728 | 100.00
| 2658 |[Maximum Number of Fish in a Grid](src/main/kotlin/g2601_2700/s2658_maximum_number_of_fish_in_a_grid)| Medium | Array, Matrix, Union_Find, Depth_First_Search, Breadth_First_Search | 269 | 80.00
| 2658 |[Maximum Number of Fish in a Grid](src/main/kotlin/g2601_2700/s2658_maximum_number_of_fish_in_a_grid)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 269 | 80.00
| 2657 |[Find the Prefix Common Array of Two Arrays](src/main/kotlin/g2601_2700/s2657_find_the_prefix_common_array_of_two_arrays)| Medium | Array, Hash_Table | 288 | 88.89
| 2656 |[Maximum Sum With Exactly K Elements](src/main/kotlin/g2601_2700/s2656_maximum_sum_with_exactly_k_elements)| Easy | Array, Greedy | 241 | 93.94
| 2654 |[Minimum Number of Operations to Make All Array Elements Equal to 1](src/main/kotlin/g2601_2700/s2654_minimum_number_of_operations_to_make_all_array_elements_equal_to_1)| Medium | Array, Math, Number_Theory | 172 | 100.00
Expand All @@ -1828,12 +1858,12 @@
| 2650 |[Design Cancellable Function](src/main/kotlin/g2601_2700/s2650_design_cancellable_function)| Hard || 58 | 95.56
| 2649 |[Nested Array Generator](src/main/kotlin/g2601_2700/s2649_nested_array_generator)| Medium || 192 | 70.11
| 2648 |[Generate Fibonacci Sequence](src/main/kotlin/g2601_2700/s2648_generate_fibonacci_sequence)| Easy || 53 | 93.66
| 2646 |[Minimize the Total Price of the Trips](src/main/kotlin/g2601_2700/s2646_minimize_the_total_price_of_the_trips)| Hard | Array, Dynamic_Programming, Tree, Graph, Depth_First_Search | 264 | 100.00
| 2646 |[Minimize the Total Price of the Trips](src/main/kotlin/g2601_2700/s2646_minimize_the_total_price_of_the_trips)| Hard | Array, Dynamic_Programming, Depth_First_Search, Tree, Graph | 264 | 100.00
| 2645 |[Minimum Additions to Make Valid String](src/main/kotlin/g2601_2700/s2645_minimum_additions_to_make_valid_string)| Medium | String, Dynamic_Programming, Greedy, Stack | 154 | 100.00
| 2644 |[Find the Maximum Divisibility Score](src/main/kotlin/g2601_2700/s2644_find_the_maximum_divisibility_score)| Easy | Array | 585 | 100.00
| 2643 |[Row With Maximum Ones](src/main/kotlin/g2601_2700/s2643_row_with_maximum_ones)| Easy | Array, Matrix | 530 | 100.00
| 2642 |[Design Graph With Shortest Path Calculator](src/main/kotlin/g2601_2700/s2642_design_graph_with_shortest_path_calculator)| Hard | Design, Heap_Priority_Queue, Graph, Shortest_Path | 789 | 100.00
| 2641 |[Cousins in Binary Tree II](src/main/kotlin/g2601_2700/s2641_cousins_in_binary_tree_ii)| Medium | Hash_Table, Tree, Binary_Tree, Depth_First_Search, Breadth_First_Search | 922 | 90.91
| 2641 |[Cousins in Binary Tree II](src/main/kotlin/g2601_2700/s2641_cousins_in_binary_tree_ii)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 922 | 90.91
| 2640 |[Find the Score of All Prefixes of an Array](src/main/kotlin/g2601_2700/s2640_find_the_score_of_all_prefixes_of_an_array)| Medium | Array, Prefix_Sum | 587 | 100.00
| 2639 |[Find the Width of Columns of a Grid](src/main/kotlin/g2601_2700/s2639_find_the_width_of_columns_of_a_grid)| Easy | Array, Matrix | 222 | 100.00
| 2637 |[Promise Time Limit](src/main/kotlin/g2601_2700/s2637_promise_time_limit)| Easy || 64 | 75.36
Expand All @@ -1856,11 +1886,11 @@
| 2616 |[Minimize the Maximum Difference of Pairs](src/main/kotlin/g2601_2700/s2616_minimize_the_maximum_difference_of_pairs)| Medium | Array, Greedy, Binary_Search | 556 | 100.00
| 2615 |[Sum of Distances](src/main/kotlin/g2601_2700/s2615_sum_of_distances)| Medium | Array, Hash_Table, Prefix_Sum | 902 | 100.00
| 2614 |[Prime In Diagonal](src/main/kotlin/g2601_2700/s2614_prime_in_diagonal)| Easy | Array, Math, Matrix, Number_Theory | 465 | 100.00
| 2612 |[Minimum Reverse Operations](src/main/kotlin/g2601_2700/s2612_minimum_reverse_operations)| Hard | Array, Ordered_Set, Breadth_First_Search | 923 | 100.00
| 2612 |[Minimum Reverse Operations](src/main/kotlin/g2601_2700/s2612_minimum_reverse_operations)| Hard | Array, Breadth_First_Search, Ordered_Set | 923 | 100.00
| 2611 |[Mice and Cheese](src/main/kotlin/g2601_2700/s2611_mice_and_cheese)| Medium | Array, Sorting, Greedy, Heap_Priority_Queue | 554 | 100.00
| 2610 |[Convert an Array Into a 2D Array With Conditions](src/main/kotlin/g2601_2700/s2610_convert_an_array_into_a_2d_array_with_conditions)| Medium | Array, Hash_Table | 240 | 88.24
| 2609 |[Find the Longest Balanced Substring of a Binary String](src/main/kotlin/g2601_2700/s2609_find_the_longest_balanced_substring_of_a_binary_string)| Easy | String | 174 | 100.00
| 2608 |[Shortest Cycle in a Graph](src/main/kotlin/g2601_2700/s2608_shortest_cycle_in_a_graph)| Hard | Graph, Breadth_First_Search | 1061 | 100.00
| 2608 |[Shortest Cycle in a Graph](src/main/kotlin/g2601_2700/s2608_shortest_cycle_in_a_graph)| Hard | Breadth_First_Search, Graph | 1061 | 100.00
| 2607 |[Make K-Subarray Sums Equal](src/main/kotlin/g2601_2700/s2607_make_k_subarray_sums_equal)| Medium | Array, Math, Sorting, Number_Theory | 1062 | 100.00
| 2606 |[Find the Substring With Maximum Cost](src/main/kotlin/g2601_2700/s2606_find_the_substring_with_maximum_cost)| Medium | Array, String, Hash_Table, Dynamic_Programming | 238 | 100.00
| 2605 |[Form Smallest Number From Two Digit Arrays](src/main/kotlin/g2601_2700/s2605_form_smallest_number_from_two_digit_arrays)| Easy | Array, Hash_Table, Enumeration | 161 | 100.00
Expand Down
18 changes: 8 additions & 10 deletions src/main/kotlin/g0001_0100/s0020_valid_parentheses/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,19 @@ An input string is valid if:
## Solution

```kotlin
import java.util.Stack

class Solution {
fun isValid(s: String): Boolean {
val stack = Stack<Char>()
val stack = ArrayDeque<Char>()
for (element in s) {
val c = element
if (c == '(' || c == '[' || c == '{') {
stack.push(c)
} else if (c == ')' && stack.isNotEmpty() && stack.peek() == '(') {
stack.pop()
} else if (c == '}' && stack.isNotEmpty() && stack.peek() == '{') {
stack.pop()
} else if (c == ']' && stack.isNotEmpty() && stack.peek() == '[') {
stack.pop()
stack.addLast(c)
} else if (c == ')' && stack.isNotEmpty() && stack.last() == '(') {
stack.removeLast()
} else if (c == '}' && stack.isNotEmpty() && stack.last() == '{') {
stack.removeLast()
} else if (c == ']' && stack.isNotEmpty() && stack.last() == '[') {
stack.removeLast()
} else {
return false
}
Expand Down
10 changes: 4 additions & 6 deletions src/main/kotlin/g0001_0100/s0077_combinations/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@ You may return the answer in **any order**.
## Solution

```kotlin
import java.util.Stack

class Solution {
fun combine(n: Int, k: Int): List<List<Int>> {
val ans: MutableList<List<Int>> = ArrayList()
// Boundary case
if (n > 20 || k < 1 || k > n) {
return ans
}
backtrack(ans, n, k, 1, Stack())
backtrack(ans, n, k, 1, ArrayDeque())
return ans
}

private fun backtrack(ans: MutableList<List<Int>>, n: Int, k: Int, s: Int, stack: Stack<Int>) {
private fun backtrack(ans: MutableList<List<Int>>, n: Int, k: Int, s: Int, stack: ArrayDeque<Int>) {
// Base case
// If k becomes 0
if (k == 0) {
Expand All @@ -55,10 +53,10 @@ class Solution {
}
// Start with s till n-k+1
for (i in s..n - k + 1) {
stack.push(i)
stack.addLast(i)
// Update start for recursion and decrease k by 1
backtrack(ans, n, k - 1, i + 1, stack)
stack.pop()
stack.removeLast()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ Given the `root` of a binary tree, return _the preorder traversal of its nodes'

```kotlin
import com_github_leetcode.TreeNode
import java.util.Stack

/*
* Example:
Expand All @@ -56,15 +55,15 @@ class Solution {
if (root == null) {
return result
}
val stack: Stack<TreeNode> = Stack<TreeNode>()
val stack: ArrayDeque<TreeNode?> = ArrayDeque()
var current: TreeNode? = root
while (current != null || stack.isNotEmpty()) {
while (current != null) {
result.add(current.`val`)
stack.push(current.right)
stack.addLast(current.right)
current = current.left
}
current = stack.pop()
current = stack.removeLast()
}
return result
}
Expand Down
13 changes: 5 additions & 8 deletions src/main/kotlin/g0101_0200/s0155_min_stack/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,28 @@ Implement the `MinStack` class:
## Solution

```kotlin
import java.util.Stack

class MinStack() {
private val stack: Stack<Pair<Int, Int>> = Stack()
private val stack: ArrayDeque<Pair<Int, Int>> = ArrayDeque()

fun push(x: Int) {
val min: Int = if (stack.isEmpty()) x
else getMin()

stack.push(x to minOf(min, x))
stack.addLast(x to minOf(min, x))
}

fun pop() {
stack.pop()
stack.removeLast()
}

fun top(): Int {
return stack
.peek()
.last()
.first
}

fun getMin(): Int {
return stack
.peek()
.last()
.second
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ Given an integer `n`, return _the **number** of possible attendance records of l
## Solution

```kotlin
import java.util.Arrays

@Suppress("NAME_SHADOWING")
class Solution {
fun checkRecord(n: Int): Int {
Expand All @@ -64,7 +62,7 @@ class Solution {
)
val e = quickPower(matrix, n - 1)
return (
(Arrays.stream(e[0]).sum() + Arrays.stream(e[1]).sum() + Arrays.stream(e[3]).sum()) %
(e[0].sum() + e[1].sum() + e[3].sum()) %
mod
).toInt()
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/g0601_0700/s0679_24_game/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ Return `true` if you can get such expression that evaluates to `24`, and `false`
## Solution

```kotlin
import java.util.Arrays
import kotlin.math.abs

class Solution {
private fun backtrack(list: DoubleArray, n: Int): Boolean {
private fun backtrack(list: Array<Double>, n: Int): Boolean {
if (n == 1) {
return abs(list[0] - 24) < EPS
}
Expand Down Expand Up @@ -89,7 +88,7 @@ class Solution {
}

fun judgePoint24(nums: IntArray): Boolean {
val a = Arrays.stream(nums).asDoubleStream().toArray()
val a = nums.map { it.toDouble() }.toTypedArray()
return backtrack(a, a.size)
}

Expand Down
21 changes: 10 additions & 11 deletions src/main/kotlin/g0701_0800/s0770_basic_calculator_iv/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ The format of the output is as follows:

```kotlin
import java.util.Collections
import java.util.Stack

class Solution {
internal inner class Node {
Expand Down Expand Up @@ -184,10 +183,10 @@ class Solution {
return 0
}

private fun helper(numS: Stack<Node>, ops: Stack<Char>): Node {
val b = numS.pop()
val a = numS.pop()
val op = ops.pop()
private fun helper(numS: ArrayDeque<Node>, ops: ArrayDeque<Char>): Node {
val b = numS.removeLast()
val a = numS.removeLast()
val op = ops.removeLast()
if (op == '*') {
return a.mul(b)
} else if (op == '+') {
Expand All @@ -206,8 +205,8 @@ class Solution {
vars[evalvarS[i]] = evalintS[i]
}
val n = expression.length
val numS = Stack<Node>()
val ops = Stack<Char>()
val numS = ArrayDeque<Node>()
val ops = ArrayDeque<Char>()
var i = 0
while (i < n) {
val a = expression[i]
Expand All @@ -220,12 +219,12 @@ class Solution {
} else if (a == '(') {
ops.add(a)
} else if (a == ')') {
while (ops.peek() != '(') {
while (ops.last() != '(') {
numS.add(helper(numS, ops))
}
ops.pop()
ops.removeLast()
} else if (a == '+' || a == '-' || a == '*') {
while (ops.isNotEmpty() && getPriority(ops.peek()) >= getPriority(a)) {
while (ops.isNotEmpty() && getPriority(ops.last()) >= getPriority(a)) {
numS.add(helper(numS, ops))
}
ops.add(a)
Expand All @@ -235,7 +234,7 @@ class Solution {
while (ops.isNotEmpty()) {
numS.add(helper(numS, ops))
}
return numS.peek().evaluate(vars).toList()
return numS.last().evaluate(vars).toList()
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ Given an integer array of even length `arr`, return `true` _if it is possible to
## Solution

```kotlin
import java.util.Arrays

class Solution {
fun canReorderDoubled(arr: IntArray): Boolean {
val max = 0.coerceAtLeast(Arrays.stream(arr).max().asInt)
val min = 0.coerceAtMost(Arrays.stream(arr).min().asInt)
val max = 0.coerceAtLeast(arr.max())
val min = 0.coerceAtMost(arr.min())
val positive = IntArray(max + 1)
val negative = IntArray(-min + 1)
for (a in arr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ Return _the largest possible sum of the array after modifying it in this way_.
## Solution

```kotlin
import java.util.Arrays

@Suppress("NAME_SHADOWING")
class Solution {
fun largestSumAfterKNegations(nums: IntArray, k: Int): Int {
Expand All @@ -68,7 +66,7 @@ class Solution {
if (k and 1 == 1) {
nums[minIndex] *= -1
}
return Arrays.stream(nums).sum()
return nums.sum()
}
}
```
13 changes: 6 additions & 7 deletions src/main/kotlin/g1101_1200/s1172_dinner_plate_stacks/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@ Implement the `DinnerPlates` class:
## Solution

```kotlin
import java.util.Stack
import java.util.TreeSet

class DinnerPlates(private val stackCap: Int) {
private val stacks: MutableList<Stack<Int>>
private val stacks: MutableList<ArrayDeque<Int>>
private val leftIndex: TreeSet<Int>

init {
Expand All @@ -84,17 +83,17 @@ class DinnerPlates(private val stackCap: Int) {
fun push(`val`: Int) {
if (leftIndex.isNotEmpty()) {
val i = leftIndex.first()
stacks[i].push(`val`)
stacks[i].addLast(`val`)
if (stacks[i].size == stackCap) {
leftIndex.remove(i)
}
return
}
if (stacks.isEmpty() || stacks[stacks.size - 1].size == stackCap) {
val newStack = Stack<Int>()
val newStack = ArrayDeque<Int>()
stacks.add(newStack)
}
stacks[stacks.size - 1].push(`val`)
stacks[stacks.size - 1].addLast(`val`)
}

fun pop(): Int {
Expand All @@ -105,7 +104,7 @@ class DinnerPlates(private val stackCap: Int) {
leftIndex.remove(stacks.size - 1)
stacks.removeAt(stacks.size - 1)
}
val `val` = stacks[stacks.size - 1].pop()
val `val` = stacks[stacks.size - 1].removeLast()
if (stacks[stacks.size - 1].isEmpty()) {
leftIndex.remove(stacks.size - 1)
stacks.removeAt(stacks.size - 1)
Expand All @@ -117,7 +116,7 @@ class DinnerPlates(private val stackCap: Int) {
if (stacks.size - 1 >= index) {
var `val` = -1
if (stacks[index].isNotEmpty()) {
`val` = stacks[index].pop()
`val` = stacks[index].removeLast()
}
if (stacks[index].isEmpty() && index == stacks.size - 1) {
leftIndex.remove(stacks.size - 1)
Expand Down
Loading