Skip to content

Commit

Permalink
Added tasks 169-226
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Dec 21, 2023
1 parent 14a847f commit 93dd3ed
Show file tree
Hide file tree
Showing 31 changed files with 951 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions src/main/php/g0101_0200/s0169_majority_element/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace leetcode\g0101_0200\s0169_majority_element;

// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Sorting #Counting
// #Divide_and_Conquer #Data_Structure_II_Day_1_Array #Udemy_Famous_Algorithm
// #Big_O_Time_O(n)_Space_O(1) #2023_12_21_Time_43_ms_(72.03%)_Space_23_MB_(100.00%)

class Solution {
/**
* @param Integer[] $nums
* @return Integer
*/
public function majorityElement($nums) {
$search = array_count_values($nums);
$count = count($nums) / 2;
foreach ($search as $key => $value) {
if ($value > $count) {
return $key;
}
}
}
}
27 changes: 27 additions & 0 deletions src/main/php/g0101_0200/s0169_majority_element/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
169\. Majority Element

Easy

Given an array `nums` of size `n`, return _the majority element_.

The majority element is the element that appears more than `⌊n / 2⌋` times. You may assume that the majority element always exists in the array.

**Example 1:**

**Input:** nums = [3,2,3]

**Output:** 3

**Example 2:**

**Input:** nums = [2,2,1,1,1,2,2]

**Output:** 2

**Constraints:**

* `n == nums.length`
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>

**Follow-up:** Could you solve the problem in linear time and in `O(1)` space?
32 changes: 32 additions & 0 deletions src/main/php/g0101_0200/s0189_rotate_array/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace leetcode\g0101_0200\s0189_rotate_array;

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Math #Two_Pointers
// #Algorithm_I_Day_2_Two_Pointers #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1)
// #2023_12_21_Time_60_ms_(67.03%)_Space_27.9_MB_(100.00%)

class Solution {
private function reverse(&$nums, $l, $r) {
while ($l <= $r) {
$temp = $nums[$l];
$nums[$l] = $nums[$r];
$nums[$r] = $temp;
$l++;
$r--;
}
}

/**
* @param Integer[] $nums
* @param Integer $k
* @return NULL
*/
public function rotate(&$nums, $k) {
$n = count($nums);
$t = $n - ($k % $n);
$this->reverse($nums, 0, $t - 1);
$this->reverse($nums, $t, $n - 1);
$this->reverse($nums, 0, $n - 1);
}
}
39 changes: 39 additions & 0 deletions src/main/php/g0101_0200/s0189_rotate_array/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
189\. Rotate Array

Medium

Given an array, rotate the array to the right by `k` steps, where `k` is non-negative.

**Example 1:**

**Input:** nums = [1,2,3,4,5,6,7], k = 3

**Output:** [5,6,7,1,2,3,4]

**Explanation:**

rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

**Example 2:**

**Input:** nums = [-1,-100,3,99], k = 2

**Output:** [3,99,-1,-100]

**Explanation:**

rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
* <code>0 <= k <= 10<sup>5</sup></code>

**Follow up:**

* Try to come up with as many solutions as you can. There are at least **three** different ways to solve this problem.
* Could you do it in-place with `O(1)` extra space?
34 changes: 34 additions & 0 deletions src/main/php/g0101_0200/s0198_house_robber/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace leetcode\g0101_0200\s0198_house_robber;

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
// #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_3
// #Level_2_Day_12_Dynamic_Programming #Udemy_Dynamic_Programming #Big_O_Time_O(n)_Space_O(n)
// #2023_12_21_Time_4_ms_(64.29%)_Space_19.8_MB_(9.52%)

class Solution {
/**
* @param Integer[] $nums
* @return Integer
*/
public function rob($nums) {
$n = count($nums);
if ($n == 0) {
return 0;
}
if ($n == 1) {
return $nums[0];
}
if ($n == 2) {
return max($nums[0], $nums[1]);
}
$profit = array();
$profit[0] = $nums[0];
$profit[1] = max($nums[1], $nums[0]);
for ($i = 2; $i < $n; $i++) {
$profit[$i] = max($profit[$i - 1], $nums[$i] + $profit[$i - 2]);
}
return $profit[$n - 1];
}
}
34 changes: 34 additions & 0 deletions src/main/php/g0101_0200/s0198_house_robber/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
198\. House Robber

Medium

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and **it will automatically contact the police if two adjacent houses were broken into on the same night**.

Given an integer array `nums` representing the amount of money of each house, return _the maximum amount of money you can rob tonight **without alerting the police**_.

**Example 1:**

**Input:** nums = [1,2,3,1]

**Output:** 4

**Explanation:**

Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.

**Example 2:**

**Input:** nums = [2,7,9,3,1]

**Output:** 12

**Explanation:**

Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.

**Constraints:**

* `1 <= nums.length <= 100`
* `0 <= nums[i] <= 400`
41 changes: 41 additions & 0 deletions src/main/php/g0101_0200/s0200_number_of_islands/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace leetcode\g0101_0200\s0200_number_of_islands;

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Depth_First_Search
// #Breadth_First_Search #Matrix #Union_Find
// #Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search
// #Graph_Theory_I_Day_1_Matrix_Related_Problems #Level_1_Day_9_Graph/BFS/DFS #Udemy_Graph
// #Big_O_Time_O(M*N)_Space_O(M*N) #2023_12_21_Time_97_ms_(82.05%)_Space_35.3_MB_(100.00%)

class Solution {
/**
* @param String[][] $grid
* @return Integer
*/
public function numIslands($grid) {
$islands = 0;
if ($grid != null && !empty($grid) && count($grid[0]) != 0) {
for ($i = 0; $i < count($grid); $i++) {
for ($j = 0; $j < count($grid[0]); $j++) {
if ($grid[$i][$j] == '1') {
$this->dfs($grid, $i, $j);
$islands++;
}
}
}
}
return $islands;
}

private function dfs(&$grid, $x, $y) {
if ($x < 0 || count($grid) <= $x || $y < 0 || count($grid[0]) <= $y || $grid[$x][$y] != '1') {
return;
}
$grid[$x][$y] = 'x';
$this->dfs($grid, $x + 1, $y);
$this->dfs($grid, $x - 1, $y);
$this->dfs($grid, $x, $y + 1);
$this->dfs($grid, $x, $y - 1);
}
}
40 changes: 40 additions & 0 deletions src/main/php/g0101_0200/s0200_number_of_islands/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
200\. Number of Islands

Medium

Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.

An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

**Example 1:**

**Input:**

grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]

**Output:** 1

**Example 2:**

**Input:**

grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]

**Output:** 3

**Constraints:**

* `m == grid.length`
* `n == grid[i].length`
* `1 <= m, n <= 300`
* `grid[i][j]` is `'0'` or `'1'`.
37 changes: 37 additions & 0 deletions src/main/php/g0201_0300/s0206_reverse_linked_list/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace leetcode\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)
// #2023_12_21_Time_0_ms_(100.00%)_Space_20.4_MB_(87.88%)

/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* }
*/
class Solution {
/**
* @param ListNode $head
* @return ListNode
*/
public function reverseList($head) {
$prev = null;
$curr = $head;
while ($curr != null) {
$next = $curr->next;
$curr->next = $prev;
$prev = $curr;
$curr = $next;
}
return $prev;
}
}
34 changes: 34 additions & 0 deletions src/main/php/g0201_0300/s0206_reverse_linked_list/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
206\. Reverse Linked List

Easy

Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg)

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

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

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg)

**Input:** head = [1,2]

**Output:** [2,1]

**Example 3:**

**Input:** head = []

**Output:** []

**Constraints:**

* The number of nodes in the list is the range `[0, 5000]`.
* `-5000 <= Node.val <= 5000`

**Follow up:** A linked list can be reversed either iteratively or recursively. Could you implement both?
Loading

0 comments on commit 93dd3ed

Please sign in to comment.