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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,10 @@

| # | Title | Difficulty | Tag | Time, ms | Time, %
|------|----------------|-------------|-------------|----------|--------
| 3357 |[Minimize the Maximum Adjacent Element Difference](src/main/kotlin/g3301_3400/s3357_minimize_the_maximum_adjacent_element_difference)| Hard | Array, Greedy, Binary_Search | 13 | 100.00
| 3356 |[Zero Array Transformation II](src/main/kotlin/g3301_3400/s3356_zero_array_transformation_ii)| Medium | Array, Binary_Search, Prefix_Sum | 5 | 100.00
| 3355 |[Zero Array Transformation I](src/main/kotlin/g3301_3400/s3355_zero_array_transformation_i)| Medium | Array, Prefix_Sum | 6 | 36.84
| 3354 |[Make Array Elements Equal to Zero](src/main/kotlin/g3301_3400/s3354_make_array_elements_equal_to_zero)| Easy | Array, Simulation, Prefix_Sum | 153 | 96.67
| 3352 |[Count K-Reducible Numbers Less Than N](src/main/kotlin/g3301_3400/s3352_count_k_reducible_numbers_less_than_n)| Hard | String, Dynamic_Programming, Math, Combinatorics | 170 | 100.00
| 3351 |[Sum of Good Subsequences](src/main/kotlin/g3301_3400/s3351_sum_of_good_subsequences)| Hard | Array, Hash_Table, Dynamic_Programming | 16 | 100.00
| 3350 |[Adjacent Increasing Subarrays Detection II](src/main/kotlin/g3301_3400/s3350_adjacent_increasing_subarrays_detection_ii)| Medium | Array, Binary_Search | 947 | 48.57
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ class Solution {
var lpsCenter = 0
var lpsRadius = 0
for (i in newStr.indices) {
dp[i] = if (friendCenter + friendRadius > i) Math.min(
dp[friendCenter * 2 - i],
friendCenter + friendRadius - i
) else 1
dp[i] = if (friendCenter + friendRadius > i) {
Math.min(
dp[friendCenter * 2 - i],
friendCenter + friendRadius - i,
)
} else {
1
}
while (i + dp[i] < newStr.size && i - dp[i] >= 0 && newStr[i + dp[i]] == newStr[i - dp[i]]) {
dp[i]++
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Solution {
i,
j - 2,
s,
p
p,
)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Solution {
nums: String,
letters: Array<String>,
curr: StringBuilder,
ans: MutableList<String>
ans: MutableList<String>,
) {
if (curr.length == nums.length) {
ans.add(curr.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class Solution {
fun mergeKLists(lists: Array<ListNode>): ListNode? {
return if (lists.isEmpty()) {
null
} else mergeKLists(lists, 0, lists.size)
} else {
mergeKLists(lists, 0, lists.size)
}
}

private fun mergeKLists(lists: Array<ListNode>, leftIndex: Int, rightIndex: Int): ListNode? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class Solution {
// move a word's length each time
var j = i
while (j + window <= s.length) {

// get the subStr
val subStr = s.substring(j, j + window)
val map: MutableMap<String, Int> = HashMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Solution {
target: Int,
start: Int,
sums: MutableList<List<Int>>,
sum: LinkedList<Int>
sum: LinkedList<Int>,
) {
if (target == 0) {
// make a deep copy of the current combination
Expand All @@ -57,7 +57,6 @@ class Solution {
}
var i = start
while (i < candidates.size && target >= candidates[i]) {

// If candidate[i] equals candidate[i-1], then solutions for i is subset of
// solution of i-1
if (i == start || i > start && candidates[i] != candidates[i - 1]) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g0001_0100/s0046_permutations/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Solution {
nums: IntArray,
finalResult: MutableList<List<Int>>,
currResult: MutableList<Int>,
used: BooleanArray
used: BooleanArray,
) {
if (currResult.size == nums.size) {
finalResult.add(ArrayList(currResult))
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g0001_0100/s0048_rotate_image/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Solution {
intArrayOf(i, j),
intArrayOf(j, n - 1 - i),
intArrayOf(n - 1 - i, n - 1 - j),
intArrayOf(n - 1 - j, i)
intArrayOf(n - 1 - j, i),
)
var t = matrix[pos[0][0]][pos[0][1]]
for (k in 1 until pos.size) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g0001_0100/s0049_group_anagrams/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Solution {
ch.sort()
val temp = String(ch)
hm.computeIfAbsent(
temp
temp,
) { _: String? -> ArrayList() }
hm.getValue(temp).add(s)
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/g0001_0100/s0050_powx_n/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class Solution {
}
return if (n < 0) {
1.0 / res
} else res
} else {
res
}
}
}
```
2 changes: 1 addition & 1 deletion src/main/kotlin/g0001_0100/s0052_n_queens_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Solution {
row: BooleanArray,
col: BooleanArray,
diagonal: BooleanArray,
antiDiagonal: BooleanArray
antiDiagonal: BooleanArray,
): Int {
if (r == n) {
return 1
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/g0001_0100/s0079_word_search/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Solution {
word: String,
index: Int,
x: Int,
y: Int
y: Int,
): Boolean {
if (index == word.length) {
return true
Expand Down Expand Up @@ -82,7 +82,7 @@ class Solution {
fun exist(board: Array<CharArray>, word: String): Boolean {
val visited = Array(board.size) {
BooleanArray(
board[0].size
board[0].size,
)
}
for (i in board.indices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Solution {
maxOfThreeNums(
largestArea(a, start, minInd),
a[minInd] * (limit - start),
largestArea(a, minInd + 1, limit)
largestArea(a, minInd + 1, limit),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Solution {
'.' +
octets[2] +
'.' +
octets[3]
octets[3],
)
} else if (count < 4 && pos < 12) {
var octet = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Solution {
i1: Int,
i2: Int,
i3: Int,
cache: Array<Array<Boolean?>>
cache: Array<Array<Boolean?>>,
): Boolean {
if (cache[i1][i2] != null) {
return cache[i1][i2]!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ class Solution {
}
return if (root.`val` <= left || root.`val` >= right) {
false
} else solve(root.left, left, root.`val`.toLong()) && solve(root.right, root.`val`.toLong(), right)
} else {
solve(root.left, left, root.`val`.toLong()) && solve(root.right, root.`val`.toLong(), right)
}
}
}
```
4 changes: 3 additions & 1 deletion src/main/kotlin/g0001_0100/s0100_same_tree/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ class Solution {
return if (n != null && m != null) {
if (n.`val` != m.`val`) {
false
} else trav(n.left, m.left) && trav(n.right, m.right)
} else {
trav(n.left, m.left) && trav(n.right, m.right)
}
} else {
n == null && m == null
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/g0101_0200/s0101_symmetric_tree/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class Solution {
fun isSymmetric(root: TreeNode?): Boolean {
return if (root == null) {
true
} else helper(root.left, root.right)
} else {
helper(root.left, root.right)
}
}

private fun helper(leftNode: TreeNode?, rightNode: TreeNode?): Boolean {
Expand All @@ -58,7 +60,9 @@ class Solution {
}
return if (leftNode.`val` != rightNode.`val`) {
false
} else helper(leftNode.left, rightNode.right) && helper(leftNode.right, rightNode.left)
} else {
helper(leftNode.left, rightNode.right) && helper(leftNode.right, rightNode.left)
}
}
}
```
4 changes: 3 additions & 1 deletion src/main/kotlin/g0101_0200/s0112_path_sum/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class Solution {
}
return if (targetSum == root.`val` && root.left == null && root.right == null) {
true
} else hasPathSum(root.left, targetSum - root.`val`) || hasPathSum(root.right, targetSum - root.`val`)
} else {
hasPathSum(root.left, targetSum - root.`val`) || hasPathSum(root.right, targetSum - root.`val`)
}
}
}
```
2 changes: 1 addition & 1 deletion src/main/kotlin/g0101_0200/s0113_path_sum_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Solution {
al: ArrayList<Int>,
sum: Int,
targetSum: Int,
root: TreeNode?
root: TreeNode?,
) {
var sum = sum
if (root == null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g0101_0200/s0120_triangle/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Solution {
triangle[row][col] +
Math.min(
dfs(triangle, dp, row + 1, col),
dfs(triangle, dp, row + 1, col + 1)
dfs(triangle, dp, row + 1, col + 1),
)
)
dp[row][col] = sum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Find the maximum profit you can achieve. You may complete **at most two transact
class Solution {
fun maxProfit(prices: IntArray): Int {
val n = prices.size
if (n <2) {
if (n < 2) {
return 0
}
val a = IntArray(n) { 0 }
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/g0101_0200/s0126_word_ladder_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Solution {
if (isLadder(word, next)) {
// construct the reverse graph from endWord
val reverseLadders = reverse.computeIfAbsent(
next
next,
) { _: String? -> HashSet() }
reverseLadders.add(word)
if (endWord == next) {
Expand Down Expand Up @@ -115,7 +115,7 @@ class Solution {
beginWord: String,
graph: Map<String, MutableSet<String>>,
ans: MutableList<List<String>>,
path: MutableSet<String>
path: MutableSet<String>,
) {
val next = graph[endWord] ?: return
for (word in next) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Solution {
if (num == lastNum) {
continue
}
length ++
length++
if (num - lastNum > 1) {
length = 1
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/g0101_0200/s0134_gas_station/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class Solution {
}
return if (sumGas < sumCost) {
-1
} else result
} else {
result
}
}
}
```
2 changes: 1 addition & 1 deletion src/main/kotlin/g0101_0200/s0140_word_break_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Solution {
wordSet: Set<String>,
index: Int,
sb: StringBuilder,
result: MutableList<String>
result: MutableList<String>,
) {
if (index == s.length) {
if (sb[sb.length - 1] == ' ') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Solution {
"/" to { a, b -> a / b },
"*" to { a, b -> a * b },
"+" to { a, b -> a + b },
"-" to { a, b -> a - b }
"-" to { a, b -> a - b },
)
fun evalRPN(tokens: Array<String>): Int {
val stack = ArrayDeque<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class Solution {
fun findMin(nums: IntArray): Int {
return if (nums.isEmpty()) {
0
} else find(0, nums.size - 1, nums)
} else {
find(0, nums.size - 1, nums)
}
}

private fun find(left: Int, right: Int, nums: IntArray): Int {
Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/g0101_0200/s0155_min_stack/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ class MinStack() {
private val stack: ArrayDeque<Pair<Int, Int>> = ArrayDeque()

fun push(x: Int) {
val min: Int = if (stack.isEmpty()) x
else getMin()
val min: Int = if (stack.isEmpty()) {
x
} else {
getMin()
}
stack.addLast(x to minOf(min, x))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Solution {
-0x10,
-0x8,
-0x4,
-0x2
-0x2,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Solution {
private fun buildGraph(
numCourses: Int,
prerequisites: Array<IntArray>,
indegrees: IntArray
indegrees: IntArray,
): List<List<Int>> {
val graph = List(numCourses) { mutableListOf<Int>() }
for ((cur, prev) in prerequisites) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ class Solution {
private fun leftHeight(root: TreeNode?): Int {
return if (root == null) {
0
} else 1 + leftHeight(root.left)
} else {
1 + leftHeight(root.left)
}
}

private fun rightHeight(root: TreeNode?): Int {
return if (root == null) {
0
} else 1 + rightHeight(root.right)
} else {
1 + rightHeight(root.right)
}
}
}
```
4 changes: 3 additions & 1 deletion src/main/kotlin/g0201_0300/s0258_add_digits/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class Solution {
}
return if (num % 9 == 0) {
9
} else num % 9
} else {
num % 9
}
}
}
```
Loading