Skip to content

Commit c07ee44

Browse files
authored
Added tasks 11-25
1 parent bd234ed commit c07ee44

File tree

30 files changed

+1025
-0
lines changed

30 files changed

+1025
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace leetcode\g0001_0100\s0011_container_with_most_water;
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Greedy #Two_Pointers
6+
// #Algorithm_II_Day_4_Two_Pointers #Big_O_Time_O(n)_Space_O(1)
7+
// #2023_12_07_Time_177_ms_(88.58%)_Space_31.7_MB_(66.67%)
8+
9+
class Solution {
10+
/**
11+
* @param Integer[] $height
12+
* @return Integer
13+
*/
14+
public function maxArea($height) {
15+
$maxArea = -1;
16+
$left = 0;
17+
$right = count($height) - 1;
18+
while ($left < $right) {
19+
if ($height[$left] < $height[$right]) {
20+
$maxArea = max($maxArea, $height[$left] * ($right - $left));
21+
$left++;
22+
} else {
23+
$maxArea = max($maxArea, $height[$right] * ($right - $left));
24+
$right--;
25+
}
26+
}
27+
return $maxArea;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
11\. Container With Most Water
2+
3+
Medium
4+
5+
Given `n` non-negative integers <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> , where each represents a point at coordinate <code>(i, a<sub>i</sub>)</code>. `n` vertical lines are drawn such that the two endpoints of the line `i` is at <code>(i, a<sub>i</sub>)</code> and `(i, 0)`. Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.
6+
7+
**Notice** that you may not slant the container.
8+
9+
**Example 1:**
10+
11+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg)
12+
13+
**Input:** height = [1,8,6,2,5,4,8,3,7]
14+
15+
**Output:** 49
16+
17+
**Explanation:** The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
18+
19+
**Example 2:**
20+
21+
**Input:** height = [1,1]
22+
23+
**Output:** 1
24+
25+
**Example 3:**
26+
27+
**Input:** height = [4,3,2,1,4]
28+
29+
**Output:** 16
30+
31+
**Example 4:**
32+
33+
**Input:** height = [1,2,1]
34+
35+
**Output:** 2
36+
37+
**Constraints:**
38+
39+
* `n == height.length`
40+
* <code>2 <= n <= 10<sup>5</sup></code>
41+
* <code>0 <= height[i] <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace leetcode\g0001_0100\s0015_3sum;
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting #Two_Pointers
6+
// #Data_Structure_II_Day_1_Array #Algorithm_II_Day_3_Two_Pointers #Udemy_Two_Pointers
7+
// #Big_O_Time_O(n^2)_Space_O(1) #Big_O_Time_O(n*log(n))_Space_O(n^2)
8+
// #2023_12_07_Time_252_ms_(77.94%)_Space_28.5_MB_(94.12%)
9+
10+
class Solution {
11+
/**
12+
* @param Integer[] $nums
13+
* @return Integer[][]
14+
*/
15+
public function threeSum($nums) {
16+
sort($nums);
17+
$len = count($nums);
18+
$result = [];
19+
for ($i = 0; $i < $len - 2; $i++) {
20+
$l = $i + 1;
21+
$r = $len - 1;
22+
while ($r > $l) {
23+
$sum = $nums[$i] + $nums[$l] + $nums[$r];
24+
if ($sum < 0) {
25+
$l++;
26+
} elseif ($sum > 0) {
27+
$r--;
28+
} else {
29+
$list = [$nums[$i], $nums[$l], $nums[$r]];
30+
$result[] = $list;
31+
while ($l < $r && $nums[$l + 1] == $nums[$l]) {
32+
$l++;
33+
}
34+
while ($r > $l && $nums[$r - 1] == $nums[$r]) {
35+
$r--;
36+
}
37+
$l++;
38+
$r--;
39+
}
40+
}
41+
while ($i < $len - 1 && $nums[$i + 1] == $nums[$i]) {
42+
$i++;
43+
}
44+
}
45+
return $result;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
15\. 3Sum
2+
3+
Medium
4+
5+
Given an integer array nums, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`.
6+
7+
Notice that the solution set must not contain duplicate triplets.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [-1,0,1,2,-1,-4]
12+
13+
**Output:** [[-1,-1,2],[-1,0,1]]
14+
15+
**Example 2:**
16+
17+
**Input:** nums = []
18+
19+
**Output:** []
20+
21+
**Example 3:**
22+
23+
**Input:** nums = [0]
24+
25+
**Output:** []
26+
27+
**Constraints:**
28+
29+
* `0 <= nums.length <= 3000`
30+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace leetcode\g0001_0100\s0017_letter_combinations_of_a_phone_number;
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Backtracking
6+
// #Algorithm_II_Day_11_Recursion_Backtracking #Udemy_Backtracking/Recursion
7+
// #Big_O_Time_O(4^n)_Space_O(n) #2023_12_07_Time_5_ms_(60.19%)_Space_19.1_MB_(59.26%)
8+
9+
class Solution {
10+
/**
11+
* @param String $digits
12+
* @return String[]
13+
*/
14+
public function letterCombinations($digits) {
15+
if (empty($digits)) {
16+
return [];
17+
}
18+
$letters = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];
19+
$ans = [];
20+
$curr = '';
21+
$this->findCombinations(0, $digits, $letters, $curr, $ans);
22+
return $ans;
23+
}
24+
25+
private function findCombinations($start, $nums, $letters, &$curr, &$ans) {
26+
if (strlen($curr) == strlen($nums)) {
27+
$ans[] = $curr;
28+
return;
29+
}
30+
for ($i = $start; $i < strlen($nums); $i++) {
31+
$n = intval($nums[$i]);
32+
for ($j = 0; $j < strlen($letters[$n]); $j++) {
33+
$ch = $letters[$n][$j];
34+
$curr .= $ch;
35+
$this->findCombinations($i + 1, $nums, $letters, $curr, $ans);
36+
$curr = substr($curr, 0, -1);
37+
}
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
17\. Letter Combinations of a Phone Number
2+
3+
Medium
4+
5+
Given a string containing digits from `2-9` inclusive, return all possible letter combinations that the number could represent. Return the answer in **any order**.
6+
7+
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
8+
9+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png)
10+
11+
**Example 1:**
12+
13+
**Input:** digits = "23"
14+
15+
**Output:** ["ad","ae","af","bd","be","bf","cd","ce","cf"]
16+
17+
**Example 2:**
18+
19+
**Input:** digits = ""
20+
21+
**Output:** []
22+
23+
**Example 3:**
24+
25+
**Input:** digits = "2"
26+
27+
**Output:** ["a","b","c"]
28+
29+
**Constraints:**
30+
31+
* `0 <= digits.length <= 4`
32+
* `digits[i]` is a digit in the range `['2', '9']`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace leetcode\g0001_0100\s0019_remove_nth_node_from_end_of_list;
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Two_Pointers #Linked_List
6+
// #Algorithm_I_Day_5_Two_Pointers #Level_2_Day_3_Linked_List #Big_O_Time_O(L)_Space_O(L)
7+
// #2023_12_07_Time_4_ms_(81.93%)_Space_18.9_MB_(91.57%)
8+
9+
use leetcode\com_github_leetcode\ListNode;
10+
11+
/*
12+
* Definition for a singly-linked list.
13+
* class ListNode {
14+
* public $val = 0;
15+
* public $next = null;
16+
* function __construct($val = 0, $next = null) {
17+
* $this->val = $val;
18+
* $this->next = $next;
19+
* }
20+
* }
21+
*/
22+
23+
class Solution {
24+
private $n;
25+
26+
public function removeNthFromEnd($head, $n) {
27+
$this->n = $n;
28+
$node = new ListNode(0, $head);
29+
$this->removeNth($node);
30+
return $node->next;
31+
}
32+
33+
private function removeNth($node) {
34+
if ($node->next === null) {
35+
return;
36+
}
37+
$this->removeNth($node->next);
38+
$this->n--;
39+
if ($this->n == 0) {
40+
$node->next = $node->next->next;
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
19\. Remove Nth Node From End of List
2+
3+
Medium
4+
5+
Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg)
10+
11+
**Input:** head = [1,2,3,4,5], n = 2
12+
13+
**Output:** [1,2,3,5]
14+
15+
**Example 2:**
16+
17+
**Input:** head = [1], n = 1
18+
19+
**Output:** []
20+
21+
**Example 3:**
22+
23+
**Input:** head = [1,2], n = 1
24+
25+
**Output:** [1]
26+
27+
**Constraints:**
28+
29+
* The number of nodes in the list is `sz`.
30+
* `1 <= sz <= 30`
31+
* `0 <= Node.val <= 100`
32+
* `1 <= n <= sz`
33+
34+
**Follow up:** Could you do this in one pass?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace leetcode\g0001_0100\s0020_valid_parentheses;
4+
5+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Stack
6+
// #Data_Structure_I_Day_9_Stack_Queue #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
7+
// #2023_12_07_Time_3_ms_(88.14%)_Space_19.1_MB_(76.99%)
8+
9+
class Solution {
10+
/**
11+
* @param String $s
12+
* @return Boolean
13+
*/
14+
public function isValid($s) {
15+
$stack = [];
16+
$length = strlen($s);
17+
for ($i = 0; $i < $length; $i++) {
18+
$c = $s[$i];
19+
if ($c == '(' || $c == '[' || $c == '{') {
20+
array_push($stack, $c);
21+
} elseif (($c == ')' && !empty($stack) && end($stack) == '(')
22+
|| ($c == '}' && !empty($stack) && end($stack) == '{')
23+
|| ($c == ']' && !empty($stack) && end($stack) == '[')
24+
) {
25+
array_pop($stack);
26+
} else {
27+
return false;
28+
}
29+
}
30+
return empty($stack);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
20\. Valid Parentheses
2+
3+
Easy
4+
5+
Given a string `s` containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid.
6+
7+
An input string is valid if:
8+
9+
1. Open brackets must be closed by the same type of brackets.
10+
2. Open brackets must be closed in the correct order.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "()"
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
**Input:** s = "()[]{}"
21+
22+
**Output:** true
23+
24+
**Example 3:**
25+
26+
**Input:** s = "(]"
27+
28+
**Output:** false
29+
30+
**Example 4:**
31+
32+
**Input:** s = "([)]"
33+
34+
**Output:** false
35+
36+
**Example 5:**
37+
38+
**Input:** s = "{[]}"
39+
40+
**Output:** true
41+
42+
**Constraints:**
43+
44+
* <code>1 <= s.length <= 10<sup>4</sup></code>
45+
* `s` consists of parentheses only `'()[]{}'`.

0 commit comments

Comments
 (0)