Skip to content

Commit a850e39

Browse files
authored
Added tasks 721, 722, 724, 725
1 parent cc26b07 commit a850e39

File tree

13 files changed

+560
-0
lines changed

13 files changed

+560
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
331331

332332
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
333333
|-|-|-|-|-|-
334+
| 0724 |[Find Pivot Index](src/main/kotlin/g0701_0800/s0724_find_pivot_index/Solution.kt)| Easy | Array, Prefix_Sum | 255 | 88.92
334335

335336
#### Day 2 String
336337

@@ -1696,6 +1697,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16961697
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys/Solution.kt)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
16971698
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16981699
| 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
1700+
| 0725 |[Split Linked List in Parts](src/main/kotlin/g0701_0800/s0725_split_linked_list_in_parts/Solution.kt)| Medium | Linked_List | 162 | 95.00
1701+
| 0724 |[Find Pivot Index](src/main/kotlin/g0701_0800/s0724_find_pivot_index/Solution.kt)| Easy | Array, Prefix_Sum, Level_1_Day_1_Prefix_Sum | 255 | 88.92
1702+
| 0722 |[Remove Comments](src/main/kotlin/g0701_0800/s0722_remove_comments/Solution.kt)| Medium | Array, String | 164 | 100.00
1703+
| 0721 |[Accounts Merge](src/main/kotlin/g0701_0800/s0721_accounts_merge/Solution.kt)| Medium | Array, String, Depth_First_Search, Breadth_First_Search, Union_Find | 364 | 100.00
16991704
| 0720 |[Longest Word in Dictionary](src/main/kotlin/g0701_0800/s0720_longest_word_in_dictionary/Solution.kt)| Medium | Array, String, Hash_Table, Sorting, Trie | 209 | 100.00
17001705
| 0719 |[Find K-th Smallest Pair Distance](src/main/kotlin/g0701_0800/s0719_find_k_th_smallest_pair_distance/Solution.kt)| Hard | Array, Sorting, Binary_Search, Two_Pointers | 172 | 100.00
17011706
| 0718 |[Maximum Length of Repeated Subarray](src/main/kotlin/g0701_0800/s0718_maximum_length_of_repeated_subarray/Solution.kt)| Medium | Array, Dynamic_Programming, Binary_Search, Sliding_Window, Hash_Function, Rolling_Hash | 270 | 91.43
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package g0701_0800.s0721_accounts_merge
2+
3+
// #Medium #Array #String #Depth_First_Search #Breadth_First_Search #Union_Find
4+
// #2023_02_28_Time_364_ms_(100.00%)_Space_45_MB_(90.91%)
5+
6+
import java.util.TreeSet
7+
8+
class Solution {
9+
class UnionFind(size: Int) {
10+
private var size: Int
11+
private var numComponents: Int
12+
private var rank: IntArray
13+
private var parent: IntArray
14+
init {
15+
require(size >= 0) { "Size <= 0 is not allowed" }
16+
this.size = size
17+
this.numComponents = size
18+
rank = IntArray(size) { 1 }
19+
parent = IntArray(size) { ind -> ind }
20+
}
21+
fun find(vertex: Int): Int {
22+
var root = parent[vertex]
23+
while (root != parent[root]) {
24+
parent[root] = parent[parent[root]]
25+
root = parent[root]
26+
}
27+
return root
28+
}
29+
fun union(vertex1: Int, vertex2: Int) {
30+
val firstRoot = find(vertex1)
31+
val secondRoot = find(vertex2)
32+
if (firstRoot == secondRoot) {
33+
return
34+
}
35+
if (rank[firstRoot] > rank[secondRoot]) {
36+
parent[secondRoot] = firstRoot
37+
rank[firstRoot] += rank[secondRoot]
38+
} else {
39+
parent[firstRoot] = secondRoot
40+
rank[secondRoot] += rank[firstRoot]
41+
}
42+
this.numComponents--
43+
}
44+
fun size(): Int {
45+
return size
46+
}
47+
}
48+
49+
fun accountsMerge(accounts: List<List<String>>): List<List<String>> {
50+
val ownersMap = mutableMapOf<String, Int>()
51+
val unionFind = UnionFind(accounts.size)
52+
for (i in accounts.indices) {
53+
for (j in 1 until accounts[i].size) {
54+
val mail = accounts[i][j]
55+
if (!ownersMap.contains(mail)) {
56+
ownersMap[mail] = i
57+
} else {
58+
val previousAccount = ownersMap[mail]!!
59+
unionFind.union(previousAccount, i)
60+
}
61+
}
62+
}
63+
val groupsMap = mutableMapOf<Int, TreeSet<String>>()
64+
for (ind in accounts.indices) {
65+
val parent = unionFind.find(ind)
66+
groupsMap.putIfAbsent(parent, TreeSet<String>())
67+
groupsMap[parent]!!.addAll(accounts[ind].subList(1, accounts[ind].size))
68+
}
69+
val res = mutableListOf<List<String>>()
70+
groupsMap.forEach { (key, value) ->
71+
val list = mutableListOf<String>()
72+
list.add(accounts[key][0])
73+
for (mail in value) {
74+
list.add(mail)
75+
}
76+
res.add(list)
77+
}
78+
return res
79+
}
80+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
721\. Accounts Merge
2+
3+
Medium
4+
5+
Given a list of `accounts` where each element `accounts[i]` is a list of strings, where the first element `accounts[i][0]` is a name, and the rest of the elements are **emails** representing emails of the account.
6+
7+
Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some common email to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.
8+
9+
After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails **in sorted order**. The accounts themselves can be returned in **any order**.
10+
11+
**Example 1:**
12+
13+
**Input:** accounts =
14+
15+
[
16+
["John","johnsmith@mail.com","john\_newyork@mail.com"],
17+
["John","johnsmith@mail.com","john00@mail.com"],
18+
["Mary","mary@mail.com"],
19+
["John","johnnybravo@mail.com"]
20+
]
21+
22+
**Output:**
23+
24+
[
25+
["John","john00@mail.com","john\_newyork@mail.com","johnsmith@mail.com"],
26+
["Mary","mary@mail.com"],
27+
["John","johnnybravo@mail.com"]
28+
]
29+
30+
**Explanation:** The first and second John's are the same person as they have the common email "johnsmith@mail.com". The third John and Mary are different people as none of their email addresses are used by other accounts. We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'], ['John', 'john00@mail.com', 'john\_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.
31+
32+
**Example 2:**
33+
34+
**Input:** accounts =
35+
36+
[
37+
["Gabe","Gabe0@m.co","Gabe3@m.co","Gabe1@m.co"],
38+
["Kevin","Kevin3@m.co","Kevin5@m.co","Kevin0@m.co"],
39+
["Ethan","Ethan5@m.co","Ethan4@m.co","Ethan0@m.co"],
40+
["Hanzo","Hanzo3@m.co","Hanzo1@m.co","Hanzo0@m.co"],
41+
["Fern","Fern5@m.co","Fern1@m.co","Fern0@m.co"]
42+
]
43+
44+
**Output:**
45+
46+
[
47+
["Ethan","Ethan0@m.co","Ethan4@m.co","Ethan5@m.co"],
48+
["Gabe","Gabe0@m.co","Gabe1@m.co","Gabe3@m.co"],
49+
["Hanzo","Hanzo0@m.co","Hanzo1@m.co","Hanzo3@m.co"],
50+
["Kevin","Kevin0@m.co","Kevin3@m.co","Kevin5@m.co"],
51+
["Fern","Fern0@m.co","Fern1@m.co","Fern5@m.co"]
52+
]
53+
54+
**Constraints:**
55+
56+
* `1 <= accounts.length <= 1000`
57+
* `2 <= accounts[i].length <= 10`
58+
* `1 <= accounts[i][j] <= 30`
59+
* `accounts[i][0]` consists of English letters.
60+
* `accounts[i][j] (for j > 0)` is a valid email.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g0701_0800.s0722_remove_comments
2+
3+
// #Medium #Array #String #2023_02_28_Time_164_ms_(100.00%)_Space_35.4_MB_(100.00%)
4+
5+
class Solution {
6+
fun removeComments(source: Array<String>): List<String> {
7+
val result: MutableList<String> = ArrayList()
8+
val sb = StringBuilder()
9+
var multiComment = false
10+
for (line in source) {
11+
val n = line.length
12+
var index = 0
13+
while (index < n) {
14+
val ch = line[index]
15+
if (!multiComment && ch == '/') {
16+
index++
17+
if (index >= n) {
18+
sb.append(ch)
19+
continue
20+
}
21+
if (line[index] == '/') {
22+
break
23+
} else if (line[index] == '*') {
24+
multiComment = true
25+
} else {
26+
sb.append(ch).append(line[index])
27+
}
28+
} else if (multiComment && ch == '*') {
29+
index++
30+
if (index >= n) {
31+
continue
32+
}
33+
if (line[index] == '/') {
34+
multiComment = false
35+
} else {
36+
index--
37+
}
38+
} else if (!multiComment) {
39+
sb.append(ch)
40+
}
41+
index++
42+
}
43+
if (sb.isNotEmpty() && !multiComment) {
44+
result.add(sb.toString())
45+
sb.setLength(0)
46+
}
47+
}
48+
return result
49+
}
50+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
722\. Remove Comments
2+
3+
Medium
4+
5+
Given a C++ program, remove comments from it. The program source is an array of strings `source` where `source[i]` is the <code>i<sup>th</sup></code> line of the source code. This represents the result of splitting the original source code string by the newline character `'\n'`.
6+
7+
In C++, there are two types of comments, line comments, and block comments.
8+
9+
* The string `"//"` denotes a line comment, which represents that it and the rest of the characters to the right of it in the same line should be ignored.
10+
* The string `"/*"` denotes a block comment, which represents that all characters until the next (non-overlapping) occurrence of `"*/"` should be ignored. (Here, occurrences happen in reading order: line by line from left to right.) To be clear, the string `"/*/"` does not yet end the block comment, as the ending would be overlapping the beginning.
11+
12+
The first effective comment takes precedence over others.
13+
14+
* For example, if the string `"//"` occurs in a block comment, it is ignored.
15+
* Similarly, if the string `"/*"` occurs in a line or block comment, it is also ignored.
16+
17+
If a certain line of code is empty after removing comments, you must not output that line: each string in the answer list will be non-empty.
18+
19+
There will be no control characters, single quote, or double quote characters.
20+
21+
* For example, `source = "string s = "/* Not a comment. */";"` will not be a test case.
22+
23+
Also, nothing else such as defines or macros will interfere with the comments.
24+
25+
It is guaranteed that every open block comment will eventually be closed, so `"/*"` outside of a line or block comment always starts a new comment.
26+
27+
Finally, implicit newline characters can be deleted by block comments. Please see the examples below for details.
28+
29+
After removing the comments from the source code, return _the source code in the same format_.
30+
31+
**Example 1:**
32+
33+
**Input:** source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"]
34+
35+
**Output:** ["int main()","{ "," ","int a, b, c;","a = b + c;","}"]
36+
37+
**Explanation:** The line by line code is visualized as below:
38+
39+
/*Test program */
40+
int main() {
41+
// variable declaration
42+
int a, b, c;
43+
/* This is a test
44+
multiline comment
45+
for testing */
46+
a = b + c;
47+
}
48+
The string /* denotes a block comment, including line 1 and lines 6-9. The string // denotes line 4 as comments.
49+
The line by line output code is visualized as below:
50+
int main() {
51+
int a, b, c;
52+
a = b + c;
53+
}
54+
55+
**Example 2:**
56+
57+
**Input:** source = ["a/*comment", "line", "more_comment*/b"]
58+
59+
**Output:** ["ab"]
60+
61+
**Explanation:** The original source string is "a/*comment\nline\nmore_comment*/b", where we have bolded the newline characters. After deletion, the implicit newline characters are deleted, leaving the string "ab", which when delimited by newline characters becomes ["ab"].
62+
63+
**Constraints:**
64+
65+
* `1 <= source.length <= 100`
66+
* `0 <= source[i].length <= 80`
67+
* `source[i]` consists of printable **ASCII** characters.
68+
* Every open block comment is eventually closed.
69+
* There are no single-quote or double-quote in the input.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0701_0800.s0724_find_pivot_index
2+
3+
// #Easy #Array #Prefix_Sum #Level_1_Day_1_Prefix_Sum
4+
// #2023_02_28_Time_255_ms_(88.92%)_Space_38.7_MB_(93.26%)
5+
6+
class Solution {
7+
fun pivotIndex(nums: IntArray?): Int {
8+
if (nums == null || nums.isEmpty()) {
9+
return -1
10+
}
11+
val sums = IntArray(nums.size)
12+
sums[0] = nums[0]
13+
for (i in 1 until nums.size) {
14+
sums[i] = sums[i - 1] + nums[i]
15+
}
16+
for (i in nums.indices) {
17+
val temp = sums[nums.size - 1] - sums[i]
18+
if (i == 0 && 0 == temp || i > 0 && sums[i - 1] == temp) {
19+
return i
20+
}
21+
}
22+
return -1
23+
}
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
724\. Find Pivot Index
2+
3+
Easy
4+
5+
Given an array of integers `nums`, calculate the **pivot index** of this array.
6+
7+
The **pivot index** is the index where the sum of all the numbers **strictly** to the left of the index is equal to the sum of all the numbers **strictly** to the index's right.
8+
9+
If the index is on the left edge of the array, then the left sum is `0` because there are no elements to the left. This also applies to the right edge of the array.
10+
11+
Return _the **leftmost pivot index**_. If no such index exists, return `-1`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,7,3,6,5,6]
16+
17+
**Output:** 3
18+
19+
**Explanation:** The pivot index is 3. Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 Right sum = nums[4] + nums[5] = 5 + 6 = 11
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1,2,3]
24+
25+
**Output:** -1
26+
27+
**Explanation:** There is no index that satisfies the conditions in the problem statement.
28+
29+
**Example 3:**
30+
31+
**Input:** nums = [2,1,-1]
32+
33+
**Output:** 0
34+
35+
**Explanation:** The pivot index is 0. Left sum = 0 (no elements to the left of index 0) Right sum = nums[1] + nums[2] = 1 + -1 = 0
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
40+
* `-1000 <= nums[i] <= 1000`
41+
42+
**Note:** This question is the same as 1991: [https://leetcode.com/problems/find-the-middle-index-in-array/](https://leetcode.com/problems/find-the-middle-index-in-array/)

0 commit comments

Comments
 (0)