Skip to content

Commit fece199

Browse files
authored
Update AmazonInterviewQuestionsByFrequency.md
1 parent 273798e commit fece199

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

AmazonInterviewQuestionsByFrequency.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,3 +2822,137 @@ public class Solution {
28222822
}
28232823
}
28242824
```
2825+
2826+
## [167. Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/)
2827+
2828+
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
2829+
2830+
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
2831+
2832+
You may assume that each input would have exactly one solution and you may not use the same element twice.
2833+
```
2834+
Input: numbers={2, 7, 11, 15}, target=9
2835+
Output: index1=1, index2=2
2836+
```
2837+
2838+
```java
2839+
class Solution {
2840+
public int[] twoSum(int[] nums, int target) {
2841+
int[] result = new int[]{0,0};
2842+
for (int i = 0, j = nums.length - 1; i < j; ) {
2843+
if (nums[i] + nums[j] == target) return new int[]{i+1, j+1};
2844+
else if (nums[i] + nums[j] > target) j--;
2845+
else i++;
2846+
}
2847+
return result;
2848+
}
2849+
}
2850+
```
2851+
2852+
## [692. Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/description/)
2853+
2854+
Given a non-empty list of words, return the k most frequent elements.
2855+
2856+
Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.
2857+
2858+
Example 1:
2859+
```
2860+
Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
2861+
Output: ["i", "love"]
2862+
Explanation: "i" and "love" are the two most frequent words.
2863+
Note that "i" comes before "love" due to a lower alphabetical order.
2864+
```
2865+
2866+
Example 2:
2867+
```
2868+
Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
2869+
Output: ["the", "is", "sunny", "day"]
2870+
Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
2871+
with the number of occurrence being 4, 3, 2 and 1 respectively.
2872+
```
2873+
2874+
Note:
2875+
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
2876+
Input words contain only lowercase letters.
2877+
2878+
Follow up:
2879+
Try to solve it in O(n log k) time and O(n) extra space.
2880+
2881+
```java
2882+
class Solution {
2883+
ArrayList<TreeSet<String>> freqToWords = new ArrayList<>();
2884+
HashMap<String, Integer> wordToFreq = new HashMap<>();
2885+
int maxFreq = 0;
2886+
2887+
public List<String> topKFrequent(String[] words, int k) {
2888+
//Build Frequency Lookup Table
2889+
for (String word: words) {
2890+
if (!wordToFreq.containsKey(word))
2891+
wordToFreq.put(word, 0);
2892+
wordToFreq.put(word, 1+wordToFreq.get(word));
2893+
maxFreq = Math.max(maxFreq, wordToFreq.get(word));
2894+
}
2895+
2896+
//Build Frequency to Words Table.
2897+
for (int i = 0; i <= maxFreq; i++) { //freqToWords[0] will not be used
2898+
freqToWords.add(new TreeSet<String>());
2899+
}
2900+
for (String word: words) {
2901+
freqToWords.get(wordToFreq.get(word)).add(word);
2902+
}
2903+
2904+
//Build Result;
2905+
List<String> result = new ArrayList<>();
2906+
int count = 0;
2907+
for (int freq = maxFreq; freq > 0; freq--) {
2908+
TreeSet<String> candidates = freqToWords.get(freq);
2909+
2910+
for (Iterator<String> it = candidates.iterator(); it.hasNext();) {
2911+
result.add(it.next());
2912+
count++;
2913+
if (count == k) return result;
2914+
}
2915+
}
2916+
return result;
2917+
}
2918+
}
2919+
```
2920+
## [78. Subsets](https://leetcode.com/problems/subsets/description/)
2921+
2922+
Given a set of distinct integers, nums, return all possible subsets (the power set).
2923+
2924+
Note: The solution set must not contain duplicate subsets.
2925+
2926+
For example,
2927+
If nums = [1,2,3], a solution is:
2928+
```
2929+
[
2930+
[3],
2931+
[1],
2932+
[2],
2933+
[1,2,3],
2934+
[1,3],
2935+
[2,3],
2936+
[1,2],
2937+
[]
2938+
]
2939+
```
2940+
2941+
```java
2942+
class Solution {
2943+
public List<List<Integer>> subsets(int[] nums) {
2944+
List<List<Integer>> results = new LinkedList<>();
2945+
backtrack(nums, 0, new ArrayList<>(), results);
2946+
return results;
2947+
}
2948+
2949+
private void backtrack(int[] nums, int start, List<Integer> prefix, List<List<Integer>> results) {
2950+
results.add(new ArrayList<>(prefix));
2951+
for (int i = start; i < nums.length; i++) {
2952+
prefix.add(nums[i]);
2953+
backtrack(nums, i+1, prefix,results);
2954+
prefix.remove(prefix.size()-1);
2955+
}
2956+
}
2957+
}
2958+
```

0 commit comments

Comments
 (0)