Skip to content

Commit 1df75a0

Browse files
authored
Added tasks 230-300
1 parent 90dce3f commit 1df75a0

File tree

31 files changed

+1017
-0
lines changed

31 files changed

+1017
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace leetcode\g0201_0300\s0230_kth_smallest_element_in_a_bst;
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree
6+
// #Binary_Search_Tree #Data_Structure_II_Day_17_Tree #Level_2_Day_9_Binary_Search_Tree
7+
// #Big_O_Time_O(n)_Space_O(n) #2023_12_23_Time_11_ms_(75.00%)_Space_22_MB_(96.88%)
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* class TreeNode {
12+
* public $val = null;
13+
* public $left = null;
14+
* public $right = null;
15+
* function __construct($val = 0, $left = null, $right = null) {
16+
* $this->val = $val;
17+
* $this->left = $left;
18+
* $this->right = $right;
19+
* }
20+
* }
21+
*/
22+
class Solution {
23+
private $k;
24+
private $count = 0;
25+
private $val;
26+
27+
/**
28+
* @param TreeNode $root
29+
* @param Integer $k
30+
* @return Integer
31+
*/
32+
public function kthSmallest($root, $k) {
33+
$this->k = $k;
34+
$this->calculate($root);
35+
return $this->val;
36+
}
37+
38+
private function calculate($node) {
39+
if ($node->left == null && $node->right == null) {
40+
$this->count++;
41+
if ($this->count == $this->k) {
42+
$this->val = $node->val;
43+
}
44+
return;
45+
}
46+
if ($node->left != null) {
47+
$this->calculate($node->left);
48+
}
49+
$this->count++;
50+
if ($this->count == $this->k) {
51+
$this->val = $node->val;
52+
return;
53+
}
54+
if ($node->right != null) {
55+
$this->calculate($node->right);
56+
}
57+
}
58+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
230\. Kth Smallest Element in a BST
2+
3+
Medium
4+
5+
Given the `root` of a binary search tree, and an integer `k`, return _the_ <code>k<sup>th</sup></code> _smallest value (**1-indexed**) of all the values of the nodes in the tree_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg)
10+
11+
**Input:** root = [3,1,4,null,2], k = 1
12+
13+
**Output:** 1
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg)
18+
19+
**Input:** root = [5,3,6,2,4,null,null,1], k = 3
20+
21+
**Output:** 3
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is `n`.
26+
* <code>1 <= k <= n <= 10<sup>4</sup></code>
27+
* <code>0 <= Node.val <= 10<sup>4</sup></code>
28+
29+
**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?
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace leetcode\g0201_0300\s0234_palindrome_linked_list;
4+
5+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Two_Pointers #Stack #Linked_List
6+
// #Recursion #Level_2_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1)
7+
// #2023_12_23_Time_123_ms_(95.45%)_Space_50.6_MB_(75.00%)
8+
9+
/**
10+
* Definition for a singly-linked list.
11+
* class ListNode {
12+
* public $val = 0;
13+
* public $next = null;
14+
* function __construct($val = 0, $next = null) {
15+
* $this->val = $val;
16+
* $this->next = $next;
17+
* }
18+
* }
19+
*/
20+
class Solution {
21+
/**
22+
* @param ListNode $head
23+
* @return Boolean
24+
*/
25+
public function isPalindrome($head) {
26+
$array = [];
27+
while ($head) {
28+
$array[] = $head->val;
29+
$head = $head->next;
30+
}
31+
$cnt = count($array);
32+
for ($x = 0; $x < $cnt / 2; $x++) {
33+
$yKey = $cnt - 1 - $x;
34+
35+
if ($array[$x] !== $array[$yKey]) {
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
234\. Palindrome Linked List
2+
3+
Easy
4+
5+
Given the `head` of a singly linked list, return `true` if it is a palindrome.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg)
10+
11+
**Input:** head = [1,2,2,1]
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg)
18+
19+
**Input:** head = [1,2]
20+
21+
**Output:** false
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.
26+
* `0 <= Node.val <= 9`
27+
28+
**Follow up:** Could you do it in `O(n)` time and `O(1)` space?
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace leetcode\g0201_0300\s0236_lowest_common_ancestor_of_a_binary_tree;
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree
6+
// #Data_Structure_II_Day_18_Tree #Udemy_Tree_Stack_Queue #Big_O_Time_O(n)_Space_O(n)
7+
// #2023_12_23_Time_14_ms_(85.71%)_Space_26.1_MB_(57.14%)
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* class TreeNode {
12+
* public $val = null;
13+
* public $left = null;
14+
* public $right = null;
15+
* function __construct($value) { $this->val = $value; }
16+
* }
17+
*/
18+
class Solution {
19+
/**
20+
* @param TreeNode $root
21+
* @param TreeNode $p
22+
* @param TreeNode $q
23+
* @return TreeNode
24+
*/
25+
public function lowestCommonAncestor($root, $p, $q) {
26+
if ($root == null) {
27+
return null;
28+
}
29+
if ($root->val == $p->val || $root->val == $q->val) {
30+
return $root;
31+
}
32+
$left = $this->lowestCommonAncestor($root->left, $p, $q);
33+
$right = $this->lowestCommonAncestor($root->right, $p, $q);
34+
if ($left != null && $right != null) {
35+
return $root;
36+
}
37+
if ($left != null) {
38+
return $left;
39+
}
40+
return $right;
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
236\. Lowest Common Ancestor of a Binary Tree
2+
3+
Medium
4+
5+
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
6+
7+
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**).”
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2018/12/14/binarytree.png)
12+
13+
**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
14+
15+
**Output:** 3
16+
17+
**Explanation:** The LCA of nodes 5 and 1 is 3.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2018/12/14/binarytree.png)
22+
23+
**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
24+
25+
**Output:** 5
26+
27+
**Explanation:** The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
28+
29+
**Example 3:**
30+
31+
**Input:** root = [1,2], p = 1, q = 2
32+
33+
**Output:** 1
34+
35+
**Constraints:**
36+
37+
* The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.
38+
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
39+
* All `Node.val` are **unique**.
40+
* `p != q`
41+
* `p` and `q` will exist in the tree.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace leetcode\g0201_0300\s0238_product_of_array_except_self;
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Prefix_Sum
6+
// #Data_Structure_II_Day_5_Array #Udemy_Arrays #Big_O_Time_O(n^2)_Space_O(n)
7+
// #2023_12_23_Time_58_ms_(86.76%)_Space_29.8_MB_(100.00%)
8+
9+
class Solution {
10+
/**
11+
* @param Integer[] $nums
12+
* @return Integer[]
13+
*/
14+
public function productExceptSelf($nums) {
15+
$product = 1;
16+
$ans = array_fill(0, count($nums), 0);
17+
foreach ($nums as $num) {
18+
$product = $product * $num;
19+
}
20+
for ($i = 0; $i < count($nums); $i++) {
21+
if ($nums[$i] != 0) {
22+
$ans[$i] = $product / $nums[$i];
23+
} else {
24+
$p = 1;
25+
for ($j = 0; $j < count($nums); $j++) {
26+
if ($j != $i) {
27+
$p = $p * $nums[$j];
28+
}
29+
}
30+
$ans[$i] = $p;
31+
}
32+
}
33+
return $ans;
34+
}
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
238\. Product of Array Except Self
2+
3+
Medium
4+
5+
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]`.
6+
7+
The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
8+
9+
You must write an algorithm that runs in `O(n)` time and without using the division operation.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,3,4]
14+
15+
**Output:** [24,12,8,6]
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [-1,1,0,-3,3]
20+
21+
**Output:** [0,0,9,0,0]
22+
23+
**Constraints:**
24+
25+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
26+
* `-30 <= nums[i] <= 30`
27+
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
28+
29+
**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.)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace leetcode\g0201_0300\s0239_sliding_window_maximum;
4+
5+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Heap_Priority_Queue
6+
// #Sliding_Window #Queue #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
7+
// #2023_12_23_Time_518_ms_(100.00%)_Space_32.9_MB_(100.00%)
8+
9+
class Solution {
10+
/**
11+
* @param Integer[] $nums
12+
* @param Integer $k
13+
* @return Integer[]
14+
*/
15+
public function maxSlidingWindow($nums, $k) {
16+
$n = count($nums);
17+
$res = array_fill(0, $n - $k + 1, 0);
18+
$x = 0;
19+
$dq = new \SplDoublyLinkedList();
20+
$i = 0;
21+
$j = 0;
22+
while ($j < count($nums)) {
23+
while (!$dq->isEmpty() && $dq->top() < $nums[$j]) {
24+
$dq->pop();
25+
}
26+
$dq->push($nums[$j]);
27+
if ($j - $i + 1 == $k) {
28+
$res[$x] = $dq->bottom();
29+
++$x;
30+
if ($dq->bottom() == $nums[$i]) {
31+
$dq->shift();
32+
}
33+
++$i;
34+
}
35+
++$j;
36+
}
37+
return $res;
38+
}
39+
}

0 commit comments

Comments
 (0)