Skip to content

Commit 28c15b4

Browse files
authored
Added tasks 215-283
1 parent 132c6b5 commit 28c15b4

File tree

11 files changed

+688
-0
lines changed

11 files changed

+688
-0
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@
250250

251251
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
252252
|-|-|-|-|-|-
253+
| 0240 |[Search a 2D Matrix II](src/main/scala/g0201_0300/s0240_search_a_2d_matrix_ii)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Big_O_Time_O(n+m)_Space_O(1) | 579 | 66.67
253254

254255
#### Day 9
255256

@@ -404,6 +405,7 @@
404405
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
405406
|-|-|-|-|-|-
406407
| 0064 |[Minimum Path Sum](src/main/scala/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 521 | 92.86
408+
| 0221 |[Maximal Square](src/main/scala/g0201_0300/s0221_maximal_square)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 626 | 100.00
407409

408410
#### Day 17
409411

@@ -463,6 +465,7 @@
463465

464466
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
465467
|-|-|-|-|-|-
468+
| 0283 |[Move Zeroes](src/main/scala/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 572 | 86.11
466469

467470
#### Day 7 Array
468471

@@ -831,6 +834,7 @@
831834
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
832835
|-|-|-|-|-|-
833836
| 0019 |[Remove Nth Node From End of List](src/main/scala/g0001_0100/s0019_remove_nth_node_from_end_of_list)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Big_O_Time_O(L)_Space_O(L) | 492 | 52.63
837+
| 0234 |[Palindrome Linked List](src/main/scala/g0201_0300/s0234_palindrome_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Stack, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 811 | 85.71
834838

835839
#### Day 4 Linked List
836840

@@ -847,6 +851,7 @@
847851

848852
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
849853
|-|-|-|-|-|-
854+
| 0226 |[Invert Binary Tree](src/main/scala/g0201_0300/s0226_invert_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 421 | 97.33
850855

851856
#### Day 7 Tree
852857

@@ -864,6 +869,7 @@
864869

865870
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
866871
|-|-|-|-|-|-
872+
| 0230 |[Kth Smallest Element in a BST](src/main/scala/g0201_0300/s0230_kth_smallest_element_in_a_bst)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 503 | 91.30
867873

868874
#### Day 10 Graph/BFS/DFS
869875

@@ -959,11 +965,14 @@
959965
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
960966
|-|-|-|-|-|-
961967
| 0121 |[Best Time to Buy and Sell Stock](src/main/scala/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 762 | 98.45
968+
| 0283 |[Move Zeroes](src/main/scala/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 572 | 86.11
962969
| 0001 |[Two Sum](src/main/scala/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 517 | 86.66
963970
| 0189 |[Rotate Array](src/main/scala/g0101_0200/s0189_rotate_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 600 | 96.59
964971
| 0055 |[Jump Game](src/main/scala/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 622 | 88.31
965972
| 0075 |[Sort Colors](src/main/scala/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 450 | 84.21
973+
| 0238 |[Product of Array Except Self](src/main/scala/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 634 | 79.44
966974
| 0041 |[First Missing Positive](src/main/scala/g0001_0100/s0041_first_missing_positive)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 650 | 100.00
975+
| 0239 |[Sliding Window Maximum](src/main/scala/g0201_0300/s0239_sliding_window_maximum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Big_O_Time_O(n\*k)_Space_O(n+k) | 1149 | 86.67
967976

968977
#### Udemy Two Pointers
969978

@@ -1004,6 +1013,7 @@
10041013
| 0206 |[Reverse Linked List](src/main/scala/g0201_0300/s0206_reverse_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 474 | 87.50
10051014
| 0021 |[Merge Two Sorted Lists](src/main/scala/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 480 | 89.72
10061015
| 0160 |[Intersection of Two Linked Lists](src/main/scala/g0101_0200/s0160_intersection_of_two_linked_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(M+N)_Space_O(1) | 564 | 96.43
1016+
| 0234 |[Palindrome Linked List](src/main/scala/g0201_0300/s0234_palindrome_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Stack, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 811 | 85.71
10071017
| 0138 |[Copy List with Random Pointer](src/main/scala/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 477 | 96.00
10081018
| 0025 |[Reverse Nodes in k-Group](src/main/scala/g0001_0100/s0025_reverse_nodes_in_k_group)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 520 | 80.00
10091019
| 0146 |[LRU Cache](src/main/scala/g0101_0200/s0146_lru_cache)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 1590 | 85.19
@@ -1014,9 +1024,11 @@
10141024
|-|-|-|-|-|-
10151025
| 0094 |[Binary Tree Inorder Traversal](src/main/scala/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 456 | 68.42
10161026
| 0102 |[Binary Tree Level Order Traversal](src/main/scala/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 522 | 83.33
1027+
| 0226 |[Invert Binary Tree](src/main/scala/g0201_0300/s0226_invert_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 421 | 97.33
10171028
| 0104 |[Maximum Depth of Binary Tree](src/main/scala/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 472 | 90.83
10181029
| 0124 |[Binary Tree Maximum Path Sum](src/main/scala/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 523 | 89.47
10191030
| 0098 |[Validate Binary Search Tree](src/main/scala/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 507 | 70.21
1031+
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/scala/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 539 | 70.73
10201032

10211033
#### Udemy Trie and Heap
10221034

@@ -1136,6 +1148,7 @@
11361148

11371149
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11381150
|-|-|-|-|-|-
1151+
| 0226 |[Invert Binary Tree](src/main/scala/g0201_0300/s0226_invert_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 421 | 97.33
11391152

11401153
#### Day 13 Tree
11411154

@@ -1175,11 +1188,13 @@
11751188

11761189
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11771190
|-|-|-|-|-|-
1191+
| 0240 |[Search a 2D Matrix II](src/main/scala/g0201_0300/s0240_search_a_2d_matrix_ii)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Big_O_Time_O(n+m)_Space_O(1) | 579 | 66.67
11781192

11791193
#### Day 5 Array
11801194

11811195
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11821196
|-|-|-|-|-|-
1197+
| 0238 |[Product of Array Except Self](src/main/scala/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 634 | 79.44
11831198

11841199
#### Day 6 String
11851200

@@ -1249,11 +1264,13 @@
12491264

12501265
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12511266
|-|-|-|-|-|-
1267+
| 0230 |[Kth Smallest Element in a BST](src/main/scala/g0201_0300/s0230_kth_smallest_element_in_a_bst)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 503 | 91.30
12521268

12531269
#### Day 18 Tree
12541270

12551271
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12561272
|-|-|-|-|-|-
1273+
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/scala/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 539 | 70.73
12571274

12581275
#### Day 19 Graph
12591276

@@ -1264,6 +1281,7 @@
12641281

12651282
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12661283
|-|-|-|-|-|-
1284+
| 0215 |[Kth Largest Element in an Array](src/main/scala/g0201_0300/s0215_kth_largest_element_in_an_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 799 | 95.45
12671285

12681286
#### Day 21 Heap Priority Queue
12691287

@@ -1288,6 +1306,7 @@
12881306

12891307
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12901308
|-|-|-|-|-|-
1309+
| 0283 |[Move Zeroes](src/main/scala/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 572 | 86.11
12911310

12921311
#### Day 4 Two Pointers
12931312

@@ -1356,6 +1375,16 @@
13561375

13571376
| # | Title | Difficulty | Tag | Time, ms | Time, %
13581377
|------|----------------|-------------|-------------|----------|--------
1378+
| 0283 |[Move Zeroes](src/main/scala/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Programming_Skills_I_Day_6_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 572 | 86.11
1379+
| 0240 |[Search a 2D Matrix II](src/main/scala/g0201_0300/s0240_search_a_2d_matrix_ii)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Data_Structure_II_Day_4_Array, Binary_Search_II_Day_8, Big_O_Time_O(n+m)_Space_O(1) | 579 | 66.67
1380+
| 0239 |[Sliding Window Maximum](src/main/scala/g0201_0300/s0239_sliding_window_maximum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays, Big_O_Time_O(n\*k)_Space_O(n+k) | 1149 | 86.67
1381+
| 0238 |[Product of Array Except Self](src/main/scala/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Data_Structure_II_Day_5_Array, Udemy_Arrays, Big_O_Time_O(n^2)_Space_O(n) | 634 | 79.44
1382+
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/scala/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_18_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 539 | 70.73
1383+
| 0234 |[Palindrome Linked List](src/main/scala/g0201_0300/s0234_palindrome_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Stack, Linked_List, Recursion, Level_2_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 811 | 85.71
1384+
| 0230 |[Kth Smallest Element in a BST](src/main/scala/g0201_0300/s0230_kth_smallest_element_in_a_bst)| 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, Big_O_Time_O(n)_Space_O(n) | 503 | 91.30
1385+
| 0226 |[Invert Binary Tree](src/main/scala/g0201_0300/s0226_invert_binary_tree)| 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, Big_O_Time_O(n)_Space_O(n) | 421 | 97.33
1386+
| 0221 |[Maximal Square](src/main/scala/g0201_0300/s0221_maximal_square)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Big_O_Time_O(m\*n)_Space_O(m\*n) | 626 | 100.00
1387+
| 0215 |[Kth Largest Element in an Array](src/main/scala/g0201_0300/s0215_kth_largest_element_in_an_array)| 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, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 799 | 95.45
13591388
| 0208 |[Implement Trie (Prefix Tree)](src/main/scala/g0201_0300/s0208_implement_trie_prefix_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Level_2_Day_16_Design, Udemy_Trie_and_Heap, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 610 | 100.00
13601389
| 0207 |[Course Schedule](src/main/scala/g0201_0300/s0207_course_schedule)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 548 | 87.81
13611390
| 0206 |[Reverse Linked List](src/main/scala/g0201_0300/s0206_reverse_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_8_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 474 | 87.50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Scala/LeetCode-in-Scala?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Scala/LeetCode-in-Scala?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala/fork)
3+
4+
## 215\. Kth Largest Element in an Array
5+
6+
Medium
7+
8+
Given an integer array `nums` and an integer `k`, return _the_ <code>k<sup>th</sup></code> _largest element in the array_.
9+
10+
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.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [3,2,1,5,6,4], k = 2
15+
16+
**Output:** 5
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [3,2,3,1,2,4,5,5,6], k = 4
21+
22+
**Output:** 4
23+
24+
**Constraints:**
25+
26+
* <code>1 <= k <= nums.length <= 10<sup>4</sup></code>
27+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
28+
29+
## Solution
30+
31+
```scala
32+
import scala.util.Sorting
33+
34+
object Solution {
35+
def findKthLargest(nums: Array[Int], k: Int): Int = {
36+
val n = nums.length
37+
Sorting.quickSort(nums)
38+
nums(n - k)
39+
}
40+
}
41+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Scala/LeetCode-in-Scala?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Scala/LeetCode-in-Scala?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala/fork)
3+
4+
## 221\. Maximal Square
5+
6+
Medium
7+
8+
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_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg)
13+
14+
**Input:** matrix = \[\["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
15+
16+
**Output:** 4
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg)
21+
22+
**Input:** matrix = \[\["0","1"],["1","0"]]
23+
24+
**Output:** 1
25+
26+
**Example 3:**
27+
28+
**Input:** matrix = \[\["0"]]
29+
30+
**Output:** 0
31+
32+
**Constraints:**
33+
34+
* `m == matrix.length`
35+
* `n == matrix[i].length`
36+
* `1 <= m, n <= 300`
37+
* `matrix[i][j]` is `'0'` or `'1'`.
38+
39+
## Solution
40+
41+
```scala
42+
object Solution {
43+
def maximalSquare(matrix: Array[Array[Char]]): Int = {
44+
val m = matrix.length
45+
if (m == 0) {
46+
return 0
47+
}
48+
val n = matrix(0).length
49+
if (n == 0) {
50+
return 0
51+
}
52+
53+
val dp = Array.ofDim[Int](m + 1, n + 1)
54+
var max = 0
55+
56+
for (i <- 0 until m) {
57+
for (j <- 0 until n) {
58+
if (matrix(i)(j) == '1') {
59+
val next = 1 + Math.min(dp(i)(j), Math.min(dp(i + 1)(j), dp(i)(j + 1)))
60+
if (next > max) {
61+
max = next
62+
}
63+
dp(i + 1)(j + 1) = next
64+
}
65+
}
66+
}
67+
68+
max * max
69+
}
70+
}
71+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Scala/LeetCode-in-Scala?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Scala/LeetCode-in-Scala?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala/fork)
3+
4+
## 226\. Invert Binary Tree
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, invert the tree, and return _its root_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg)
13+
14+
**Input:** root = [4,2,7,1,3,6,9]
15+
16+
**Output:** [4,7,2,9,6,3,1]
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg)
21+
22+
**Input:** root = [2,1,3]
23+
24+
**Output:** [2,3,1]
25+
26+
**Example 3:**
27+
28+
**Input:** root = []
29+
30+
**Output:** []
31+
32+
**Constraints:**
33+
34+
* The number of nodes in the tree is in the range `[0, 100]`.
35+
* `-100 <= Node.val <= 100`
36+
37+
## Solution
38+
39+
```scala
40+
import com_github_leetcode.TreeNode
41+
42+
/*
43+
* Definition for a binary tree node.
44+
* class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
45+
* var value: Int = _value
46+
* var left: TreeNode = _left
47+
* var right: TreeNode = _right
48+
* }
49+
*/
50+
object Solution {
51+
def invertTree(root: TreeNode): TreeNode = {
52+
if (root == null) {
53+
return null
54+
}
55+
val temp = root.left
56+
root.left = invertTree(root.right)
57+
root.right = invertTree(temp)
58+
root
59+
}
60+
}
61+
```

0 commit comments

Comments
 (0)