From 05e88ca562dd371e704059e47250cec11737de43 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 7 Nov 2023 11:43:53 +0200 Subject: [PATCH] Added tasks 215-283 --- README.md | 29 ++++++ .../readme.md | 41 +++++++++ .../g0201_0300/s0221_maximal_square/readme.md | 71 +++++++++++++++ .../s0226_invert_binary_tree/readme.md | 61 +++++++++++++ .../readme.md | 61 +++++++++++++ .../s0234_palindrome_linked_list/readme.md | 84 +++++++++++++++++ .../readme.md | 78 ++++++++++++++++ .../readme.md | 63 +++++++++++++ .../s0239_sliding_window_maximum/readme.md | 89 +++++++++++++++++++ .../s0240_search_a_2d_matrix_ii/readme.md | 60 +++++++++++++ .../g0201_0300/s0283_move_zeroes/readme.md | 51 +++++++++++ 11 files changed, 688 insertions(+) create mode 100644 src/main/scala/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md create mode 100644 src/main/scala/g0201_0300/s0221_maximal_square/readme.md create mode 100644 src/main/scala/g0201_0300/s0226_invert_binary_tree/readme.md create mode 100644 src/main/scala/g0201_0300/s0230_kth_smallest_element_in_a_bst/readme.md create mode 100644 src/main/scala/g0201_0300/s0234_palindrome_linked_list/readme.md create mode 100644 src/main/scala/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md create mode 100644 src/main/scala/g0201_0300/s0238_product_of_array_except_self/readme.md create mode 100644 src/main/scala/g0201_0300/s0239_sliding_window_maximum/readme.md create mode 100644 src/main/scala/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md create mode 100644 src/main/scala/g0201_0300/s0283_move_zeroes/readme.md diff --git a/README.md b/README.md index b59d5e8..51f4004 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 9 @@ -404,6 +405,7 @@ | | | | | | |-|-|-|-|-|- | 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 +| 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 #### Day 17 @@ -463,6 +465,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 7 Array @@ -831,6 +834,7 @@ | | | | | | |-|-|-|-|-|- | 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 +| 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 #### Day 4 Linked List @@ -847,6 +851,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 7 Tree @@ -864,6 +869,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 10 Graph/BFS/DFS @@ -959,11 +965,14 @@ | | | | | | |-|-|-|-|-|- | 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 +| 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 | 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 | 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 | 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 | 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 +| 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 | 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 +| 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 #### Udemy Two Pointers @@ -1004,6 +1013,7 @@ | 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 | 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 | 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 +| 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 | 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 | 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 | 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 @@ |-|-|-|-|-|- | 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 | 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 +| 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 | 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 | 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 | 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 +| 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 #### Udemy Trie and Heap @@ -1136,6 +1148,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 13 Tree @@ -1175,11 +1188,13 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 5 Array | | | | | | |-|-|-|-|-|- +| 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 #### Day 6 String @@ -1249,11 +1264,13 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 18 Tree | | | | | | |-|-|-|-|-|- +| 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 #### Day 19 Graph @@ -1264,6 +1281,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 21 Heap Priority Queue @@ -1288,6 +1306,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 4 Two Pointers @@ -1356,6 +1375,16 @@ | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|-------- +| 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 +| 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 +| 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 +| 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 +| 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 +| 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 +| 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 +| 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 +| 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 +| 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 | 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 | 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 | 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 diff --git a/src/main/scala/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md b/src/main/scala/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md new file mode 100644 index 0000000..d87b27b --- /dev/null +++ b/src/main/scala/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md @@ -0,0 +1,41 @@ +[![](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) +[![](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) + +## 215\. Kth Largest Element in an Array + +Medium + +Given an integer array `nums` and an integer `k`, return _the_ kth _largest element in the array_. + +Note that it is the kth largest element in the sorted order, not the kth distinct element. + +**Example 1:** + +**Input:** nums = [3,2,1,5,6,4], k = 2 + +**Output:** 5 + +**Example 2:** + +**Input:** nums = [3,2,3,1,2,4,5,5,6], k = 4 + +**Output:** 4 + +**Constraints:** + +* 1 <= k <= nums.length <= 104 +* -104 <= nums[i] <= 104 + +## Solution + +```scala +import scala.util.Sorting + +object Solution { + def findKthLargest(nums: Array[Int], k: Int): Int = { + val n = nums.length + Sorting.quickSort(nums) + nums(n - k) + } +} +``` \ No newline at end of file diff --git a/src/main/scala/g0201_0300/s0221_maximal_square/readme.md b/src/main/scala/g0201_0300/s0221_maximal_square/readme.md new file mode 100644 index 0000000..1eefd74 --- /dev/null +++ b/src/main/scala/g0201_0300/s0221_maximal_square/readme.md @@ -0,0 +1,71 @@ +[![](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) +[![](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) + +## 221\. Maximal Square + +Medium + +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_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg) + +**Input:** matrix = \[\["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] + +**Output:** 4 + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg) + +**Input:** matrix = \[\["0","1"],["1","0"]] + +**Output:** 1 + +**Example 3:** + +**Input:** matrix = \[\["0"]] + +**Output:** 0 + +**Constraints:** + +* `m == matrix.length` +* `n == matrix[i].length` +* `1 <= m, n <= 300` +* `matrix[i][j]` is `'0'` or `'1'`. + +## Solution + +```scala +object Solution { + def maximalSquare(matrix: Array[Array[Char]]): Int = { + val m = matrix.length + if (m == 0) { + return 0 + } + val n = matrix(0).length + if (n == 0) { + return 0 + } + + val dp = Array.ofDim[Int](m + 1, n + 1) + var max = 0 + + for (i <- 0 until m) { + for (j <- 0 until n) { + if (matrix(i)(j) == '1') { + val next = 1 + Math.min(dp(i)(j), Math.min(dp(i + 1)(j), dp(i)(j + 1))) + if (next > max) { + max = next + } + dp(i + 1)(j + 1) = next + } + } + } + + max * max + } +} +``` \ No newline at end of file diff --git a/src/main/scala/g0201_0300/s0226_invert_binary_tree/readme.md b/src/main/scala/g0201_0300/s0226_invert_binary_tree/readme.md new file mode 100644 index 0000000..e8f7ca5 --- /dev/null +++ b/src/main/scala/g0201_0300/s0226_invert_binary_tree/readme.md @@ -0,0 +1,61 @@ +[![](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) +[![](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) + +## 226\. Invert Binary Tree + +Easy + +Given the `root` of a binary tree, invert the tree, and return _its root_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg) + +**Input:** root = [4,2,7,1,3,6,9] + +**Output:** [4,7,2,9,6,3,1] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg) + +**Input:** root = [2,1,3] + +**Output:** [2,3,1] + +**Example 3:** + +**Input:** root = [] + +**Output:** [] + +**Constraints:** + +* The number of nodes in the tree is in the range `[0, 100]`. +* `-100 <= Node.val <= 100` + +## Solution + +```scala +import com_github_leetcode.TreeNode + +/* + * Definition for a binary tree node. + * class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) { + * var value: Int = _value + * var left: TreeNode = _left + * var right: TreeNode = _right + * } + */ +object Solution { + def invertTree(root: TreeNode): TreeNode = { + if (root == null) { + return null + } + val temp = root.left + root.left = invertTree(root.right) + root.right = invertTree(temp) + root + } +} +``` \ No newline at end of file diff --git a/src/main/scala/g0201_0300/s0230_kth_smallest_element_in_a_bst/readme.md b/src/main/scala/g0201_0300/s0230_kth_smallest_element_in_a_bst/readme.md new file mode 100644 index 0000000..7596cec --- /dev/null +++ b/src/main/scala/g0201_0300/s0230_kth_smallest_element_in_a_bst/readme.md @@ -0,0 +1,61 @@ +[![](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) +[![](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) + +## 230\. Kth Smallest Element in a BST + +Medium + +Given the `root` of a binary search tree, and an integer `k`, return _the_ kth _smallest value (**1-indexed**) of all the values of the nodes in the tree_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg) + +**Input:** root = [3,1,4,null,2], k = 1 + +**Output:** 1 + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg) + +**Input:** root = [5,3,6,2,4,null,null,1], k = 3 + +**Output:** 3 + +**Constraints:** + +* The number of nodes in the tree is `n`. +* 1 <= k <= n <= 104 +* 0 <= Node.val <= 104 + +**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? + +## Solution + +```scala +import com_github_leetcode.TreeNode + +object Solution { + var index = 0 + var value = -1 + + def kthSmallest(root: TreeNode, k: Int): Int = { + index = 0 + value = -1 + if (root == null) -1 + else inorder(root, k) + value + } + + // Using In order + def inorder(root: TreeNode, k: Int): Unit = { + if (root != null && index < k) { + inorder(root.left, k) + index += 1 + if (index == k) value = root.value + inorder(root.right, k) + } + } +} +``` \ No newline at end of file diff --git a/src/main/scala/g0201_0300/s0234_palindrome_linked_list/readme.md b/src/main/scala/g0201_0300/s0234_palindrome_linked_list/readme.md new file mode 100644 index 0000000..4fe4f5f --- /dev/null +++ b/src/main/scala/g0201_0300/s0234_palindrome_linked_list/readme.md @@ -0,0 +1,84 @@ +[![](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) +[![](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) + +## 234\. Palindrome Linked List + +Easy + +Given the `head` of a singly linked list, return `true` if it is a palindrome. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg) + +**Input:** head = [1,2,2,1] + +**Output:** true + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg) + +**Input:** head = [1,2] + +**Output:** false + +**Constraints:** + +* The number of nodes in the list is in the range [1, 105]. +* `0 <= Node.val <= 9` + +**Follow up:** Could you do it in `O(n)` time and `O(1)` space? + +## Solution + +```scala +import com_github_leetcode.ListNode + +/* + * Definition for singly-linked list. + * class ListNode(_x: Int = 0, _next: ListNode = null) { + * var next: ListNode = _next + * var x: Int = _x + * } + */ +object Solution { + def isPalindrome(head: ListNode): Boolean = { + var len = 0 + var right = head + + // Calculate the length + while (right != null) { + right = right.next + len += 1 + } + + // Reverse the right half of the list + len = len / 2 + right = head + for (_ <- 0 until len) { + right = right.next + } + + var prev: ListNode = null + while (right != null) { + val next = right.next + right.next = prev + prev = right + right = next + } + var head2 = head + // Compare left half and right half + for (_ <- 0 until len) { + if (prev != null && head2.x == prev.x) { + head2 = head2.next + prev = prev.next + } else { + return false + } + } + + true + } +} +``` \ No newline at end of file diff --git a/src/main/scala/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md b/src/main/scala/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md new file mode 100644 index 0000000..f83bd5b --- /dev/null +++ b/src/main/scala/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md @@ -0,0 +1,78 @@ +[![](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) +[![](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) + +## 236\. Lowest Common Ancestor of a Binary Tree + +Medium + +Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. + +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**).” + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2018/12/14/binarytree.png) + +**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 + +**Output:** 3 + +**Explanation:** The LCA of nodes 5 and 1 is 3. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2018/12/14/binarytree.png) + +**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 + +**Output:** 5 + +**Explanation:** The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition. + +**Example 3:** + +**Input:** root = [1,2], p = 1, q = 2 + +**Output:** 1 + +**Constraints:** + +* The number of nodes in the tree is in the range [2, 105]. +* -109 <= Node.val <= 109 +* All `Node.val` are **unique**. +* `p != q` +* `p` and `q` will exist in the tree. + +## Solution + +```scala +import com_github_leetcode.TreeNode + +/* + * Definition for a binary tree node. + * class TreeNode(var _value: Int) { + * var value: Int = _value + * var left: TreeNode = null + * var right: TreeNode = null + * } + */ +object Solution { + def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = { + if (root == null) { + return null + } + if (root.value == p.value || root.value == q.value) { + return root + } + val left = lowestCommonAncestor(root.left, p, q) + val right = lowestCommonAncestor(root.right, p, q) + if (left != null && right != null) { + return root + } + if (left != null) { + return left + } + right + } +} +``` \ No newline at end of file diff --git a/src/main/scala/g0201_0300/s0238_product_of_array_except_self/readme.md b/src/main/scala/g0201_0300/s0238_product_of_array_except_self/readme.md new file mode 100644 index 0000000..a34f947 --- /dev/null +++ b/src/main/scala/g0201_0300/s0238_product_of_array_except_self/readme.md @@ -0,0 +1,63 @@ +[![](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) +[![](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) + +## 238\. Product of Array Except Self + +Medium + +Given an integer array `nums`, return _an array_ `answer` _such that_ `answer[i]` _is equal to the product of all the elements of_ `nums` _except_ `nums[i]`. + +The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer. + +You must write an algorithm that runs in `O(n)` time and without using the division operation. + +**Example 1:** + +**Input:** nums = [1,2,3,4] + +**Output:** [24,12,8,6] + +**Example 2:** + +**Input:** nums = [-1,1,0,-3,3] + +**Output:** [0,0,9,0,0] + +**Constraints:** + +* 2 <= nums.length <= 105 +* `-30 <= nums[i] <= 30` +* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer. + +**Follow up:** Can you solve the problem in `O(1) `extra space complexity? (The output array **does not** count as extra space for space complexity analysis.) + +## Solution + +```scala +object Solution { + def productExceptSelf(nums: Array[Int]): Array[Int] = { + var product = 1 + val ans = new Array[Int](nums.length) + + for (num <- nums) { + product = product * num + } + + for (i <- nums.indices) { + if (nums(i) != 0) { + ans(i) = product / nums(i) + } else { + var p = 1 + for (j <- nums.indices) { + if (j != i) { + p = p * nums(j) + } + } + ans(i) = p + } + } + + ans + } +} +``` \ No newline at end of file diff --git a/src/main/scala/g0201_0300/s0239_sliding_window_maximum/readme.md b/src/main/scala/g0201_0300/s0239_sliding_window_maximum/readme.md new file mode 100644 index 0000000..ff79f42 --- /dev/null +++ b/src/main/scala/g0201_0300/s0239_sliding_window_maximum/readme.md @@ -0,0 +1,89 @@ +[![](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) +[![](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) + +## 239\. Sliding Window Maximum + +Hard + +You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position. + +Return _the max sliding window_. + +**Example 1:** + +**Input:** nums = [1,3,-1,-3,5,3,6,7], k = 3 + +**Output:** [3,3,5,5,6,7] + +**Explanation:** + + Window position Max + --------------- ----- + [1 3 -1] -3 5 3 6 7 3 + 1 [3 -1 -3] 5 3 6 7 3 + 1 3 [-1 -3 5] 3 6 7 5 + 1 3 -1 [-3 5 3] 6 7 5 + 1 3 -1 -3 [5 3 6] 7 6 + 1 3 -1 -3 5 [3 6 7] 7 + +**Example 2:** + +**Input:** nums = [1], k = 1 + +**Output:** [1] + +**Example 3:** + +**Input:** nums = [1,-1], k = 1 + +**Output:** [1,-1] + +**Example 4:** + +**Input:** nums = [9,11], k = 2 + +**Output:** [11] + +**Example 5:** + +**Input:** nums = [4,-2], k = 2 + +**Output:** [4] + +**Constraints:** + +* 1 <= nums.length <= 105 +* -104 <= nums[i] <= 104 +* `1 <= k <= nums.length` + +## Solution + +```scala +object Solution { + def maxSlidingWindow(nums: Array[Int], k: Int): Array[Int] = { + val q = new java.util.ArrayDeque[(Int, Int)]() + + def populate(num: Int, index: Int): Unit = { + while (!q.isEmpty && q.peekLast()._2 + k <= index) { + q.pollLast() + } + while (!q.isEmpty && (q.peekFirst()._1 <= num)) { + q.pollFirst() + } + q.addFirst((num, index)) + } + + if (k == 1) { + nums + } else { + for (i <- (0 until k - 1)) { + populate(nums(i), i) + } + ((k - 1) until nums.size).map { i => + populate(nums(i), i) + q.peekLast()._1 + }.toArray + } + } +} +``` \ No newline at end of file diff --git a/src/main/scala/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md b/src/main/scala/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md new file mode 100644 index 0000000..c5dd8f0 --- /dev/null +++ b/src/main/scala/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md @@ -0,0 +1,60 @@ +[![](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) +[![](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) + +## 240\. Search a 2D Matrix II + +Medium + +Write an efficient algorithm that searches for a `target` value in an `m x n` integer `matrix`. The `matrix` has the following properties: + +* Integers in each row are sorted in ascending from left to right. +* Integers in each column are sorted in ascending from top to bottom. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg) + +**Input:** matrix = \[\[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5 + +**Output:** true + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg) + +**Input:** matrix = \[\[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20 + +**Output:** false + +**Constraints:** + +* `m == matrix.length` +* `n == matrix[i].length` +* `1 <= n, m <= 300` +* -109 <= matrix[i][j] <= 109 +* All the integers in each row are **sorted** in ascending order. +* All the integers in each column are **sorted** in ascending order. +* -109 <= target <= 109 + +## Solution + +```scala +object Solution { + def searchMatrix(matrix: Array[Array[Int]], target: Int): Boolean = { + var r = 0 + var c = matrix(0).length - 1 + + while (r < matrix.length && c >= 0) { + if (matrix(r)(c) == target) { + return true + } else if (matrix(r)(c) > target) { + c -= 1 + } else { + r += 1 + } + } + + false + } +} +``` \ No newline at end of file diff --git a/src/main/scala/g0201_0300/s0283_move_zeroes/readme.md b/src/main/scala/g0201_0300/s0283_move_zeroes/readme.md new file mode 100644 index 0000000..480f35e --- /dev/null +++ b/src/main/scala/g0201_0300/s0283_move_zeroes/readme.md @@ -0,0 +1,51 @@ +[![](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) +[![](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) + +## 283\. Move Zeroes + +Easy + +Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements. + +**Note** that you must do this in-place without making a copy of the array. + +**Example 1:** + +**Input:** nums = [0,1,0,3,12] + +**Output:** [1,3,12,0,0] + +**Example 2:** + +**Input:** nums = [0] + +**Output:** [0] + +**Constraints:** + +* 1 <= nums.length <= 104 +* -231 <= nums[i] <= 231 - 1 + +**Follow up:** Could you minimize the total number of operations done? + +## Solution + +```scala +object Solution { + def moveZeroes(nums: Array[Int]): Unit = { + var firstZero = 0 + for (i <- nums.indices) { + if (nums(i) != 0) { + swap(firstZero, i, nums) + firstZero += 1 + } + } + } + + private def swap(index1: Int, index2: Int, numbers: Array[Int]): Unit = { + val val2 = numbers(index2) + numbers(index2) = numbers(index1) + numbers(index1) = val2 + } +} +``` \ No newline at end of file