Skip to content

Commit e553a86

Browse files
authored
Added tasks 583, 584, 586, 587
1 parent fdd7017 commit e553a86

File tree

13 files changed

+426
-0
lines changed

13 files changed

+426
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
336336

337337
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
338338
|-|-|-|-|-|-
339+
| 0584 |[Find Customer Referee](src/main/kotlin/g0501_0600/s0584_find_customer_referee/script.sql)| Easy | Database | 779 | 43.48
339340
| 0183 |[Customers Who Never Order](src/main/kotlin/g0101_0200/s0183_customers_who_never_order/script.sql)| Easy | Database | 712 | 33.67
340341

341342
#### Day 2 Select and Order
@@ -376,6 +377,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
376377

377378
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
378379
|-|-|-|-|-|-
380+
| 0586 |[Customer Placing the Largest Number of Orders](src/main/kotlin/g0501_0600/s0586_customer_placing_the_largest_number_of_orders/script.sql)| Easy | LeetCode_Curated_SQL_70, Database | 768 | 44.85
379381
| 0511 |[Game Play Analysis I](src/main/kotlin/g0501_0600/s0511_game_play_analysis_i/script.sql)| Easy | LeetCode_Curated_SQL_70, Database | 790 | 45.04
380382

381383
#### Day 9 Control of Flow
@@ -1289,6 +1291,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
12891291
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12901292
|-|-|-|-|-|-
12911293
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 307 | 38.36
1294+
| 0583 |[Delete Operation for Two Strings](src/main/kotlin/g0501_0600/s0583_delete_operation_for_two_strings/Solution.kt)| Medium | String, Dynamic_Programming | 197 | 100.00
12921295

12931296
#### Day 18 Dynamic Programming
12941297

@@ -1659,6 +1662,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16591662
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16601663
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
16611664
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
1665+
| 0587 |[Erect the Fence](src/main/kotlin/g0501_0600/s0587_erect_the_fence/Solution.kt)| Hard | Array, Math, Geometry | 470 | 100.00
1666+
| 0586 |[Customer Placing the Largest Number of Orders](src/main/kotlin/g0501_0600/s0586_customer_placing_the_largest_number_of_orders/script.sql)| Easy | LeetCode_Curated_SQL_70, Database, SQL_I_Day_8_Function | 768 | 44.85
1667+
| 0584 |[Find Customer Referee](src/main/kotlin/g0501_0600/s0584_find_customer_referee/script.sql)| Easy | Database, SQL_I_Day_1_Select | 779 | 43.48
1668+
| 0583 |[Delete Operation for Two Strings](src/main/kotlin/g0501_0600/s0583_delete_operation_for_two_strings/Solution.kt)| Medium | String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming | 197 | 100.00
16621669
| 0581 |[Shortest Unsorted Continuous Subarray](src/main/kotlin/g0501_0600/s0581_shortest_unsorted_continuous_subarray/Solution.kt)| Medium | Array, Sorting, Greedy, Two_Pointers, Stack, Monotonic_Stack | 246 | 100.00
16631670
| 0576 |[Out of Boundary Paths](src/main/kotlin/g0501_0600/s0576_out_of_boundary_paths/Solution.kt)| Medium | Dynamic_Programming | 153 | 100.00
16641671
| 0575 |[Distribute Candies](src/main/kotlin/g0501_0600/s0575_distribute_candies/Solution.kt)| Easy | Array, Hash_Table | 538 | 76.92
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0501_0600.s0583_delete_operation_for_two_strings
2+
3+
// #Medium #String #Dynamic_Programming #Algorithm_II_Day_17_Dynamic_Programming
4+
// #2023_01_30_Time_197_ms_(100.00%)_Space_35.8_MB_(100.00%)
5+
6+
class Solution {
7+
fun minDistance(word1: String, word2: String): Int {
8+
val m = word1.length
9+
val n = word2.length
10+
val dp = Array(m + 1) { IntArray(n + 1) }
11+
for (i in 1..m) {
12+
for (j in 1..n) {
13+
dp[i][j] = if (word1[i - 1] == word2[j - 1]) dp[i - 1][j - 1] + 1 else Math.max(
14+
dp[i - 1][j],
15+
dp[i][j - 1]
16+
)
17+
}
18+
}
19+
return m + n - 2 * dp[m][n]
20+
}
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
583\. Delete Operation for Two Strings
2+
3+
Medium
4+
5+
Given two strings `word1` and `word2`, return _the minimum number of **steps** required to make_ `word1` _and_ `word2` _the same_.
6+
7+
In one **step**, you can delete exactly one character in either string.
8+
9+
**Example 1:**
10+
11+
**Input:** word1 = "sea", word2 = "eat"
12+
13+
**Output:** 2
14+
15+
**Explanation:** You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
16+
17+
**Example 2:**
18+
19+
**Input:** word1 = "leetcode", word2 = "etco"
20+
21+
**Output:** 4
22+
23+
**Constraints:**
24+
25+
* `1 <= word1.length, word2.length <= 500`
26+
* `word1` and `word2` consist of only lowercase English letters.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
584\. Find Customer Referee
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Customer`
8+
9+
+-------------+---------+
10+
| Column Name | Type |
11+
+-------------+---------+
12+
| id | int |
13+
| name | varchar |
14+
| referee_id | int |
15+
+-------------+---------+
16+
id is the primary key column for this table.
17+
Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them.
18+
19+
Write an SQL query to report the IDs of the customer that are **not referred by** the customer with `id = 2`.
20+
21+
Return the result table in **any order**.
22+
23+
The query result format is in the following example.
24+
25+
**Example 1:**
26+
27+
**Input:**
28+
29+
Customer table:
30+
+----+------+------------+
31+
| id | name | referee_id |
32+
+----+------+------------+
33+
| 1 | Will | null |
34+
| 2 | Jane | null |
35+
| 3 | Alex | 2 |
36+
| 4 | Bill | null |
37+
| 5 | Zack | 1 |
38+
| 6 | Mark | 2 |
39+
+----+------+------------+
40+
41+
**Output:**
42+
43+
+------+
44+
| name |
45+
+------+
46+
| Will |
47+
| Jane |
48+
| Bill |
49+
| Zack |
50+
+------+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #SQL_I_Day_1_Select #2023_01_30_Time_779_ms_(43.48%)_Space_0B_(100.00%)
3+
SELECT name FROM customer where referee_id != 2 or referee_id is null;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
586\. Customer Placing the Largest Number of Orders
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Orders`
8+
9+
+-----------------+----------+
10+
| Column Name | Type |
11+
+-----------------+----------+
12+
| order_number | int |
13+
| customer_number | int |
14+
+-----------------+----------+
15+
order_number is the primary key for this table.
16+
This table contains information about the order ID and the customer ID.
17+
18+
Write an SQL query to find the `customer_number` for the customer who has placed **the largest number of orders**.
19+
20+
The test cases are generated so that **exactly one customer** will have placed more orders than any other customer.
21+
22+
The query result format is in the following example.
23+
24+
**Example 1:**
25+
26+
**Input:**
27+
28+
Orders table:
29+
+--------------+-----------------+
30+
| order_number | customer_number |
31+
+--------------+-----------------+
32+
| 1 | 1 |
33+
| 2 | 2 |
34+
| 3 | 3 |
35+
| 4 | 3 |
36+
+--------------+-----------------+
37+
38+
**Output:**
39+
40+
+-----------------+
41+
| customer_number |
42+
+-----------------+
43+
| 3 |
44+
+-----------------+
45+
46+
**Explanation:**
47+
48+
The customer with number 3 has two orders, which is greater than either customer 1 or 2 because each of them only has one order.
49+
50+
So the result is customer\_number 3.
51+
52+
**Follow up:** What if more than one customer has the largest number of orders, can you find all the `customer_number` in this case?
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Write your MySQL query statement below
2+
# #Easy #LeetCode_Curated_SQL_70 #Database #SQL_I_Day_8_Function
3+
# #2023_01_30_Time_768_ms_(44.85%)_Space_0B_(100.00%)
4+
select customer_number from orders group by customer_number order by count(customer_number) desc limit 1
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package g0501_0600.s0587_erect_the_fence
2+
3+
import kotlin.math.abs
4+
import kotlin.math.atan2
5+
import kotlin.math.pow
6+
import kotlin.math.sqrt
7+
8+
// #Hard #Array #Math #Geometry #2023_01_30_Time_470_ms_(100.00%)_Space_54.7_MB_(100.00%)
9+
10+
class Solution {
11+
private fun dist(p1: Pair<Int, Int>, p2: Pair<Int, Int>): Double {
12+
return sqrt((p2.second - p1.second).toDouble().pow(2.0) + Math.pow((p2.first - p1.first).toDouble(), 2.0))
13+
}
14+
15+
private fun angle(p1: Pair<Int, Int>, p2: Pair<Int, Int>): Double {
16+
return atan2((p2.second - p1.second).toDouble(), (p2.first - p1.first).toDouble()).let {
17+
if (it < 0) return (2.0 * Math.PI + it) else it
18+
}
19+
}
20+
21+
fun outerTrees(trees: Array<IntArray>): Array<IntArray> {
22+
if (trees.size < 3) {
23+
return trees
24+
}
25+
val left = trees.asSequence().map { it[0] to it[1] }.toMutableList()
26+
left.sortWith(compareBy<Pair<Int, Int>> { it.second }.thenBy { it.first })
27+
val firstPoint = left[0]
28+
var nowPoint = firstPoint
29+
val pointList = mutableListOf(nowPoint)
30+
var prevAngle = 0.0
31+
while (true) {
32+
val nowList = mutableListOf<Pair<Pair<Int, Int>, Double>>()
33+
var nowMinAngleDiff = 7.0
34+
var nowMinAngle = 7.0
35+
left.forEach {
36+
if (it != nowPoint) {
37+
val angle = angle(nowPoint, it)
38+
if (abs(angle - nowMinAngle) < 0.0000001) {
39+
nowList.add(it to dist(it, nowPoint))
40+
} else {
41+
val diff = if (angle >= prevAngle) (angle - prevAngle) else 2.0 * Math.PI - (angle - prevAngle)
42+
if ((diff) < nowMinAngleDiff) {
43+
nowMinAngle = angle
44+
nowMinAngleDiff = (diff)
45+
nowList.clear()
46+
nowList.add(it to dist(it, nowPoint))
47+
}
48+
}
49+
}
50+
}
51+
prevAngle = nowMinAngle
52+
nowList.sortBy { it.second }
53+
val nowListOnlyPoints = nowList.map { it.first }.toMutableList()
54+
if (nowListOnlyPoints.last() == firstPoint) {
55+
nowListOnlyPoints.removeAt(nowListOnlyPoints.size - 1)
56+
left.removeAll(nowListOnlyPoints)
57+
pointList.addAll(nowListOnlyPoints)
58+
break
59+
} else {
60+
nowPoint = nowListOnlyPoints.last()
61+
left.removeAll(nowListOnlyPoints)
62+
pointList.addAll(nowListOnlyPoints)
63+
}
64+
}
65+
return pointList.asSequence().map { intArrayOf(it.first, it.second) }.toList().toTypedArray()
66+
}
67+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
587\. Erect the Fence
2+
3+
Hard
4+
5+
You are given an array `trees` where <code>trees[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the location of a tree in the garden.
6+
7+
Fence the entire garden using the minimum length of rope, as it is expensive. The garden is well-fenced only if **all the trees are enclosed**.
8+
9+
Return _the coordinates of trees that are exactly located on the fence perimeter_. You may return the answer in **any order**.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/04/24/erect2-plane.jpg)
14+
15+
**Input:** trees = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
16+
17+
**Output:** [[1,1],[2,0],[4,2],[3,3],[2,4]]
18+
19+
**Explanation:** All the trees will be on the perimeter of the fence except the tree at [2, 2], which will be inside the fence.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2021/04/24/erect1-plane.jpg)
24+
25+
**Input:** trees = [[1,2],[2,2],[4,2]]
26+
27+
**Output:** [[4,2],[2,2],[1,2]]
28+
29+
**Explanation:** The fence forms a line that passes through all the trees.
30+
31+
**Constraints:**
32+
33+
* `1 <= trees.length <= 3000`
34+
* `trees[i].length == 2`
35+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 100</code>
36+
* All the given positions are **unique**.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0501_0600.s0583_delete_operation_for_two_strings
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun minDistance() {
10+
assertThat(Solution().minDistance("sea", "eat"), equalTo(2))
11+
}
12+
13+
@Test
14+
fun minDistance2() {
15+
assertThat(Solution().minDistance("leetcode", "etco"), equalTo(4))
16+
}
17+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g0501_0600.s0584_find_customer_referee
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
import org.zapodot.junit.db.annotations.EmbeddedDatabase
7+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest
8+
import org.zapodot.junit.db.common.CompatibilityMode
9+
import java.io.BufferedReader
10+
import java.io.FileNotFoundException
11+
import java.io.FileReader
12+
import java.sql.SQLException
13+
import java.util.stream.Collectors
14+
import javax.sql.DataSource
15+
16+
@EmbeddedDatabaseTest(
17+
compatibilityMode = CompatibilityMode.MySQL,
18+
initialSqls = [
19+
"CREATE TABLE Customer(id INTEGER PRIMARY KEY, name VARCHAR, referee_id INTEGER); " +
20+
"INSERT INTO Customer(id, name, referee_id) VALUES (1, 'Will', NULL); " +
21+
"INSERT INTO Customer(id, name, referee_id) VALUES (2, 'Jane', NULL); " +
22+
"INSERT INTO Customer(id, name, referee_id) VALUES (3, 'Alex', 2); " +
23+
"INSERT INTO Customer(id, name, referee_id) VALUES (4, 'Bill', NULL); " +
24+
"INSERT INTO Customer(id, name, referee_id) VALUES (5, 'Zack', 1); " +
25+
"INSERT INTO Customer(id, name, referee_id) VALUES (6, 'Mark', 2); "
26+
]
27+
)
28+
internal class MysqlTest {
29+
@Test
30+
@Throws(SQLException::class, FileNotFoundException::class)
31+
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
32+
dataSource.getConnection().use { connection ->
33+
connection.createStatement().use { statement ->
34+
statement.executeQuery(
35+
BufferedReader(
36+
FileReader(
37+
"src/main/kotlin/g0501_0600/" +
38+
"s0584_find_customer_referee/script.sql"
39+
)
40+
)
41+
.lines()
42+
.collect(Collectors.joining("\n"))
43+
.replace("#.*?\\r?\\n".toRegex(), "")
44+
).use { resultSet ->
45+
assertThat(resultSet.next(), equalTo(true))
46+
assertThat(resultSet.getString(1), equalTo("Will"))
47+
assertThat(resultSet.next(), equalTo(true))
48+
assertThat(resultSet.getString(1), equalTo("Jane"))
49+
assertThat(resultSet.next(), equalTo(true))
50+
assertThat(resultSet.getString(1), equalTo("Bill"))
51+
assertThat(resultSet.next(), equalTo(true))
52+
assertThat(resultSet.getString(1), equalTo("Zack"))
53+
assertThat(resultSet.next(), equalTo(false))
54+
}
55+
}
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)