Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6399120

Browse files
authored
Added tasks 322-560
1 parent1df75a0 commit6399120

File tree

31 files changed

+904
-0
lines changed

31 files changed

+904
-0
lines changed

‎README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
315315

316316
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
317317
|-|-|-|-|-|-
318+
| 0322 |[Coin Change](src/main/php/g0301_0400/s0322_coin_change/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m\*n)_Space_O(amount) | 185 | 96.30
318319

319320
####Day 21
320321

@@ -448,6 +449,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
448449

449450
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
450451
|-|-|-|-|-|-
452+
| 0438 |[Find All Anagrams in a String](src/main/php/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.php)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 29 | 66.67
451453

452454
####Day 13
453455

@@ -687,6 +689,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
687689

688690
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
689691
|-|-|-|-|-|-
692+
| 0438 |[Find All Anagrams in a String](src/main/php/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.php)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 29 | 66.67
690693

691694
####Day 13 Hashmap
692695

@@ -698,6 +701,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
698701

699702
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
700703
|-|-|-|-|-|-
704+
| 0394 |[Decode String](src/main/php/g0301_0400/s0394_decode_string/Solution.php)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 3 | 77.78
701705

702706
####Day 15 Heap
703707

@@ -744,6 +748,8 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
744748

745749
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
746750
|-|-|-|-|-|-
751+
| 0543 |[Diameter of Binary Tree](src/main/php/g0501_0600/s0543_diameter_of_binary_tree/Solution.php)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
752+
| 0437 |[Path Sum III](src/main/php/g0401_0500/s0437_path_sum_iii/Solution.php)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 8 | 100.00
747753

748754
####Day 8 Binary Search
749755

@@ -773,11 +779,13 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
773779
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
774780
|-|-|-|-|-|-
775781
| 0198 |[House Robber](src/main/php/g0101_0200/s0198_house_robber/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 4 | 64.29
782+
| 0322 |[Coin Change](src/main/php/g0301_0400/s0322_coin_change/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m\*n)_Space_O(amount) | 185 | 96.30
776783

777784
####Day 13 Dynamic Programming
778785

779786
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
780787
|-|-|-|-|-|-
788+
| 0416 |[Partition Equal Subset Sum](src/main/php/g0401_0500/s0416_partition_equal_subset_sum/Solution.php)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Big_O_Time_O(n\*sums)_Space_O(n\*sums) | 245 | 86.67
781789
| 0152 |[Maximum Product Subarray](src/main/php/g0101_0200/s0152_maximum_product_subarray/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 15 | 92.86
782790

783791
####Day 14 Sliding Window/Two Pointer
@@ -840,6 +848,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
840848
| 0003 |[Longest Substring Without Repeating Characters](src/main/php/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1) | 4 | 99.33
841849
| 0020 |[Valid Parentheses](src/main/php/g0001_0100/s0020_valid_parentheses/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 3 | 88.14
842850
| 0005 |[Longest Palindromic Substring](src/main/php/g0001_0100/s0005_longest_palindromic_substring/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 13 | 98.57
851+
| 0394 |[Decode String](src/main/php/g0301_0400/s0394_decode_string/Solution.php)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 3 | 77.78
843852
| 0049 |[Group Anagrams](src/main/php/g0001_0100/s0049_group_anagrams/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 27 | 80.85
844853

845854
####Udemy Binary Search
@@ -913,6 +922,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
913922
|-|-|-|-|-|-
914923
| 0094 |[Binary Tree Inorder Traversal](src/main/php/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 3 | 82.09
915924
| 0102 |[Binary Tree Level Order Traversal](src/main/php/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 4 | 96.08
925+
| 0543 |[Diameter of Binary Tree](src/main/php/g0501_0600/s0543_diameter_of_binary_tree/Solution.php)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
916926
| 0226 |[Invert Binary Tree](src/main/php/g0201_0300/s0226_invert_binary_tree/Solution.php)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 6 | 62.79
917927
| 0104 |[Maximum Depth of Binary Tree](src/main/php/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 9 | 63.06
918928
| 0124 |[Binary Tree Maximum Path Sum](src/main/php/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 16 | 100.00
@@ -958,6 +968,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
958968

959969
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
960970
|-|-|-|-|-|-
971+
| 0338 |[Counting Bits](src/main/php/g0301_0400/s0338_counting_bits/Solution.php)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 13 | 97.92
961972

962973
####Udemy Design
963974

@@ -1085,6 +1096,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
10851096
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
10861097
|-|-|-|-|-|-
10871098
| 0238 |[Product of Array Except Self](src/main/php/g0201_0300/s0238_product_of_array_except_self/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 58 | 86.76
1099+
| 0560 |[Subarray Sum Equals K](src/main/php/g0501_0600/s0560_subarray_sum_equals_k/Solution.php)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Big_O_Time_O(n)_Space_O(n) | 82 | 100.00
10881100

10891101
####Day 6 String
10901102

@@ -1172,6 +1184,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
11721184
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
11731185
|-|-|-|-|-|-
11741186
| 0215 |[Kth Largest Element in an Array](src/main/php/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 222 | 49.15
1187+
| 0347 |[Top K Frequent Elements](src/main/php/g0301_0400/s0347_top_k_frequent_elements/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Big_O_Time_O(n\*log(n))_Space_O(k) | 23 | 84.85
11751188

11761189
####Day 21 Heap Priority Queue
11771190

@@ -1293,6 +1306,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
12931306

12941307
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
12951308
|-|-|-|-|-|-
1309+
| 0438 |[Find All Anagrams in a String](src/main/php/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.php)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 29 | 66.67
12961310

12971311
####Day 6 Breadth First Search Depth First Search
12981312

@@ -1371,6 +1385,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
13711385
|<!----> |<!----> |<!----> |<!----> |<!----> |<!---->
13721386
|-|-|-|-|-|-
13731387
| 0072 |[Edit Distance](src/main/php/g0001_0100/s0072_edit_distance/Solution.php)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 29 | 75.00
1388+
| 0322 |[Coin Change](src/main/php/g0301_0400/s0322_coin_change/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m\*n)_Space_O(amount) | 185 | 96.30
13741389

13751390
####Day 19 Bit Manipulation
13761391

@@ -1391,6 +1406,16 @@ Php-based LeetCode algorithm problem solutions, regularly updated.
13911406

13921407
| # | Title | Difficulty | Tag | Time, ms | Time, %
13931408
|------|----------------|-------------|-------------|----------|---------
1409+
| 0560 |[Subarray Sum Equals K](src/main/php/g0501_0600/s0560_subarray_sum_equals_k/Solution.php)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array, Big_O_Time_O(n)_Space_O(n) | 82 | 100.00
1410+
| 0543 |[Diameter of Binary Tree](src/main/php/g0501_0600/s0543_diameter_of_binary_tree/Solution.php)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1411+
| 0494 |[Target Sum](src/main/php/g0401_0500/s0494_target_sum/Solution.php)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Backtracking, Big_O_Time_O(n\*(sum+s))_Space_O(n\*(sum+s)) | 27 | 75.00
1412+
| 0438 |[Find All Anagrams in a String](src/main/php/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.php)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer, Big_O_Time_O(n+m)_Space_O(1) | 29 | 66.67
1413+
| 0437 |[Path Sum III](src/main/php/g0401_0500/s0437_path_sum_iii/Solution.php)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Big_O_Time_O(n)_Space_O(n) | 8 | 100.00
1414+
| 0416 |[Partition Equal Subset Sum](src/main/php/g0401_0500/s0416_partition_equal_subset_sum/Solution.php)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming, Big_O_Time_O(n\*sums)_Space_O(n\*sums) | 245 | 86.67
1415+
| 0394 |[Decode String](src/main/php/g0301_0400/s0394_decode_string/Solution.php)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 3 | 77.78
1416+
| 0347 |[Top K Frequent Elements](src/main/php/g0301_0400/s0347_top_k_frequent_elements/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Data_Structure_II_Day_20_Heap_Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(k) | 23 | 84.85
1417+
| 0338 |[Counting Bits](src/main/php/g0301_0400/s0338_counting_bits/Solution.php)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 13 | 97.92
1418+
| 0322 |[Coin Change](src/main/php/g0301_0400/s0322_coin_change/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(amount) | 185 | 96.30
13941419
| 0300 |[Longest Increasing Subsequence](src/main/php/g0201_0300/s0300_longest_increasing_subsequence/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Algorithm_II_Day_16_Dynamic_Programming, Binary_Search_II_Day_3, Dynamic_Programming_I_Day_18, Udemy_Dynamic_Programming, Big_O_Time_O(n\*log_n)_Space_O(n) | 24 | 90.91
13951420
| 0295 |[Find Median from Data Stream](src/main/php/g0201_0300/s0295_find_median_from_data_stream/MedianFinder.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream, Big_O_Time_O(n\*log_n)_Space_O(n) | 332 | 75.00
13961421
| 0287 |[Find the Duplicate Number](src/main/php/g0201_0300/s0287_find_the_duplicate_number/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5, Big_O_Time_O(n)_Space_O(n) | 174 | 76.92
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespaceleetcode\g0301_0400\s0322_coin_change;
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
6+
// #Breadth_First_Search #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_20
7+
// #Level_2_Day_12_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(amount)
8+
// #2023_12_24_Time_185_ms_(96.30%)_Space_19.4_MB_(62.96%)
9+
10+
class Solution {
11+
/**
12+
* @param Integer[] $coins
13+
* @param Integer $amount
14+
* @return Integer
15+
*/
16+
publicfunctioncoinChange($coins,$amount) {
17+
$dp =array_fill(0,$amount +1,0);
18+
$dp[0] =1;
19+
foreach ($coinsas$coin) {
20+
for ($i =$coin;$i <=$amount;$i++) {
21+
$prev =$dp[$i -$coin];
22+
if ($prev >0) {
23+
if ($dp[$i] ==0) {
24+
$dp[$i] =$prev +1;
25+
}else {
26+
$dp[$i] =min($dp[$i],$prev +1);
27+
}
28+
}
29+
}
30+
}
31+
return$dp[$amount] -1;
32+
}
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
322\. Coin Change
2+
3+
Medium
4+
5+
You are given an integer array`coins` representing coins of different denominations and an integer`amount` representing a total amount of money.
6+
7+
Return_the fewest number of coins that you need to make up that amount_. If that amount of money cannot be made up by any combination of the coins, return`-1`.
8+
9+
You may assume that you have an infinite number of each kind of coin.
10+
11+
**Example 1:**
12+
13+
**Input:** coins =[1,2,5], amount = 11
14+
15+
**Output:** 3
16+
17+
**Explanation:** 11 = 5 + 5 + 1
18+
19+
**Example 2:**
20+
21+
**Input:** coins =[2], amount = 3
22+
23+
**Output:** -1
24+
25+
**Example 3:**
26+
27+
**Input:** coins =[1], amount = 0
28+
29+
**Output:** 0
30+
31+
**Constraints:**
32+
33+
*`1 <= coins.length <= 12`
34+
* <code>1 <= coins[i] <= 2<sup>31</sup> - 1</code>
35+
* <code>0 <= amount <= 10<sup>4</sup></code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespaceleetcode\g0301_0400\s0338_counting_bits;
4+
5+
// #Easy #Top_100_Liked_Questions #Dynamic_Programming #Bit_Manipulation #Udemy_Bit_Manipulation
6+
// #Big_O_Time_O(num)_Space_O(num) #2023_12_24_Time_13_ms_(97.92%)_Space_24.8_MB_(97.92%)
7+
8+
class Solution {
9+
/**
10+
* @param Integer $n
11+
* @return Integer[]
12+
*/
13+
publicfunctioncountBits($num) {
14+
$result =array_fill(0,$num +1,0);
15+
$borderPos =1;
16+
$incrPos =1;
17+
for ($i =1;$i <count($result);$i++) {
18+
// when we reach pow of 2 , reset borderPos and incrPos
19+
if ($incrPos ==$borderPos) {
20+
$result[$i] =1;
21+
$incrPos =1;
22+
$borderPos =$i;
23+
}else {
24+
$result[$i] =1 +$result[$incrPos++];
25+
}
26+
}
27+
return$result;
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
338\. Counting Bits
2+
3+
Easy
4+
5+
Given an integer`n`, return_an array_`ans`_of length_`n + 1`_such that for each_`i` (`0 <= i <= n`)_,_`ans[i]`_is the**number of**_`1`_**'s** in the binary representation of_`i`.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 2
10+
11+
**Output:**[0,1,1]
12+
13+
**Explanation:**
14+
15+
0 --> 0
16+
1 --> 1
17+
2 --> 10
18+
19+
**Example 2:**
20+
21+
**Input:** n = 5
22+
23+
**Output:**[0,1,1,2,1,2]
24+
25+
**Explanation:**
26+
27+
0 --> 0
28+
1 --> 1
29+
2 --> 10
30+
3 --> 11
31+
4 --> 100
32+
5 --> 101
33+
34+
**Constraints:**
35+
36+
* <code>0 <= n <= 10<sup>5</sup></code>
37+
38+
**Follow up:**
39+
40+
* It is very easy to come up with a solution with a runtime of`O(n log n)`. Can you do it in linear time`O(n)` and possibly in a single pass?
41+
* Can you do it without using any built-in function (i.e., like`__builtin_popcount` in C++)?
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespaceleetcode\g0301_0400\s0347_top_k_frequent_elements;
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Sorting
6+
// #Heap_Priority_Queue #Counting #Divide_and_Conquer #Quickselect #Bucket_Sort
7+
// #Data_Structure_II_Day_20_Heap_Priority_Queue #Big_O_Time_O(n*log(n))_Space_O(k)
8+
// #2023_12_24_Time_23_ms_(84.85%)_Space_21.6_MB_(100.00%)
9+
10+
class Solution {
11+
/**
12+
* @param Integer[] $nums
13+
* @param Integer $k
14+
* @return Integer[]
15+
*/
16+
publicfunctiontopKFrequent($nums,$k) {
17+
$count = [];
18+
foreach ($numsas$num) {
19+
if (!isset($count[$num])) {
20+
$count[$num] =0;
21+
}
22+
$count[$num] +=1;
23+
}
24+
arsort($count);
25+
$result = [];
26+
$r =0;
27+
foreach ($countas$i =>$num) {
28+
$result[] =$i;
29+
$r++;
30+
if ($r ===$k) {
31+
break;
32+
}
33+
}
34+
return$result;
35+
}
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
347\. Top K Frequent Elements
2+
3+
Medium
4+
5+
Given an integer array`nums` and an integer`k`, return_the_`k`_most frequent elements_. You may return the answer in**any order**.
6+
7+
**Example 1:**
8+
9+
**Input:** nums =[1,1,1,2,2,3], k = 2
10+
11+
**Output:**[1,2]
12+
13+
**Example 2:**
14+
15+
**Input:** nums =[1], k = 1
16+
17+
**Output:**[1]
18+
19+
**Constraints:**
20+
21+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
22+
*`k` is in the range`[1, the number of unique elements in the array]`.
23+
* It is**guaranteed** that the answer is**unique**.
24+
25+
**Follow up:** Your algorithm's time complexity must be better than`O(n log n)`, where n is the array's size.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespaceleetcode\g0301_0400\s0394_decode_string;
4+
5+
// #Medium #Top_100_Liked_Questions #String #Stack #Recursion #Level_1_Day_14_Stack #Udemy_Strings
6+
// #Big_O_Time_O(n)_Space_O(n) #2023_12_24_Time_3_ms_(77.78%)_Space_19.7_MB_(11.11%)
7+
8+
class Solution {
9+
private$i =0;
10+
11+
/**
12+
* @param String $s
13+
* @return String
14+
*/
15+
publicfunctiondecodeString($s) {
16+
$count =0;
17+
$sb ="";
18+
while ($this->i <strlen($s)) {
19+
$c =$s[$this->i];
20+
$this->i++;
21+
if (ctype_alpha($c)) {
22+
$sb .=$c;
23+
}elseif (ctype_digit($c)) {
24+
$count =$count *10 +intval($c);
25+
}elseif ($c ==']') {
26+
break;
27+
}elseif ($c =='[') {
28+
// sub problem
29+
$repeat =$this->decodeString($s);
30+
while ($count >0) {
31+
$sb .=$repeat;
32+
$count--;
33+
}
34+
}
35+
}
36+
return$sb;
37+
}
38+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp