Skip to content

Commit 96100b5

Browse files
authored
Added tasks 1070-1148
1 parent 96a9b85 commit 96100b5

File tree

39 files changed

+3763
-765
lines changed

39 files changed

+3763
-765
lines changed

README.md

Lines changed: 808 additions & 765 deletions
Large diffs are not rendered by default.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 1070\. Product Sales Analysis III
5+
6+
Medium
7+
8+
SQL Schema
9+
10+
Table: `Sales`
11+
12+
+-------------+-------+
13+
| Column Name | Type |
14+
+-------------+-------+
15+
| sale_id | int |
16+
| product_id | int |
17+
| year | int |
18+
| quantity | int |
19+
| price | int |
20+
+-------------+-------+
21+
22+
(sale_id, year) is the primary key of this table. product_id is a foreign key to `Product` table.
23+
24+
Each row of this table shows a sale on the product product_id in a certain year.
25+
26+
Note that the price is per unit.
27+
28+
Table: `Product`
29+
30+
+--------------+---------+
31+
| Column Name | Type |
32+
+--------------+---------+
33+
| product_id | int |
34+
| product_name | varchar |
35+
+--------------+---------+
36+
37+
product_id is the primary key of this table.
38+
39+
Each row of this table indicates the product name of each product.
40+
41+
Write an SQL query that selects the **product id**, **year**, **quantity**, and **price** for the **first year** of every product sold.
42+
43+
Return the resulting table in **any order**.
44+
45+
The query result format is in the following example.
46+
47+
**Example 1:**
48+
49+
**Input:** Sales table:
50+
51+
+---------+------------+------+----------+-------+
52+
| sale_id | product_id | year | quantity | price |
53+
+---------+------------+------+----------+-------+
54+
| 1 | 100 | 2008 | 10 | 5000 |
55+
| 2 | 100 | 2009 | 12 | 5000 |
56+
| 7 | 200 | 2011 | 15 | 9000 |
57+
+---------+------------+------+----------+-------+
58+
59+
Product table:
60+
61+
+------------+--------------+
62+
| product_id | product_name |
63+
+------------+--------------+
64+
| 100 | Nokia |
65+
| 200 | Apple |
66+
| 300 | Samsung |
67+
+------------+--------------+
68+
69+
**Output:**
70+
71+
+------------+------------+----------+-------+
72+
| product_id | first_year | quantity | price |
73+
+------------+------------+----------+-------+
74+
| 100 | 2008 | 10 | 5000 |
75+
| 200 | 2011 | 15 | 9000 |
76+
+------------+------------+----------+-------+
77+
78+
## Solution
79+
80+
```sql
81+
# Write your MySQL query statement below
82+
SELECT
83+
a.product_id, sale_year AS first_year, quantity, price
84+
FROM
85+
(
86+
SELECT
87+
*,
88+
RANK() OVER(PARTITION BY product_id ORDER BY sale_year) as rk
89+
FROM
90+
Sales
91+
) AS a
92+
LEFT JOIN
93+
Product AS p
94+
ON
95+
a.product_id = p.product_id
96+
WHERE
97+
rk = 1
98+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 1071\. Greatest Common Divisor of Strings
5+
6+
Easy
7+
8+
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).
9+
10+
Given two strings `str1` and `str2`, return _the largest string_ `x` _such that_ `x` _divides both_ `str1` _and_ `str2`.
11+
12+
**Example 1:**
13+
14+
**Input:** str1 = "ABCABC", str2 = "ABC"
15+
16+
**Output:** "ABC"
17+
18+
**Example 2:**
19+
20+
**Input:** str1 = "ABABAB", str2 = "ABAB"
21+
22+
**Output:** "AB"
23+
24+
**Example 3:**
25+
26+
**Input:** str1 = "LEET", str2 = "CODE"
27+
28+
**Output:** ""
29+
30+
**Constraints:**
31+
32+
* `1 <= str1.length, str2.length <= 1000`
33+
* `str1` and `str2` consist of English uppercase letters.
34+
35+
## Solution
36+
37+
```kotlin
38+
class Solution {
39+
fun gcdOfStrings(str1: String?, str2: String?): String {
40+
if (str1 == null || str2 == null) {
41+
return ""
42+
}
43+
if (str1 == str2) {
44+
return str1
45+
}
46+
val m = str1.length
47+
val n = str2.length
48+
if (m > n && str1.substring(0, n) == str2) {
49+
return gcdOfStrings(str1.substring(n), str2)
50+
}
51+
return if (n > m && str2.substring(0, m) == str1) {
52+
gcdOfStrings(str2.substring(m), str1)
53+
} else ""
54+
}
55+
}
56+
```
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 1072\. Flip Columns For Maximum Number of Equal Rows
5+
6+
Medium
7+
8+
You are given an `m x n` binary matrix `matrix`.
9+
10+
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).
11+
12+
Return _the maximum number of rows that have all values equal after some number of flips_.
13+
14+
**Example 1:**
15+
16+
**Input:** matrix = \[\[0,1],[1,1]]
17+
18+
**Output:** 1
19+
20+
**Explanation:** After flipping no values, 1 row has all values equal.
21+
22+
**Example 2:**
23+
24+
**Input:** matrix = \[\[0,1],[1,0]]
25+
26+
**Output:** 2
27+
28+
**Explanation:** After flipping values in the first column, both rows have equal values.
29+
30+
**Example 3:**
31+
32+
**Input:** matrix = \[\[0,0,0],[0,0,1],[1,1,0]]
33+
34+
**Output:** 2
35+
36+
**Explanation:** After flipping values in the first two columns, the last two rows have equal values.
37+
38+
**Constraints:**
39+
40+
* `m == matrix.length`
41+
* `n == matrix[i].length`
42+
* `1 <= m, n <= 300`
43+
* `matrix[i][j]` is either `0` or `1`.
44+
45+
## Solution
46+
47+
```kotlin
48+
class Solution {
49+
fun maxEqualRowsAfterFlips(matrix: Array<IntArray>): Int {
50+
/*
51+
Idea:
52+
For a given row[i], 0<=i<m, row[j], 0<=j<m and j!=i, if either of them can have
53+
all values equal after some number of flips, then
54+
row[i]==row[j] <1> or
55+
row[i]^row[j] == 111...111 <2> (xor result is a row full of '1')
56+
57+
Go further, in case<2> row[j] can turn to row[i] by flipping each column of row[j]
58+
IF assume row[i][0] is 0, then question is convert into:
59+
1> flipping each column of each row if row[i][0] is not '0',
60+
2> count the frequency of each row.
61+
The biggest number of frequencies is the answer.
62+
*/
63+
64+
// O(M*N), int M = matrix.length, N = matrix[0].length;
65+
var answer = 0
66+
val frequency: MutableMap<String, Int> = HashMap()
67+
for (row in matrix) {
68+
val rowStr = StringBuilder()
69+
for (c in row) {
70+
if (row[0] == 1) {
71+
rowStr.append(if (c == 1) 0 else 1)
72+
} else {
73+
rowStr.append(c)
74+
}
75+
}
76+
val key = rowStr.toString()
77+
val value = frequency.getOrDefault(key, 0) + 1
78+
frequency[key] = value
79+
answer = answer.coerceAtLeast(value)
80+
}
81+
return answer
82+
}
83+
}
84+
```
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 1073\. Adding Two Negabinary Numbers
5+
6+
Medium
7+
8+
Given two numbers `arr1` and `arr2` in base **\-2**, return the result of adding them together.
9+
10+
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`.
11+
12+
Return the result of adding `arr1` and `arr2` in the same format: as an array of 0s and 1s with no leading zeros.
13+
14+
**Example 1:**
15+
16+
**Input:** arr1 = [1,1,1,1,1], arr2 = [1,0,1]
17+
18+
**Output:** [1,0,0,0,0]
19+
20+
**Explanation:** arr1 represents 11, arr2 represents 5, the output represents 16.
21+
22+
**Example 2:**
23+
24+
**Input:** arr1 = [0], arr2 = [0]
25+
26+
**Output:** [0]
27+
28+
**Example 3:**
29+
30+
**Input:** arr1 = [0], arr2 = [1]
31+
32+
**Output:** [1]
33+
34+
**Constraints:**
35+
36+
* `1 <= arr1.length, arr2.length <= 1000`
37+
* `arr1[i]` and `arr2[i]` are `0` or `1`
38+
* `arr1` and `arr2` have no leading zeros
39+
40+
## Solution
41+
42+
```kotlin
43+
class Solution {
44+
fun addNegabinary(arr1: IntArray, arr2: IntArray): IntArray {
45+
val len1 = arr1.size
46+
val len2 = arr2.size
47+
val reverseArr1 = IntArray(len1)
48+
for (i in len1 - 1 downTo 0) {
49+
reverseArr1[len1 - i - 1] = arr1[i]
50+
}
51+
val reverseArr2 = IntArray(len2)
52+
for (i in len2 - 1 downTo 0) {
53+
reverseArr2[len2 - i - 1] = arr2[i]
54+
}
55+
val sumArray = IntArray(len1.coerceAtLeast(len2) + 2)
56+
System.arraycopy(reverseArr1, 0, sumArray, 0, len1)
57+
for (i in sumArray.indices) {
58+
if (i < len2) {
59+
sumArray[i] += reverseArr2[i]
60+
}
61+
if (sumArray[i] > 1) {
62+
sumArray[i] -= 2
63+
sumArray[i + 1]--
64+
} else if (sumArray[i] == -1) {
65+
sumArray[i] = 1
66+
sumArray[i + 1]++
67+
}
68+
}
69+
var resultLen = sumArray.size
70+
for (i in sumArray.indices.reversed()) {
71+
if (sumArray[i] == 0) {
72+
resultLen--
73+
} else {
74+
break
75+
}
76+
}
77+
if (resultLen == 0) {
78+
return intArrayOf(0)
79+
}
80+
val result = IntArray(resultLen)
81+
for (i in resultLen - 1 downTo 0) {
82+
result[resultLen - i - 1] = sumArray[i]
83+
}
84+
return result
85+
}
86+
}
87+
```

0 commit comments

Comments
 (0)