Skip to content

Commit cb64f93

Browse files
authored
Added tasks 231, 232, 233, 235.
1 parent cb297de commit cb64f93

File tree

13 files changed

+363
-0
lines changed

13 files changed

+363
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
420420

421421
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
422422
|-|-|-|-|-|-
423+
| 0232 |[Implement Queue using Stacks](src/main/kotlin/g0201_0300/s0232_implement_queue_using_stacks/MyQueue.kt)| Easy | Stack, Design, Queue | 258 | 70.86
423424
| 0217 |[Contains Duplicate](src/main/kotlin/g0201_0300/s0217_contains_duplicate/Solution.kt)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting | 719 | 73.49
424425

425426
#### Day 12 Class and Object
@@ -727,6 +728,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
727728
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
728729
|-|-|-|-|-|-
729730
| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 330 | 41.38
731+
| 0235 |[Lowest Common Ancestor of a Binary Search Tree](src/main/kotlin/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 404 | 75.59
730732

731733
#### Day 9 Graph/BFS/DFS
732734

@@ -878,6 +880,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
878880

879881
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
880882
|-|-|-|-|-|-
883+
| 0232 |[Implement Queue using Stacks](src/main/kotlin/g0201_0300/s0232_implement_queue_using_stacks/MyQueue.kt)| Easy | Stack, Design, Queue | 258 | 70.86
881884
| 0155 |[Min Stack](src/main/kotlin/g0101_0200/s0155_min_stack/MinStack.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design | 331 | 84.88
882885
| 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 | 689 | 61.00
883886

@@ -1143,6 +1146,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
11431146
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11441147
|-|-|-|-|-|-
11451148
| 0020 |[Valid Parentheses](src/main/kotlin/g0001_0100/s0020_valid_parentheses/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack | 226 | 72.53
1149+
| 0232 |[Implement Queue using Stacks](src/main/kotlin/g0201_0300/s0232_implement_queue_using_stacks/MyQueue.kt)| Easy | Stack, Design, Queue | 258 | 70.86
11461150

11471151
#### Day 10 Tree
11481152

@@ -1177,6 +1181,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
11771181
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11781182
|-|-|-|-|-|-
11791183
| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 330 | 41.38
1184+
| 0235 |[Lowest Common Ancestor of a Binary Search Tree](src/main/kotlin/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 404 | 75.59
11801185

11811186
### Data Structure II
11821187

@@ -1400,6 +1405,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
14001405

14011406
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14021407
|-|-|-|-|-|-
1408+
| 0231 |[Power of Two](src/main/kotlin/g0201_0300/s0231_power_of_two/Solution.kt)| Easy | Math, Bit_Manipulation, Recursion | 161 | 86.81
14031409
| 0191 |[Number of 1 Bits](src/main/kotlin/g0101_0200/s0191_number_of_1_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation | 237 | 68.44
14041410

14051411
#### Day 14 Bit Manipulation
@@ -1580,7 +1586,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15801586
| 0239 |[Sliding Window Maximum](src/main/kotlin/g0201_0300/s0239_sliding_window_maximum/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays | 1059 | 86.14
15811587
| 0238 |[Product of Array Except Self](src/main/kotlin/g0201_0300/s0238_product_of_array_except_self/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Data_Structure_II_Day_5_Array, Udemy_Arrays | 669 | 48.96
15821588
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/kotlin/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_18_Tree, Udemy_Tree_Stack_Queue | 386 | 45.21
1589+
| 0235 |[Lowest Common Ancestor of a Binary Search Tree](src/main/kotlin/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree | 404 | 75.59
15831590
| 0234 |[Palindrome Linked List](src/main/kotlin/g0201_0300/s0234_palindrome_linked_list/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Stack, Linked_List, Recursion, Level_2_Day_3_Linked_List, Udemy_Linked_List | 641 | 79.53
1591+
| 0233 |[Number of Digit One](src/main/kotlin/g0201_0300/s0233_number_of_digit_one/Solution.kt)| Hard | Dynamic_Programming, Math, Recursion | 129 | 100.00
1592+
| 0232 |[Implement Queue using Stacks](src/main/kotlin/g0201_0300/s0232_implement_queue_using_stacks/MyQueue.kt)| Easy | Stack, Design, Queue, Data_Structure_I_Day_9_Stack_Queue, Programming_Skills_I_Day_11_Containers_and_Libraries, Level_2_Day_16_Design | 258 | 70.86
1593+
| 0231 |[Power of Two](src/main/kotlin/g0201_0300/s0231_power_of_two/Solution.kt)| Easy | Math, Bit_Manipulation, Recursion, Algorithm_I_Day_13_Bit_Manipulation | 161 | 86.81
15841594
| 0230 |[Kth Smallest Element in a BST](src/main/kotlin/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_II_Day_17_Tree, Level_2_Day_9_Binary_Search_Tree | 393 | 33.33
15851595
| 0229 |[Majority Element II](src/main/kotlin/g0201_0300/s0229_majority_element_ii/Solution.kt)| Medium | Array, Hash_Table, Sorting, Counting | 408 | 71.21
15861596
| 0228 |[Summary Ranges](src/main/kotlin/g0201_0300/s0228_summary_ranges/Solution.kt)| Easy | Array | 169 | 91.89
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package g0201_0300.s0231_power_of_two
2+
3+
// #Easy #Math #Bit_Manipulation #Recursion #Algorithm_I_Day_13_Bit_Manipulation
4+
// #2022_10_26_Time_161_ms_(86.81%)_Space_33.6_MB_(88.19%)
5+
6+
class Solution {
7+
fun isPowerOfTwo(n: Int): Boolean {
8+
return n > 0 && Integer.bitCount(n) == 1
9+
}
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
231\. Power of Two
2+
3+
Easy
4+
5+
Given an integer `n`, return _`true` if it is a power of two. Otherwise, return `false`_.
6+
7+
An integer `n` is a power of two, if there exists an integer `x` such that <code>n == 2<sup>x</sup></code>.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 1
12+
13+
**Output:** true
14+
15+
**Explanation:** 2<sup>0</sup> = 1
16+
17+
**Example 2:**
18+
19+
**Input:** n = 16
20+
21+
**Output:** true
22+
23+
**Explanation:** 2<sup>4</sup> = 16
24+
25+
**Example 3:**
26+
27+
**Input:** n = 3
28+
29+
**Output:** false
30+
31+
**Constraints:**
32+
33+
* <code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code>
34+
35+
**Follow up:** Could you solve it without loops/recursion?
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0201_0300.s0232_implement_queue_using_stacks
2+
3+
// #Easy #Stack #Design #Queue #Data_Structure_I_Day_9_Stack_Queue
4+
// #Programming_Skills_I_Day_11_Containers_and_Libraries #Level_2_Day_16_Design
5+
// #2022_10_26_Time_258_ms_(70.86%)_Space_36_MB_(58.94%)
6+
7+
import java.util.Deque
8+
import java.util.LinkedList
9+
10+
class MyQueue() {
11+
private val deque: Deque<Int> = LinkedList()
12+
13+
fun push(x: Int) {
14+
deque.add(x)
15+
}
16+
17+
fun pop(): Int = deque.removeFirst()
18+
19+
fun peek(): Int = deque.first()
20+
21+
fun empty(): Boolean = deque.isEmpty()
22+
}
23+
24+
/*
25+
* Your MyQueue object will be instantiated and called as such:
26+
* var obj = MyQueue()
27+
* obj.push(x)
28+
* var param_2 = obj.pop()
29+
* var param_3 = obj.peek()
30+
* var param_4 = obj.empty()
31+
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
232\. Implement Queue using Stacks
2+
3+
Easy
4+
5+
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (`push`, `peek`, `pop`, and `empty`).
6+
7+
Implement the `MyQueue` class:
8+
9+
* `void push(int x)` Pushes element x to the back of the queue.
10+
* `int pop()` Removes the element from the front of the queue and returns it.
11+
* `int peek()` Returns the element at the front of the queue.
12+
* `boolean empty()` Returns `true` if the queue is empty, `false` otherwise.
13+
14+
**Notes:**
15+
16+
* You must use **only** standard operations of a stack, which means only `push to top`, `peek/pop from top`, `size`, and `is empty` operations are valid.
17+
* Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
18+
19+
**Example 1:**
20+
21+
**Input:**
22+
23+
["MyQueue", "push", "push", "peek", "pop", "empty"]
24+
[[], [1], [2], [], [], []]
25+
26+
**Output:**
27+
28+
[null, null, null, 1, 1, false]
29+
30+
**Explanation:**
31+
32+
MyQueue myQueue = new MyQueue();
33+
myQueue.push(1); // queue is: [1]
34+
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
35+
myQueue.peek(); // return 1
36+
myQueue.pop(); // return 1, queue is [2]
37+
myQueue.empty(); // return false
38+
39+
**Constraints:**
40+
41+
* `1 <= x <= 9`
42+
* At most `100` calls will be made to `push`, `pop`, `peek`, and `empty`.
43+
* All the calls to `pop` and `peek` are valid.
44+
45+
**Follow-up:** Can you implement the queue such that each operation is **[amortized](https://en.wikipedia.org/wiki/Amortized_analysis)** `O(1)` time complexity? In other words, performing `n` operations will take overall `O(n)` time even if one of those operations may take longer.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0201_0300.s0233_number_of_digit_one
2+
3+
// #Hard #Dynamic_Programming #Math #Recursion
4+
// #2022_10_26_Time_129_ms_(100.00%)_Space_32.6_MB_(100.00%)
5+
6+
class Solution {
7+
fun countDigitOne(n: Int): Int {
8+
var ans = 0
9+
var k = n
10+
var cum = 0
11+
var curr10 = 1
12+
while (k > 0) {
13+
val rem = k % 10
14+
val q = k / 10
15+
ans += if (rem == 0) {
16+
q * curr10
17+
} else if (rem == 1) {
18+
q * curr10 + cum + 1
19+
} else {
20+
(q + 1) * curr10
21+
}
22+
k = q
23+
cum += rem * curr10
24+
curr10 *= 10
25+
}
26+
return ans
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
233\. Number of Digit One
2+
3+
Hard
4+
5+
Given an integer `n`, count _the total number of digit_ `1` _appearing in all non-negative integers less than or equal to_ `n`.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 13
10+
11+
**Output:** 6
12+
13+
**Example 2:**
14+
15+
**Input:** n = 0
16+
17+
**Output:** 0
18+
19+
**Constraints:**
20+
21+
* <code>0 <= n <= 10<sup>9</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0201_0300.s0235_lowest_common_ancestor_of_a_binary_search_tree
2+
3+
// #Easy #Depth_First_Search #Tree #Binary_Tree #Binary_Search_Tree #Data_Structure_I_Day_14_Tree
4+
// #Level_1_Day_8_Binary_Search_Tree #2022_10_26_Time_404_ms_(75.59%)_Space_46.1_MB_(78.74%)
5+
6+
import com_github_leetcode.TreeNode
7+
8+
/*
9+
* Definition for a binary tree node.
10+
* class TreeNode(var `val`: Int = 0) {
11+
* var left: TreeNode? = null
12+
* var right: TreeNode? = null
13+
* }
14+
*/
15+
class Solution {
16+
fun lowestCommonAncestor(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode? {
17+
if (root == null) {
18+
return null
19+
}
20+
if (p!!.`val` < root.`val` && q!!.`val` < root.`val`) {
21+
return lowestCommonAncestor(root.left, q, p)
22+
}
23+
if (p.`val` > root.`val` && q!!.`val` > root.`val`) {
24+
return lowestCommonAncestor(root.right, q, p)
25+
}
26+
return root
27+
}
28+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
235\. Lowest Common Ancestor of a Binary Search Tree
2+
3+
Medium
4+
5+
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
6+
7+
According to the [definition of LCA on Wikipedia](https://en.wikipedia.org/wiki/Lowest_common_ancestor): “The lowest common ancestor is defined between two nodes `p` and `q` as the lowest node in `T` that has both `p` and `q` as descendants (where we allow **a node to be a descendant of itself**).”
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png)
12+
13+
**Input:** root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
14+
15+
**Output:** 6
16+
17+
**Explanation:** The LCA of nodes 2 and 8 is 6.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png)
22+
23+
**Input:** root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
24+
25+
**Output:** 2
26+
27+
**Explanation:** The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
28+
29+
**Example 3:**
30+
31+
**Input:** root = [2,1], p = 2, q = 1
32+
33+
**Output:** 2
34+
35+
**Constraints:**
36+
37+
* The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.
38+
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
39+
* All `Node.val` are **unique**.
40+
* `p != q`
41+
* `p` and `q` will exist in the BST.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0201_0300.s0231_power_of_two
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 isPowerOfTwo() {
10+
assertThat(Solution().isPowerOfTwo(1), equalTo(true))
11+
}
12+
13+
@Test
14+
fun isPowerOfTwo2() {
15+
assertThat(Solution().isPowerOfTwo(16), equalTo(true))
16+
}
17+
18+
@Test
19+
fun isPowerOfTwo3() {
20+
assertThat(Solution().isPowerOfTwo(3), equalTo(false))
21+
}
22+
23+
@Test
24+
fun isPowerOfTwo4() {
25+
assertThat(Solution().isPowerOfTwo(4), equalTo(true))
26+
}
27+
28+
@Test
29+
fun isPowerOfTwo5() {
30+
assertThat(Solution().isPowerOfTwo(5), equalTo(false))
31+
}
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0201_0300.s0232_implement_queue_using_stacks
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class MyQueueTest {
8+
@Test
9+
fun queueUsingStacks() {
10+
val myQueue = MyQueue()
11+
myQueue.push(1)
12+
myQueue.push(2)
13+
assertThat(myQueue.peek(), equalTo(1))
14+
assertThat(myQueue.pop(), equalTo(1))
15+
assertThat(myQueue.empty(), equalTo(false))
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0201_0300.s0233_number_of_digit_one
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 countDigitOne() {
10+
assertThat(Solution().countDigitOne(13), equalTo(6))
11+
}
12+
13+
@Test
14+
fun countDigitOne2() {
15+
assertThat(Solution().countDigitOne(0), equalTo(0))
16+
}
17+
}

0 commit comments

Comments
 (0)