Skip to content

Commit 7ad0a38

Browse files
authored
Added tasks 1053, 1054, 1061, 1068
1 parent 2bab3bc commit 7ad0a38

File tree

13 files changed

+449
-0
lines changed

13 files changed

+449
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17811781
| 1155 |[Number of Dice Rolls With Target Sum](src/main/kotlin/g1101_1200/s1155_number_of_dice_rolls_with_target_sum/Solution.kt)| Medium | Dynamic_Programming | 158 | 80.95
17821782
| 1154 |[Day of the Year](src/main/kotlin/g1101_1200/s1154_day_of_the_year/Solution.kt)| Easy | String, Math | 317 | 70.00
17831783
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
1784+
| 1068 |[Product Sales Analysis I](src/main/kotlin/g1001_1100/s1068_product_sales_analysis_i/script.sql)| Easy | Database | 1897 | 79.88
1785+
| 1061 |[Lexicographically Smallest Equivalent String](src/main/kotlin/g1001_1100/s1061_lexicographically_smallest_equivalent_string/Solution.kt)| Medium | String, Union_Find | 166 | 100.00
1786+
| 1054 |[Distant Barcodes](src/main/kotlin/g1001_1100/s1054_distant_barcodes/Solution.kt)| Medium | Array, Hash_Table, Sorting, Greedy, Heap_Priority_Queue, Counting | 622 | 75.00
1787+
| 1053 |[Previous Permutation With One Swap](src/main/kotlin/g1001_1100/s1053_previous_permutation_with_one_swap/Solution.kt)| Medium | Array, Greedy | 338 | 25.00
17841788
| 1052 |[Grumpy Bookstore Owner](src/main/kotlin/g1001_1100/s1052_grumpy_bookstore_owner/Solution.kt)| Medium | Array, Sliding_Window | 268 | 80.00
17851789
| 1051 |[Height Checker](src/main/kotlin/g1001_1100/s1051_height_checker/Solution.kt)| Easy | Array, Sorting, Counting_Sort | 140 | 94.37
17861790
| 1050 |[Actors and Directors Who Cooperated At Least Three Times](src/main/kotlin/g1001_1100/s1050_actors_and_directors_who_cooperated_at_least_three_times/script.sql)| Easy | LeetCode_Curated_SQL_70, Database, SQL_I_Day_10_Where | 629 | 81.02
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g1001_1100.s1053_previous_permutation_with_one_swap
2+
3+
// #Medium #Array #Greedy #2023_05_30_Time_338_ms_(25.00%)_Space_71.2_MB_(25.00%)
4+
5+
class Solution {
6+
fun prevPermOpt1(arr: IntArray): IntArray {
7+
for (i in arr.indices.reversed()) {
8+
var diff = Int.MAX_VALUE
9+
var index = i
10+
for (j in i + 1 until arr.size) {
11+
if (arr[i] - arr[j] in 1 until diff) {
12+
diff = arr[i] - arr[j]
13+
index = j
14+
}
15+
}
16+
if (diff != Int.MAX_VALUE) {
17+
val temp = arr[i]
18+
arr[i] = arr[index]
19+
arr[index] = temp
20+
break
21+
}
22+
}
23+
return arr
24+
}
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1053\. Previous Permutation With One Swap
2+
3+
Medium
4+
5+
Given an array of positive integers `arr` (not necessarily distinct), return _the_ _lexicographically_ _largest permutation that is smaller than_ `arr`, that can be **made with exactly one swap**. If it cannot be done, then return the same array.
6+
7+
**Note** that a _swap_ exchanges the positions of two numbers `arr[i]` and `arr[j]`
8+
9+
**Example 1:**
10+
11+
**Input:** arr = [3,2,1]
12+
13+
**Output:** [3,1,2]
14+
15+
**Explanation:** Swapping 2 and 1.
16+
17+
**Example 2:**
18+
19+
**Input:** arr = [1,1,5]
20+
21+
**Output:** [1,1,5]
22+
23+
**Explanation:** This is already the smallest permutation.
24+
25+
**Example 3:**
26+
27+
**Input:** arr = [1,9,4,6,7]
28+
29+
**Output:** [1,7,4,6,9]
30+
31+
**Explanation:** Swapping 9 and 7.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= arr.length <= 10<sup>4</sup></code>
36+
* <code>1 <= arr[i] <= 10<sup>4</sup></code>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g1001_1100.s1054_distant_barcodes
2+
3+
// #Medium #Array #Hash_Table #Sorting #Greedy #Heap_Priority_Queue #Counting
4+
// #2023_05_30_Time_622_ms_(75.00%)_Space_102.5_MB_(75.00%)
5+
6+
import java.util.PriorityQueue
7+
8+
class Solution {
9+
fun rearrangeBarcodes(barcodes: IntArray): IntArray {
10+
val map = barcodes.groupBy { it }.mapValues { it.value.size }
11+
val pq = PriorityQueue<Pair<Int, Int>> { a, b -> b.second - a.second }
12+
map.forEach { kv -> pq.offer(kv.toPair()) }
13+
val result = IntArray(barcodes.size)
14+
var ind = 0
15+
while (pq.isNotEmpty()) {
16+
val remainingBcs = mutableListOf<Pair<Int, Int>>()
17+
for (i in 0 until 2) {
18+
if (pq.isNotEmpty()) {
19+
val max = pq.poll()
20+
result[ind++] = max.first
21+
if (max.second - 1 != 0) {
22+
remainingBcs.add(Pair(max.first, max.second - 1))
23+
}
24+
}
25+
}
26+
remainingBcs.forEach { bc -> pq.offer(bc) }
27+
}
28+
return result
29+
}
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
1054\. Distant Barcodes
2+
3+
Medium
4+
5+
In a warehouse, there is a row of barcodes, where the <code>i<sup>th</sup></code> barcode is `barcodes[i]`.
6+
7+
Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer, and it is guaranteed an answer exists.
8+
9+
**Example 1:**
10+
11+
**Input:** barcodes = [1,1,1,2,2,2]
12+
13+
**Output:** [2,1,2,1,2,1]
14+
15+
**Example 2:**
16+
17+
**Input:** barcodes = [1,1,1,1,2,2,3,3]
18+
19+
**Output:** [1,3,1,3,1,2,1,2]
20+
21+
**Constraints:**
22+
23+
* `1 <= barcodes.length <= 10000`
24+
* `1 <= barcodes[i] <= 10000`
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g1001_1100.s1061_lexicographically_smallest_equivalent_string
2+
3+
// #Medium #String #Union_Find #2023_05_30_Time_166_ms_(100.00%)_Space_50.7_MB_(25.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
lateinit var parent: IntArray
8+
9+
fun smallestEquivalentString(s1: String, s2: String, baseStr: String): String? {
10+
parent = IntArray(26)
11+
val n = s1.length
12+
var result = ""
13+
for (i in 0..25) parent[i] = i
14+
for (i in 0 until n) {
15+
union(s1[i].code - 'a'.code, s2[i].code - 'a'.code)
16+
}
17+
val base = 'a'.code
18+
for (element in baseStr) {
19+
result += (base + find(element.code - 'a'.code)).toChar()
20+
}
21+
return result
22+
}
23+
24+
private fun union(a: Int, b: Int) {
25+
val parentA = find(a)
26+
val parentB = find(b)
27+
if (parentA != parentB) {
28+
if (parentA < parentB) {
29+
parent[parentB] = parentA
30+
} else {
31+
parent[parentA] = parentB
32+
}
33+
}
34+
}
35+
36+
private fun find(x: Int): Int {
37+
var x = x
38+
while (parent[x] != x) {
39+
x = parent[x]
40+
}
41+
return x
42+
}
43+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
1061\. Lexicographically Smallest Equivalent String
2+
3+
Medium
4+
5+
You are given two strings of the same length `s1` and `s2` and a string `baseStr`.
6+
7+
We say `s1[i]` and `s2[i]` are equivalent characters.
8+
9+
* For example, if `s1 = "abc"` and `s2 = "cde"`, then we have `'a' == 'c'`, `'b' == 'd'`, and `'c' == 'e'`.
10+
11+
Equivalent characters follow the usual rules of any equivalence relation:
12+
13+
* **Reflexivity:** `'a' == 'a'`.
14+
* **Symmetry:** `'a' == 'b'` implies `'b' == 'a'`.
15+
* **Transitivity:** `'a' == 'b'` and `'b' == 'c'` implies `'a' == 'c'`.
16+
17+
For example, given the equivalency information from `s1 = "abc"` and `s2 = "cde"`, `"acd"` and `"aab"` are equivalent strings of `baseStr = "eed"`, and `"aab"` is the lexicographically smallest equivalent string of `baseStr`.
18+
19+
Return _the lexicographically smallest equivalent string of_ `baseStr` _by using the equivalency information from_ `s1` _and_ `s2`.
20+
21+
**Example 1:**
22+
23+
**Input:** s1 = "parker", s2 = "morris", baseStr = "parser"
24+
25+
**Output:** "makkek"
26+
27+
**Explanation:** Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
28+
29+
The characters in each group are equivalent and sorted in lexicographical order.
30+
31+
So the answer is "makkek".
32+
33+
**Example 2:**
34+
35+
**Input:** s1 = "hello", s2 = "world", baseStr = "hold"
36+
37+
**Output:** "hdld"
38+
39+
**Explanation:** Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
40+
41+
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
42+
43+
**Example 3:**
44+
45+
**Input:** s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
46+
47+
**Output:** "aauaaaaada"
48+
49+
**Explanation:** We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
50+
51+
**Constraints:**
52+
53+
* `1 <= s1.length, s2.length, baseStr <= 1000`
54+
* `s1.length == s2.length`
55+
* `s1`, `s2`, and `baseStr` consist of lowercase English letters.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
1068\. Product Sales Analysis I
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Sales`
8+
9+
+-------------+-------+
10+
| Column Name | Type |
11+
+-------------+-------+
12+
| sale_id | int |
13+
| product_id | int |
14+
| year | int |
15+
| quantity | int |
16+
| price | int |
17+
+-------------+-------+
18+
19+
(sale_id, year) is the primary key of this table.
20+
21+
product_id is a foreign key to `Product` table.
22+
23+
Each row of this table shows a sale on the product product_id in a certain year.
24+
25+
Note that the price is per unit.
26+
27+
Table: `Product`
28+
29+
+--------------+---------+
30+
| Column Name | Type |
31+
+--------------+---------+
32+
| product_id | int |
33+
| product_name | varchar |
34+
+--------------+---------+
35+
36+
product_id is the primary key of this table.
37+
38+
Each row of this table indicates the product name of each product.
39+
40+
Write an SQL query that reports the `product_name`, `year`, and `price` for each `sale_id` in the `Sales` table.
41+
42+
Return the resulting table in **any order**.
43+
44+
The query result format is in the following example.
45+
46+
**Example 1:**
47+
48+
**Input:** Sales table:
49+
50+
+---------+------------+------+----------+-------+
51+
| sale_id | product_id | year | quantity | price |
52+
+---------+------------+------+----------+-------+
53+
| 1 | 100 | 2008 | 10 | 5000 |
54+
| 2 | 100 | 2009 | 12 | 5000 |
55+
| 7 | 200 | 2011 | 15 | 9000 |
56+
+---------+------------+------+----------+-------+
57+
58+
Product table:
59+
60+
+------------+--------------+
61+
| product_id | product_name |
62+
+------------+--------------+
63+
| 100 | Nokia |
64+
| 200 | Apple |
65+
| 300 | Samsung |
66+
+------------+--------------+
67+
68+
**Output:**
69+
70+
+--------------+-------+-------+
71+
| product_name | year | price |
72+
+--------------+-------+-------+
73+
| Nokia | 2008 | 5000 |
74+
| Nokia | 2009 | 5000 |
75+
| Apple | 2011 | 9000 |
76+
+--------------+-------+-------+
77+
78+
**Explanation:** From sale\_id = 1, we can conclude that Nokia was sold for 5000 in the year 2008. From sale\_id = 2, we can conclude that Nokia was sold for 5000 in the year 2009. From sale\_id = 7, we can conclude that Apple was sold for 9000 in the year 2011.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #2023_05_30_Time_1897_ms_(79.88%)_Space_0B_(100.00%)
3+
Select Product.product_name, Sales.sale_year, Sales.price
4+
from Sales
5+
Inner Join Product On Product.product_id = Sales.product_id
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g1001_1100.s1053_previous_permutation_with_one_swap
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 prevPermOpt1() {
10+
assertThat(Solution().prevPermOpt1(intArrayOf(3, 2, 1)), equalTo(intArrayOf(3, 1, 2)))
11+
}
12+
13+
@Test
14+
fun prevPermOpt2() {
15+
assertThat(Solution().prevPermOpt1(intArrayOf(1, 1, 5)), equalTo(intArrayOf(1, 1, 5)))
16+
}
17+
18+
@Test
19+
fun prevPermOpt3() {
20+
assertThat(
21+
Solution().prevPermOpt1(intArrayOf(1, 9, 4, 6, 7)),
22+
equalTo(intArrayOf(1, 7, 4, 6, 9))
23+
)
24+
}
25+
}

0 commit comments

Comments
 (0)