Skip to content

Commit 95fa223

Browse files
authored
Added tasks 1074, 1075, 1078, 1079
1 parent eeca2a8 commit 95fa223

File tree

13 files changed

+1178
-769
lines changed

13 files changed

+1178
-769
lines changed

README.md

Lines changed: 773 additions & 769 deletions
Large diffs are not rendered by default.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g1001_1100.s1074_number_of_submatrices_that_sum_to_target
2+
3+
// #Hard #Array #Hash_Table #Matrix #Prefix_Sum
4+
// #2023_06_01_Time_770_ms_(100.00%)_Space_115.7_MB_(100.00%)
5+
6+
class Solution {
7+
fun numSubmatrixSumTarget(matrix: Array<IntArray>, target: Int): Int {
8+
val rows = matrix.size
9+
val columns = matrix[0].size
10+
for (i in 0 until rows) {
11+
for (j in 1 until columns) {
12+
matrix[i][j] += matrix[i][j - 1]
13+
}
14+
}
15+
val sumMap: HashMap<Int, Int> = HashMap()
16+
var cur: Int
17+
var res = 0
18+
for (i in 0 until columns) {
19+
for (j in i until columns) {
20+
sumMap.clear()
21+
sumMap[0] = 1
22+
cur = 0
23+
for (ints in matrix) {
24+
cur += ints[j] - if (i > 0) ints[i - 1] else 0
25+
res += sumMap.getOrDefault(cur - target, 0)
26+
sumMap[cur] = sumMap.getOrDefault(cur, 0) + 1
27+
}
28+
}
29+
}
30+
return res
31+
}
32+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1074\. Number of Submatrices That Sum to Target
2+
3+
Hard
4+
5+
Given a `matrix` and a `target`, return the number of non-empty submatrices that sum to target.
6+
7+
A submatrix `x1, y1, x2, y2` is the set of all cells `matrix[x][y]` with `x1 <= x <= x2` and `y1 <= y <= y2`.
8+
9+
Two submatrices `(x1, y1, x2, y2)` and `(x1', y1', x2', y2')` are different if they have some coordinate that is different: for example, if `x1 != x1'`.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg)
14+
15+
**Input:** matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
16+
17+
**Output:** 4
18+
19+
**Explanation:** The four 1x1 submatrices that only contain 0.
20+
21+
**Example 2:**
22+
23+
**Input:** matrix = [[1,-1],[-1,1]], target = 0
24+
25+
**Output:** 5
26+
27+
**Explanation:** The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.
28+
29+
**Example 3:**
30+
31+
**Input:** matrix = [[904]], target = 0
32+
33+
**Output:** 0
34+
35+
**Constraints:**
36+
37+
* `1 <= matrix.length <= 100`
38+
* `1 <= matrix[0].length <= 100`
39+
* `-1000 <= matrix[i] <= 1000`
40+
* `-10^8 <= target <= 10^8`
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
1075\. Project Employees I
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Project`
8+
9+
+-------------+---------+
10+
| Column Name | Type |
11+
+-------------+---------+
12+
| project_id | int |
13+
| employee_id | int |
14+
+-------------+---------+
15+
16+
(project_id, employee_id) is the primary key of this table.
17+
18+
employee_id is a foreign key to `Employee` table.
19+
20+
Each row of this table indicates that the employee with employee_id is working on the project with project_id.
21+
22+
Table: `Employee`
23+
24+
+------------------+---------+
25+
| Column Name | Type |
26+
+------------------+---------+
27+
| employee_id | int |
28+
| name | varchar |
29+
| experience_years | int |
30+
+------------------+---------+
31+
32+
employee_id is the primary key of this table. It's guaranteed that experience_years is not NULL.
33+
34+
Each row of this table contains information about one employee.
35+
36+
Write an SQL query that reports the **average** experience years of all the employees for each project, **rounded to 2 digits**.
37+
38+
Return the result table in **any order**.
39+
40+
The query result format is in the following example.
41+
42+
**Example 1:**
43+
44+
**Input:** Project table:
45+
46+
+-------------+-------------+
47+
| project_id | employee_id |
48+
+-------------+-------------+
49+
| 1 | 1 |
50+
| 1 | 2 |
51+
| 1 | 3 |
52+
| 2 | 1 |
53+
| 2 | 4 |
54+
+-------------+-------------+
55+
56+
Employee table:
57+
58+
+-------------+--------+------------------+
59+
| employee_id | name | experience_years |
60+
+-------------+--------+------------------+
61+
| 1 | Khaled | 3 |
62+
| 2 | Ali | 2 |
63+
| 3 | John | 1 |
64+
| 4 | Doe | 2 |
65+
+-------------+--------+------------------+
66+
67+
**Output:**
68+
69+
+-------------+---------------+
70+
| project_id | average_years |
71+
+-------------+---------------+
72+
| 1 | 2.00 |
73+
| 2 | 2.50 |
74+
+-------------+---------------+
75+
76+
**Explanation:** The average experience years for the first project is (3 + 2 + 1) / 3 = 2.00 and for the second project is (3 + 2) / 2 = 2.50
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #2023_06_01_Time_1268_ms_(94.71%)_Space_0B_(100.00%)
3+
select project_id,round(avg(
4+
experience_years
5+
),2) as average_years from Project p
6+
left join
7+
Employee e on p.employee_id=e.employee_id group by p.project_id;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g1001_1100.s1078_occurrences_after_bigram
2+
3+
// #Easy #String #2023_06_01_Time_151_ms_(88.24%)_Space_36.5_MB_(70.59%)
4+
5+
class Solution {
6+
fun findOcurrences(text: String, first: String, second: String): Array<String?> {
7+
val list: MutableList<String> = ArrayList()
8+
val str = text.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
9+
for (i in str.indices) {
10+
if (str[i] == first && str.size - 1 >= i + 2 && str[i + 1] == second) {
11+
list.add(str[i + 2])
12+
}
13+
}
14+
val s = arrayOfNulls<String>(list.size)
15+
var j = 0
16+
for (ele in list) {
17+
s[j++] = ele
18+
}
19+
return s
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
1078\. Occurrences After Bigram
2+
3+
Easy
4+
5+
Given two strings `first` and `second`, consider occurrences in some text of the form `"first second third"`, where `second` comes immediately after `first`, and `third` comes immediately after `second`.
6+
7+
Return _an array of all the words_ `third` _for each occurrence of_ `"first second third"`.
8+
9+
**Example 1:**
10+
11+
**Input:** text = "alice is a good girl she is a good student", first = "a", second = "good"
12+
13+
**Output:** ["girl","student"]
14+
15+
**Example 2:**
16+
17+
**Input:** text = "we will we will rock you", first = "we", second = "will"
18+
19+
**Output:** ["we","rock"]
20+
21+
**Constraints:**
22+
23+
* `1 <= text.length <= 1000`
24+
* `text` consists of lowercase English letters and spaces.
25+
* All the words in `text` a separated by **a single space**.
26+
* `1 <= first.length, second.length <= 10`
27+
* `first` and `second` consist of lowercase English letters.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g1001_1100.s1079_letter_tile_possibilities
2+
3+
// #Medium #String #Backtracking #2023_06_01_Time_144_ms_(100.00%)_Space_34.2_MB_(100.00%)
4+
5+
class Solution {
6+
private var count = 0
7+
8+
fun numTilePossibilities(tiles: String): Int {
9+
count = 0
10+
val chars = tiles.toCharArray()
11+
chars.sort()
12+
val visited = BooleanArray(chars.size)
13+
dfs(chars, 0, visited)
14+
return count
15+
}
16+
17+
private fun dfs(chars: CharArray, length: Int, visited: BooleanArray) {
18+
if (length == chars.size) {
19+
return
20+
}
21+
for (i in chars.indices) {
22+
if (visited[i] || i - 1 >= 0 && chars[i] == chars[i - 1] && !visited[i - 1]) {
23+
continue
24+
}
25+
count++
26+
visited[i] = true
27+
dfs(chars, length + 1, visited)
28+
visited[i] = false
29+
}
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
1079\. Letter Tile Possibilities
2+
3+
Medium
4+
5+
You have `n` `tiles`, where each tile has one letter `tiles[i]` printed on it.
6+
7+
Return _the number of possible non-empty sequences of letters_ you can make using the letters printed on those `tiles`.
8+
9+
**Example 1:**
10+
11+
**Input:** tiles = "AAB"
12+
13+
**Output:** 8
14+
15+
**Explanation:** The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
16+
17+
**Example 2:**
18+
19+
**Input:** tiles = "AAABBC"
20+
21+
**Output:** 188
22+
23+
**Example 3:**
24+
25+
**Input:** tiles = "V"
26+
27+
**Output:** 1
28+
29+
**Constraints:**
30+
31+
* `1 <= tiles.length <= 7`
32+
* `tiles` consists of uppercase English letters.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g1001_1100.s1074_number_of_submatrices_that_sum_to_target
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 numSubmatrixSumTarget() {
10+
assertThat(
11+
Solution()
12+
.numSubmatrixSumTarget(arrayOf(intArrayOf(0, 1, 0), intArrayOf(1, 1, 1), intArrayOf(0, 1, 0)), 0),
13+
equalTo(4)
14+
)
15+
}
16+
17+
@Test
18+
fun numSubmatrixSumTarget2() {
19+
assertThat(
20+
Solution().numSubmatrixSumTarget(arrayOf(intArrayOf(1, -1), intArrayOf(-1, 1)), 0),
21+
equalTo(5)
22+
)
23+
}
24+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g1001_1100.s1075_project_employees_i
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 Project(project_id INTEGER, employee_id INTEGER); " +
20+
"INSERT INTO Project(project_id, employee_id)" +
21+
" VALUES (1, 1); " +
22+
"INSERT INTO Project(project_id, employee_id)" +
23+
" VALUES (1, 2); " +
24+
"INSERT INTO Project(project_id, employee_id)" +
25+
" VALUES (1, 3); " +
26+
"INSERT INTO Project(project_id, employee_id)" +
27+
" VALUES (2, 1); " +
28+
"INSERT INTO Project(project_id, employee_id)" +
29+
" VALUES (2, 4); " +
30+
"CREATE TABLE Employee(employee_id INTEGER, name VARCHAR, experience_years INTEGER); " +
31+
"INSERT INTO Employee(employee_id, name, experience_years)" +
32+
" VALUES (1, 'Khaled', 3); " +
33+
"INSERT INTO Employee(employee_id, name, experience_years)" +
34+
" VALUES (2, 'Ali', 2); " +
35+
"INSERT INTO Employee(employee_id, name, experience_years)" +
36+
" VALUES (3, 'John', 1); " +
37+
"INSERT INTO Employee(employee_id, name, experience_years)" +
38+
" VALUES (4, 'Doe', 2); "
39+
]
40+
)
41+
internal class MysqlTest {
42+
@Test
43+
@Throws(SQLException::class, FileNotFoundException::class)
44+
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
45+
dataSource.connection.use { connection ->
46+
connection.createStatement().use { statement ->
47+
statement.executeQuery(
48+
BufferedReader(
49+
FileReader(
50+
"src/main/kotlin/g1001_1100/s1075_project_employees_i" +
51+
"/script.sql"
52+
)
53+
)
54+
.lines()
55+
.collect(Collectors.joining("\n"))
56+
.replace("#.*?\\r?\\n".toRegex(), "")
57+
).use { resultSet ->
58+
assertThat(resultSet.next(), equalTo(true))
59+
assertThat(resultSet.getInt(1), equalTo(1))
60+
assertThat(resultSet.getDouble(2), equalTo(2.00))
61+
assertThat(resultSet.next(), equalTo(true))
62+
assertThat(resultSet.getInt(1), equalTo(2))
63+
assertThat(resultSet.getInt(2), equalTo(3))
64+
assertThat(resultSet.next(), equalTo(false))
65+
}
66+
}
67+
}
68+
}
69+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g1001_1100.s1078_occurrences_after_bigram
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 findOcurrences() {
10+
assertThat(
11+
Solution()
12+
.findOcurrences("alice is a good girl she is a good student", "a", "good"),
13+
equalTo(arrayOf("girl", "student"))
14+
)
15+
}
16+
17+
@Test
18+
fun findOcurrences2() {
19+
assertThat(
20+
Solution().findOcurrences("we will we will rock you", "we", "will"),
21+
equalTo(arrayOf("we", "rock"))
22+
)
23+
}
24+
}

0 commit comments

Comments
 (0)