Skip to content

Commit d1c7179

Browse files
authored
Added tasks 215-283
1 parent 928d4d0 commit d1c7179

File tree

31 files changed

+875
-0
lines changed

31 files changed

+875
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g0201_0300.s0215_kth_largest_element_in_an_array
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting #Heap_Priority_Queue
4+
// #Divide_and_Conquer #Quickselect #Data_Structure_II_Day_20_Heap_Priority_Queue
5+
// #Big_O_Time_O(n*log(n))_Space_O(log(n)) #2023_11_07_Time_799_ms_(95.45%)_Space_77.8_MB_(62.12%)
6+
7+
import scala.util.Sorting
8+
9+
object Solution {
10+
def findKthLargest(nums: Array[Int], k: Int): Int = {
11+
val n = nums.length
12+
Sorting.quickSort(nums)
13+
nums(n - k)
14+
}
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
215\. Kth Largest Element in an Array
2+
3+
Medium
4+
5+
Given an integer array `nums` and an integer `k`, return _the_ <code>k<sup>th</sup></code> _largest element in the array_.
6+
7+
Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [3,2,1,5,6,4], k = 2
12+
13+
**Output:** 5
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [3,2,3,1,2,4,5,5,6], k = 4
18+
19+
**Output:** 4
20+
21+
**Constraints:**
22+
23+
* <code>1 <= k <= nums.length <= 10<sup>4</sup></code>
24+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0201_0300.s0221_maximal_square
2+
3+
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Matrix
4+
// #Dynamic_Programming_I_Day_16 #Big_O_Time_O(m*n)_Space_O(m*n)
5+
// #2023_11_07_Time_626_ms_(100.00%)_Space_65.3_MB_(100.00%)
6+
7+
object Solution {
8+
def maximalSquare(matrix: Array[Array[Char]]): Int = {
9+
val m = matrix.length
10+
if (m == 0) {
11+
return 0
12+
}
13+
val n = matrix(0).length
14+
if (n == 0) {
15+
return 0
16+
}
17+
18+
val dp = Array.ofDim[Int](m + 1, n + 1)
19+
var max = 0
20+
21+
for (i <- 0 until m) {
22+
for (j <- 0 until n) {
23+
if (matrix(i)(j) == '1') {
24+
val next = 1 + Math.min(dp(i)(j), Math.min(dp(i + 1)(j), dp(i)(j + 1)))
25+
if (next > max) {
26+
max = next
27+
}
28+
dp(i + 1)(j + 1) = next
29+
}
30+
}
31+
}
32+
33+
max * max
34+
}
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
221\. Maximal Square
2+
3+
Medium
4+
5+
Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, _find the largest square containing only_ `1`'s _and return its area_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg)
10+
11+
**Input:** matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
12+
13+
**Output:** 4
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg)
18+
19+
**Input:** matrix = [["0","1"],["1","0"]]
20+
21+
**Output:** 1
22+
23+
**Example 3:**
24+
25+
**Input:** matrix = [["0"]]
26+
27+
**Output:** 0
28+
29+
**Constraints:**
30+
31+
* `m == matrix.length`
32+
* `n == matrix[i].length`
33+
* `1 <= m, n <= 300`
34+
* `matrix[i][j]` is `'0'` or `'1'`.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0201_0300.s0226_invert_binary_tree
2+
3+
// #Easy #Top_100_Liked_Questions #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
4+
// #Data_Structure_I_Day_12_Tree #Level_2_Day_6_Tree #Udemy_Tree_Stack_Queue
5+
// #Big_O_Time_O(n)_Space_O(n) #2023_11_07_Time_421_ms_(97.33%)_Space_58.9_MB_(6.67%)
6+
7+
import com_github_leetcode.TreeNode
8+
9+
/*
10+
* Definition for a binary tree node.
11+
* class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
12+
* var value: Int = _value
13+
* var left: TreeNode = _left
14+
* var right: TreeNode = _right
15+
* }
16+
*/
17+
object Solution {
18+
def invertTree(root: TreeNode): TreeNode = {
19+
if (root == null) {
20+
return null
21+
}
22+
val temp = root.left
23+
root.left = invertTree(root.right)
24+
root.right = invertTree(temp)
25+
root
26+
}
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
226\. Invert Binary Tree
2+
3+
Easy
4+
5+
Given the `root` of a binary tree, invert the tree, and return _its root_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg)
10+
11+
**Input:** root = [4,2,7,1,3,6,9]
12+
13+
**Output:** [4,7,2,9,6,3,1]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg)
18+
19+
**Input:** root = [2,1,3]
20+
21+
**Output:** [2,3,1]
22+
23+
**Example 3:**
24+
25+
**Input:** root = []
26+
27+
**Output:** []
28+
29+
**Constraints:**
30+
31+
* The number of nodes in the tree is in the range `[0, 100]`.
32+
* `-100 <= Node.val <= 100`
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0201_0300.s0230_kth_smallest_element_in_a_bst
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree
4+
// #Binary_Search_Tree #Data_Structure_II_Day_17_Tree #Level_2_Day_9_Binary_Search_Tree
5+
// #Big_O_Time_O(n)_Space_O(n) #2023_11_07_Time_503_ms_(91.30%)_Space_57.9_MB_(60.87%)
6+
7+
import com_github_leetcode.TreeNode
8+
9+
object Solution {
10+
var index = 0
11+
var value = -1
12+
13+
def kthSmallest(root: TreeNode, k: Int): Int = {
14+
index = 0
15+
value = -1
16+
if (root == null) -1
17+
else inorder(root, k)
18+
value
19+
}
20+
21+
// Using In order
22+
def inorder(root: TreeNode, k: Int): Unit = {
23+
if (root != null && index < k) {
24+
inorder(root.left, k)
25+
index += 1
26+
if (index == k) value = root.value
27+
inorder(root.right, k)
28+
}
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
230\. Kth Smallest Element in a BST
2+
3+
Medium
4+
5+
Given the `root` of a binary search tree, and an integer `k`, return _the_ <code>k<sup>th</sup></code> _smallest value (**1-indexed**) of all the values of the nodes in the tree_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg)
10+
11+
**Input:** root = [3,1,4,null,2], k = 1
12+
13+
**Output:** 1
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg)
18+
19+
**Input:** root = [5,3,6,2,4,null,null,1], k = 3
20+
21+
**Output:** 3
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is `n`.
26+
* <code>1 <= k <= n <= 10<sup>4</sup></code>
27+
* <code>0 <= Node.val <= 10<sup>4</sup></code>
28+
29+
**Follow up:** If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g0201_0300.s0234_palindrome_linked_list
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Two_Pointers #Stack #Linked_List
4+
// #Recursion #Level_2_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1)
5+
// #2023_11_07_Time_811_ms_(85.71%)_Space_67.7_MB_(78.57%)
6+
7+
import com_github_leetcode.ListNode
8+
9+
/*
10+
* Definition for singly-linked list.
11+
* class ListNode(_x: Int = 0, _next: ListNode = null) {
12+
* var next: ListNode = _next
13+
* var x: Int = _x
14+
* }
15+
*/
16+
object Solution {
17+
def isPalindrome(head: ListNode): Boolean = {
18+
var len = 0
19+
var right = head
20+
21+
// Calculate the length
22+
while (right != null) {
23+
right = right.next
24+
len += 1
25+
}
26+
27+
// Reverse the right half of the list
28+
len = len / 2
29+
right = head
30+
for (_ <- 0 until len) {
31+
right = right.next
32+
}
33+
34+
var prev: ListNode = null
35+
while (right != null) {
36+
val next = right.next
37+
right.next = prev
38+
prev = right
39+
right = next
40+
}
41+
var head2 = head
42+
// Compare left half and right half
43+
for (_ <- 0 until len) {
44+
if (prev != null && head2.x == prev.x) {
45+
head2 = head2.next
46+
prev = prev.next
47+
} else {
48+
return false
49+
}
50+
}
51+
52+
true
53+
}
54+
}

0 commit comments

Comments
 (0)