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
1,573 changes: 808 additions & 765 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
[![](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)

## 1070\. Product Sales Analysis III

Medium

SQL Schema

Table: `Sales`

+-------------+-------+
| Column Name | Type |
+-------------+-------+
| sale_id | int |
| product_id | int |
| year | int |
| quantity | int |
| price | int |
+-------------+-------+

(sale_id, year) is the primary key of this table. product_id is a foreign key to `Product` table.

Each row of this table shows a sale on the product product_id in a certain year.

Note that the price is per unit.

Table: `Product`

+--------------+---------+
| Column Name | Type |
+--------------+---------+
| product_id | int |
| product_name | varchar |
+--------------+---------+

product_id is the primary key of this table.

Each row of this table indicates the product name of each product.

Write an SQL query that selects the **product id**, **year**, **quantity**, and **price** for the **first year** of every product sold.

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

The query result format is in the following example.

**Example 1:**

**Input:** Sales table:

+---------+------------+------+----------+-------+
| sale_id | product_id | year | quantity | price |
+---------+------------+------+----------+-------+
| 1 | 100 | 2008 | 10 | 5000 |
| 2 | 100 | 2009 | 12 | 5000 |
| 7 | 200 | 2011 | 15 | 9000 |
+---------+------------+------+----------+-------+

Product table:

+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 100 | Nokia |
| 200 | Apple |
| 300 | Samsung |
+------------+--------------+

**Output:**

+------------+------------+----------+-------+
| product_id | first_year | quantity | price |
+------------+------------+----------+-------+
| 100 | 2008 | 10 | 5000 |
| 200 | 2011 | 15 | 9000 |
+------------+------------+----------+-------+

## Solution

```sql
# Write your MySQL query statement below
SELECT
a.product_id, sale_year AS first_year, quantity, price
FROM
(
SELECT
*,
RANK() OVER(PARTITION BY product_id ORDER BY sale_year) as rk
FROM
Sales
) AS a
LEFT JOIN
Product AS p
ON
a.product_id = p.product_id
WHERE
rk = 1
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[![](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)

## 1071\. Greatest Common Divisor of Strings

Easy

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).

Given two strings `str1` and `str2`, return _the largest string_ `x` _such that_ `x` _divides both_ `str1` _and_ `str2`.

**Example 1:**

**Input:** str1 = "ABCABC", str2 = "ABC"

**Output:** "ABC"

**Example 2:**

**Input:** str1 = "ABABAB", str2 = "ABAB"

**Output:** "AB"

**Example 3:**

**Input:** str1 = "LEET", str2 = "CODE"

**Output:** ""

**Constraints:**

* `1 <= str1.length, str2.length <= 1000`
* `str1` and `str2` consist of English uppercase letters.

## Solution

```kotlin
class Solution {
fun gcdOfStrings(str1: String?, str2: String?): String {
if (str1 == null || str2 == null) {
return ""
}
if (str1 == str2) {
return str1
}
val m = str1.length
val n = str2.length
if (m > n && str1.substring(0, n) == str2) {
return gcdOfStrings(str1.substring(n), str2)
}
return if (n > m && str2.substring(0, m) == str1) {
gcdOfStrings(str2.substring(m), str1)
} else ""
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[![](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)

## 1072\. Flip Columns For Maximum Number of Equal Rows

Medium

You are given an `m x n` binary matrix `matrix`.

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).

Return _the maximum number of rows that have all values equal after some number of flips_.

**Example 1:**

**Input:** matrix = \[\[0,1],[1,1]]

**Output:** 1

**Explanation:** After flipping no values, 1 row has all values equal.

**Example 2:**

**Input:** matrix = \[\[0,1],[1,0]]

**Output:** 2

**Explanation:** After flipping values in the first column, both rows have equal values.

**Example 3:**

**Input:** matrix = \[\[0,0,0],[0,0,1],[1,1,0]]

**Output:** 2

**Explanation:** After flipping values in the first two columns, the last two rows have equal values.

**Constraints:**

* `m == matrix.length`
* `n == matrix[i].length`
* `1 <= m, n <= 300`
* `matrix[i][j]` is either `0` or `1`.

## Solution

```kotlin
class Solution {
fun maxEqualRowsAfterFlips(matrix: Array<IntArray>): Int {
/*
Idea:
For a given row[i], 0<=i<m, row[j], 0<=j<m and j!=i, if either of them can have
all values equal after some number of flips, then
row[i]==row[j] <1> or
row[i]^row[j] == 111...111 <2> (xor result is a row full of '1')

Go further, in case<2> row[j] can turn to row[i] by flipping each column of row[j]
IF assume row[i][0] is 0, then question is convert into:
1> flipping each column of each row if row[i][0] is not '0',
2> count the frequency of each row.
The biggest number of frequencies is the answer.
*/

// O(M*N), int M = matrix.length, N = matrix[0].length;
var answer = 0
val frequency: MutableMap<String, Int> = HashMap()
for (row in matrix) {
val rowStr = StringBuilder()
for (c in row) {
if (row[0] == 1) {
rowStr.append(if (c == 1) 0 else 1)
} else {
rowStr.append(c)
}
}
val key = rowStr.toString()
val value = frequency.getOrDefault(key, 0) + 1
frequency[key] = value
answer = answer.coerceAtLeast(value)
}
return answer
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
[![](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)

## 1073\. Adding Two Negabinary Numbers

Medium

Given two numbers `arr1` and `arr2` in base **\-2**, return the result of adding them together.

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`.

Return the result of adding `arr1` and `arr2` in the same format: as an array of 0s and 1s with no leading zeros.

**Example 1:**

**Input:** arr1 = [1,1,1,1,1], arr2 = [1,0,1]

**Output:** [1,0,0,0,0]

**Explanation:** arr1 represents 11, arr2 represents 5, the output represents 16.

**Example 2:**

**Input:** arr1 = [0], arr2 = [0]

**Output:** [0]

**Example 3:**

**Input:** arr1 = [0], arr2 = [1]

**Output:** [1]

**Constraints:**

* `1 <= arr1.length, arr2.length <= 1000`
* `arr1[i]` and `arr2[i]` are `0` or `1`
* `arr1` and `arr2` have no leading zeros

## Solution

```kotlin
class Solution {
fun addNegabinary(arr1: IntArray, arr2: IntArray): IntArray {
val len1 = arr1.size
val len2 = arr2.size
val reverseArr1 = IntArray(len1)
for (i in len1 - 1 downTo 0) {
reverseArr1[len1 - i - 1] = arr1[i]
}
val reverseArr2 = IntArray(len2)
for (i in len2 - 1 downTo 0) {
reverseArr2[len2 - i - 1] = arr2[i]
}
val sumArray = IntArray(len1.coerceAtLeast(len2) + 2)
System.arraycopy(reverseArr1, 0, sumArray, 0, len1)
for (i in sumArray.indices) {
if (i < len2) {
sumArray[i] += reverseArr2[i]
}
if (sumArray[i] > 1) {
sumArray[i] -= 2
sumArray[i + 1]--
} else if (sumArray[i] == -1) {
sumArray[i] = 1
sumArray[i + 1]++
}
}
var resultLen = sumArray.size
for (i in sumArray.indices.reversed()) {
if (sumArray[i] == 0) {
resultLen--
} else {
break
}
}
if (resultLen == 0) {
return intArrayOf(0)
}
val result = IntArray(resultLen)
for (i in resultLen - 1 downTo 0) {
result[resultLen - i - 1] = sumArray[i]
}
return result
}
}
```
Loading