Skip to content

Commit d742b38

Browse files
committed
add Top_K_Frequent_Elements
1 parent 8e22839 commit d742b38

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

Python/Longest_Common_Subsequence.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Question(#1143): Given two strings text1 and text2, return the length of their longest common subsequence.
2+
# Solution: Use the longest common susequence dynamic programming algorithm
3+
4+
def longestCommonSubsequence(text1: str, text2: str) -> int:
5+
grid = [[0 for _ in range(len(text2)+1)] for _ in range(len(text1)+1)]
6+
7+
for i in range(1, len(text1)+1):
8+
for j in range(1, len(text2)+1):
9+
if text1[i-1] == text2[j-1]:
10+
grid[i][j] = 1 + grid[i-1][j-1]
11+
else:
12+
grid[i][j] = max(grid[i][j-1], grid[i-1][j])
13+
14+
return grid[-1][-1]

Python/Top_K_Frequent_Elements.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Question(#347): Given a non-empty array of integers, return the k most frequent elements.
2+
# Solution: Use a counting sort to place the most frequent items into buckets, loop through the buckets backwards,
3+
# (since the last bucket cooresponds to the most common item), add the items in the bucket to our result and decrement
4+
# value of k by the amount of items in the bucket
5+
6+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
7+
# Make an array with len(nums) + 1 positions, because the max frequency of any element
8+
# can coorespond to the len(nums) + 1 index.
9+
buckets = [[] for _ in range(len(nums) + 1)]
10+
result = []
11+
12+
# Save a number to the bucket which cooresponds to its frequency
13+
for a, b in collections.Counter(nums).items(): buckets[b] += [a]
14+
15+
# Loop through the buckets backwards, since the ones at the end are the most frequent elements
16+
for i in buckets[::-1]:
17+
# If k becomes 0 or negative, we've found enough items
18+
if k <= 0: return result
19+
20+
# If there's stuff in the current bucket, add that stuff to our result, and
21+
# decrement k by the amount of things we just added
22+
if i:
23+
result += i
24+
k -= len(i)
25+
26+
return result

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
| Product of Array Except Self | 22 | [238](https://leetcode.com/problems/product-of-array-except-self) | Medium | Array | [Python](Python/Product_of_Array_Except_Self.py)|
3131
| Trapping Rain Water | 23 | [42](https://leetcode.com/problems/trapping-rain-water) | Hard | Array, Two Pointer | [Python](Python/Trapping_Rain_Water.py) |
3232
| Longest Consecutive Sequence | 24 | [128](https://leetcode.com/problems/longest-consecutive-sequence/) | Hard | Array | [Python](Python/Longest_Consecutive_Sequence.py) |
33+
| Top K Frequent Elements | 25 | [347](https://leetcode.com/problems/top-k-frequent-elements/) | Medium | Array, Counting Sort, Hashmap | [Python](Python/Top_K_Frequent_Elements.py) |
34+
3335

3436
## 🧵 Strings
3537
| Title | Leetcode # | Difficulty | Tags | Solution |
@@ -143,6 +145,7 @@
143145
| Unique Paths | 4 | [62](https://leetcode.com/problems/unique-paths/) | Medium | Dynamic Programming, Top Down | [Python](Python/Unique_Paths.py) |
144146
| Delete Operation for Two Strings | 5 | [583](https://leetcode.com/problems/delete-operation-for-two-strings/) | Medium | Dynamic Programming | [Python](Python/Delete_Operation_for_Two_Strings.py) |
145147
| Edit Distance | 6 | [72](https://leetcode.com/problems/edit-distance/) | Hard | Dynamic Programming, Top Down | [Python](Python/Edit_Distance.py) |
148+
| Longest Common Subsequence | 7 | [1143](https://leetcode.com/problems/longest-common-subsequence/) | Hard | Dynamic Programming, Bottom Up | [Python](Python/Longest_Common_Subsequence.py) |
146149

147150

148151
## 🎨 Design

0 commit comments

Comments
 (0)