Skip to content

Commit 78b29ed

Browse files
committed
1104
1 parent 346bff9 commit 78b29ed

2 files changed

Lines changed: 40 additions & 15 deletions

File tree

README.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,22 @@ Feel free to add issues, create pull requests and be a contributer.
9393
| Website | Title | Solution | Time | Space | Difficulty | Note|
9494
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
9595
| Leetcode | [1. Two Sum](https://leetcode.com/problems/two-sum) | [Java](./java/Twosum.java) | _O(n)_ | _O(n)_ | Easy | |
96-
| Leetcode | [359. Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/description/) | [Java](./java/shouldPrintMessage.java) | _O(n)_ | _O(n)_ | Easy | |
97-
| Leetcode | [535. Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/description/) | [Java](./java/Codec.java) | _O(n)_ | _O(1)_ | Medium | |
96+
| Leetcode | [169. Majority Element](https://leetcode.com/problems/majority-element/description/) | [Java](./java/majorityElement.java) | _O(n)_ | _O(n)_ | Easy | |
97+
| Leetcode | [170. Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/description/) | [Java](./java/twoSum.java) | _O(n)_ | _O(n)_ | Medium | |
98+
| Leetcode | [219. Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/description/) | [Java](./java/containsNearbyDuplicate.java) | _O(n)_ | _O(n)_ | Easy | |
9899
| Leetcode | [266. Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/description/) | [Java](./java/canPermutePalindrome.java) | _O(n)_ | _O(1)_ | Easy | |
100+
| Leetcode | [359. Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/description/) | [Java](./java/topKFrequent.java) | _O(n)_ | _O(n)_ | Easy | |
101+
| Leetcode | [347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/description/) | [Java](./java/shouldPrintMessage.java) | _O(n)_ | _O(n)_ | Medium | |
102+
| Leetcode | [454. 4Sum II](https://leetcode.com/problems/4sum-ii/description/) | [Java](./java/fourSumCount.java) | _O(n^2)_ | _O(n^2)_ | Medium | |
103+
| Leetcode | [506. Relative Ranks](https://leetcode.com/problems/relative-ranks/description/) | [Java](./java/findRelativeRanks.java) | _O(n)_ | _O(n)_ | Easy | |
99104
| Leetcode | [525. Contiguous Array](https://leetcode.com/problems/contiguous-array/description/) | [Java](./java/FindMaxLength.java) | _O( )_ | _O( )_ | Medium | |
100-
| Leetcode | [170. Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/description/) | [Java](./java/twoSum.java) | _O(n)_ | _O(n)_ | Medium | |
101-
| Leetcode | [594. Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/description/) | [Java](./java/findLHS.java) | _O(n)_ | _O(n)_ | Easy | |
102105
| Leetcode | [532. K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/description/) | [Java](./java/findPairs.java) | _O(n)_ | _O(n)_ | Easy | |
103-
| Leetcode | [506. Relative Ranks](https://leetcode.com/problems/relative-ranks/description/) | [Java](./java/findRelativeRanks.java) | _O(n)_ | _O(n)_ | Easy | |
106+
| Leetcode | [535. Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/description/) | [Java](./java/Codec.java) | _O(n)_ | _O(1)_ | Medium | |
107+
| Leetcode | [594. Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/description/) | [Java](./java/findLHS.java) | _O(n)_ | _O(n)_ | Easy | |
104108
| Leetcode | [677. Map Sum Pairs](https://leetcode.com/contest/leetcode-weekly-contest-50/problems/map-sum-pairs/) | [Java](./java/MapSum.java) | _O(n)_ | _O(1)_ | Easy | |
105-
| Leetcode | [219. Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/description/) | [Java](./java/containsNearbyDuplicate.java) | _O(n)_ | _O(n)_ | Easy | |
106-
| Leetcode | [454. 4Sum II](https://leetcode.com/problems/4sum-ii/description/) | [Java](./java/fourSumCount.java) | _O(n^2)_ | _O(n^2)_ | Medium | |
107-
| Leetcode | [169. Majority Element](https://leetcode.com/problems/majority-element/description/) | [Java](./java/majorityElement.java) | _O(n)_ | _O(n)_ | Easy | |
109+
110+
111+
108112

109113

110114
## Trees
@@ -149,14 +153,9 @@ Feel free to add issues, create pull requests and be a contributer.
149153
| Website | Title | Solution | Time | Space | Difficulty | Note|
150154
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
151155
| Leetcode | [50. Pow(x, n)](https://leetcode.com/problems/powx-n/description/) | [Java](./java/myPow.java) | _O()_ | _O(n)_ | Medium | |
152-
| Leetcode | [654. Maximum Binary Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/) | [Java](./java/sortedListToBST.java) | _O(n)_ | _O(logn)_ | Medium | |
153-
| Leetcode | [654. Maximum Binary Tree](https://leetcode.com/contest/leetcode-weekly-contest-44/problems/maximum-binary-tree/) | [Java](./java/ConstructMaximumBinaryTree.java) | _O(n log n)_ | _O(n)_ | Easy | |
154-
155-
156-
## Recursion
157-
| Website | Title | Solution | Time | Space | Difficulty | Note|
158-
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
156+
| Leetcode | [109. Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/) | [Java](./java/sortedListToBST.java) | _O(n)_ | _O(logn)_ | Medium | |
159157
| Leetcode | [339. Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/description/) | [Java](./java/depthSum.java) | _O( )_ | _O( )_ | Easy | |
158+
| Leetcode | [654. Maximum Binary Tree](https://leetcode.com/contest/leetcode-weekly-contest-44/problems/maximum-binary-tree/) | [Java](./java/ConstructMaximumBinaryTree.java) | _O(n log n)_ | _O(n)_ | Easy | |
160159

161160

162161
## Heaps

java/topKFrequent.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public List<Integer> topKFrequent(int[] nums, int k) {
3+
4+
List<Integer>[] bucket = new List[nums.length+1];
5+
HashMap<Integer, Integer> map = new HashMap<>();
6+
7+
for(int num:nums)
8+
map.put(num, map.getOrDefault(num,0)+1);
9+
10+
for(int key:map.keySet())
11+
{
12+
int freq = map.get(key);
13+
if(bucket[freq] == null)
14+
bucket[freq] = new LinkedList<>();
15+
bucket[freq].add(key);
16+
}
17+
18+
List<Integer> result = new LinkedList<>();
19+
for(int pos = bucket.length-1; pos >= 0 && result.size() < k; pos--)
20+
{
21+
if(bucket[pos] != null)
22+
result.addAll(bucket[pos]);
23+
}
24+
return result;
25+
}
26+
}

0 commit comments

Comments
 (0)