Skip to content

Commit 77f29e5

Browse files
authored
Added tasks 674, 675, 676, 677
1 parent aa96546 commit 77f29e5

File tree

13 files changed

+443
-0
lines changed

13 files changed

+443
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16761676
| 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
16771677
| 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
16781678
| 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
1679+
| 0677 |[Map Sum Pairs](src/main/kotlin/g0601_0700/s0677_map_sum_pairs/MapSum.kt)| Medium | String, Hash_Table, Design, Trie | 197 | 80.00
1680+
| 0676 |[Implement Magic Dictionary](src/main/kotlin/g0601_0700/s0676_implement_magic_dictionary/MagicDictionary.kt)| Medium | String, Hash_Table, Design, Trie | 256 | 100.00
1681+
| 0675 |[Cut Off Trees for Golf Event](src/main/kotlin/g0601_0700/s0675_cut_off_trees_for_golf_event/Solution.kt)| Hard | Array, Breadth_First_Search, Matrix, Heap_Priority_Queue | 777 | 100.00
1682+
| 0674 |[Longest Continuous Increasing Subsequence](src/main/kotlin/g0601_0700/s0674_longest_continuous_increasing_subsequence/Solution.kt)| Easy | Array | 201 | 84.21
16791683
| 0673 |[Number of Longest Increasing Subsequence](src/main/kotlin/g0601_0700/s0673_number_of_longest_increasing_subsequence/Solution.kt)| Medium | Array, Dynamic_Programming, Segment_Tree, Binary_Indexed_Tree, Algorithm_II_Day_16_Dynamic_Programming | 226 | 91.67
16801684
| 0672 |[Bulb Switcher II](src/main/kotlin/g0601_0700/s0672_bulb_switcher_ii/Solution.kt)| Medium | Math, Depth_First_Search, Breadth_First_Search, Bit_Manipulation | 131 | 100.00
16811685
| 0671 |[Second Minimum Node In a Binary Tree](src/main/kotlin/g0601_0700/s0671_second_minimum_node_in_a_binary_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree | 128 | 100.00
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0601_0700.s0674_longest_continuous_increasing_subsequence
2+
3+
// #Easy #Array #2023_02_15_Time_201_ms_(84.21%)_Space_36.9_MB_(68.42%)
4+
5+
class Solution {
6+
fun findLengthOfLCIS(nums: IntArray): Int {
7+
var ans = 1
8+
var count = 1
9+
for (i in 0 until nums.size - 1) {
10+
if (nums[i] < nums[i + 1]) {
11+
count++
12+
} else {
13+
ans = max(count, ans)
14+
count = 1
15+
}
16+
}
17+
return max(ans, count)
18+
}
19+
20+
private fun max(n1: Int, n2: Int): Int {
21+
return Math.max(n1, n2)
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
674\. Longest Continuous Increasing Subsequence
2+
3+
Easy
4+
5+
Given an unsorted array of integers `nums`, return _the length of the longest **continuous increasing subsequence** (i.e. subarray)_. The subsequence must be **strictly** increasing.
6+
7+
A **continuous increasing subsequence** is defined by two indices `l` and `r` (`l < r`) such that it is `[nums[l], nums[l + 1], ..., nums[r - 1], nums[r]]` and for each `l <= i < r`, `nums[i] < nums[i + 1]`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,3,5,4,7]
12+
13+
**Output:** 3
14+
15+
**Explanation:** The longest continuous increasing subsequence is [1,3,5] with length 3. Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element 4.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [2,2,2,2,2]
20+
21+
**Output:** 1
22+
23+
**Explanation:** The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly increasing.
24+
25+
**Constraints:**
26+
27+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
28+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package g0601_0700.s0675_cut_off_trees_for_golf_event
2+
3+
// #Hard #Array #Breadth_First_Search #Matrix #Heap_Priority_Queue
4+
// #2023_02_15_Time_777_ms_(100.00%)_Space_43.4_MB_(100.00%)
5+
6+
import java.util.LinkedList
7+
import java.util.Objects
8+
import java.util.PriorityQueue
9+
import java.util.Queue
10+
11+
class Solution {
12+
private var r = 0
13+
private var c = 0
14+
fun cutOffTree(forest: List<List<Int>>): Int {
15+
val pq = PriorityQueue<Int>()
16+
for (integers in forest) {
17+
for (v in integers) {
18+
if (v > 1) {
19+
pq.add(v)
20+
}
21+
}
22+
}
23+
var steps = 0
24+
while (!pq.isEmpty()) {
25+
val count = minSteps(forest, pq.poll())
26+
if (count == -1) {
27+
return -1
28+
}
29+
steps += count
30+
}
31+
return steps
32+
}
33+
34+
private fun minSteps(forest: List<List<Int>>, target: Int): Int {
35+
var steps = 0
36+
val dirs = arrayOf(intArrayOf(0, 1), intArrayOf(0, -1), intArrayOf(1, 0), intArrayOf(-1, 0))
37+
val visited = Array(forest.size) {
38+
BooleanArray(
39+
forest[0].size
40+
)
41+
}
42+
val q: Queue<IntArray> = LinkedList()
43+
q.add(intArrayOf(r, c))
44+
visited[r][c] = true
45+
while (!q.isEmpty()) {
46+
val qSize = q.size
47+
for (i in 0 until qSize) {
48+
val curr = q.poll()
49+
if (forest[Objects.requireNonNull(curr)[0]][curr[1]] == target) {
50+
r = curr[0]
51+
c = curr[1]
52+
return steps
53+
}
54+
for (k in 0..3) {
55+
val dir = dirs[k]
56+
val nr = dir[0] + curr[0]
57+
val nc = dir[1] + curr[1]
58+
if (nr < 0 || nr == visited.size || nc < 0 || nc == visited[0].size ||
59+
visited[nr][nc] || forest[nr][nc] == 0
60+
) {
61+
continue
62+
}
63+
q.add(intArrayOf(nr, nc))
64+
visited[nr][nc] = true
65+
}
66+
}
67+
steps++
68+
}
69+
return -1
70+
}
71+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
675\. Cut Off Trees for Golf Event
2+
3+
Hard
4+
5+
You are asked to cut off all the trees in a forest for a golf event. The forest is represented as an `m x n` matrix. In this matrix:
6+
7+
* `0` means the cell cannot be walked through.
8+
* `1` represents an empty cell that can be walked through.
9+
* A number greater than `1` represents a tree in a cell that can be walked through, and this number is the tree's height.
10+
11+
In one step, you can walk in any of the four directions: north, east, south, and west. If you are standing in a cell with a tree, you can choose whether to cut it off.
12+
13+
You must cut off the trees in order from shortest to tallest. When you cut off a tree, the value at its cell becomes `1` (an empty cell).
14+
15+
Starting from the point `(0, 0)`, return _the minimum steps you need to walk to cut off all the trees_. If you cannot cut off all the trees, return `-1`.
16+
17+
**Note:** The input is generated such that no two trees have the same height, and there is at least one tree needs to be cut off.
18+
19+
**Example 1:**
20+
21+
![](https://assets.leetcode.com/uploads/2020/11/26/trees1.jpg)
22+
23+
**Input:** forest = [[1,2,3],[0,0,4],[7,6,5]]
24+
25+
**Output:** 6
26+
27+
**Explanation:** Following the path above allows you to cut off the trees from shortest to tallest in 6 steps.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2020/11/26/trees2.jpg)
32+
33+
**Input:** forest = [[1,2,3],[0,0,0],[7,6,5]]
34+
35+
**Output:** -1
36+
37+
**Explanation:** The trees in the bottom row cannot be accessed as the middle row is blocked.
38+
39+
**Example 3:**
40+
41+
**Input:** forest = [[2,3,4],[0,0,5],[8,7,6]]
42+
43+
**Output:** 6
44+
45+
**Explanation:** You can follow the same path as Example 1 to cut off all the trees. Note that you can cut off the first tree at (0, 0) before making any steps.
46+
47+
**Constraints:**
48+
49+
* `m == forest.length`
50+
* `n == forest[i].length`
51+
* `1 <= m, n <= 50`
52+
* <code>0 <= forest[i][j] <= 10<sup>9</sup></code>
53+
* Heights of all trees are **distinct**.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g0601_0700.s0676_implement_magic_dictionary
2+
3+
// #Medium #String #Hash_Table #Design #Trie
4+
// #2023_02_15_Time_256_ms_(100.00%)_Space_37.7_MB_(100.00%)
5+
6+
class MagicDictionary {
7+
private var dictionaryWords: Array<String>? = null
8+
fun buildDict(dictionary: Array<String>?) {
9+
dictionaryWords = dictionary
10+
}
11+
12+
fun search(searchWord: String): Boolean {
13+
for (word in dictionaryWords!!) {
14+
if (isOffByOneLetter(word, searchWord)) {
15+
return true
16+
}
17+
}
18+
return false
19+
}
20+
21+
private fun isOffByOneLetter(word: String, searchWord: String): Boolean {
22+
if (isDifferentLengths(word, searchWord) || word == searchWord) {
23+
return false
24+
}
25+
var numDifferentLetters = 0
26+
for (i in word.indices) {
27+
if (isNotTheSameLetter(word[i], searchWord[i])) {
28+
numDifferentLetters++
29+
}
30+
if (numDifferentLetters > 1) {
31+
return false
32+
}
33+
}
34+
return numDifferentLetters == 1
35+
}
36+
37+
private fun isDifferentLengths(word: String, searchWord: String): Boolean {
38+
return word.length != searchWord.length
39+
}
40+
41+
private fun isNotTheSameLetter(c1: Char, c2: Char): Boolean {
42+
return c1 != c2
43+
}
44+
}
45+
46+
/*
47+
* Your MagicDictionary object will be instantiated and called as such:
48+
* var obj = MagicDictionary()
49+
* obj.buildDict(dictionary)
50+
* var param_2 = obj.search(searchWord)
51+
*/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
676\. Implement Magic Dictionary
2+
3+
Medium
4+
5+
Design a data structure that is initialized with a list of **different** words. Provided a string, you should determine if you can change exactly one character in this string to match any word in the data structure.
6+
7+
Implement the `MagicDictionary` class:
8+
9+
* `MagicDictionary()` Initializes the object.
10+
* `void buildDict(String[] dictionary)` Sets the data structure with an array of distinct strings `dictionary`.
11+
* `bool search(String searchWord)` Returns `true` if you can change **exactly one character** in `searchWord` to match any string in the data structure, otherwise returns `false`.
12+
13+
**Example 1:**
14+
15+
**Input**
16+
17+
["MagicDictionary", "buildDict", "search", "search", "search", "search"] [[],
18+
19+
[["hello", "leetcode"]], ["hello"], ["hhllo"], ["hell"], ["leetcoded"]]
20+
21+
**Output:** [null, null, false, true, false, false]
22+
23+
**Explanation:**
24+
25+
MagicDictionary magicDictionary = new MagicDictionary();
26+
magicDictionary.buildDict(["hello", "leetcode"]);
27+
magicDictionary.search("hello"); // return False
28+
magicDictionary.search("hhllo"); // We can change the second 'h' to 'e' to match "hello" so we return True
29+
magicDictionary.search("hell"); // return False
30+
magicDictionary.search("leetcoded"); // return False
31+
32+
**Constraints:**
33+
34+
* `1 <= dictionary.length <= 100`
35+
* `1 <= dictionary[i].length <= 100`
36+
* `dictionary[i]` consists of only lower-case English letters.
37+
* All the strings in `dictionary` are **distinct**.
38+
* `1 <= searchWord.length <= 100`
39+
* `searchWord` consists of only lower-case English letters.
40+
* `buildDict` will be called only once before `search`.
41+
* At most `100` calls will be made to `search`.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g0601_0700.s0677_map_sum_pairs
2+
3+
// #Medium #String #Hash_Table #Design #Trie #2023_02_15_Time_197_ms_(80.00%)_Space_36.1_MB_(55.00%)
4+
5+
class MapSum {
6+
internal class Node {
7+
var `val`: Int = 0
8+
var child: Array<Node?> = arrayOfNulls(26)
9+
}
10+
11+
private val root: Node = Node()
12+
13+
fun insert(key: String, `val`: Int) {
14+
var curr: Node? = root
15+
for (c in key.toCharArray()) {
16+
if (curr!!.child[c.code - 'a'.code] == null) {
17+
curr.child[c.code - 'a'.code] = Node()
18+
}
19+
curr = curr.child[c.code - 'a'.code]
20+
}
21+
curr!!.`val` = `val`
22+
}
23+
24+
private fun sumHelper(root: Node?): Int {
25+
var o = 0
26+
for (i in 0..25) {
27+
if (root!!.child[i] != null) {
28+
o += root.child[i]!!.`val` + sumHelper(root.child[i])
29+
}
30+
}
31+
return o
32+
}
33+
34+
fun sum(prefix: String): Int {
35+
var curr: Node? = root
36+
for (c in prefix.toCharArray()) {
37+
if (curr!!.child[c.code - 'a'.code] == null) {
38+
return 0
39+
}
40+
curr = curr.child[c.code - 'a'.code]
41+
}
42+
val sum = curr!!.`val`
43+
return sum + sumHelper(curr)
44+
}
45+
}
46+
47+
/*
48+
* Your MapSum object will be instantiated and called as such:
49+
* var obj = MapSum()
50+
* obj.insert(key,`val`)
51+
* var param_2 = obj.sum(prefix)
52+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
677\. Map Sum Pairs
2+
3+
Medium
4+
5+
Design a map that allows you to do the following:
6+
7+
* Maps a string key to a given value.
8+
* Returns the sum of the values that have a key with a prefix equal to a given string.
9+
10+
Implement the `MapSum` class:
11+
12+
* `MapSum()` Initializes the `MapSum` object.
13+
* `void insert(String key, int val)` Inserts the `key-val` pair into the map. If the `key` already existed, the original `key-value` pair will be overridden to the new one.
14+
* `int sum(string prefix)` Returns the sum of all the pairs' value whose `key` starts with the `prefix`.
15+
16+
**Example 1:**
17+
18+
**Input**
19+
20+
["MapSum", "insert", "sum", "insert", "sum"] [[],
21+
22+
["apple", 3], ["ap"], ["app", 2], ["ap"]]
23+
24+
**Output:** [null, null, 3, null, 5]
25+
26+
**Explanation:**
27+
28+
MapSum mapSum = new MapSum();
29+
mapSum.insert("apple", 3);
30+
mapSum.sum("ap"); // return 3 (apple = 3)
31+
mapSum.insert("app", 2);
32+
mapSum.sum("ap"); // return 5 (apple + app = 3 + 2 = 5)
33+
34+
**Constraints:**
35+
36+
* `1 <= key.length, prefix.length <= 50`
37+
* `key` and `prefix` consist of only lowercase English letters.
38+
* `1 <= val <= 1000`
39+
* At most `50` calls will be made to `insert` and `sum`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0601_0700.s0674_longest_continuous_increasing_subsequence
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 findNumberOfLIS2() {
10+
assertThat(Solution().findLengthOfLCIS(intArrayOf(1, 3, 5, 4, 7)), equalTo(3))
11+
}
12+
13+
@Test
14+
fun findNumberOfLIS22() {
15+
assertThat(Solution().findLengthOfLCIS(intArrayOf(2, 2, 2, 2, 2)), equalTo(1))
16+
}
17+
}

0 commit comments

Comments
 (0)