Skip to content

Commit 4cecadb

Browse files
authored
Added tasks 1017, 1018, 1019, 1020
1 parent cebc566 commit 4cecadb

File tree

13 files changed

+388
-0
lines changed

13 files changed

+388
-0
lines changed

README.md

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

16091609
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16101610
|-|-|-|-|-|-
1611+
| 1020 |[Number of Enclaves](src/main/kotlin/g1001_1100/s1020_number_of_enclaves/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 369 | 76.26
16111612

16121613
#### Day 4 Matrix Related Problems
16131614

@@ -1749,6 +1750,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17491750
| # | Title | Difficulty | Tag | Time, ms | Time, %
17501751
|------|----------------|-------------|-------------|----------|---------
17511752
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
1753+
| 1020 |[Number of Enclaves](src/main/kotlin/g1001_1100/s1020_number_of_enclaves/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Graph_Theory_I_Day_3_Matrix_Related_Problems | 369 | 76.26
1754+
| 1019 |[Next Greater Node In Linked List](src/main/kotlin/g1001_1100/s1019_next_greater_node_in_linked_list/Solution.kt)| Medium | Array, Stack, Linked_List, Monotonic_Stack | 472 | 75.00
1755+
| 1018 |[Binary Prefix Divisible By 5](src/main/kotlin/g1001_1100/s1018_binary_prefix_divisible_by_5/Solution.kt)| Easy | Array | 297 | 100.00
1756+
| 1017 |[Convert to Base -2](src/main/kotlin/g1001_1100/s1017_convert_to_base_2/Solution.kt)| Medium | Math | 138 | 100.00
17521757
| 1016 |[Binary String With Substrings Representing 1 To N](src/main/kotlin/g1001_1100/s1016_binary_string_with_substrings_representing_1_to_n/Solution.kt)| Medium | String | 134 | 75.00
17531758
| 1015 |[Smallest Integer Divisible by K](src/main/kotlin/g1001_1100/s1015_smallest_integer_divisible_by_k/Solution.kt)| Medium | Hash_Table, Math | 123 | 100.00
17541759
| 1014 |[Best Sightseeing Pair](src/main/kotlin/g1001_1100/s1014_best_sightseeing_pair/Solution.kt)| Medium | Array, Dynamic_Programming, Dynamic_Programming_I_Day_7 | 336 | 66.67
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g1001_1100.s1017_convert_to_base_2
2+
3+
// #Medium #Math #2023_05_21_Time_138_ms_(100.00%)_Space_34.5_MB_(100.00%)
4+
5+
class Solution {
6+
fun baseNeg2(n: Int): String {
7+
val sb = StringBuilder(Integer.toBinaryString(n))
8+
sb.reverse()
9+
var carry = 0
10+
var sum: Int
11+
var pos = 0
12+
while (pos < sb.length) {
13+
sum = carry + sb[pos].code - '0'.code
14+
sb.setCharAt(pos, if (sum % 2 == 0) '0' else '1')
15+
carry = sum / 2
16+
if (pos % 2 == 1 && sb[pos] == '1') {
17+
carry += 1
18+
}
19+
pos++
20+
if (pos >= sb.length && carry > 0) {
21+
sb.append(Integer.toBinaryString(carry))
22+
carry = 0
23+
}
24+
}
25+
return sb.reverse().toString()
26+
}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
1017\. Convert to Base -2
2+
3+
Medium
4+
5+
Given an integer `n`, return _a binary string representing its representation in base_ `-2`.
6+
7+
**Note** that the returned string should not have leading zeros unless the string is `"0"`.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 2
12+
13+
**Output:** "110" **Explantion:** (-2)<sup>2</sup> + (-2)<sup>1</sup> = 2
14+
15+
**Example 2:**
16+
17+
**Input:** n = 3
18+
19+
**Output:** "111" **Explantion:** (-2)<sup>2</sup> + (-2)<sup>1</sup> + (-2)<sup>0</sup> = 3
20+
21+
**Example 3:**
22+
23+
**Input:** n = 4
24+
25+
**Output:** "100" **Explantion:** (-2)<sup>2</sup> = 4
26+
27+
**Constraints:**
28+
29+
* <code>0 <= n <= 10<sup>9</sup></code>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g1001_1100.s1018_binary_prefix_divisible_by_5
2+
3+
// #Easy #Array #2023_05_21_Time_297_ms_(100.00%)_Space_50_MB_(100.00%)
4+
5+
class Solution {
6+
fun prefixesDivBy5(nums: IntArray): List<Boolean> {
7+
val result: MutableList<Boolean> = ArrayList(nums.size)
8+
var remainder = 0
9+
for (j in nums) {
10+
remainder = (j + (remainder shl 1)) % 5
11+
result.add(remainder == 0)
12+
}
13+
return result
14+
}
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
1018\. Binary Prefix Divisible By 5
2+
3+
Easy
4+
5+
You are given a binary array `nums` (**0-indexed**).
6+
7+
We define <code>x<sub>i</sub></code> as the number whose binary representation is the subarray `nums[0..i]` (from most-significant-bit to least-significant-bit).
8+
9+
* For example, if `nums = [1,0,1]`, then <code>x<sub>0</sub> = 1</code>, <code>x<sub>1</sub> = 2</code>, and <code>x<sub>2</sub> = 5</code>.
10+
11+
Return _an array of booleans_ `answer` _where_ `answer[i]` _is_ `true` _if_ <code>x<sub>i</sub></code> _is divisible by_ `5`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [0,1,1]
16+
17+
**Output:** [true,false,false]
18+
19+
**Explanation:** The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1,1,1]
24+
25+
**Output:** [false,false,false]
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
30+
* `nums[i]` is either `0` or `1`.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package g1001_1100.s1019_next_greater_node_in_linked_list
2+
3+
// #Medium #Array #Stack #Linked_List #Monotonic_Stack
4+
// #2023_05_21_Time_472_ms_(75.00%)_Space_97.4_MB_(25.00%)
5+
6+
import com_github_leetcode.ListNode
7+
8+
/*
9+
* Example:
10+
* var li = ListNode(5)
11+
* var v = li.`val`
12+
* Definition for singly-linked list.
13+
* class ListNode(var `val`: Int) {
14+
* var next: ListNode? = null
15+
* }
16+
*/
17+
@Suppress("NAME_SHADOWING")
18+
class Solution {
19+
fun nextLargerNodes(head: ListNode?): IntArray {
20+
var head = head
21+
val len = length(head)
22+
var i = 0
23+
val arr = IntArray(len)
24+
val idx = IntArray(len)
25+
while (head != null) {
26+
arr[i] = head.`val`
27+
head = head.next
28+
i++
29+
}
30+
hlp(arr, idx, 0)
31+
i = 0
32+
while (i < idx.size) {
33+
val j = idx[i]
34+
if (j != -1) {
35+
arr[i] = arr[j]
36+
} else {
37+
arr[i] = 0
38+
}
39+
i++
40+
}
41+
arr[i - 1] = 0
42+
return arr
43+
}
44+
45+
private fun hlp(arr: IntArray, idx: IntArray, i: Int) {
46+
if (i == arr.size - 1) {
47+
idx[i] = -1
48+
return
49+
}
50+
hlp(arr, idx, i + 1)
51+
var j = i + 1
52+
while (j != -1 && arr[i] >= arr[j]) {
53+
j = idx[j]
54+
}
55+
if (j != -1 && arr[i] >= arr[j]) {
56+
idx[i] = -1
57+
} else {
58+
idx[i] = j
59+
}
60+
}
61+
62+
private fun length(head: ListNode?): Int {
63+
var head = head
64+
var len = 0
65+
while (head != null) {
66+
head = head.next
67+
len++
68+
}
69+
return len
70+
}
71+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
1019\. Next Greater Node In Linked List
2+
3+
Medium
4+
5+
You are given the `head` of a linked list with `n` nodes.
6+
7+
For each node in the list, find the value of the **next greater node**. That is, for each node, find the value of the first node that is next to it and has a **strictly larger** value than it.
8+
9+
Return an integer array `answer` where `answer[i]` is the value of the next greater node of the <code>i<sup>th</sup></code> node (**1-indexed**). If the <code>i<sup>th</sup></code> node does not have a next greater node, set `answer[i] = 0`.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/08/05/linkedlistnext1.jpg)
14+
15+
**Input:** head = [2,1,5]
16+
17+
**Output:** [5,5,0]
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/08/05/linkedlistnext2.jpg)
22+
23+
**Input:** head = [2,7,4,3,5]
24+
25+
**Output:** [7,0,5,5,0]
26+
27+
**Constraints:**
28+
29+
* The number of nodes in the list is `n`.
30+
* <code>1 <= n <= 10<sup>4</sup></code>
31+
* <code>1 <= Node.val <= 10<sup>9</sup></code>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g1001_1100.s1020_number_of_enclaves
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find
4+
// #Graph_Theory_I_Day_3_Matrix_Related_Problems
5+
// #2023_05_21_Time_369_ms_(76.26%)_Space_90.3_MB_(16.91%)
6+
7+
class Solution {
8+
fun numEnclaves(grid: Array<IntArray>): Int {
9+
val visited = Array(grid.size) {
10+
BooleanArray(
11+
grid[0].size
12+
)
13+
}
14+
for (i in grid.indices) {
15+
for (j in grid[0].indices) {
16+
if (grid[i][j] == 1 && (i == 0 || j == 0 || i == grid.size - 1 || j == grid[0].size - 1)) {
17+
move(grid, i, j, visited)
18+
}
19+
}
20+
}
21+
var count = 0
22+
for (i in 1 until visited.size - 1) {
23+
for (j in 1 until visited[0].size - 1) {
24+
if (!visited[i][j] && grid[i][j] == 1) count++
25+
}
26+
}
27+
return count
28+
}
29+
30+
companion object {
31+
fun move(g: Array<IntArray>, i: Int, j: Int, b: Array<BooleanArray>) {
32+
if (i < 0 || j < 0 || i == g.size || j == g[0].size || g[i][j] == 0 || b[i][j]) return
33+
b[i][j] = true
34+
move(g, i + 1, j, b)
35+
move(g, i - 1, j, b)
36+
move(g, i, j - 1, b)
37+
move(g, i, j + 1, b)
38+
}
39+
}
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1020\. Number of Enclaves
2+
3+
Medium
4+
5+
You are given an `m x n` binary matrix `grid`, where `0` represents a sea cell and `1` represents a land cell.
6+
7+
A **move** consists of walking from one land cell to another adjacent (**4-directionally**) land cell or walking off the boundary of the `grid`.
8+
9+
Return _the number of land cells in_ `grid` _for which we cannot walk off the boundary of the grid in any number of **moves**_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/02/18/enclaves1.jpg)
14+
15+
**Input:** grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
16+
17+
**Output:** 3
18+
19+
**Explanation:** There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2021/02/18/enclaves2.jpg)
24+
25+
**Input:** grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
26+
27+
**Output:** 0
28+
29+
**Explanation:** All 1s are either on the boundary or can reach the boundary.
30+
31+
**Constraints:**
32+
33+
* `m == grid.length`
34+
* `n == grid[i].length`
35+
* `1 <= m, n <= 500`
36+
* `grid[i][j]` is either `0` or `1`.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g1001_1100.s1017_convert_to_base_2
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 baseNeg2() {
10+
assertThat(Solution().baseNeg2(2), equalTo("110"))
11+
}
12+
13+
@Test
14+
fun baseNeg22() {
15+
assertThat(Solution().baseNeg2(3), equalTo("111"))
16+
}
17+
18+
@Test
19+
fun baseNeg23() {
20+
assertThat(Solution().baseNeg2(4), equalTo("100"))
21+
}
22+
}

0 commit comments

Comments
 (0)