Skip to content

Commit ff10207

Browse files
authored
Added tasks 211, 212, 213, 214.
1 parent e924972 commit ff10207

File tree

14 files changed

+490
-0
lines changed

14 files changed

+490
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
245245
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
246246
|-|-|-|-|-|-
247247
| 0198 |[House Robber](src/main/kotlin/g0101_0200/s0198_house_robber/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming | 156 | 92.24
248+
| 0213 |[House Robber II](src/main/kotlin/g0201_0300/s0213_house_robber_ii/Solution.kt)| Medium | Array, Dynamic_Programming | 257 | 59.62
248249

249250
#### Day 4
250251

@@ -1043,6 +1044,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
10431044
| 0139 |[Word Break](src/main/kotlin/g0101_0200/s0139_word_break/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization | 197 | 87.17
10441045
| 0152 |[Maximum Product Subarray](src/main/kotlin/g0101_0200/s0152_maximum_product_subarray/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming | 253 | 88.42
10451046
| 0198 |[House Robber](src/main/kotlin/g0101_0200/s0198_house_robber/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming | 156 | 92.24
1047+
| 0213 |[House Robber II](src/main/kotlin/g0201_0300/s0213_house_robber_ii/Solution.kt)| Medium | Array, Dynamic_Programming | 257 | 59.62
10461048
| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 127 | 97.06
10471049
| 0064 |[Minimum Path Sum](src/main/kotlin/g0001_0100/s0064_minimum_path_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix | 222 | 95.70
10481050
| 0300 |[Longest Increasing Subsequence](src/main/kotlin/g0201_0300/s0300_longest_increasing_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search | 318 | 82.28
@@ -1483,6 +1485,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
14831485

14841486
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14851487
|-|-|-|-|-|-
1488+
| 0213 |[House Robber II](src/main/kotlin/g0201_0300/s0213_house_robber_ii/Solution.kt)| Medium | Array, Dynamic_Programming | 257 | 59.62
14861489
| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 670 | 66.67
14871490

14881491
#### Day 13 Dynamic Programming
@@ -1576,6 +1579,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15761579
| 0226 |[Invert Binary Tree](src/main/kotlin/g0201_0300/s0226_invert_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_12_Tree, Level_2_Day_6_Tree, Udemy_Tree_Stack_Queue | 233 | 54.90
15771580
| 0221 |[Maximal Square](src/main/kotlin/g0201_0300/s0221_maximal_square/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16 | 614 | 44.00
15781581
| 0215 |[Kth Largest Element in an Array](src/main/kotlin/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Data_Structure_II_Day_20_Heap_Priority_Queue | 839 | 34.43
1582+
| 0214 |[Shortest Palindrome](src/main/kotlin/g0201_0300/s0214_shortest_palindrome/Solution.kt)| Hard | String, Hash_Function, String_Matching, Rolling_Hash | 278 | 100.00
1583+
| 0213 |[House Robber II](src/main/kotlin/g0201_0300/s0213_house_robber_ii/Solution.kt)| Medium | Array, Dynamic_Programming, Algorithm_II_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Udemy_Dynamic_Programming | 257 | 59.62
1584+
| 0212 |[Word Search II](src/main/kotlin/g0201_0300/s0212_word_search_ii/Solution.kt)| Hard | Top_Interview_Questions, Array, String, Matrix, Backtracking, Trie | 268 | 100.00
1585+
| 0211 |[Design Add and Search Words Data Structure](src/main/kotlin/g0201_0300/s0211_design_add_and_search_words_data_structure/WordDictionary.kt)| Medium | String, Depth_First_Search, Design, Trie | 2256 | 87.04
15791586
| 0210 |[Course Schedule II](src/main/kotlin/g0201_0300/s0210_course_schedule_ii/Solution.kt)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Level_2_Day_11_Graph/BFS/DFS | 266 | 96.32
15801587
| 0209 |[Minimum Size Subarray Sum](src/main/kotlin/g0201_0300/s0209_minimum_size_subarray_sum/Solution.kt)| Medium | Array, Binary_Search, Prefix_Sum, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Binary_Search_II_Day_1 | 315 | 96.73
15811588
| 0208 |[Implement Trie (Prefix Tree)](src/main/kotlin/g0201_0300/s0208_implement_trie_prefix_tree/Trie.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Level_2_Day_16_Design, Udemy_Trie_and_Heap | 689 | 61.00
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g0201_0300.s0211_design_add_and_search_words_data_structure
2+
3+
// #Medium #String #Depth_First_Search #Design #Trie
4+
// #2022_10_24_Time_2256_ms_(87.04%)_Space_237.4_MB_(83.33%)
5+
6+
class WordDictionary {
7+
class TrieNode() {
8+
val children = Array<TrieNode?>(26) { null }
9+
var isWord = false
10+
}
11+
12+
val trieTree = TrieNode()
13+
14+
fun addWord(word: String) {
15+
var p = trieTree
16+
17+
for (w in word) {
18+
val i = w - 'a'
19+
if (p.children[i] == null) p.children[i] = TrieNode()
20+
p = p.children[i]!!
21+
}
22+
23+
p.isWord = true
24+
}
25+
26+
fun search(word: String): Boolean {
27+
fun dfs(p: TrieNode?, start: Int): Boolean {
28+
if (p == null) return false
29+
30+
if (start == word.length) return p.isWord
31+
32+
if (word[start] == '.') {
33+
for (i in 0..25) {
34+
if (dfs(p.children[i], start + 1)) {
35+
return true
36+
}
37+
}
38+
return false
39+
} else {
40+
val i = word[start] - 'a'
41+
return dfs(p.children[i], start + 1)
42+
}
43+
}
44+
45+
return dfs(trieTree, 0)
46+
}
47+
}
48+
49+
/*
50+
* Your WordDictionary object will be instantiated and called as such:
51+
* var obj = WordDictionary()
52+
* obj.addWord(word)
53+
* var param_2 = obj.search(word)
54+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
211\. Design Add and Search Words Data Structure
2+
3+
Medium
4+
5+
Design a data structure that supports adding new words and finding if a string matches any previously added string.
6+
7+
Implement the `WordDictionary` class:
8+
9+
* `WordDictionary()` Initializes the object.
10+
* `void addWord(word)` Adds `word` to the data structure, it can be matched later.
11+
* `bool search(word)` Returns `true` if there is any string in the data structure that matches `word` or `false` otherwise. `word` may contain dots `'.'` where dots can be matched with any letter.
12+
13+
**Example:**
14+
15+
**Input**
16+
17+
["WordDictionary","addWord","addWord","addWord","search","search","search","search"] [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
18+
19+
**Output**
20+
21+
[null,null,null,null,false,true,true,true]
22+
23+
**Explanation**
24+
25+
WordDictionary wordDictionary = new WordDictionary();
26+
wordDictionary.addWord("bad");
27+
wordDictionary.addWord("dad");
28+
wordDictionary.addWord("mad");
29+
wordDictionary.search("pad"); // return False
30+
wordDictionary.search("bad"); // return True
31+
wordDictionary.search(".ad"); // return True
32+
wordDictionary.search("b.."); // return True
33+
34+
**Constraints:**
35+
36+
* `1 <= word.length <= 500`
37+
* `word` in `addWord` consists lower-case English letters.
38+
* `word` in `search` consist of `'.'` or lower-case English letters.
39+
* At most `50000` calls will be made to `addWord` and `search`.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g0201_0300.s0212_word_search_ii
2+
3+
// #Hard #Top_Interview_Questions #Array #String #Matrix #Backtracking #Trie
4+
// #2022_10_24_Time_268_ms_(100.00%)_Space_37.5_MB_(95.08%)
5+
6+
class Solution {
7+
private var root: Tree? = null
8+
fun findWords(board: Array<CharArray>, words: Array<String?>): List<String> {
9+
if (board.size < 1 || board[0].size < 1) {
10+
return emptyList()
11+
}
12+
root = Tree()
13+
for (word in words) {
14+
Tree.addWord(root, word!!)
15+
}
16+
val collected: MutableList<String> = ArrayList()
17+
for (i in board.indices) {
18+
for (j in board[0].indices) {
19+
dfs(board, i, j, root, collected)
20+
}
21+
}
22+
return collected
23+
}
24+
25+
private fun dfs(board: Array<CharArray>, i: Int, j: Int, cur: Tree?, collected: MutableList<String>) {
26+
var cur: Tree? = cur
27+
val c = board[i][j]
28+
if (c == '-') {
29+
return
30+
}
31+
cur = cur!!.getChild(c)
32+
if (cur == null) {
33+
return
34+
}
35+
if (cur.end != null) {
36+
val s: String = cur.end!!
37+
collected.add(s)
38+
cur.end = null
39+
if (cur.len() === 0) {
40+
Tree.deleteWord(root, s)
41+
}
42+
}
43+
board[i][j] = '-'
44+
if (i > 0) {
45+
dfs(board, i - 1, j, cur, collected)
46+
}
47+
if (i + 1 < board.size) {
48+
dfs(board, i + 1, j, cur, collected)
49+
}
50+
if (j > 0) {
51+
dfs(board, i, j - 1, cur, collected)
52+
}
53+
if (j + 1 < board[0].size) {
54+
dfs(board, i, j + 1, cur, collected)
55+
}
56+
board[i][j] = c
57+
}
58+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g0201_0300.s0212_word_search_ii
2+
3+
class Tree {
4+
private var children: Array<Tree?>? = null
5+
var end: String? = null
6+
private var length = 0
7+
private fun addChild(c: Char): Tree? {
8+
val i = c.code - 'a'.code
9+
if (children == null) {
10+
children = arrayOfNulls(26)
11+
}
12+
if (children!![i] == null) {
13+
children!![i] = Tree()
14+
++length
15+
}
16+
return children!![i]
17+
}
18+
19+
fun getChild(c: Char): Tree? {
20+
return if (children == null) null else children!![c.code - 'a'.code]
21+
}
22+
23+
fun len(): Int {
24+
return length
25+
}
26+
27+
companion object {
28+
fun addWord(root: Tree?, word: String) {
29+
var t = root
30+
for (c in word.toCharArray()) {
31+
t = t!!.addChild(c)
32+
}
33+
t!!.end = word
34+
}
35+
36+
fun deleteWord(root: Tree?, word: String) {
37+
var root = root
38+
var toDelOn = root
39+
var toDel = word[0]
40+
for (c in word.toCharArray()) {
41+
if (root!!.length > 1) {
42+
toDelOn = root
43+
toDel = c
44+
}
45+
root = root.getChild(c)
46+
requireNotNull(root)
47+
}
48+
toDelOn!!.children!![toDel.code - 'a'.code] = null
49+
--toDelOn.length
50+
require(root!!.length == 0)
51+
}
52+
}
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
212\. Word Search II
2+
3+
Hard
4+
5+
Given an `m x n` `board` of characters and a list of strings `words`, return _all words on the board_.
6+
7+
Each word must be constructed from letters of sequentially adjacent cells, where **adjacent cells** are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/11/07/search1.jpg)
12+
13+
**Input:** board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
14+
15+
**Output:** ["eat","oath"]
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2020/11/07/search2.jpg)
20+
21+
**Input:** board = [["a","b"],["c","d"]], words = ["abcb"]
22+
23+
**Output:** []
24+
25+
**Constraints:**
26+
27+
* `m == board.length`
28+
* `n == board[i].length`
29+
* `1 <= m, n <= 12`
30+
* `board[i][j]` is a lowercase English letter.
31+
* <code>1 <= words.length <= 3 * 10<sup>4</sup></code>
32+
* `1 <= words[i].length <= 10`
33+
* `words[i]` consists of lowercase English letters.
34+
* All the strings of `words` are unique.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g0201_0300.s0213_house_robber_ii
2+
3+
// #Medium #Array #Dynamic_Programming #Algorithm_II_Day_12_Dynamic_Programming
4+
// #Dynamic_Programming_I_Day_3 #Udemy_Dynamic_Programming
5+
// #2022_10_24_Time_257_ms_(59.62%)_Space_34.3_MB_(80.77%)
6+
7+
class Solution {
8+
fun rob(nums: IntArray): Int {
9+
val n = nums.size
10+
if (n == 0) {
11+
return 0
12+
}
13+
if (n == 1) {
14+
return nums[0]
15+
}
16+
if (n == 2) {
17+
return Math.max(nums[0], nums[1])
18+
}
19+
if (n == 3) {
20+
return Math.max(nums[0], Math.max(nums[1], nums[2]))
21+
}
22+
var max = Int.MIN_VALUE
23+
val inc = IntArray(n)
24+
val exc = IntArray(n)
25+
inc[0] = nums[0]
26+
exc[0] = 0
27+
inc[1] = nums[0]
28+
exc[1] = nums[1]
29+
inc[2] = nums[2] + nums[0]
30+
exc[2] = nums[2]
31+
for (i in 3 until n - 1) {
32+
inc[i] = Math.max(inc[i - 2], inc[i - 3]) + nums[i]
33+
exc[i] = Math.max(exc[i - 2], exc[i - 3]) + nums[i]
34+
}
35+
inc[n - 1] = inc[n - 2]
36+
exc[n - 1] = Math.max(exc[n - 3], exc[n - 4]) + nums[n - 1]
37+
for (i in 0 until n) {
38+
max = Math.max(max, Math.max(inc[i], exc[i]))
39+
}
40+
return max
41+
}
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
213\. House Robber II
2+
3+
Medium
4+
5+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatically contact the police if two adjacent houses were broken into on the same night**.
6+
7+
Given an integer array `nums` representing the amount of money of each house, return _the maximum amount of money you can rob tonight **without alerting the police**_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [2,3,2]
12+
13+
**Output:** 3
14+
15+
**Explanation:** You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [1,2,3,1]
20+
21+
**Output:** 4
22+
23+
**Explanation:** Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [1,2,3]
28+
29+
**Output:** 3
30+
31+
**Constraints:**
32+
33+
* `1 <= nums.length <= 100`
34+
* `0 <= nums[i] <= 1000`

0 commit comments

Comments
 (0)