Skip to content

Commit 103772d

Browse files
authored
Added tasks 1070, 1071, 1072, 1073
1 parent ff00c93 commit 103772d

File tree

13 files changed

+455
-0
lines changed

13 files changed

+455
-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+
| 1073 |[Adding Two Negabinary Numbers](src/main/kotlin/g1001_1100/s1073_adding_two_negabinary_numbers/Solution.kt)| Medium | Array, Math | 187 | 100.00
1785+
| 1072 |[Flip Columns For Maximum Number of Equal Rows](src/main/kotlin/g1001_1100/s1072_flip_columns_for_maximum_number_of_equal_rows/Solution.kt)| Medium | Array, Hash_Table, Matrix | 536 | 100.00
1786+
| 1071 |[Greatest Common Divisor of Strings](src/main/kotlin/g1001_1100/s1071_greatest_common_divisor_of_strings/Solution.kt)| Easy | String, Math | 150 | 80.68
1787+
| 1070 |[Product Sales Analysis III](src/main/kotlin/g1001_1100/s1070_product_sales_analysis_iii/script.sql)| Medium | Database | 1561 | 95.47
17841788
| 1068 |[Product Sales Analysis I](src/main/kotlin/g1001_1100/s1068_product_sales_analysis_i/script.sql)| Easy | Database | 1897 | 79.88
17851789
| 1061 |[Lexicographically Smallest Equivalent String](src/main/kotlin/g1001_1100/s1061_lexicographically_smallest_equivalent_string/Solution.kt)| Medium | String, Union_Find | 166 | 100.00
17861790
| 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
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
1070\. Product Sales Analysis III
2+
3+
Medium
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. product_id is a foreign key to `Product` table.
20+
21+
Each row of this table shows a sale on the product product_id in a certain year.
22+
23+
Note that the price is per unit.
24+
25+
Table: `Product`
26+
27+
+--------------+---------+
28+
| Column Name | Type |
29+
+--------------+---------+
30+
| product_id | int |
31+
| product_name | varchar |
32+
+--------------+---------+
33+
34+
product_id is the primary key of this table.
35+
36+
Each row of this table indicates the product name of each product.
37+
38+
Write an SQL query that selects the **product id**, **year**, **quantity**, and **price** for the **first year** of every product sold.
39+
40+
Return the resulting table in **any order**.
41+
42+
The query result format is in the following example.
43+
44+
**Example 1:**
45+
46+
**Input:** Sales table:
47+
48+
+---------+------------+------+----------+-------+
49+
| sale_id | product_id | year | quantity | price |
50+
+---------+------------+------+----------+-------+
51+
| 1 | 100 | 2008 | 10 | 5000 |
52+
| 2 | 100 | 2009 | 12 | 5000 |
53+
| 7 | 200 | 2011 | 15 | 9000 |
54+
+---------+------------+------+----------+-------+
55+
56+
Product table:
57+
58+
+------------+--------------+
59+
| product_id | product_name |
60+
+------------+--------------+
61+
| 100 | Nokia |
62+
| 200 | Apple |
63+
| 300 | Samsung |
64+
+------------+--------------+
65+
66+
**Output:**
67+
68+
+------------+------------+----------+-------+
69+
| product_id | first_year | quantity | price |
70+
+------------+------------+----------+-------+
71+
| 100 | 2008 | 10 | 5000 |
72+
| 200 | 2011 | 15 | 9000 |
73+
+------------+------------+----------+-------+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Write your MySQL query statement below
2+
# #Medium #Database #2023_05_31_Time_1561_ms_(95.47%)_Space_0B_(100.00%)
3+
SELECT
4+
a.product_id, sale_year AS first_year, quantity, price
5+
FROM
6+
(
7+
SELECT
8+
*,
9+
RANK() OVER(PARTITION BY product_id ORDER BY sale_year) as rk
10+
FROM
11+
Sales
12+
) AS a
13+
LEFT JOIN
14+
Product AS p
15+
ON
16+
a.product_id = p.product_id
17+
WHERE
18+
rk = 1
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g1001_1100.s1071_greatest_common_divisor_of_strings
2+
3+
// #Easy #String #Math #2023_05_31_Time_150_ms_(80.68%)_Space_36.2_MB_(84.09%)
4+
5+
class Solution {
6+
fun gcdOfStrings(str1: String?, str2: String?): String {
7+
if (str1 == null || str2 == null) {
8+
return ""
9+
}
10+
if (str1 == str2) {
11+
return str1
12+
}
13+
val m = str1.length
14+
val n = str2.length
15+
if (m > n && str1.substring(0, n) == str2) {
16+
return gcdOfStrings(str1.substring(n), str2)
17+
}
18+
return if (n > m && str2.substring(0, m) == str1) {
19+
gcdOfStrings(str2.substring(m), str1)
20+
} else ""
21+
}
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
1071\. Greatest Common Divisor of Strings
2+
3+
Easy
4+
5+
For two strings `s` and `t`, we say "`t` divides `s`" if and only if `s = t + ... + t` (i.e., `t` is concatenated with itself one or more times).
6+
7+
Given two strings `str1` and `str2`, return _the largest string_ `x` _such that_ `x` _divides both_ `str1` _and_ `str2`.
8+
9+
**Example 1:**
10+
11+
**Input:** str1 = "ABCABC", str2 = "ABC"
12+
13+
**Output:** "ABC"
14+
15+
**Example 2:**
16+
17+
**Input:** str1 = "ABABAB", str2 = "ABAB"
18+
19+
**Output:** "AB"
20+
21+
**Example 3:**
22+
23+
**Input:** str1 = "LEET", str2 = "CODE"
24+
25+
**Output:** ""
26+
27+
**Constraints:**
28+
29+
* `1 <= str1.length, str2.length <= 1000`
30+
* `str1` and `str2` consist of English uppercase letters.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g1001_1100.s1072_flip_columns_for_maximum_number_of_equal_rows
2+
3+
// #Medium #Array #Hash_Table #Matrix #2023_05_31_Time_536_ms_(100.00%)_Space_108.4_MB_(50.00%)
4+
5+
class Solution {
6+
fun maxEqualRowsAfterFlips(matrix: Array<IntArray>): Int {
7+
/*
8+
Idea:
9+
For a given row[i], 0<=i<m, row[j], 0<=j<m and j!=i, if either of them can have
10+
all values equal after some number of flips, then
11+
row[i]==row[j] <1> or
12+
row[i]^row[j] == 111...111 <2> (xor result is a row full of '1')
13+
14+
Go further, in case<2> row[j] can turn to row[i] by flipping each column of row[j]
15+
IF assume row[i][0] is 0, then question is convert into:
16+
1> flipping each column of each row if row[i][0] is not '0',
17+
2> count the frequency of each row.
18+
The biggest number of frequencies is the answer.
19+
*/
20+
21+
// O(M*N), int M = matrix.length, N = matrix[0].length;
22+
var answer = 0
23+
val frequency: MutableMap<String, Int> = HashMap()
24+
for (row in matrix) {
25+
val rowStr = StringBuilder()
26+
for (c in row) {
27+
if (row[0] == 1) {
28+
rowStr.append(if (c == 1) 0 else 1)
29+
} else {
30+
rowStr.append(c)
31+
}
32+
}
33+
val key = rowStr.toString()
34+
val value = frequency.getOrDefault(key, 0) + 1
35+
frequency[key] = value
36+
answer = answer.coerceAtLeast(value)
37+
}
38+
return answer
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1072\. Flip Columns For Maximum Number of Equal Rows
2+
3+
Medium
4+
5+
You are given an `m x n` binary matrix `matrix`.
6+
7+
You can choose any number of columns in the matrix and flip every cell in that column (i.e., Change the value of the cell from `0` to `1` or vice versa).
8+
9+
Return _the maximum number of rows that have all values equal after some number of flips_.
10+
11+
**Example 1:**
12+
13+
**Input:** matrix = [[0,1],[1,1]]
14+
15+
**Output:** 1
16+
17+
**Explanation:** After flipping no values, 1 row has all values equal.
18+
19+
**Example 2:**
20+
21+
**Input:** matrix = [[0,1],[1,0]]
22+
23+
**Output:** 2
24+
25+
**Explanation:** After flipping values in the first column, both rows have equal values.
26+
27+
**Example 3:**
28+
29+
**Input:** matrix = [[0,0,0],[0,0,1],[1,1,0]]
30+
31+
**Output:** 2
32+
33+
**Explanation:** After flipping values in the first two columns, the last two rows have equal values.
34+
35+
**Constraints:**
36+
37+
* `m == matrix.length`
38+
* `n == matrix[i].length`
39+
* `1 <= m, n <= 300`
40+
* `matrix[i][j]` is either `0` or `1`.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g1001_1100.s1073_adding_two_negabinary_numbers
2+
3+
// #Medium #Array #Math #2023_05_31_Time_187_ms_(100.00%)_Space_40.9_MB_(100.00%)
4+
5+
class Solution {
6+
fun addNegabinary(arr1: IntArray, arr2: IntArray): IntArray {
7+
val len1 = arr1.size
8+
val len2 = arr2.size
9+
val reverseArr1 = IntArray(len1)
10+
for (i in len1 - 1 downTo 0) {
11+
reverseArr1[len1 - i - 1] = arr1[i]
12+
}
13+
val reverseArr2 = IntArray(len2)
14+
for (i in len2 - 1 downTo 0) {
15+
reverseArr2[len2 - i - 1] = arr2[i]
16+
}
17+
val sumArray = IntArray(len1.coerceAtLeast(len2) + 2)
18+
System.arraycopy(reverseArr1, 0, sumArray, 0, len1)
19+
for (i in sumArray.indices) {
20+
if (i < len2) {
21+
sumArray[i] += reverseArr2[i]
22+
}
23+
if (sumArray[i] > 1) {
24+
sumArray[i] -= 2
25+
sumArray[i + 1]--
26+
} else if (sumArray[i] == -1) {
27+
sumArray[i] = 1
28+
sumArray[i + 1]++
29+
}
30+
}
31+
var resultLen = sumArray.size
32+
for (i in sumArray.indices.reversed()) {
33+
if (sumArray[i] == 0) {
34+
resultLen--
35+
} else {
36+
break
37+
}
38+
}
39+
if (resultLen == 0) {
40+
return intArrayOf(0)
41+
}
42+
val result = IntArray(resultLen)
43+
for (i in resultLen - 1 downTo 0) {
44+
result[resultLen - i - 1] = sumArray[i]
45+
}
46+
return result
47+
}
48+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
1073\. Adding Two Negabinary Numbers
2+
3+
Medium
4+
5+
Given two numbers `arr1` and `arr2` in base **\-2**, return the result of adding them together.
6+
7+
Each number is given in _array format_: as an array of 0s and 1s, from most significant bit to least significant bit. For example, `arr = [1,1,0,1]` represents the number `(-2)^3 + (-2)^2 + (-2)^0 = -3`. A number `arr` in _array, format_ is also guaranteed to have no leading zeros: either `arr == [0]` or `arr[0] == 1`.
8+
9+
Return the result of adding `arr1` and `arr2` in the same format: as an array of 0s and 1s with no leading zeros.
10+
11+
**Example 1:**
12+
13+
**Input:** arr1 = [1,1,1,1,1], arr2 = [1,0,1]
14+
15+
**Output:** [1,0,0,0,0]
16+
17+
**Explanation:** arr1 represents 11, arr2 represents 5, the output represents 16.
18+
19+
**Example 2:**
20+
21+
**Input:** arr1 = [0], arr2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** arr1 = [0], arr2 = [1]
28+
29+
**Output:** [1]
30+
31+
**Constraints:**
32+
33+
* `1 <= arr1.length, arr2.length <= 1000`
34+
* `arr1[i]` and `arr2[i]` are `0` or `1`
35+
* `arr1` and `arr2` have no leading zeros
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package g1001_1100.s1070_product_sales_analysis_iii
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 Sales(sale_id INTEGER, product_id INTEGER," +
20+
" sale_year INTEGER, quantity INTEGER, price INTEGER); " +
21+
"INSERT INTO Sales(sale_id, product_id, sale_year, quantity, price)" +
22+
" VALUES (1, 100, 2008, 10, 5000); " +
23+
"INSERT INTO Sales(sale_id, product_id, sale_year, quantity, price)" +
24+
" VALUES (2, 100, 2009, 12, 5000); " +
25+
"INSERT INTO Sales(sale_id, product_id, sale_year, quantity, price)" +
26+
" VALUES (7, 200, 2011, 15, 9000); " +
27+
"CREATE TABLE Product(product_id INTEGER, product_name VARCHAR); " +
28+
"INSERT INTO Product(product_id, product_name)" +
29+
" VALUES (100, 'Nokia'); " +
30+
"INSERT INTO Product(product_id, product_name)" +
31+
" VALUES (200, 'Apple'); " +
32+
"INSERT INTO Product(product_id, product_name)" +
33+
" VALUES (300, 'Samsung'); "
34+
]
35+
)
36+
internal class MysqlTest {
37+
@Test
38+
@Throws(SQLException::class, FileNotFoundException::class)
39+
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
40+
dataSource.connection.use { connection ->
41+
connection.createStatement().use { statement ->
42+
statement.executeQuery(
43+
BufferedReader(
44+
FileReader(
45+
"src/main/kotlin/g1001_1100/s1070_product_sales_analysis_iii" +
46+
"/script.sql"
47+
)
48+
)
49+
.lines()
50+
.collect(Collectors.joining("\n"))
51+
.replace("#.*?\\r?\\n".toRegex(), "")
52+
).use { resultSet ->
53+
assertThat(resultSet.next(), equalTo(true))
54+
assertThat(resultSet.getInt(1), equalTo(100))
55+
assertThat(resultSet.getInt(2), equalTo(2008))
56+
assertThat(resultSet.getInt(3), equalTo(10))
57+
assertThat(resultSet.getInt(4), equalTo(5000))
58+
assertThat(resultSet.next(), equalTo(true))
59+
assertThat(resultSet.getInt(1), equalTo(200))
60+
assertThat(resultSet.getInt(2), equalTo(2011))
61+
assertThat(resultSet.getInt(3), equalTo(15))
62+
assertThat(resultSet.getInt(4), equalTo(9000))
63+
assertThat(resultSet.next(), equalTo(false))
64+
}
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)