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
180 changes: 179 additions & 1 deletion README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Given a string `s` containing only digits, return _all possible valid IP address
## Solution

```kotlin
class Solution() {
class Solution {
fun restoreIpAddresses(s: String): List<String> {
val results: MutableList<String> = ArrayList()
step(s, 0, IntArray(4), 0, results)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g0601_0700/s0638_shopping_offers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ You cannot add more items, though only $9 for 2A ,2B and 1C.
## Solution

```kotlin
class Solution() {
class Solution {
fun shoppingOffers(
price: List<Int>,
special: List<List<Int>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Return _the smallest possible total score that you can achieve with some triangu
## Solution

```kotlin
class Solution() {
class Solution {
private val dp = Array(101) { IntArray(101) }
fun minScoreTriangulation(values: IntArray): Int {
val n = values.size
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
[![](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)

## 1378\. Replace Employee ID With The Unique Identifier

Easy

SQL Schema

Table: `Employees`

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| name | varchar |
+---------------+---------+
id is the primary key for this table.
Each row of this table contains the id and the name of an employee in a company.

Table: `EmployeeUNI`

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| unique_id | int |
+---------------+---------+
(id, unique_id) is the primary key for this table.
Each row of this table contains the id and the corresponding unique id of an employee in the company.

Write an SQL query to show the **unique ID** of each user, If a user does not have a unique ID replace just show `null`.

Return the result table in **any** order.

The query result format is in the following example.

**Example 1:**

**Input:**,

Employees table:
+----+----------+
| id | name |
+----+----------+
| 1 | Alice |
| 7 | Bob |
| 11 | Meir |
| 90 | Winston |
| 3 | Jonathan |
+----+----------+

EmployeeUNI table:
+----+-----------+
| id | unique_id |
+----+-----------+
| 3 | 1 |
| 11 | 2 |
| 90 | 3 |
+----+-----------+

**Output:**

+-----------+----------+
| unique_id | name |
+-----------+----------+
| null | Alice |
| null | Bob |
| 2 | Meir |
| 3 | Winston |
| 1 | Jonathan |
+-----------+----------+

**Explanation:**

Alice and Bob do not have a unique ID, We will show null instead.

The unique ID of Meir is 2.

The unique ID of Winston is 3.

The unique ID of Jonathan is 1.

## Solution

```sql
# Write your MySQL query statement below
select u.unique_id, e.name
from Employees e
left join EmployeeUNI u
on e.id = u.id;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[![](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)

## 1517\. Find Users With Valid E-Mails

Easy

SQL Schema

Table: `Users`

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| name | varchar |
| mail | varchar |
+---------------+---------+
user_id is the primary key for this table.
This table contains information of the users signed up in a website. Some e-mails are invalid.

Write an SQL query to find the users who have **valid emails**.

A valid e-mail has a prefix name and a domain where:

* **The prefix name** is a string that may contain letters (upper or lower case), digits, underscore `'_'`, period `'.'`, and/or dash `'-'`. The prefix name **must** start with a letter.
* **The domain** is `'@leetcode.com'`.

Return the result table in **any order**.

The query result format is in the following example.

**Example 1:**

**Input:**

Users table:
+---------+-----------+-------------------------+
| user_id | name | mail |
+---------+-----------+-------------------------+
| 1 | Winston | winston@leetcode.com |
| 2 | Jonathan | jonathanisgreat |
| 3 | Annabelle | bella-@leetcode.com |
| 4 | Sally | sally.come@leetcode.com |
| 5 | Marwan | quarz#2020@leetcode.com |
| 6 | David | david69@gmail.com |
| 7 | Shapiro | .shapo@leetcode.com |
+---------+-----------+-------------------------+

**Output:**

+---------+-----------+-------------------------+
| user_id | name | mail |
+---------+-----------+-------------------------+
| 1 | Winston | winston@leetcode.com |
| 3 | Annabelle | bella-@leetcode.com |
| 4 | Sally | sally.come@leetcode.com |
+---------+-----------+-------------------------+

**Explanation:**

The mail of user 2 does not have a domain.

The mail of user 5 has the # sign which is not allowed.

The mail of user 6 does not have the leetcode domain.

The mail of user 7 starts with a period.

## Solution

```sql
# Write your MySQL query statement below
select * from Users
where mail REGEXP '^[a-zA-Z][a-zA-Z0-9_.-]*@leetcode.com' and mail LIKE '%@leetcode.com'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[![](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)

## 1608\. Special Array With X Elements Greater Than or Equal X

Easy

You are given an array `nums` of non-negative integers. `nums` is considered **special** if there exists a number `x` such that there are **exactly** `x` numbers in `nums` that are **greater than or equal to** `x`.

Notice that `x` **does not** have to be an element in `nums`.

Return `x` _if the array is **special**, otherwise, return_ `-1`. It can be proven that if `nums` is special, the value for `x` is **unique**.

**Example 1:**

**Input:** nums = [3,5]

**Output:** 2

**Explanation:** There are 2 values (3 and 5) that are greater than or equal to 2.

**Example 2:**

**Input:** nums = [0,0]

**Output:** -1

**Explanation:** No numbers fit the criteria for x.

If x = 0, there should be 0 numbers >= x, but there are 2.

If x = 1, there should be 1 number >= x, but there are 0.

If x = 2, there should be 2 numbers >= x, but there are 0.

x cannot be greater since there are only 2 numbers in nums.

**Example 3:**

**Input:** nums = [0,4,3,0,4]

**Output:** 3

**Explanation:** There are 3 values that are greater than or equal to 3.

**Constraints:**

* `1 <= nums.length <= 100`
* `0 <= nums[i] <= 1000`

## Solution

```kotlin
class Solution {
fun specialArray(nums: IntArray): Int {
nums.sort()
val max = nums[nums.size - 1]
for (x in 1..max) {
var found = 0
var i = nums.size - 1
while (i >= 0 && nums[i] >= x) {
i--
found++
}
if (found == x) {
return x
}
}
return -1
}
}
```
118 changes: 118 additions & 0 deletions src/main/kotlin/g1601_1700/s1609_even_odd_tree/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
[![](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)

## 1609\. Even Odd Tree

Medium

A binary tree is named **Even-Odd** if it meets the following conditions:

* The root of the binary tree is at level index `0`, its children are at level index `1`, their children are at level index `2`, etc.
* For every **even-indexed** level, all nodes at the level have **odd** integer values in **strictly increasing** order (from left to right).
* For every **odd-indexed** level, all nodes at the level have **even** integer values in **strictly decreasing** order (from left to right).

Given the `root` of a binary tree, _return_ `true` _if the binary tree is **Even-Odd**, otherwise return_ `false`_._

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/09/15/sample_1_1966.png)

**Input:** root = [1,10,4,3,null,7,9,12,8,6,null,null,2]

**Output:** true

**Explanation:** The node values on each level are:

Level 0: [1]

Level 1: [10,4]

Level 2: [3,7,9]

Level 3: [12,8,6,2]

Since levels 0 and 2 are all odd and increasing and levels 1 and 3 are all even and decreasing, the tree is Even-Odd.

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/09/15/sample_2_1966.png)

**Input:** root = [5,4,2,3,3,7]

**Output:** false

**Explanation:** The node values on each level are:

Level 0: [5]

Level 1: [4,2]

Level 2: [3,3,7]

Node values in level 2 must be in strictly increasing order, so the tree is not Even-Odd.

**Example 3:**

![](https://assets.leetcode.com/uploads/2020/09/22/sample_1_333_1966.png)

**Input:** root = [5,9,1,3,5,7]

**Output:** false

**Explanation:** Node values in the level 1 should be even integers.

**Constraints:**

* The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.
* <code>1 <= Node.val <= 10<sup>6</sup></code>

## Solution

```kotlin
import com_github_leetcode.TreeNode

/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
private val comp: MutableList<Int> = ArrayList()

fun isEvenOddTree(root: TreeNode?): Boolean {
return find(root, 0)
}

private fun find(root: TreeNode?, height: Int): Boolean {
if (root == null) {
return true
}
if (height % 2 == 0 && root.`val` % 2 == 0 || height % 2 == 1 && root.`val` % 2 == 1) {
return false
}
if (comp.size == height) {
comp.add(root.`val`)
} else {
if (height % 2 == 0) {
if (comp[height] >= root.`val`) {
return false
} else {
comp[height] = root.`val`
}
} else {
if (comp[height] <= root.`val`) {
return false
} else {
comp[height] = root.`val`
}
}
}
return find(root.left, height + 1) && find(root.right, height + 1)
}
}
```
Loading