Skip to content

Commit

Permalink
Added tasks 73-98
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Dec 10, 2023
1 parent 69de6cb commit 571354a
Show file tree
Hide file tree
Showing 32 changed files with 1,096 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0074 |[Search a 2D Matrix](src/main/php/g0001_0100/s0074_search_a_2d_matrix/Solution.php)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 9 | 68.18

#### Day 9

Expand Down Expand Up @@ -254,6 +255,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0096 |[Unique Binary Search Trees](src/main/php/g0001_0100/s0096_unique_binary_search_trees/Solution.php)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00

#### Day 12

Expand Down Expand Up @@ -642,6 +644,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0098 |[Validate Binary Search Tree](src/main/php/g0001_0100/s0098_validate_binary_search_tree/Solution.php)| 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)) | 10 | 70.97

#### Day 9 Graph/BFS/DFS

Expand Down Expand Up @@ -723,6 +726,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0074 |[Search a 2D Matrix](src/main/php/g0001_0100/s0074_search_a_2d_matrix/Solution.php)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 9 | 68.18
| 0033 |[Search in Rotated Sorted Array](src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 7 | 83.17

#### Day 9 Binary Search Tree
Expand Down Expand Up @@ -755,6 +759,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0003 |[Longest Substring Without Repeating Characters](src/main/php/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1) | 4 | 99.33
| 0076 |[Minimum Window Substring](src/main/php/g0001_0100/s0076_minimum_window_substring/Solution.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(s.length())_Space_O(1) | 21 | 100.00

#### Day 15 Tree

Expand Down Expand Up @@ -819,6 +824,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
|-|-|-|-|-|-
| 0001 |[Two Sum](src/main/php/g0001_0100/s0001_two_sum/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 9 | 97.47
| 0055 |[Jump Game](src/main/php/g0001_0100/s0055_jump_game/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 129 | 74.56
| 0075 |[Sort Colors](src/main/php/g0001_0100/s0075_sort_colors/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 3 | 88.78
| 0041 |[First Missing Positive](src/main/php/g0001_0100/s0041_first_missing_positive/Solution.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 111 | 90.48

#### Udemy Two Pointers
Expand All @@ -843,7 +849,9 @@ Php-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0074 |[Search a 2D Matrix](src/main/php/g0001_0100/s0074_search_a_2d_matrix/Solution.php)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 9 | 68.18
| 0048 |[Rotate Image](src/main/php/g0001_0100/s0048_rotate_image/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Big_O_Time_O(n^2)_Space_O(1) | 4 | 81.58
| 0073 |[Set Matrix Zeroes](src/main/php/g0001_0100/s0073_set_matrix_zeroes/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Big_O_Time_O(m\*n)_Space_O(1) | 37 | 61.76
| 0056 |[Merge Intervals](src/main/php/g0001_0100/s0056_merge_intervals/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 48 | 80.00

#### Udemy Linked List
Expand All @@ -858,6 +866,8 @@ Php-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0094 |[Binary Tree Inorder Traversal](src/main/php/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 3 | 82.09
| 0098 |[Validate Binary Search Tree](src/main/php/g0001_0100/s0098_validate_binary_search_tree/Solution.php)| 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)) | 10 | 70.97

#### Udemy Trie and Heap

Expand All @@ -884,6 +894,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
|-|-|-|-|-|-
| 0022 |[Generate Parentheses](src/main/php/g0001_0100/s0022_generate_parentheses/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 4 | 83.67
| 0039 |[Combination Sum](src/main/php/g0001_0100/s0039_combination_sum/Solution.php)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 11 | 87.10
| 0078 |[Subsets](src/main/php/g0001_0100/s0078_subsets/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Big_O_Time_O(2^n)_Space_O(n\*2^n) | 7 | 69.23
| 0017 |[Letter Combinations of a Phone Number](src/main/php/g0001_0100/s0017_letter_combinations_of_a_phone_number/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 5 | 60.19
| 0046 |[Permutations](src/main/php/g0001_0100/s0046_permutations/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 4 | 92.42

Expand Down Expand Up @@ -925,6 +936,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0074 |[Search a 2D Matrix](src/main/php/g0001_0100/s0074_search_a_2d_matrix/Solution.php)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 9 | 68.18

#### Day 6 String

Expand Down Expand Up @@ -952,6 +964,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0094 |[Binary Tree Inorder Traversal](src/main/php/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 3 | 82.09

#### Day 11 Tree

Expand All @@ -972,6 +985,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0098 |[Validate Binary Search Tree](src/main/php/g0001_0100/s0098_validate_binary_search_tree/Solution.php)| 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)) | 10 | 70.97

### Data Structure II

Expand All @@ -985,6 +999,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0075 |[Sort Colors](src/main/php/g0001_0100/s0075_sort_colors/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 3 | 88.78
| 0056 |[Merge Intervals](src/main/php/g0001_0100/s0056_merge_intervals/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 48 | 80.00

#### Day 3 Array
Expand Down Expand Up @@ -1174,6 +1189,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
|-|-|-|-|-|-
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 19 | 81.01
| 0033 |[Search in Rotated Sorted Array](src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 7 | 83.17
| 0074 |[Search a 2D Matrix](src/main/php/g0001_0100/s0074_search_a_2d_matrix/Solution.php)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 9 | 68.18

#### Day 2 Binary Search

Expand Down Expand Up @@ -1216,6 +1232,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0078 |[Subsets](src/main/php/g0001_0100/s0078_subsets/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Big_O_Time_O(2^n)_Space_O(n\*2^n) | 7 | 69.23

#### Day 10 Recursion Backtracking

Expand All @@ -1229,6 +1246,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
|-|-|-|-|-|-
| 0017 |[Letter Combinations of a Phone Number](src/main/php/g0001_0100/s0017_letter_combinations_of_a_phone_number/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 5 | 60.19
| 0022 |[Generate Parentheses](src/main/php/g0001_0100/s0022_generate_parentheses/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 4 | 83.67
| 0079 |[Word Search](src/main/php/g0001_0100/s0079_word_search/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Backtracking, Big_O_Time_O(4^(m\*n))_Space_O(m\*n) | 697 | 100.00

#### Day 12 Dynamic Programming

Expand Down Expand Up @@ -1289,6 +1307,16 @@ Php-based LeetCode algorithm problem solutions, regularly updated.

| # | Title | Difficulty | Tag | Time, ms | Time, %
|------|----------------|-------------|-------------|----------|---------
| 0098 |[Validate Binary Search Tree](src/main/php/g0001_0100/s0098_validate_binary_search_tree/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(log(N)) | 10 | 70.97
| 0096 |[Unique Binary Search Trees](src/main/php/g0001_0100/s0096_unique_binary_search_trees/Solution.php)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
| 0094 |[Binary Tree Inorder Traversal](src/main/php/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 3 | 82.09
| 0084 |[Largest Rectangle in Histogram](src/main/php/g0001_0100/s0084_largest_rectangle_in_histogram/Solution.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Stack, Monotonic_Stack, Big_O_Time_O(n_log_n)_Space_O(log_n) | 298 | 100.00
| 0079 |[Word Search](src/main/php/g0001_0100/s0079_word_search/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking, Big_O_Time_O(4^(m\*n))_Space_O(m\*n) | 697 | 100.00
| 0078 |[Subsets](src/main/php/g0001_0100/s0078_subsets/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(2^n)_Space_O(n\*2^n) | 7 | 69.23
| 0076 |[Minimum Window Substring](src/main/php/g0001_0100/s0076_minimum_window_substring/Solution.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer, Big_O_Time_O(s.length())_Space_O(1) | 21 | 100.00
| 0075 |[Sort Colors](src/main/php/g0001_0100/s0075_sort_colors/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_II_Day_2_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 3 | 88.78
| 0074 |[Search a 2D Matrix](src/main/php/g0001_0100/s0074_search_a_2d_matrix/Solution.php)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Data_Structure_I_Day_5_Array, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_8, Level_2_Day_8_Binary_Search, Udemy_2D_Arrays/Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 9 | 68.18
| 0073 |[Set Matrix Zeroes](src/main/php/g0001_0100/s0073_set_matrix_zeroes/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Udemy_2D_Arrays/Matrix, Big_O_Time_O(m\*n)_Space_O(1) | 37 | 61.76
| 0072 |[Edit Distance](src/main/php/g0001_0100/s0072_edit_distance/Solution.php)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 29 | 75.00
| 0070 |[Climbing Stairs](src/main/php/g0001_0100/s0070_climbing_stairs/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 3 | 82.81
| 0064 |[Minimum Path Sum](src/main/php/g0001_0100/s0064_minimum_path_sum/Solution.php)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Udemy_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(m\*n) | 20 | 81.48
Expand Down
52 changes: 52 additions & 0 deletions src/main/php/com_github_leetcode/TreeNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace leetcode\com_github_leetcode;

class TreeNode {
public $val;
public $left;
public $right;

function __construct($val, $left = null, $right = null) {
$this->val = $val;
$this->left = $left;
$this->right = $right;
}

public static function create($treeValues) {
$root = empty($treeValues) ? null : new TreeNode($treeValues[0]);
$queue = array();
array_push($queue, $root);
$i = 1;
while ($i < count($treeValues)) {
$curr = array_shift($queue);
if ($treeValues[$i] != null) {
$curr->left = new TreeNode($treeValues[$i]);
array_push($queue, $curr->left);
}
if (++$i < count($treeValues) && $treeValues[$i] != null) {
$curr->right = new TreeNode($treeValues[$i]);
array_push($queue, $curr->right);
}
$i++;
}
return $root;
}

public function __toString() {
if ($this->left == null && $this->right == null) {
return "" . $this->val;
} else {
$root = "" . $this->val;
$leftValue = "null";
$rightValue = "null";
if ($this->left != null) {
$leftValue = $this->left->__toString();
}
if ($this->right != null) {
$rightValue = $this->right->__toString();
}
return $root . "," . $leftValue . "," . $rightValue;
}
}
}
63 changes: 63 additions & 0 deletions src/main/php/g0001_0100/s0073_set_matrix_zeroes/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace leetcode\g0001_0100\s0073_set_matrix_zeroes;

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Matrix
// #Udemy_2D_Arrays/Matrix #Big_O_Time_O(m*n)_Space_O(1)
// #2023_12_10_Time_37_ms_(61.76%)_Space_23.4_MB_(44.12%)

class Solution {
/**
* @param Integer[][] $matrix
* @return NULL
*/
public function setZeroes(&$matrix) {
$m = count($matrix);
$n = count($matrix[0]);
$row0 = false;
$col0 = false;
// Check if 0th col needs to be market all 0s in future
foreach ($matrix as $ints) {
if ($ints[0] == 0) {
$col0 = true;
break;
}
}
// Check if 0th row needs to be market all 0s in future
for ($i = 0; $i < $n; $i++) {
if ($matrix[0][$i] == 0) {
$row0 = true;
break;
}
}
// Store the signals in 0th row and column
for ($i = 1; $i < $m; $i++) {
for ($j = 1; $j < $n; $j++) {
if ($matrix[$i][$j] == 0) {
$matrix[$i][0] = 0;
$matrix[0][$j] = 0;
}
}
}
// Mark 0 for all cells based on signal from 0th row and 0th column
for ($i = 1; $i < $m; $i++) {
for ($j = 1; $j < $n; $j++) {
if ($matrix[$i][0] == 0 || $matrix[0][$j] == 0) {
$matrix[$i][$j] = 0;
}
}
}
// Set 0th column
for ($i = 0; $i < $m; $i++) {
if ($col0) {
$matrix[$i][0] = 0;
}
}
// Set 0th row
for ($i = 0; $i < $n; $i++) {
if ($row0) {
$matrix[0][$i] = 0;
}
}
}
}
36 changes: 36 additions & 0 deletions src/main/php/g0001_0100/s0073_set_matrix_zeroes/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
73\. Set Matrix Zeroes

Medium

Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s, and return _the matrix_.

You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm).

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg)

**Input:** matrix = [[1,1,1],[1,0,1],[1,1,1]]

**Output:** [[1,0,1],[0,0,0],[1,0,1]]

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg)

**Input:** matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]

**Output:** [[0,0,0,0],[0,4,5,0],[0,3,1,0]]

**Constraints:**

* `m == matrix.length`
* `n == matrix[0].length`
* `1 <= m, n <= 200`
* <code>-2<sup>31</sup> <= matrix[i][j] <= 2<sup>31</sup> - 1</code>

**Follow up:**

* A straightforward solution using `O(mn)` space is probably a bad idea.
* A simple improvement uses `O(m + n)` space, but still not the best solution.
* Could you devise a constant space solution?
35 changes: 35 additions & 0 deletions src/main/php/g0001_0100/s0074_search_a_2d_matrix/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace leetcode\g0001_0100\s0074_search_a_2d_matrix;

// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Data_Structure_I_Day_5_Array
// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_8 #Level_2_Day_8_Binary_Search
// #Udemy_2D_Arrays/Matrix #Big_O_Time_O(endRow+endCol)_Space_O(1)
// #2023_12_10_Time_9_ms_(68.18%)_Space_19.8_MB_(51.82%)

class Solution {
/**
* @param Integer[][] $matrix
* @param Integer $target
* @return Boolean
*/
public function searchMatrix($matrix, $target) {
$endRow = count($matrix);
$endCol = count($matrix[0]);
$targetRow = 0;
$result = false;
for ($i = 0; $i < $endRow; $i++) {
if ($matrix[$i][$endCol - 1] >= $target) {
$targetRow = $i;
break;
}
}
for ($i = 0; $i < $endCol; $i++) {
if ($matrix[$targetRow][$i] == $target) {
$result = true;
break;
}
}
return $result;
}
}
Loading

0 comments on commit 571354a

Please sign in to comment.