Skip to content

Commit aca1c92

Browse files
authored
Added tasks 546, 547, 551, 552
1 parent f2cf9d5 commit aca1c92

File tree

13 files changed

+427
-0
lines changed

13 files changed

+427
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
293293

294294
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
295295
|-|-|-|-|-|-
296+
| 0547 |[Number of Provinces](src/main/kotlin/g0501_0600/s0547_number_of_provinces/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 229 | 79.73
296297

297298
#### Day 9 Standard Traversal
298299

@@ -615,6 +616,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
615616

616617
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
617618
|-|-|-|-|-|-
619+
| 0547 |[Number of Provinces](src/main/kotlin/g0501_0600/s0547_number_of_provinces/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 229 | 79.73
618620

619621
#### Day 20 Brute Force/Backtracking
620622

@@ -1205,6 +1207,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
12051207
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12061208
|-|-|-|-|-|-
12071209
| 0200 |[Number of Islands](src.save/main/kotlin/g0101_0200/s0200_number_of_islands/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 252 | 95.41
1210+
| 0547 |[Number of Provinces](src/main/kotlin/g0501_0600/s0547_number_of_provinces/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 229 | 79.73
12081211

12091212
#### Day 7 Breadth First Search Depth First Search
12101213

@@ -1651,6 +1654,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16511654
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
16521655
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16531656
| 0560 |[Subarray Sum Equals K](src/main/kotlin/g0501_0600/s0560_subarray_sum_equals_k/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 692 | 53.27
1657+
| 0552 |[Student Attendance Record II](src/main/kotlin/g0501_0600/s0552_student_attendance_record_ii/Solution.kt)| Hard | Dynamic_Programming | 151 | 100.00
1658+
| 0551 |[Student Attendance Record I](src/main/kotlin/g0501_0600/s0551_student_attendance_record_i/Solution.kt)| Easy | String | 151 | 95.00
1659+
| 0547 |[Number of Provinces](src/main/kotlin/g0501_0600/s0547_number_of_provinces/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_8_Standard_Traversal, Level_2_Day_19_Union_Find | 229 | 79.73
1660+
| 0546 |[Remove Boxes](src/main/kotlin/g0501_0600/s0546_remove_boxes/Solution.kt)| Hard | Array, Dynamic_Programming, Memoization | 283 | 100.00
16541661
| 0543 |[Diameter of Binary Tree](src/main/kotlin/g0501_0600/s0543_diameter_of_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue | 307 | 43.93
16551662
| 0542 |[01 Matrix](src/main/kotlin/g0501_0600/s0542_01_matrix/Solution.kt)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_5_Matrix_Related_Problems | 441 | 94.06
16561663
| 0541 |[Reverse String II](src/main/kotlin/g0501_0600/s0541_reverse_string_ii/Solution.kt)| Easy | String, Two_Pointers | 200 | 83.33
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g0501_0600.s0546_remove_boxes
2+
3+
// #Hard #Array #Dynamic_Programming #Memoization
4+
// #2023_01_17_Time_283_ms_(100.00%)_Space_46.9_MB_(100.00%)
5+
6+
internal class Solution {
7+
// dp for memoization
8+
lateinit var dp: Array<Array<IntArray>>
9+
fun removeBoxes(boxes: IntArray): Int {
10+
val n = boxes.size
11+
dp = Array(n + 1) {
12+
Array(n + 1) {
13+
IntArray(
14+
n + 1
15+
)
16+
}
17+
}
18+
return get(boxes, 0, boxes.size - 1, 0)
19+
}
20+
21+
operator fun get(boxes: IntArray, i: Int, j: Int, streak: Int): Int {
22+
var i = i
23+
var streak = streak
24+
if (i > j) {
25+
return 0
26+
}
27+
28+
// first we traverse till the adjacent values are different
29+
while (i + 1 <= j && boxes[i] == boxes[i + 1]) {
30+
i++
31+
streak++
32+
}
33+
34+
// memoization
35+
if (dp[i][j][streak] > 0) {
36+
return dp[i][j][streak]
37+
}
38+
39+
// we calculate the ans here which is streak (length of similar elements) and move
40+
// forward to the remaining block through recursion
41+
var ans = (streak + 1) * (streak + 1) + get(boxes, i + 1, j, 0)
42+
// also another way we can choose is to choose the inner elements first then the outer similar elements can be combined to get even
43+
// larger value
44+
for (k in i + 1..j) {
45+
// we traverse from k (i has moved from 0 to just before the beginning of different elements) and keep searching for same value as
46+
// in i. after that the middle elements (between i+1 and k-1) are sent to differnt partition and from k to j(ending) we send the updated streak
47+
if (boxes[i] == boxes[k]) {
48+
ans = Math.max(ans, get(boxes, i + 1, k - 1, 0) + get(boxes, k, j, streak + 1))
49+
}
50+
}
51+
// return ans here
52+
return ans.also { dp[i][j][streak] = it }
53+
}
54+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
546\. Remove Boxes
2+
3+
Hard
4+
5+
You are given several `boxes` with different colors represented by different positive numbers.
6+
7+
You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (i.e., composed of `k` boxes, `k >= 1`), remove them and get `k * k` points.
8+
9+
Return _the maximum points you can get_.
10+
11+
**Example 1:**
12+
13+
**Input:** boxes = [1,3,2,2,2,3,4,3,1]
14+
15+
**Output:** 23
16+
17+
**Explanation:** [1, 3, 2, 2, 2, 3, 4, 3, 1] ----> [1, 3, 3, 4, 3, 1] (3\*3=9 points) ----> [1, 3, 3, 3, 1] (1\*1=1 points) ----> [1, 1] (3\*3=9 points) ----> [] (2\*2=4 points)
18+
19+
**Example 2:**
20+
21+
**Input:** boxes = [1,1,1]
22+
23+
**Output:** 9
24+
25+
**Example 3:**
26+
27+
**Input:** boxes = [1]
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* `1 <= boxes.length <= 100`
34+
* `1 <= boxes[i] <= 100`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g0501_0600.s0547_number_of_provinces
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
4+
// #Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search
5+
// #Graph_Theory_I_Day_8_Standard_Traversal #Level_2_Day_19_Union_Find
6+
// #2023_01_17_Time_229_ms_(79.73%)_Space_43_MB_(66.22%)
7+
8+
import java.util.Arrays
9+
10+
class Solution {
11+
fun findCircleNum(arr: Array<IntArray>): Int {
12+
val parent = IntArray(arr.size)
13+
Arrays.fill(parent, -1)
14+
var ans = 0
15+
for (i in 0 until arr.size - 1) {
16+
for (j in i + 1 until arr[i].size) {
17+
if (arr[i][j] == 1) {
18+
ans += union(i, j, parent)
19+
}
20+
}
21+
}
22+
return arr.size - ans
23+
}
24+
25+
private fun union(a: Int, b: Int, arr: IntArray): Int {
26+
val ga = find(a, arr)
27+
val gb = find(b, arr)
28+
if (ga != gb) {
29+
arr[gb] = ga
30+
return 1
31+
}
32+
return 0
33+
}
34+
35+
private fun find(a: Int, arr: IntArray): Int {
36+
if (arr[a] == -1) {
37+
return a
38+
}
39+
arr[a] = find(arr[a], arr)
40+
return arr[a]
41+
}
42+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
547\. Number of Provinces
2+
3+
Medium
4+
5+
There are `n` cities. Some of them are connected, while some are not. If city `a` is connected directly with city `b`, and city `b` is connected directly with city `c`, then city `a` is connected indirectly with city `c`.
6+
7+
A **province** is a group of directly or indirectly connected cities and no other cities outside of the group.
8+
9+
You are given an `n x n` matrix `isConnected` where `isConnected[i][j] = 1` if the <code>i<sup>th</sup></code> city and the <code>j<sup>th</sup></code> city are directly connected, and `isConnected[i][j] = 0` otherwise.
10+
11+
Return _the total number of **provinces**_.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg)
16+
17+
**Input:** isConnected = [[1,1,0],[1,1,0],[0,0,1]]
18+
19+
**Output:** 2
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg)
24+
25+
**Input:** isConnected = [[1,0,0],[0,1,0],[0,0,1]]
26+
27+
**Output:** 3
28+
29+
**Constraints:**
30+
31+
* `1 <= n <= 200`
32+
* `n == isConnected.length`
33+
* `n == isConnected[i].length`
34+
* `isConnected[i][j]` is `1` or `0`.
35+
* `isConnected[i][i] == 1`
36+
* `isConnected[i][j] == isConnected[j][i]`
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0501_0600.s0551_student_attendance_record_i
2+
3+
// #Easy #String #2023_01_17_Time_151_ms_(95.00%)_Space_33.6_MB_(100.00%)
4+
5+
class Solution {
6+
fun checkRecord(s: String): Boolean {
7+
var aCount = 0
8+
var i = 0
9+
while (i < s.length) {
10+
if (s[i] == 'A') {
11+
aCount++
12+
if (aCount > 1) {
13+
return false
14+
}
15+
} else if (s[i] == 'L') {
16+
var continuousLCount = 0
17+
while (i < s.length && s[i] == 'L') {
18+
i++
19+
continuousLCount++
20+
if (continuousLCount > 2) {
21+
return false
22+
}
23+
}
24+
i--
25+
}
26+
i++
27+
}
28+
return true
29+
}
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
551\. Student Attendance Record I
2+
3+
Easy
4+
5+
You are given a string `s` representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:
6+
7+
* `'A'`: Absent.
8+
* `'L'`: Late.
9+
* `'P'`: Present.
10+
11+
The student is eligible for an attendance award if they meet **both** of the following criteria:
12+
13+
* The student was absent (`'A'`) for **strictly** fewer than 2 days **total**.
14+
* The student was **never** late (`'L'`) for 3 or more **consecutive** days.
15+
16+
Return `true` _if the student is eligible for an attendance award, or_ `false` _otherwise_.
17+
18+
**Example 1:**
19+
20+
**Input:** s = "PPALLP"
21+
22+
**Output:** true
23+
24+
**Explanation:** The student has fewer than 2 absences and was never late 3 or more consecutive days.
25+
26+
**Example 2:**
27+
28+
**Input:** s = "PPALLL"
29+
30+
**Output:** false
31+
32+
**Explanation:** The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.
33+
34+
**Constraints:**
35+
36+
* `1 <= s.length <= 1000`
37+
* `s[i]` is either `'A'`, `'L'`, or `'P'`.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g0501_0600.s0552_student_attendance_record_ii
2+
3+
// #Hard #Dynamic_Programming #2023_01_17_Time_151_ms_(100.00%)_Space_33.3_MB_(100.00%)
4+
5+
import java.util.Arrays
6+
7+
class Solution {
8+
fun checkRecord(n: Int): Int {
9+
if (n == 0 || n == 1 || n == 2) {
10+
return n * 3 + n * (n - 1)
11+
}
12+
val mod: Long = 1000000007
13+
val matrix = arrayOf(
14+
longArrayOf(1, 1, 0, 1, 0, 0),
15+
longArrayOf(1, 0, 1, 1, 0, 0),
16+
longArrayOf(1, 0, 0, 1, 0, 0),
17+
longArrayOf(0, 0, 0, 1, 1, 0),
18+
longArrayOf(0, 0, 0, 1, 0, 1),
19+
longArrayOf(0, 0, 0, 1, 0, 0)
20+
)
21+
val e = quickPower(matrix, n - 1)
22+
return (
23+
(Arrays.stream(e[0]).sum() + Arrays.stream(e[1]).sum() + Arrays.stream(e[3]).sum()) %
24+
mod
25+
).toInt()
26+
}
27+
28+
private fun quickPower(a: Array<LongArray>, times: Int): Array<LongArray> {
29+
var a = a
30+
var times = times
31+
val n = a.size
32+
var e = Array(n) { LongArray(n) }
33+
for (i in 0 until n) {
34+
e[i][i] = 1
35+
}
36+
while (times != 0) {
37+
if (times % 2 == 1) {
38+
e = multiple(e, a, n)
39+
}
40+
times = times shr 1
41+
a = multiple(a, a, n)
42+
}
43+
return e
44+
}
45+
46+
private fun multiple(a: Array<LongArray>, b: Array<LongArray>, n: Int): Array<LongArray> {
47+
val target = Array(n) { LongArray(n) }
48+
for (j in 0 until n) {
49+
for (k in 0 until n) {
50+
for (l in 0 until n) {
51+
target[j][k] += a[j][l] * b[l][k]
52+
target[j][k] = target[j][k] % 1000000007
53+
}
54+
}
55+
}
56+
return target
57+
}
58+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
552\. Student Attendance Record II
2+
3+
Hard
4+
5+
An attendance record for a student can be represented as a string where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:
6+
7+
* `'A'`: Absent.
8+
* `'L'`: Late.
9+
* `'P'`: Present.
10+
11+
Any student is eligible for an attendance award if they meet **both** of the following criteria:
12+
13+
* The student was absent (`'A'`) for **strictly** fewer than 2 days **total**.
14+
* The student was **never** late (`'L'`) for 3 or more **consecutive** days.
15+
16+
Given an integer `n`, return _the **number** of possible attendance records of length_ `n` _that make a student eligible for an attendance award. The answer may be very large, so return it **modulo**_ <code>10<sup>9</sup> + 7</code>.
17+
18+
**Example 1:**
19+
20+
**Input:** n = 2
21+
22+
**Output:** 8
23+
24+
**Explanation:** There are 8 records with length 2 that are eligible for an award: "PP", "AP", "PA", "LP", "PL", "AL", "LA", "LL" Only "AA" is not eligible because there are 2 absences (there need to be fewer than 2).
25+
26+
**Example 2:**
27+
28+
**Input:** n = 1
29+
30+
**Output:** 3
31+
32+
**Example 3:**
33+
34+
**Input:** n = 10101
35+
36+
**Output:** 183236316
37+
38+
**Constraints:**
39+
40+
* <code>1 <= n <= 10<sup>5</sup></code>

0 commit comments

Comments
 (0)