Skip to content

Commit 9d7d984

Browse files
committed
Reformated sources
1 parent 2ebe100 commit 9d7d984

File tree

164 files changed

+1711
-1314
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+1711
-1314
lines changed

src/Algorithms/s0001_two_sum/Solution.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
4+
// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
5+
// #2024_01_16_Time_15_ms_(85.61%)_Space_20_MB_(40.39%)
6+
17
class Solution {
28

39
/**
@@ -13,9 +19,9 @@ function twoSum($nums, $target) {
1319
for ($i = 0; $i < count($nums); $i++) {
1420
$complement = $target - $nums[$i];
1521
if (array_key_exists($complement, $map) && $map[$complement] != $i) {
16-
return [$i, $map[$complement] ];
22+
return [$i, $map[$complement]];
1723
}
1824
}
19-
throw new IllegalArgumentException("No two sum solution");
25+
throw new IllegalArgumentException("No two sum solution");
2026
}
2127
}

src/Algorithms/s0002_add_two_numbers/Solution.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
4+
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
5+
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #2024_01_16_Time_14_ms_(73.15%)_Space_19.6_MB_(25.98%)
6+
17
/**
28
* Definition for a singly-linked list.
39
* class ListNode {
@@ -15,7 +21,9 @@ class Solution {
1521
*/
1622
function addTwoNumbers($l1, $l2) {
1723
$dummyHead = new ListNode(0);
18-
$p = $l1; $q = $l2; $curr = $dummyHead;
24+
$p = $l1;
25+
$q = $l2;
26+
$curr = $dummyHead;
1927
$carry = 0;
2028
while ($p != null || $q != null) {
2129
$x = ($p != null) ? $p->val : 0;
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1+
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
4+
// #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
5+
// #Big_O_Time_O(n)_Space_O(1) #2024_01_16_Time_14_ms_(75.32%)_Space_19.8_MB_(36.83%)
6+
17
class Solution {
28

39
/**
410
* @param String $s
511
* @return Integer
612
*/
713
function lengthOfLongestSubstring($s) {
8-
if (strlen($s)==0) return 0;
14+
if (strlen($s) == 0) return 0;
915
$map = [];
1016
$max = 0;
11-
for ($i=0, $j=0; $i < strlen($s); ++$i) {
12-
if (array_key_exists($s[$i], $map)){
17+
for ($i = 0, $j = 0; $i < strlen($s); ++$i) {
18+
if (array_key_exists($s[$i], $map)) {
1319
$j = max($j, $map[$s[$i]] + 1);
1420
}
1521
$map[$s[$i]] = $i;
1622
$max = max($max, $i - $j + 1);
1723
}
1824
return $max;
1925
}
20-
}
26+
}

src/Algorithms/s0004_median_of_two_sorted_arrays/Solution.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
3+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
4+
// #Big_O_Time_O(log(min(N,M)))_Space_O(1) #2024_01_16_Time_28_ms_(75.00%)_Space_19.6_MB_(54.49%)
5+
16
class Solution {
27

38
/**
@@ -7,30 +12,23 @@ class Solution {
712
*/
813
function findMedianSortedArrays($nums1, $nums2) {
914
$array = range(0, count($nums1) + count($nums2) - 1);
10-
$p=0; $q=0;
15+
$p = 0;
16+
$q = 0;
1117

1218

13-
for($i =0;$i< count($array);$i++)
14-
{
15-
if(($p<count($nums1) && $q< count($nums2) && $nums1[$p]<=$nums2[$q]) || $q==count($nums2))
16-
{
17-
$array[$i]=$nums1[$p];
19+
for ($i = 0; $i < count($array); $i++) {
20+
if (($p < count($nums1) && $q < count($nums2) && $nums1[$p] <= $nums2[$q]) || $q == count($nums2)) {
21+
$array[$i] = $nums1[$p];
1822
$p++;
19-
}
20-
21-
else if(($p<count($nums1) && $q< count($nums2) && $nums1[$p]>$nums2[$q]) || $p==count($nums1))
22-
{
23-
$array[$i]=$nums2[$q];
23+
} else if (($p < count($nums1) && $q < count($nums2) && $nums1[$p] > $nums2[$q]) || $p == count($nums1)) {
24+
$array[$i] = $nums2[$q];
2425
$q++;
2526
}
2627
}
2728

28-
if (intval(count($array) % 2)==0)
29-
{
30-
return doubleval($array[count($array) / 2] + $array[(count($array) / 2) - 1])/2;
31-
}
32-
else
33-
{
29+
if (intval(count($array) % 2) == 0) {
30+
return doubleval($array[count($array) / 2] + $array[(count($array) / 2) - 1]) / 2;
31+
} else {
3432
return doubleval($array[count($array) / 2]);
3533
}
3634
}
Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1+
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
4+
// #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
5+
// #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
6+
// #2024_01_16_Time_1146_ms_(40.69%)_Space_37.4_MB_(6.37%)
7+
18
class Solution {
29

310
/**
411
* @param String $s
512
* @return String
613
*/
714
function longestPalindrome($s) {
8-
if(strlen($s) == 0) {
15+
if (strlen($s) == 0) {
916
return $s;
10-
}
11-
$dp = array_fill(0, strlen($s), array_fill(0, strlen($s), false));
12-
$longestPalindromeStart = 0; $longestPalindromeLength = 1;
13-
for($i=strlen($s)-1;$i>=0;$i--){
17+
}
18+
$dp = array_fill(0, strlen($s), array_fill(0, strlen($s), false));
19+
$longestPalindromeStart = 0;
20+
$longestPalindromeLength = 1;
21+
for ($i = strlen($s) - 1; $i >= 0; $i--) {
1422
$dp[$i][$i] = true; //single character is a palindrome so making it true
15-
for($j=$i+1;$j<strlen($s);$j++){
16-
if(($s[$i]==$s[$j] && $dp[$i+1][$j-1])|| ($i+1==$j&&$s[$i]==$s[$j] )){ //checking for two edge cases 1.if characters are equal check for diagonal lower left one if true make it true 2. or if both equal and its the first elelment in the loop make it as true
17-
$dp[$i][$j] = true;
23+
for ($j = $i + 1; $j < strlen($s); $j++) {
24+
if (($s[$i] == $s[$j] && $dp[$i + 1][$j - 1]) || ($i + 1 == $j && $s[$i] == $s[$j])) { //checking for two edge cases 1.if characters are equal check for diagonal lower left one if true make it true 2. or if both equal and its the first elelment in the loop make it as true
25+
$dp[$i][$j] = true;
1826
if ($j - $i + 1 > $longestPalindromeLength) { // update the length if greater than previous
1927
$longestPalindromeLength = $j - $i + 1;
2028
$longestPalindromeStart = $i;
@@ -23,6 +31,6 @@ function longestPalindrome($s) {
2331
}
2432
}
2533
// return the substring using starting and ending index
26-
return substr($s, $longestPalindromeStart,$longestPalindromeLength);
34+
return substr($s, $longestPalindromeStart, $longestPalindromeLength);
2735
}
28-
}
36+
}

src/Algorithms/s0006_zigzag_conversion/Solution.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2+
3+
// #Medium #String #2024_01_16_Time_10_ms_(90.40%)_Space_19.5_MB_(58.08%)
4+
15
class Solution {
26

37
/**
@@ -21,4 +25,4 @@ function convert($s, $numRows) {
2125
}
2226
return $ret;
2327
}
24-
}
28+
}
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
2+
3+
// #Medium #Top_Interview_Questions #Math #Udemy_Integers
4+
// #2024_01_16_Time_7_ms_(67.94%)_Space_19.5_MB_(33.82%)
5+
16
class Solution {
27

38
/**
49
* @param Integer $x
510
* @return Integer
611
*/
712
function reverse($x) {
8-
$rev = 0;
13+
$rev = 0;
914
while ($x != 0) {
1015
$rev = ($rev * 10) + ($x % 10);
1116
$x = intval($x / 10);
1217
}
13-
if (intval($rev) > 2147483647 || intval($rev) < -2147483647 ) return 0;
14-
return intval($rev);
18+
if (intval($rev) > 2147483647 || intval($rev) < -2147483647) return 0;
19+
return intval($rev);
1520
}
1621
}
Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
1+
2+
3+
// #Medium #Top_Interview_Questions #String #2024_01_16_Time_4_ms_(85.00%)_Space_19.8_MB_(8.00%)
4+
15
class Solution {
26

37
/**
48
* @param String $str
59
* @return Integer
610
*/
711
function myAtoi($str) {
8-
if (trim($str) == '')
9-
return 0;
10-
$str = trim($str);
11-
$i = 0; $ans = 0; $sign = 1; $len = strlen($str);
12-
if ($str[$i] == '-' || $str[$i] == '+')
13-
$sign = $str[$i++] == '+' ? 1 : -1;
14-
for (; $i < $len; ++$i) {
15-
$tmp = ord($str[$i]) - ord('0');
16-
if ($tmp < 0 || $tmp > 9)
17-
break;
18-
if ($ans > intval(2147483647 / 10)
19-
|| ($ans == intval(2147483647 / 10) && (2147483647 % 10) < $tmp)) {
20-
return $sign == 1 ? 2147483647 : -2147483648;
21-
} else
22-
$ans = $ans * 10 + $tmp;
23-
}
24-
return $sign * $ans;
12+
if (trim($str) == '')
13+
return 0;
14+
$str = trim($str);
15+
$i = 0;
16+
$ans = 0;
17+
$sign = 1;
18+
$len = strlen($str);
19+
if ($str[$i] == '-' || $str[$i] == '+')
20+
$sign = $str[$i++] == '+' ? 1 : -1;
21+
for (; $i < $len; ++$i) {
22+
$tmp = ord($str[$i]) - ord('0');
23+
if ($tmp < 0 || $tmp > 9)
24+
break;
25+
if ($ans > intval(2147483647 / 10)
26+
|| ($ans == intval(2147483647 / 10) && (2147483647 % 10) < $tmp)) {
27+
return $sign == 1 ? 2147483647 : -2147483648;
28+
} else
29+
$ans = $ans * 10 + $tmp;
30+
}
31+
return $sign * $ans;
2532
}
2633
}
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
2+
3+
// #Easy #Math #Udemy_Integers #2024_01_16_Time_25_ms_(72.15%)_Space_19.6_MB_(10.33%)
4+
15
class Solution {
26

37
/**
48
* @param Integer $x
59
* @return Boolean
610
*/
711
function isPalindrome($x) {
8-
if($x<0) return false;
9-
$i = 0; $rev = 0; $ori = $x;
10-
for($i=10; $x>0; $x= intval($x / 10)){
12+
if ($x < 0) return false;
13+
$i = 0;
14+
$rev = 0;
15+
$ori = $x;
16+
for ($i = 10; $x > 0; $x = intval($x / 10)) {
1117
$rev *= 10;
1218
$rev += $x % $i;
1319
}
14-
return $rev==$ori;
20+
return $rev == $ori;
1521
}
16-
}
22+
}
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2+
3+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming #Recursion
4+
// #Udemy_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n)
5+
// #2024_01_16_Time_16_ms_(25.58%)_Space_19.3_MB_(79.07%)
6+
17
class Solution {
28

39
/**
@@ -9,18 +15,18 @@ function isMatch($s, $p) {
915
$dp = array_fill(0, strlen($s) + 1, array_fill(0, strlen($p) + 1, false));
1016
$dp[strlen($s)][strlen($p)] = true;
1117

12-
for ($i = strlen($s); $i >= 0; $i--){
13-
for ($j = strlen($p) - 1; $j >= 0; $j--){
18+
for ($i = strlen($s); $i >= 0; $i--) {
19+
for ($j = strlen($p) - 1; $j >= 0; $j--) {
1420
$first_match = ($i < strlen($s) &&
15-
($p[$j] == $s[$i] ||
16-
$p[$j] == '.'));
17-
if ($j + 1 < strlen($p) && $p[$j+1] == '*'){
18-
$dp[$i][$j] = $dp[$i][$j+2] || $first_match && $dp[$i+1][$j];
21+
($p[$j] == $s[$i] ||
22+
$p[$j] == '.'));
23+
if ($j + 1 < strlen($p) && $p[$j + 1] == '*') {
24+
$dp[$i][$j] = $dp[$i][$j + 2] || $first_match && $dp[$i + 1][$j];
1925
} else {
20-
$dp[$i][$j] = $first_match && $dp[$i+1][$j+1];
26+
$dp[$i][$j] = $first_match && $dp[$i + 1][$j + 1];
2127
}
2228
}
2329
}
2430
return $dp[0][0];
2531
}
26-
}
32+
}

src/Algorithms/s0011_container_with_most_water/Solution.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
2+
13
class Solution {
24

35
/**
46
* @param Integer[] $height
57
* @return Integer
68
*/
79
function maxArea($height) {
8-
$maxarea = 0; $l = 0; $r = count($height) - 1;
10+
$maxarea = 0;
11+
$l = 0;
12+
$r = count($height) - 1;
913
while ($l < $r) {
1014
$maxarea = max($maxarea, min($height[$l], $height[$r]) * ($r - $l));
1115
if ($height[$l] < $height[$r])

src/Algorithms/s0012_integer_to_roman/Solution.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
class Solution {
24

35
/**
@@ -9,15 +11,15 @@ function intToRoman($num) {
911
$values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
1012
$roman = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
1113
$i = 0;
12-
13-
while($num > 0){
14-
$repeats = intval($num/$values[$i]);
15-
if($repeats >= 1){
16-
while($repeats != 0){
14+
15+
while ($num > 0) {
16+
$repeats = intval($num / $values[$i]);
17+
if ($repeats >= 1) {
18+
while ($repeats != 0) {
1719
$sb .= $roman[$i];
1820
$repeats--;
1921
}
20-
$num = $num - intval($num/$values[$i])*$values[$i];
22+
$num = $num - intval($num / $values[$i]) * $values[$i];
2123
}
2224
$i++;
2325
}

0 commit comments

Comments
 (0)