-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
data_structures/src/KeyPatterns/Hashes/HashMap/java/LongestSubstringWithoutRepeating.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package KeyPatterns.Hashes.HashMap.java; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class LongestSubstringWithoutRepeating { | ||
public static void main(String[] args) { | ||
String s = "abcabcbb"; | ||
System.out.println(longestSubString(s)); | ||
} | ||
|
||
private static int longestSubString(String s) { | ||
Map<Character, Integer> charMap = new HashMap<>(); | ||
int maxLength = 0, left = 0; | ||
|
||
for (int right = 0; right < s.length(); right++) { | ||
if (charMap.containsKey(s.charAt(right))) { | ||
left = Math.max(left, charMap.getOrDefault(s.charAt(right), 0) + 1); | ||
} | ||
charMap.put(s.charAt(right), right); | ||
maxLength = Math.max(maxLength, right - left + 1); | ||
} | ||
|
||
return maxLength; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
data_structures/src/KeyPatterns/Hashes/HashMap/java/SubarraySumEqualsK.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package KeyPatterns.Hashes.HashMap.java; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class SubarraySumEqualsK { | ||
public static void main(String[] args) { | ||
int[] nums = { 1, 1, 1 }; | ||
int k = 2; | ||
System.out.println(subArraySum(nums, k)); | ||
} | ||
|
||
private static int subArraySum(int[] nums, int k) { | ||
Map<Integer, Integer> prefixMap = new HashMap<>(); | ||
prefixMap.put(0, 1); | ||
int count = 0, prefixSum = 0; | ||
|
||
for (int num : nums) { | ||
prefixSum += num; | ||
count += prefixMap.getOrDefault(prefixSum - k, 0); | ||
prefixMap.put(prefixSum, prefixMap.getOrDefault(prefixSum, 0) + 1); | ||
} | ||
|
||
return count; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
data_structures/src/KeyPatterns/Hashes/HashMap/java/TopKFrequentElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package KeyPatterns.Hashes.HashMap.java; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.PriorityQueue; | ||
|
||
public class TopKFrequentElement { | ||
public static void main(String[] args) { | ||
int[] nums = { 1, 1, 1, 2, 2, 3 }; | ||
int k = 2; | ||
System.out.println(Arrays.toString(topKFrequent(nums, k))); | ||
} | ||
|
||
private static int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> frequencyMap = new HashMap<>(); | ||
for (int num : nums) { | ||
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); | ||
} | ||
|
||
PriorityQueue<Integer> minHeap = new PriorityQueue<>((a, b) -> frequencyMap.get(a) - frequencyMap.get(b)); | ||
|
||
for (int num : frequencyMap.keySet()) { | ||
minHeap.add(num); | ||
if (minHeap.size() > k) { | ||
minHeap.poll(); | ||
} | ||
} | ||
|
||
int[] result = new int[k]; | ||
for (int i = 0; i < k; i++) { | ||
result[i] = minHeap.poll(); | ||
} | ||
|
||
return result; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ructures/src/KeyPatterns/Hashes/HashMap/typescript/longest_substring_without_repeating.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function longestSubStringWithoutRepeating(str: string) { | ||
const charMap = new Map<string, number>(); | ||
let maxLength = 0, | ||
left = 0; | ||
|
||
for (let right = 0; right < str.length; right++) { | ||
if (charMap.has(str.charAt(right))) { | ||
left = Math.max(left, (charMap.get(str.charAt(right)) || 0) + 1); | ||
} | ||
charMap.set(str.charAt(right), right); | ||
maxLength = Math.max(maxLength, right - left + 1); | ||
} | ||
|
||
return maxLength; | ||
} | ||
|
||
const result = longestSubStringWithoutRepeating('abcabcbb'); | ||
console.log('result', result); |
15 changes: 15 additions & 0 deletions
15
data_structures/src/KeyPatterns/Hashes/HashMap/typescript/subarray_sum_equals_k.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function subArraySum(nums: number[], k: number): number { | ||
const prefixMap = new Map<number, number>(); | ||
prefixMap.set(0, 1); | ||
let count = 0, | ||
prefixSum = 0; | ||
|
||
for (const num of nums) { | ||
prefixSum += num; | ||
count += prefixMap.get(prefixSum - k) || 0; | ||
prefixMap.set(prefixSum, (prefixMap.get(prefixSum) || 0) + 1); | ||
} | ||
|
||
return count; | ||
} | ||
console.log(subArraySum([1, 1, 1], 2)); |
28 changes: 28 additions & 0 deletions
28
data_structures/src/KeyPatterns/Hashes/HashMap/typescript/top_k_frequent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function topKFrequent(nums: number[], k: number): number[] { | ||
const freqMap = new Map<number, number>(); | ||
|
||
for (const num of nums) { | ||
freqMap.set(num, (freqMap.get(num) || 0) + 1); | ||
} | ||
|
||
/** | ||
* Min Heap (Priority Queue) to keep track of top k frequent elements | ||
* - [num, frequency] | ||
*/ | ||
const minHeap: [number, number][] = []; | ||
|
||
for (const [num, frequency] of freqMap) { | ||
minHeap.push([num, frequency]); | ||
minHeap.sort((a, b) => a[1] - b[1]); | ||
|
||
if (minHeap.length > k) { | ||
minHeap.shift(); | ||
} | ||
} | ||
|
||
return minHeap.map(([num]) => num); | ||
} | ||
|
||
const nums = [1, 1, 1, 2, 2, 3]; | ||
const k = 2; | ||
console.log(topKFrequent(nums, k)); |