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
334 changes: 192 additions & 142 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ A **word** is a maximal substring consisting of non-space characters only.

```kotlin
class Solution {
fun lengthOfLastWord(str: String): Int {
fun lengthOfLastWord(s: String): Int {
var len = 0
for (i in str.length - 1 downTo 0) {
val ch = str[i]
for (i in s.length - 1 downTo 0) {
val ch = s[i]
if (ch == ' ' && len > 0) {
break
} else if (ch != ' ') {
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/g0001_0100/s0072_edit_distance/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ exection -> execution (insert 'u')

```kotlin
class Solution {
fun minDistance(w1: String, w2: String): Int {
val n1 = w1.length
val n2 = w2.length
fun minDistance(word1: String, word2: String): Int {
val n1 = word1.length
val n2 = word2.length
if (n2 > n1) {
return minDistance(w2, w1)
return minDistance(word2, word1)
}
val dp = IntArray(n2 + 1)
for (j in 0..n2) {
Expand All @@ -69,7 +69,7 @@ class Solution {
dp[0] = i
for (j in 1..n2) {
val tmp = dp[j]
dp[j] = if (w1[i - 1] != w2[j - 1]) 1 + Math.min(pre, Math.min(dp[j], dp[j - 1])) else pre
dp[j] = if (word1[i - 1] != word2[j - 1]) 1 + Math.min(pre, Math.min(dp[j], dp[j - 1])) else pre
pre = tmp
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/g0001_0100/s0090_subsets_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ The solution set **must not** contain duplicate subsets. Return the solution in

```kotlin
class Solution {
var allComb: MutableList<List<Int>> = ArrayList()
var comb: MutableList<Int> = ArrayList()
lateinit var nums: IntArray
private var allComb: MutableList<List<Int>> = ArrayList()
private var comb: MutableList<Int> = ArrayList()
private lateinit var nums: IntArray

fun subsetsWithDup(nums: IntArray): List<List<Int>> {
nums.sort()
this.nums = nums
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Given two integer arrays `preorder` and `inorder` where `preorder` is the preord

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

/*
* Example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import kotlin.collections.ArrayList
*/
class Solution {
private val order: MutableList<MutableList<Int>> = ArrayList()

fun levelOrderBottom(root: TreeNode?): List<MutableList<Int>> {
getOrder(root, 0)
Collections.reverse(order)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ The format of the output is as follows:
import java.util.Collections
import java.util.Stack

internal class Solution {
class Solution {
internal inner class Node {
var mem: MutableMap<List<String>, Int> = HashMap()
fun update(cur: List<String>, cnt: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import com_github_leetcode.TreeNode
* var right: TreeNode? = null
* }
*/
internal class Solution {
class Solution {
fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int {
var ans = 0
if (root == null) return 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 2546\. Apply Bitwise Operations to Make Strings Equal

Medium

You are given two **0-indexed binary** strings `s` and `target` of the same length `n`. You can do the following operation on `s` **any** number of times:

* Choose two **different** indices `i` and `j` where `0 <= i, j < n`.
* Simultaneously, replace `s[i]` with (`s[i]` **OR** `s[j]`) and `s[j]` with (`s[i]` **XOR** `s[j]`).

For example, if `s = "0110"`, you can choose `i = 0` and `j = 2`, then simultaneously replace `s[0]` with (`s[0]` **OR** `s[2]` = `0` **OR** `1` = `1`), and `s[2]` with (`s[0]` **XOR** `s[2]` = `0` **XOR** `1` = `1`), so we will have `s = "1110"`.

Return `true` _if you can make the string_ `s` _equal to_ `target`_, or_ `false` _otherwise_.

**Example 1:**

**Input:** s = "1010", target = "0110"

**Output:** true

**Explanation:** We can do the following operations:

- Choose i = 2 and j = 0. We have now s = "**<ins>0</ins>**0**<ins>1</ins>**0".

- Choose i = 2 and j = 1. We have now s = "0**<ins>11</ins>**0". Since we can make s equal to target, we return true.

**Example 2:**

**Input:** s = "11", target = "00"

**Output:** false

**Explanation:** It is not possible to make s equal to target with any number of operations.

**Constraints:**

* `n == s.length == target.length`
* <code>2 <= n <= 10<sup>5</sup></code>
* `s` and `target` consist of only the digits `0` and `1`.

## Solution

```kotlin
class Solution {
fun makeStringsEqual(s: String, target: String): Boolean {
val strLen = s.length
var ans1 = false
var ans2 = false
for (i in 0 until strLen) {
if (s[i] == '1') {
ans1 = true
}
if (target[i] == '1') {
ans2 = true
}
}
return ans1 == ans2
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 2547\. Minimum Cost to Split an Array

Hard

You are given an integer array `nums` and an integer `k`.

Split the array into some number of non-empty subarrays. The **cost** of a split is the sum of the **importance value** of each subarray in the split.

Let `trimmed(subarray)` be the version of the subarray where all numbers which appear only once are removed.

* For example, `trimmed([3,1,2,4,3,4]) = [3,4,3,4].`

The **importance value** of a subarray is `k + trimmed(subarray).length`.

* For example, if a subarray is `[1,2,3,3,3,4,4]`, then trimmed(`[1,2,3,3,3,4,4]) = [3,3,3,4,4].`The importance value of this subarray will be `k + 5`.

Return _the minimum possible cost of a split of_ `nums`.

A **subarray** is a contiguous **non-empty** sequence of elements within an array.

**Example 1:**

**Input:** nums = [1,2,1,2,1,3,3], k = 2

**Output:** 8

**Explanation:** We split nums to have two subarrays: [1,2], [1,2,1,3,3]. '

The importance value of [1,2] is 2 + (0) = 2.

The importance value of [1,2,1,3,3] is 2 + (2 + 2) = 6.

The cost of the split is 2 + 6 = 8. It can be shown that this is the minimum possible cost among all the possible splits.

**Example 2:**

**Input:** nums = [1,2,1,2,1], k = 2

**Output:** 6

**Explanation:** We split nums to have two subarrays: [1,2], [1,2,1].

The importance value of [1,2] is 2 + (0) = 2.

The importance value of [1,2,1] is 2 + (2) = 4.

The cost of the split is 2 + 4 = 6. It can be shown that this is the minimum possible cost among all the possible splits.

**Example 3:**

**Input:** nums = [1,2,1,2,1], k = 5

**Output:** 10

**Explanation:** We split nums to have one subarray: [1,2,1,2,1].

The importance value of [1,2,1,2,1] is 5 + (3 + 2) = 10.

The cost of the split is 10. It can be shown that this is the minimum possible cost among all the possible splits.

**Constraints:**

* `1 <= nums.length <= 1000`
* `0 <= nums[i] < nums.length`
* <code>1 <= k <= 10<sup>9</sup></code>

## Solution

```kotlin
class Solution {
fun minCost(nums: IntArray, k: Int): Int {
val n = nums.size
val dp = IntArray(n)
dp.fill(-1)
val len = Array(n) { IntArray(n) }
for (r in len) r.fill(0)
for (i in 0 until n) {
val count = IntArray(n)
count.fill(0)
var c = 0
for (j in i until n) {
count[nums[j]] += 1
if (count[nums[j]] == 2) c += 2 else if (count[nums[j]] > 2) c += 1
len[i][j] = c
}
}
return f(0, nums, k, len, dp)
}

private fun f(ind: Int, nums: IntArray, k: Int, len: Array<IntArray>, dp: IntArray): Int {
if (ind >= nums.size) return 0
if (dp[ind] != -1) return dp[ind]
dp[ind] = Int.MAX_VALUE
for (i in ind until nums.size) {
val current = len[ind][i] + k
val next = f(i + 1, nums, k, len, dp)
dp[ind] = dp[ind].coerceAtMost(current + next)
}
return dp[ind]
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 2549\. Count Distinct Numbers on Board

Easy

You are given a positive integer `n`, that is initially placed on a board. Every day, for <code>10<sup>9</sup></code> days, you perform the following procedure:

* For each number `x` present on the board, find all numbers `1 <= i <= n` such that `x % i == 1`.
* Then, place those numbers on the board.

Return _the number of **distinct** integers present on the board after_ <code>10<sup>9</sup></code> _days have elapsed_.

**Note:**

* Once a number is placed on the board, it will remain on it until the end.
* `%` stands for the modulo operation. For example, `14 % 3` is `2`.

**Example 1:**

**Input:** n = 5

**Output:** 4

**Explanation:** Initially, 5 is present on the board.

The next day, 2 and 4 will be added since 5 % 2 == 1 and 5 % 4 == 1.

After that day, 3 will be added to the board because 4 % 3 == 1.

At the end of a billion days, the distinct numbers on the board will be 2, 3, 4, and 5.

**Example 2:**

**Input:** n = 3

**Output:** 2

**Explanation:**

Since 3 % 2 == 1, 2 will be added to the board.

After a billion days, the only two distinct numbers on the board are 2 and 3.

**Constraints:**

* `1 <= n <= 100`

## Solution

```kotlin
class Solution {
fun distinctIntegers(n: Int): Int {
return 1.coerceAtLeast(n - 1)
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 2550\. Count Collisions of Monkeys on a Polygon

Medium

There is a regular convex polygon with `n` vertices. The vertices are labeled from `0` to `n - 1` in a clockwise direction, and each vertex has **exactly one monkey**. The following figure shows a convex polygon of `6` vertices.

![](https://assets.leetcode.com/uploads/2023/01/22/hexagon.jpg)

Each monkey moves simultaneously to a neighboring vertex. A neighboring vertex for a vertex `i` can be:

* the vertex `(i + 1) % n` in the clockwise direction, or
* the vertex `(i - 1 + n) % n` in the counter-clockwise direction.

A **collision** happens if at least two monkeys reside on the same vertex after the movement or intersect on an edge.

Return _the number of ways the monkeys can move so that at least **one collision**_ _happens_. Since the answer may be very large, return it modulo <code>10<sup>9</sup> + 7</code>.

**Note** that each monkey can only move once.

**Example 1:**

**Input:** n = 3

**Output:** 6

**Explanation:** There are 8 total possible movements.

Two ways such that they collide at some point are:

- Monkey 1 moves in a clockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 2 collide.

- Monkey 1 moves in an anticlockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 3 collide. It can be shown 6 total movements result in a collision.

**Example 2:**

**Input:** n = 4

**Output:** 14

**Explanation:** It can be shown that there are 14 ways for the monkeys to collide.

**Constraints:**

* <code>3 <= n <= 10<sup>9</sup></code>

## Solution

```kotlin
class Solution {
fun monkeyMove(n: Int): Int {
return (((modPow2(n - 2) - 1 shl 2) + 2) % 1000000007).toInt()
}

private fun modPow2(n: Int): Long {
if (n == 0) return 1
val b = modPow2(n shr 1)
return (b * b shl (n and 1)) % 1000000007
}
}
```
Loading