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
Added tasks 2001-2400
  • Loading branch information
javadev committed Jul 3, 2023
commit 13920c68b60f01f93f73b09fd538020a53889314
586 changes: 439 additions & 147 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[![](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)

## 1978\. Employees Whose Manager Left the Company

Easy

SQL Schema

Table: `Employees`

+-------------+----------+
| Column Name | Type |
+-------------+----------+
| employee_id | int |
| name | varchar |
| manager_id | int |
| salary | int |
+-------------+----------+

employee_id is the primary key for this table. This table contains information about the employees, their salary, and the ID of their manager. Some employees do not have a manager (manager_id is null).

Write an SQL query to report the IDs of the employees whose salary is strictly less than `$30000` and whose manager left the company. When a manager leaves the company, their information is deleted from the `Employees` table, but the reports still have their `manager_id` set to the manager that left.

Return the result table ordered by `employee_id`.

The query result format is in the following example.

**Example 1:**

**Input: ** Employees table:

+-------------+-----------+------------+--------+
| employee_id | name | manager_id | salary |
+-------------+-----------+------------+--------+
| 3 | Mila | 9 | 60301 |
| 12 | Antonella | null | 31000 |
| 13 | Emery | null | 67084 |
| 1 | Kalel | 11 | 21241 |
| 9 | Mikaela | null | 50937 |
| 11 | Joziah | 6 | 28485 |
+-------------+-----------+------------+--------+

**Output:**

+-------------+
| employee_id |
+-------------+
| 11 |
+-------------+

**Explanation:**

The employees with a salary less than $30000 are 1 (Kalel) and 11 (Joziah).

Kalel's manager is employee 11, who is still in the company (Joziah).

Joziah's manager is employee 6, who left the company because there is no row for employee 6 as it was deleted.

## Solution

```sql
# Write your MySQL query statement below
select employee_id from employees where manager_id not in (select distinct (employee_id) from employees)
AND salary < 30000 order by employee_id
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[![](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)

## 2001\. Number of Pairs of Interchangeable Rectangles

Medium

You are given `n` rectangles represented by a **0-indexed** 2D integer array `rectangles`, where <code>rectangles[i] = [width<sub>i</sub>, height<sub>i</sub>]</code> denotes the width and height of the <code>i<sup>th</sup></code> rectangle.

Two rectangles `i` and `j` (`i < j`) are considered **interchangeable** if they have the **same** width-to-height ratio. More formally, two rectangles are **interchangeable** if <code>width<sub>i</sub>/height<sub>i</sub> == width<sub>j</sub>/height<sub>j</sub></code> (using decimal division, not integer division).

Return _the **number** of pairs of **interchangeable** rectangles in_ `rectangles`.

**Example 1:**

**Input:** rectangles = \[\[4,8],[3,6],[10,20],[15,30]]

**Output:** 6

**Explanation:** The following are the interchangeable pairs of rectangles by index (0-indexed):

- Rectangle 0 with rectangle 1: 4/8 == 3/6.

- Rectangle 0 with rectangle 2: 4/8 == 10/20.

- Rectangle 0 with rectangle 3: 4/8 == 15/30.

- Rectangle 1 with rectangle 2: 3/6 == 10/20.

- Rectangle 1 with rectangle 3: 3/6 == 15/30.

- Rectangle 2 with rectangle 3: 10/20 == 15/30.

**Example 2:**

**Input:** rectangles = \[\[4,5],[7,8]]

**Output:** 0

**Explanation:** There are no interchangeable pairs of rectangles.

**Constraints:**

* `n == rectangles.length`
* <code>1 <= n <= 10<sup>5</sup></code>
* `rectangles[i].length == 2`
* <code>1 <= width<sub>i</sub>, height<sub>i</sub> <= 10<sup>5</sup></code>

## Solution

```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
private fun factorial(n: Long): Long {
var n = n
var m: Long = 0
while (n > 0) {
m += n
n -= 1
}
return m
}

fun interchangeableRectangles(rec: Array<IntArray>): Long {
val ratio = DoubleArray(rec.size)
for (i in rec.indices) {
ratio[i] = rec[i][0].toDouble() / rec[i][1]
}
ratio.sort()
var res: Long = 0
var k = 0
for (j in 0 until ratio.size - 1) {
if (ratio[j] == ratio[j + 1]) {
k++
}
if (ratio[j] != ratio[j + 1] || j + 2 == ratio.size) {
res += factorial(k.toLong())
k = 0
}
}
return res
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
[![](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)

## 2002\. Maximum Product of the Length of Two Palindromic Subsequences

Medium

Given a string `s`, find two **disjoint palindromic subsequences** of `s` such that the **product** of their lengths is **maximized**. The two subsequences are **disjoint** if they do not both pick a character at the same index.

Return _the **maximum** possible **product** of the lengths of the two palindromic subsequences_.

A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. A string is **palindromic** if it reads the same forward and backward.

**Example 1:**

![example-1](https://assets.leetcode.com/uploads/2021/08/24/two-palindromic-subsequences.png)

**Input:** s = "leetcodecom"

**Output:** 9

**Explanation:** An optimal solution is to choose "ete" for the 1<sup>st</sup> subsequence and "cdc" for the 2<sup>nd</sup> subsequence.

The product of their lengths is: 3 \* 3 = 9.

**Example 2:**

**Input:** s = "bb"

**Output:** 1

**Explanation:** An optimal solution is to choose "b" (the first character) for the 1<sup>st</sup> subsequence and "b" (the second character) for the 2<sup>nd</sup> subsequence.

The product of their lengths is: 1 \* 1 = 1.

**Example 3:**

**Input:** s = "accbcaxxcxx"

**Output:** 25

**Explanation:** An optimal solution is to choose "accca" for the 1<sup>st</sup> subsequence and "xxcxx" for the 2<sup>nd</sup> subsequence.

The product of their lengths is: 5 \* 5 = 25.

**Constraints:**

* `2 <= s.length <= 12`
* `s` consists of lowercase English letters only.

## Solution

```kotlin
class Solution {
fun maxProduct(s: String): Int {
if (s.length == 2) {
return 1
}
val list: MutableList<State> = ArrayList()
val chars = s.toCharArray()
val visited: MutableSet<State> = HashSet()
for (i in chars.indices) {
val mask = 1 shl i
recur(chars, State(i, i, 0, mask), list, visited)
recur(chars, State(i, i + 1, 0, mask), list, visited)
}
list.sortWith { a: State, b: State -> b.cnt - a.cnt }
var res = 1
val explored: MutableSet<Int> = HashSet()
for (i in 0 until list.size - 1) {
if (explored.contains(i)) {
continue
}
val cur = list[i]
if (cur.cnt == 1) {
break
}
for (j in i + 1 until list.size) {
val cand = list[j]
if (cur.mask and cand.mask < 1) {
if (explored.add(j)) {
res = res.coerceAtLeast(cur.cnt * cand.cnt)
}
break
}
}
}
return res
}

private fun recur(chars: CharArray, s: State, list: MutableList<State>, visited: MutableSet<State>) {
if (s.i < 0 || s.j >= chars.size) {
return
}
if (!visited.add(s)) {
return
}
if (chars[s.i] == chars[s.j]) {
val m = s.mask or (1 shl s.i) or (1 shl s.j)
val nextCnt = s.cnt + if (s.i < s.j) 2 else 1
list.add(State(s.i, s.j, nextCnt, m))
recur(chars, State(s.i - 1, s.j + 1, nextCnt, m), list, visited)
}
recur(chars, State(s.i - 1, s.j, s.cnt, s.mask), list, visited)
recur(chars, State(s.i, s.j + 1, s.cnt, s.mask), list, visited)
}

private class State(var i: Int, var j: Int, var cnt: Int, var mask: Int) {
override fun equals(other: Any?): Boolean {
if (other == null || other.javaClass != this.javaClass) {
return false
}
val s = other as State
return i == s.i && j == s.j && mask == s.mask
}

override fun hashCode(): Int {
return (i * 31 + j) * 31 + mask
}
}
}
```
Loading