Skip to content

Commit 8a38bbd

Browse files
authored
Added tasks 692, 693, 695, 696
1 parent 5b1d0be commit 8a38bbd

File tree

13 files changed

+349
-0
lines changed

13 files changed

+349
-0
lines changed

README.md

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

191191
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
192192
|-|-|-|-|-|-
193+
| 0695 |[Max Area of Island](src/main/kotlin/g0601_0700/s0695_max_area_of_island/Solution.kt)| |||
193194

194195
#### Day 3 Matrix Related Problems
195196

@@ -419,6 +420,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
419420

420421
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
421422
|-|-|-|-|-|-
423+
| 0692 |[Top K Frequent Words](src/main/kotlin/g0601_0700/s0692_top_k_frequent_words/Solution.kt)| Medium | String, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Trie, Bucket_Sort | 239 | 81.10
422424

423425
### Level 2
424426

@@ -1057,6 +1059,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
10571059

10581060
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10591061
|-|-|-|-|-|-
1062+
| 0695 |[Max Area of Island](src/main/kotlin/g0601_0700/s0695_max_area_of_island/Solution.kt)| |||
10601063

10611064
#### Day 8 Breadth First Search Depth First Search
10621065

@@ -1676,6 +1679,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16761679
| 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
16771680
| 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
16781681
| 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
1682+
| 0696 |[Count Binary Substrings](src/main/kotlin/g0601_0700/s0696_count_binary_substrings/Solution.kt)| Easy | String, Two_Pointers | 222 | 100.00
1683+
| 0695 |[Max Area of Island](src/main/kotlin/g0601_0700/s0695_max_area_of_island/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_I_Day_7_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_2_Matrix_Related_Problems | 324 | 24.06
1684+
| 0693 |[Binary Number with Alternating Bits](src/main/kotlin/g0601_0700/s0693_binary_number_with_alternating_bits/Solution.kt)| Easy | Bit_Manipulation | 129 | 100.00
1685+
| 0692 |[Top K Frequent Words](src/main/kotlin/g0601_0700/s0692_top_k_frequent_words/Solution.kt)| Medium | String, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Trie, Bucket_Sort, Level_1_Day_15_Heap | 239 | 81.10
16791686
| 0691 |[Stickers to Spell Word](src/main/kotlin/g0601_0700/s0691_stickers_to_spell_word/Solution.kt)| Hard | Array, String, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask | 249 | 100.00
16801687
| 0690 |[Employee Importance](src/main/kotlin/g0601_0700/s0690_employee_importance/Solution.kt)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search | 255 | 100.00
16811688
| 0689 |[Knight Probability in Chessboard](src/main/kotlin/g0601_0700/s0689_maximum_sum_of_3_non_overlapping_subarrays/Solution.kt)| Hard | Array, Dynamic_Programming | 248 | 100.00
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0601_0700.s0692_top_k_frequent_words
2+
3+
// #Medium #String #Hash_Table #Sorting #Heap_Priority_Queue #Counting #Trie #Bucket_Sort
4+
// #Level_1_Day_15_Heap #2023_02_21_Time_239_ms_(81.10%)_Space_37.1_MB_(70.87%)
5+
6+
import java.util.SortedSet
7+
import java.util.TreeSet
8+
9+
@Suppress("NAME_SHADOWING")
10+
class Solution {
11+
fun topKFrequent(words: Array<String>, k: Int): List<String> {
12+
var k = k
13+
val map: MutableMap<String, Int> = HashMap()
14+
for (word in words) {
15+
map[word] = map.getOrDefault(word, 0) + 1
16+
}
17+
val sortedset: SortedSet<Map.Entry<String, Int>> = TreeSet(
18+
java.util.Comparator { (key, value): Map.Entry<String, Int>, (key1, value1): Map.Entry<String, Int> ->
19+
if (value != value1) {
20+
return@Comparator value1 - value
21+
} else {
22+
return@Comparator key.compareTo(key1, ignoreCase = true)
23+
}
24+
}
25+
)
26+
sortedset.addAll(map.entries)
27+
val result: MutableList<String> = ArrayList()
28+
val iterator: Iterator<Map.Entry<String, Int>> = sortedset.iterator()
29+
while (iterator.hasNext() && k-- > 0) {
30+
result.add(iterator.next().key)
31+
}
32+
return result
33+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
692\. Top K Frequent Words
2+
3+
Medium
4+
5+
Given an array of strings `words` and an integer `k`, return _the_ `k` _most frequent strings_.
6+
7+
Return the answer **sorted** by **the frequency** from highest to lowest. Sort the words with the same frequency by their **lexicographical order**.
8+
9+
**Example 1:**
10+
11+
**Input:** words = ["i","love","leetcode","i","love","coding"], k = 2
12+
13+
**Output:** ["i","love"]
14+
15+
**Explanation:** "i" and "love" are the two most frequent words. Note that "i" comes before "love" due to a lower alphabetical order.
16+
17+
**Example 2:**
18+
19+
**Input:** words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
20+
21+
**Output:** ["the","is","sunny","day"]
22+
23+
**Explanation:** "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.
24+
25+
**Constraints:**
26+
27+
* `1 <= words.length <= 500`
28+
* `1 <= words[i].length <= 10`
29+
* `words[i]` consists of lowercase English letters.
30+
* `k` is in the range <code>[1, The number of **unique** words[i]]</code>
31+
32+
**Follow-up:** Could you solve it in `O(n log(k))` time and `O(n)` extra space?
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0601_0700.s0693_binary_number_with_alternating_bits
2+
3+
// #Easy #Bit_Manipulation #2023_02_21_Time_129_ms_(100.00%)_Space_32.8_MB_(100.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun hasAlternatingBits(n: Int): Boolean {
8+
var n = n
9+
var prev = -1
10+
while (n != 0) {
11+
val v = n and 1
12+
n = n shr 1
13+
if (prev == v) {
14+
return false
15+
}
16+
prev = v
17+
}
18+
return true
19+
}
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
693\. Binary Number with Alternating Bits
2+
3+
Easy
4+
5+
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 5
10+
11+
**Output:** true
12+
13+
**Explanation:** The binary representation of 5 is: 101
14+
15+
**Example 2:**
16+
17+
**Input:** n = 7
18+
19+
**Output:** false
20+
21+
**Explanation:** The binary representation of 7 is: 111.
22+
23+
**Example 3:**
24+
25+
**Input:** n = 11
26+
27+
**Output:** false
28+
29+
**Explanation:** The binary representation of 11 is: 1011.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g0601_0700.s0695_max_area_of_island
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find
4+
// #Algorithm_I_Day_7_Breadth_First_Search_Depth_First_Search
5+
// #Graph_Theory_I_Day_2_Matrix_Related_Problems
6+
// #2023_02_22_Time_324_ms_(24.06%)_Space_47.2_MB_(21.92%)
7+
8+
@Suppress("NAME_SHADOWING")
9+
class Solution {
10+
fun maxAreaOfIsland(grid: Array<IntArray>?): Int {
11+
if (grid.isNullOrEmpty()) {
12+
return 0
13+
}
14+
val m = grid.size
15+
val n = grid[0].size
16+
var max = 0
17+
for (i in 0 until m) {
18+
for (j in 0 until n) {
19+
if (grid[i][j] == 1) {
20+
val area = dfs(grid, i, j, m, n, 0)
21+
max = Math.max(area, max)
22+
}
23+
}
24+
}
25+
return max
26+
}
27+
28+
private fun dfs(grid: Array<IntArray>, i: Int, j: Int, m: Int, n: Int, area: Int): Int {
29+
var area = area
30+
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {
31+
return area
32+
}
33+
grid[i][j] = 0
34+
area++
35+
area = dfs(grid, i + 1, j, m, n, area)
36+
area = dfs(grid, i, j + 1, m, n, area)
37+
area = dfs(grid, i - 1, j, m, n, area)
38+
area = dfs(grid, i, j - 1, m, n, area)
39+
return area
40+
}
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
695\. Max Area of Island
2+
3+
Medium
4+
5+
You are given an `m x n` binary matrix `grid`. An island is a group of `1`'s (representing land) connected **4-directionally** (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
6+
7+
The **area** of an island is the number of cells with a value `1` in the island.
8+
9+
Return _the maximum **area** of an island in_ `grid`. If there is no island, return `0`.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/05/01/maxarea1-grid.jpg)
14+
15+
**Input:** grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
16+
17+
**Output:** 6
18+
19+
**Explanation:** The answer is not 11, because the island must be connected 4-directionally.
20+
21+
**Example 2:**
22+
23+
**Input:** grid = [[0,0,0,0,0,0,0,0]]
24+
25+
**Output:** 0
26+
27+
**Constraints:**
28+
29+
* `m == grid.length`
30+
* `n == grid[i].length`
31+
* `1 <= m, n <= 50`
32+
* `grid[i][j]` is either `0` or `1`.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0601_0700.s0696_count_binary_substrings
2+
3+
// #Easy #String #Two_Pointers #2023_02_21_Time_222_ms_(100.00%)_Space_36.5_MB_(55.56%)
4+
5+
class Solution {
6+
fun countBinarySubstrings(s: String): Int {
7+
var start = 0
8+
var ans = 0
9+
val arr = s.toCharArray()
10+
for (i in 1 until arr.size) {
11+
if (arr[i] != arr[i - 1]) {
12+
ans++
13+
start = i - 1
14+
} else if (start > 0 && arr[--start] != arr[i]) {
15+
// if start isn't 0, we may still have a valid substring
16+
ans++
17+
} else {
18+
// if not, then reset start to 0
19+
start = 0
20+
}
21+
}
22+
return ans
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
696\. Count Binary Substrings
2+
3+
Easy
4+
5+
Given a binary string `s`, return the number of non-empty substrings that have the same number of `0`'s and `1`'s, and all the `0`'s and all the `1`'s in these substrings are grouped consecutively.
6+
7+
Substrings that occur multiple times are counted the number of times they occur.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "00110011"
12+
13+
**Output:** 6
14+
15+
**Explanation:** There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01". Notice that some of these substrings repeat and are counted the number of times they occur. Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.
16+
17+
**Example 2:**
18+
19+
**Input:** s = "10101"
20+
21+
**Output:** 4
22+
23+
**Explanation:** There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.
24+
25+
**Constraints:**
26+
27+
* <code>1 <= s.length <= 10<sup>5</sup></code>
28+
* `s[i]` is either `'0'` or `'1'`.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0601_0700.s0692_top_k_frequent_words
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 topKFrequent() {
10+
assertThat(
11+
Solution()
12+
.topKFrequent(arrayOf("i", "love", "leetcode", "i", "love", "coding"), 2),
13+
equalTo(listOf("i", "love"))
14+
)
15+
}
16+
17+
@Test
18+
fun topKFrequent2() {
19+
assertThat(
20+
Solution()
21+
.topKFrequent(
22+
arrayOf(
23+
"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is",
24+
"is"
25+
),
26+
4
27+
),
28+
equalTo(listOf("the", "is", "sunny", "day"))
29+
)
30+
}
31+
}

0 commit comments

Comments
 (0)