Skip to content

Commit 7aa7e3a

Browse files
authored
Added tasks 896, 897, 898, 899
1 parent 62b0432 commit 7aa7e3a

File tree

13 files changed

+355
-0
lines changed

13 files changed

+355
-0
lines changed

README.md

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

15051505
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15061506
|-|-|-|-|-|-
1507+
| 0896 |[Monotonic Array](src/main/kotlin/g0801_0900/s0896_monotonic_array/Solution.kt)| Easy | Array | 576 | 90.91
15071508
| 0028 |[Find the Index of the First Occurrence in a String](src.save/main/kotlin/g0001_0100/s0028_implement_strstr/Solution.kt)| Easy | Top_Interview_Questions, String, Two_Pointers, String_Matching | 257 | 32.35
15081509

15091510
#### Day 2
@@ -1725,6 +1726,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
17251726
|------|----------------|-------------|-------------|----------|---------
17261727
| 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
17271728
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
1729+
| 0899 |[Orderly Queue](src/main/kotlin/g0801_0900/s0899_orderly_queue/Solution.kt)| Hard | String, Math, Sorting | 148 | 100.00
1730+
| 0898 |[Bitwise ORs of Subarrays](src/main/kotlin/g0801_0900/s0898_bitwise_ors_of_subarrays/Solution.kt)| Medium | Array, Dynamic_Programming, Bit_Manipulation | 812 | 100.00
1731+
| 0897 |[Increasing Order Search Tree](src/main/kotlin/g0801_0900/s0897_increasing_order_search_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Stack, Binary_Search_Tree | 128 | 85.71
1732+
| 0896 |[Monotonic Array](src/main/kotlin/g0801_0900/s0896_monotonic_array/Solution.kt)| Easy | Array, Programming_Skills_II_Day_1 | 576 | 90.91
17281733
| 0895 |[Maximum Frequency Stack](src/main/kotlin/g0801_0900/s0895_maximum_frequency_stack/FreqStack.kt)| Hard | Hash_Table, Stack, Design, Ordered_Set | 617 | 100.00
17291734
| 0894 |[All Possible Full Binary Trees](src/main/kotlin/g0801_0900/s0894_all_possible_full_binary_trees/Solution.kt)| Medium | Dynamic_Programming, Tree, Binary_Tree, Recursion, Memoization | 257 | 100.00
17301735
| 0893 |[Groups of Special-Equivalent Strings](src/main/kotlin/g0801_0900/s0893_groups_of_special_equivalent_strings/Solution.kt)| Medium | Array, String, Hash_Table | 141 | 100.00
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0801_0900.s0896_monotonic_array
2+
3+
// #Easy #Array #Programming_Skills_II_Day_1 #2023_04_12_Time_576_ms_(90.91%)_Space_55.5_MB_(95.45%)
4+
5+
class Solution {
6+
fun isMonotonic(nums: IntArray): Boolean {
7+
var i = 0
8+
while (i < nums.size - 1) {
9+
if (nums[i] > nums[i + 1]) {
10+
break
11+
}
12+
i++
13+
}
14+
if (i == nums.size - 1) {
15+
return true
16+
}
17+
i = 0
18+
while (i < nums.size - 1) {
19+
if (nums[i] < nums[i + 1]) {
20+
break
21+
}
22+
i++
23+
}
24+
return i == nums.size - 1
25+
}
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
896\. Monotonic Array
2+
3+
Easy
4+
5+
An array is **monotonic** if it is either monotone increasing or monotone decreasing.
6+
7+
An array `nums` is monotone increasing if for all `i <= j`, `nums[i] <= nums[j]`. An array `nums` is monotone decreasing if for all `i <= j`, `nums[i] >= nums[j]`.
8+
9+
Given an integer array `nums`, return `true` _if the given array is monotonic, or_ `false` _otherwise_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,2,3]
14+
15+
**Output:** true
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [6,5,4,4]
20+
21+
**Output:** true
22+
23+
**Example 3:**
24+
25+
**Input:** nums = [1,3,2]
26+
27+
**Output:** false
28+
29+
**Constraints:**
30+
31+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
32+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0801_0900.s0897_increasing_order_search_tree
2+
3+
// #Easy #Depth_First_Search #Tree #Binary_Tree #Stack #Binary_Search_Tree
4+
// #2023_04_12_Time_128_ms_(85.71%)_Space_34_MB_(14.29%)
5+
6+
import com_github_leetcode.TreeNode
7+
import java.util.LinkedList
8+
9+
/*
10+
* Example:
11+
* var ti = TreeNode(5)
12+
* var v = ti.`val`
13+
* Definition for a binary tree node.
14+
* class TreeNode(var `val`: Int) {
15+
* var left: TreeNode? = null
16+
* var right: TreeNode? = null
17+
* }
18+
*/
19+
class Solution {
20+
fun increasingBST(root: TreeNode?): TreeNode {
21+
val list: MutableList<TreeNode> = LinkedList<TreeNode>()
22+
traverse(root, list)
23+
for (i in 1 until list.size) {
24+
list[i - 1].right = list[i]
25+
list[i].left = null
26+
}
27+
return list[0]
28+
}
29+
30+
private fun traverse(root: TreeNode?, list: MutableList<TreeNode>) {
31+
if (root != null) {
32+
traverse(root.left, list)
33+
list.add(root)
34+
traverse(root.right, list)
35+
}
36+
}
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
897\. Increasing Order Search Tree
2+
3+
Easy
4+
5+
Given the `root` of a binary search tree, rearrange the tree in **in-order** so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/11/17/ex1.jpg)
10+
11+
**Input:** root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
12+
13+
**Output:** [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/11/17/ex2.jpg)
18+
19+
**Input:** root = [5,1,7]
20+
21+
**Output:** [1,null,5,null,7]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the given tree will be in the range `[1, 100]`.
26+
* `0 <= Node.val <= 1000`
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0801_0900.s0898_bitwise_ors_of_subarrays
2+
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation
4+
// #2023_04_12_Time_812_ms_(100.00%)_Space_60.1_MB_(100.00%)
5+
6+
class Solution {
7+
fun subarrayBitwiseORs(arr: IntArray): Int {
8+
val set: MutableSet<Int> = HashSet()
9+
for (i in arr.indices) {
10+
set.add(arr[i])
11+
for (j in i - 1 downTo 0) {
12+
if (arr[i] or arr[j] == arr[j]) {
13+
break
14+
}
15+
arr[j] = arr[j] or arr[i]
16+
set.add(arr[j])
17+
}
18+
}
19+
return set.size
20+
}
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
898\. Bitwise ORs of Subarrays
2+
3+
Medium
4+
5+
Given an integer array `arr`, return _the number of distinct bitwise ORs of all the non-empty subarrays of_ `arr`.
6+
7+
The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
8+
9+
A **subarray** is a contiguous non-empty sequence of elements within an array.
10+
11+
**Example 1:**
12+
13+
**Input:** arr = [0]
14+
15+
**Output:** 1
16+
17+
**Explanation:** There is only one possible result: 0.
18+
19+
**Example 2:**
20+
21+
**Input:** arr = [1,1,2]
22+
23+
**Output:** 3
24+
25+
**Explanation:** The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2].
26+
27+
These yield the results 1, 1, 2, 1, 3, 3.
28+
29+
There are 3 unique values, so the answer is 3.
30+
31+
**Example 3:**
32+
33+
**Input:** arr = [1,2,4]
34+
35+
**Output:** 6
36+
37+
**Explanation:** The possible results are 1, 2, 3, 4, 6, and 7.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= arr.length <= 5 * 10<sup>4</sup></code>
42+
* <code>0 <= arr[i] <= 10<sup>9</sup></code>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0801_0900.s0899_orderly_queue
2+
3+
// #Hard #String #Math #Sorting #2023_04_12_Time_148_ms_(100.00%)_Space_35.6_MB_(66.67%)
4+
5+
class Solution {
6+
fun orderlyQueue(s: String, k: Int): String {
7+
if (k > 1) {
8+
val ans = s.toCharArray()
9+
ans.sort()
10+
return String(ans)
11+
}
12+
var min = 'z'
13+
val list = ArrayList<Int>()
14+
for (element in s) {
15+
if (element < min) {
16+
min = element
17+
}
18+
}
19+
for (i in s.indices) {
20+
if (s[i] == min) {
21+
list.add(i)
22+
}
23+
}
24+
var ans = s
25+
for (integer in list) {
26+
val after = s.substring(0, integer)
27+
val before = s.substring(integer)
28+
val f = before + after
29+
if (f < ans) {
30+
ans = f
31+
}
32+
}
33+
return ans
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
899\. Orderly Queue
2+
3+
Hard
4+
5+
You are given a string `s` and an integer `k`. You can choose one of the first `k` letters of `s` and append it at the end of the string..
6+
7+
Return _the lexicographically smallest string you could have after applying the mentioned step any number of moves_.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "cba", k = 1
12+
13+
**Output:** "acb"
14+
15+
**Explanation:**
16+
17+
In the first move, we move the 1<sup>st</sup> character 'c' to the end, obtaining the string "bac".
18+
19+
In the second move, we move the 1<sup>st</sup> character 'b' to the end, obtaining the final result "acb".
20+
21+
**Example 2:**
22+
23+
**Input:** s = "baaca", k = 3
24+
25+
**Output:** "aaabc"
26+
27+
**Explanation:**
28+
29+
In the first move, we move the 1<sup>st</sup> character 'b' to the end, obtaining the string "aacab".
30+
31+
In the second move, we move the 3<sup>rd</sup> character 'c' to the end, obtaining the final result "aaabc".
32+
33+
**Constraints:**
34+
35+
* `1 <= k <= s.length <= 1000`
36+
* `s` consist of lowercase English letters.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0801_0900.s0896_monotonic_array
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 isMonotonic() {
10+
assertThat(Solution().isMonotonic(intArrayOf(1, 2, 2, 3)), equalTo(true))
11+
}
12+
13+
@Test
14+
fun isMonotonic2() {
15+
assertThat(Solution().isMonotonic(intArrayOf(6, 5, 4, 4)), equalTo(true))
16+
}
17+
18+
@Test
19+
fun isMonotonic3() {
20+
assertThat(Solution().isMonotonic(intArrayOf(1, 3, 2)), equalTo(false))
21+
}
22+
}

0 commit comments

Comments
 (0)