Skip to content

Commit

Permalink
Added tasks 230-300
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Dec 23, 2023
1 parent 90dce3f commit 1df75a0
Show file tree
Hide file tree
Showing 31 changed files with 1,017 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace leetcode\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) #2023_12_23_Time_11_ms_(75.00%)_Space_22_MB_(96.88%)

/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($val = 0, $left = null, $right = null) {
* $this->val = $val;
* $this->left = $left;
* $this->right = $right;
* }
* }
*/
class Solution {
private $k;
private $count = 0;
private $val;

/**
* @param TreeNode $root
* @param Integer $k
* @return Integer
*/
public function kthSmallest($root, $k) {
$this->k = $k;
$this->calculate($root);
return $this->val;
}

private function calculate($node) {
if ($node->left == null && $node->right == null) {
$this->count++;
if ($this->count == $this->k) {
$this->val = $node->val;
}
return;
}
if ($node->left != null) {
$this->calculate($node->left);
}
$this->count++;
if ($this->count == $this->k) {
$this->val = $node->val;
return;
}
if ($node->right != null) {
$this->calculate($node->right);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
230\. Kth Smallest Element in a BST

Medium

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_.

**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`.
* <code>1 <= k <= n <= 10<sup>4</sup></code>
* <code>0 <= Node.val <= 10<sup>4</sup></code>

**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?
41 changes: 41 additions & 0 deletions src/main/php/g0201_0300/s0234_palindrome_linked_list/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace leetcode\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)
// #2023_12_23_Time_123_ms_(95.45%)_Space_50.6_MB_(75.00%)

/**
* 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 Boolean
*/
public function isPalindrome($head) {
$array = [];
while ($head) {
$array[] = $head->val;
$head = $head->next;
}
$cnt = count($array);
for ($x = 0; $x < $cnt / 2; $x++) {
$yKey = $cnt - 1 - $x;

if ($array[$x] !== $array[$yKey]) {
return false;
}
}
return true;
}
}
28 changes: 28 additions & 0 deletions src/main/php/g0201_0300/s0234_palindrome_linked_list/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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 <code>[1, 10<sup>5</sup>]</code>.
* `0 <= Node.val <= 9`

**Follow up:** Could you do it in `O(n)` time and `O(1)` space?
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace leetcode\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)
// #2023_12_23_Time_14_ms_(85.71%)_Space_26.1_MB_(57.14%)

/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution {
/**
* @param TreeNode $root
* @param TreeNode $p
* @param TreeNode $q
* @return TreeNode
*/
public function lowestCommonAncestor($root, $p, $q) {
if ($root == null) {
return null;
}
if ($root->val == $p->val || $root->val == $q->val) {
return $root;
}
$left = $this->lowestCommonAncestor($root->left, $p, $q);
$right = $this->lowestCommonAncestor($root->right, $p, $q);
if ($left != null && $right != null) {
return $root;
}
if ($left != null) {
return $left;
}
return $right;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 <code>[2, 10<sup>5</sup>]</code>.
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
* All `Node.val` are **unique**.
* `p != q`
* `p` and `q` will exist in the tree.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace leetcode\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)
// #2023_12_23_Time_58_ms_(86.76%)_Space_29.8_MB_(100.00%)

class Solution {
/**
* @param Integer[] $nums
* @return Integer[]
*/
public function productExceptSelf($nums) {
$product = 1;
$ans = array_fill(0, count($nums), 0);
foreach ($nums as $num) {
$product = $product * $num;
}
for ($i = 0; $i < count($nums); $i++) {
if ($nums[$i] != 0) {
$ans[$i] = $product / $nums[$i];
} else {
$p = 1;
for ($j = 0; $j < count($nums); $j++) {
if ($j != $i) {
$p = $p * $nums[$j];
}
}
$ans[$i] = $p;
}
}
return $ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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:**

* <code>2 <= nums.length <= 10<sup>5</sup></code>
* `-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.)
39 changes: 39 additions & 0 deletions src/main/php/g0201_0300/s0239_sliding_window_maximum/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace leetcode\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)
// #2023_12_23_Time_518_ms_(100.00%)_Space_32.9_MB_(100.00%)

class Solution {
/**
* @param Integer[] $nums
* @param Integer $k
* @return Integer[]
*/
public function maxSlidingWindow($nums, $k) {
$n = count($nums);
$res = array_fill(0, $n - $k + 1, 0);
$x = 0;
$dq = new \SplDoublyLinkedList();
$i = 0;
$j = 0;
while ($j < count($nums)) {
while (!$dq->isEmpty() && $dq->top() < $nums[$j]) {
$dq->pop();
}
$dq->push($nums[$j]);
if ($j - $i + 1 == $k) {
$res[$x] = $dq->bottom();
++$x;
if ($dq->bottom() == $nums[$i]) {
$dq->shift();
}
++$i;
}
++$j;
}
return $res;
}
}
Loading

0 comments on commit 1df75a0

Please sign in to comment.