Skip to content

Commit cb297de

Browse files
authored
Added tasks 225, 227, 228, 229.
1 parent 81d138d commit cb297de

File tree

13 files changed

+468
-0
lines changed

13 files changed

+468
-0
lines changed

README.md

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

893893
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
894894
|-|-|-|-|-|-
895+
| 0227 |[Basic Calculator II](src/main/kotlin/g0201_0300/s0227_basic_calculator_ii/Solution.kt)| Medium | Top_Interview_Questions, String, Math, Stack | 383 | 62.50
895896

896897
#### Day 19 Union Find
897898

@@ -1581,7 +1582,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15811582
| 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
15821583
| 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
15831584
| 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
1585+
| 0229 |[Majority Element II](src/main/kotlin/g0201_0300/s0229_majority_element_ii/Solution.kt)| Medium | Array, Hash_Table, Sorting, Counting | 408 | 71.21
1586+
| 0228 |[Summary Ranges](src/main/kotlin/g0201_0300/s0228_summary_ranges/Solution.kt)| Easy | Array | 169 | 91.89
1587+
| 0227 |[Basic Calculator II](src/main/kotlin/g0201_0300/s0227_basic_calculator_ii/Solution.kt)| Medium | Top_Interview_Questions, String, Math, Stack, Level_2_Day_18_Stack | 383 | 62.50
15841588
| 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
1589+
| 0225 |[Implement Stack using Queues](src/main/kotlin/g0201_0300/s0225_implement_stack_using_queues/MyStack.kt)| Easy | Stack, Design, Queue | 248 | 73.44
15851590
| 0224 |[Basic Calculator](src/main/kotlin/g0201_0300/s0224_basic_calculator/Solution.kt)| Hard | String, Math, Stack, Recursion | 294 | 93.33
15861591
| 0223 |[Rectangle Area](src/main/kotlin/g0201_0300/s0223_rectangle_area/Solution.kt)| Medium | Math, Geometry | 291 | 66.67
15871592
| 0222 |[Count Complete Tree Nodes](src/main/kotlin/g0201_0300/s0222_count_complete_tree_nodes/Solution.kt)| |||
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package g0201_0300.s0225_implement_stack_using_queues
2+
3+
// #Easy #Stack #Design #Queue #2022_10_26_Time_248_ms_(73.44%)_Space_36.1_MB_(43.75%)
4+
5+
import java.util.LinkedList
6+
7+
class MyStack {
8+
private var queuePair = Pair(LinkedList<Int>(), LinkedList<Int>())
9+
private var top: Int? = null
10+
11+
fun push(x: Int) {
12+
queuePair.first.addLast(x)
13+
top = x
14+
}
15+
16+
fun pop(): Int {
17+
if (isQueuesEmpty()) {
18+
throw Exception()
19+
}
20+
val queuePair = selectSourceAndDestinationQueues(queuePair)
21+
var value = 0
22+
repeat(queuePair.first.size) {
23+
when (queuePair.first.size) {
24+
2 -> {
25+
top = queuePair.first.removeFirst()
26+
queuePair.second.addLast(top)
27+
}
28+
1 -> {
29+
value = queuePair.first.removeFirst()
30+
}
31+
else -> {
32+
queuePair.second.addLast(queuePair.first.removeFirst())
33+
}
34+
}
35+
}
36+
return value
37+
}
38+
39+
fun top(): Int {
40+
if (isQueuesEmpty()) {
41+
throw Exception()
42+
}
43+
return top!!
44+
}
45+
46+
fun empty(): Boolean {
47+
return isQueuesEmpty()
48+
}
49+
50+
private fun isQueuesEmpty(): Boolean {
51+
if (queuePair.first.isEmpty() && queuePair.second.isEmpty()) {
52+
return true
53+
}
54+
return false
55+
}
56+
57+
private fun selectSourceAndDestinationQueues(queuePair: Pair<LinkedList<Int>, LinkedList<Int>>):
58+
Pair<LinkedList<Int>, LinkedList<Int>> {
59+
return if (queuePair.first.isNotEmpty()) {
60+
Pair(queuePair.first, queuePair.second)
61+
} else {
62+
Pair(queuePair.second, queuePair.first)
63+
}
64+
}
65+
}
66+
67+
/*
68+
* Your MyStack object will be instantiated and called as such:
69+
* var obj = MyStack()
70+
* obj.push(x)
71+
* var param_2 = obj.pop()
72+
* var param_3 = obj.top()
73+
* var param_4 = obj.empty()
74+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
225\. Implement Stack using Queues
2+
3+
Easy
4+
5+
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`, `top`, `pop`, and `empty`).
6+
7+
Implement the `MyStack` class:
8+
9+
* `void push(int x)` Pushes element x to the top of the stack.
10+
* `int pop()` Removes the element on the top of the stack and returns it.
11+
* `int top()` Returns the element on the top of the stack.
12+
* `boolean empty()` Returns `true` if the stack is empty, `false` otherwise.
13+
14+
**Notes:**
15+
16+
* You must use **only** standard operations of a queue, which means that only `push to back`, `peek/pop from front`, `size` and `is empty` operations are valid.
17+
* Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.
18+
19+
**Example 1:**
20+
21+
**Input** ["MyStack", "push", "push", "top", "pop", "empty"] [[], [1], [2], [], [], []]
22+
23+
**Output:** [null, null, null, 2, 2, false]
24+
25+
**Explanation:**
26+
27+
MyStack myStack = new MyStack();
28+
myStack.push(1);
29+
myStack.push(2);
30+
myStack.top(); // return 2
31+
myStack.pop(); // return 2
32+
myStack.empty(); // return False
33+
34+
**Constraints:**
35+
36+
* `1 <= x <= 9`
37+
* At most `100` calls will be made to `push`, `pop`, `top`, and `empty`.
38+
* All the calls to `pop` and `top` are valid.
39+
40+
**Follow-up:** Can you implement the stack using only one queue?
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0201_0300.s0227_basic_calculator_ii
2+
3+
// #Medium #Top_Interview_Questions #String #Math #Stack #Level_2_Day_18_Stack
4+
// #2022_10_26_Time_383_ms_(62.50%)_Space_39.5_MB_(83.33%)
5+
6+
class Solution {
7+
fun calculate(s: String): Int {
8+
var sum = 0
9+
var tempSum = 0
10+
var num = 0
11+
var lastSign = '+'
12+
for (i in 0 until s.length) {
13+
val c = s[i]
14+
if (Character.isDigit(c)) {
15+
num = num * 10 + c.code - '0'.code
16+
}
17+
if (i == s.length - 1 || !Character.isDigit(c) && c != ' ') {
18+
when (lastSign) {
19+
'+' -> {
20+
sum += tempSum
21+
tempSum = num
22+
}
23+
'-' -> {
24+
sum += tempSum
25+
tempSum = -num
26+
}
27+
'*' -> tempSum *= num
28+
'/' -> if (num != 0) {
29+
tempSum /= num
30+
}
31+
}
32+
lastSign = c
33+
num = 0
34+
}
35+
}
36+
sum += tempSum
37+
return sum
38+
}
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
227\. Basic Calculator II
2+
3+
Medium
4+
5+
Given a string `s` which represents an expression, _evaluate this expression and return its value_.
6+
7+
The integer division should truncate toward zero.
8+
9+
You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.
10+
11+
**Note:** You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as `eval()`.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "3+2\*2"
16+
17+
**Output:** 7
18+
19+
**Example 2:**
20+
21+
**Input:** s = " 3/2 "
22+
23+
**Output:** 1
24+
25+
**Example 3:**
26+
27+
**Input:** s = " 3+5 / 2 "
28+
29+
**Output:** 5
30+
31+
**Constraints:**
32+
33+
* <code>1 <= s.length <= 3 * 10<sup>5</sup></code>
34+
* `s` consists of integers and operators `('+', '-', '*', '/')` separated by some number of spaces.
35+
* `s` represents **a valid expression**.
36+
* All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.
37+
* The answer is **guaranteed** to fit in a **32-bit integer**.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g0201_0300.s0228_summary_ranges
2+
3+
// #Easy #Array #2022_10_26_Time_169_ms_(91.89%)_Space_34_MB_(95.95%)
4+
5+
class Solution {
6+
fun summaryRanges(nums: IntArray): List<String> {
7+
val ranges: MutableList<String> = ArrayList()
8+
if (nums.size == 0) {
9+
return ranges
10+
}
11+
// size of array
12+
val n = nums.size
13+
// start of range
14+
var a = nums[0]
15+
// end of range
16+
var b = a
17+
val strB = StringBuilder()
18+
for (i in 1 until n) {
19+
// we need to make a decision if the next element
20+
// will expand the range
21+
// i starts at 1, not 0, because 1 is the next
22+
// candidate for expanding the range
23+
if (nums[i] != b + 1) {
24+
// only when our next element does not expand the range
25+
// do we add the range a->b to our list of ranges
26+
strB.append(a)
27+
if (a != b) {
28+
strB.append("->").append(b)
29+
}
30+
ranges.add(strB.toString())
31+
// since nums[i] is not accounted for by our range a->b
32+
// because nums[i] is not b+1, we need to set a and b
33+
// to this new range start point of bigger than b+1
34+
// maybe it is b+2? b+3? b+4? all we know is it is not b+1
35+
a = nums[i]
36+
b = a
37+
// Reset string builder
38+
strB.setLength(0)
39+
} else {
40+
// if the next element expands our range we do so
41+
b++
42+
}
43+
}
44+
// the only range that is not accounted for at this point is the last range
45+
// if our a and b are not equal then we add the range accordingly
46+
strB.append(a)
47+
if (a != b) {
48+
strB.append("->").append(b)
49+
}
50+
ranges.add(strB.toString())
51+
return ranges
52+
}
53+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
228\. Summary Ranges
2+
3+
Easy
4+
5+
You are given a **sorted unique** integer array `nums`.
6+
7+
Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is no integer `x` such that `x` is in one of the ranges but not in `nums`.
8+
9+
Each range `[a,b]` in the list should be output as:
10+
11+
* `"a->b"` if `a != b`
12+
* `"a"` if `a == b`
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [0,1,2,4,5,7]
17+
18+
**Output:** ["0->2","4->5","7"]
19+
20+
**Explanation:** The ranges are: [0,2] --> "0->2" [4,5] --> "4->5" [7,7] --> "7"
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [0,2,3,4,6,8,9]
25+
26+
**Output:** ["0","2->4","6","8->9"]
27+
28+
**Explanation:** The ranges are: [0,0] --> "0" [2,4] --> "2->4" [6,6] --> "6" [8,9] --> "8->9"
29+
30+
**Example 3:**
31+
32+
**Input:** nums = []
33+
34+
**Output:** []
35+
36+
**Example 4:**
37+
38+
**Input:** nums = [-1]
39+
40+
**Output:** ["-1"]
41+
42+
**Example 5:**
43+
44+
**Input:** nums = [0]
45+
46+
**Output:** ["0"]
47+
48+
**Constraints:**
49+
50+
* `0 <= nums.length <= 20`
51+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
52+
* All the values of `nums` are **unique**.
53+
* `nums` is sorted in ascending order.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g0201_0300.s0229_majority_element_ii
2+
3+
// #Medium #Array #Hash_Table #Sorting #Counting
4+
// #2022_10_26_Time_408_ms_(71.21%)_Space_47.7_MB_(60.61%)
5+
6+
class Solution {
7+
fun majorityElement(nums: IntArray): List<Int> {
8+
val results: MutableList<Int> = ArrayList()
9+
val len = nums.size
10+
var first = 0
11+
var second = 1
12+
var count1 = 0
13+
var count2 = 0
14+
for (temp in nums) {
15+
if (temp == first) {
16+
count1++
17+
} else if (temp == second) {
18+
count2++
19+
} else if (count1 == 0) {
20+
first = temp
21+
count1++
22+
} else if (count2 == 0) {
23+
second = temp
24+
count2++
25+
} else {
26+
count1--
27+
count2--
28+
}
29+
}
30+
count1 = 0
31+
count2 = 0
32+
for (temp in nums) {
33+
if (temp == first) {
34+
count1++
35+
}
36+
if (temp == second) {
37+
count2++
38+
}
39+
}
40+
if (count1 > len / 3) {
41+
results.add(first)
42+
}
43+
if (count2 > len / 3) {
44+
results.add(second)
45+
}
46+
return results
47+
}
48+
}

0 commit comments

Comments
 (0)