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

Commitfbcbf2f

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon.
1 parent03a2113 commitfbcbf2f

4 files changed

+176
-1
lines changed

‎leetcode/212. Word Search II.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,54 @@ class Solution {
198198
}
199199
}
200200
```
201+
202+
###Amazon session
203+
* Method 1: dfs
204+
```Java
205+
class Solution {
206+
private char[][] board;
207+
private int height, width;
208+
private static final int[][] dir = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
209+
public List<String> findWords(char[][] board, String[] words) {
210+
List<String> result = new ArrayList<>();
211+
if(board == null || board.length == 0 || board[0].length == 0) return result;
212+
this.board = board;
213+
this.height = board.length;
214+
this.width = board[0].length;
215+
LABEL:
216+
for(String word: words){
217+
for(int i = 0; i < height; i++){
218+
for(int j = 0; j < width; j++){
219+
if(word.charAt(0) == board[i][j]){
220+
Set<Integer> visited = new HashSet<>();
221+
visited.add(i * width + j);
222+
if(dfs(i, j, word, 1, visited)){
223+
result.add(word);
224+
continue LABEL;
225+
}
226+
}
227+
}
228+
}
229+
}
230+
return result;
231+
}
232+
private boolean dfs(int x, int y, String word, int index, Set<Integer> visited){
233+
if(index == word.length()) return true;
234+
else if(index < word.length()){
235+
int tx = 0, ty = 0;
236+
for(int d = 0; d < 4; d++){
237+
tx = x + dir[d][0];
238+
ty = y + dir[d][1];
239+
if(tx >= 0 && tx < height && ty >= 0 && ty < width
240+
&& !visited.contains(tx * width + ty)
241+
&& board[tx][ty] == word.charAt(index)){
242+
visited.add(tx * width + ty);
243+
if(dfs(tx, ty, word, index + 1, visited)) return true;
244+
visited.remove(tx * width + ty);
245+
}
246+
}
247+
}
248+
return false;
249+
}
250+
}
251+
```

‎leetcode/269. Alien Dictionary.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,51 @@ Note:
9494
return result.toString();
9595
}
9696
}
97-
```
97+
```
98+
99+
###Amazon session
100+
* Method 1: Topological sort
101+
```Java
102+
class Solution {
103+
public String alienOrder(String[] words) {
104+
Map<Character, Integer> indegree = new HashMap<>(); // key: letter, value: its indegree
105+
Map<Character, Set<Character>> map = new HashMap<>(); // key: request, value: its childrens
106+
for(String word: words){
107+
for(char c : word.toCharArray()){
108+
indegree.put(c, 0);
109+
}
110+
}
111+
for(int i = 1; i < words.length; i++){
112+
int minLen = Math.min(words[i].length(), words[i - 1].length());
113+
char[] arr1 = words[i - 1].toCharArray(), arr2 = words[i].toCharArray();
114+
for(int j = 0; j < minLen; j++){
115+
if(arr1[j] != arr2[j]){ //arr1[j] is ahead of arr2[j]
116+
Set<Character> set = map.getOrDefault(arr1[j], new HashSet<>());
117+
if(!set.contains(arr2[j])) indegree.put(arr2[j], indegree.get(arr2[j]) + 1);
118+
set.add(arr2[j]);
119+
map.put(arr1[j], set);
120+
break;
121+
}
122+
}
123+
}
124+
Queue<Character> q = new LinkedList<>();
125+
for(Map.Entry<Character, Integer> entry: indegree.entrySet()){
126+
if(entry.getValue() == 0){
127+
q.offer(entry.getKey());
128+
}
129+
}
130+
StringBuilder sb = new StringBuilder();
131+
while(!q.isEmpty()){
132+
char cur = q.poll();
133+
sb.append(cur);
134+
if(!map.containsKey(cur)) continue; // current character doesn't have neighbour.
135+
Set<Character> neighbours = map.get(cur);
136+
for(char c : neighbours){
137+
indegree.put(c, indegree.get(c) - 1);
138+
if(indegree.get(c) == 0) q.offer(c);
139+
}
140+
}
141+
return sb.length() == indegree.size() ? sb.toString(): "";
142+
}
143+
}
144+
```

‎leetcode/472. Concatenated Words.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,42 @@ Note:
6262
return dp[len];
6363
}
6464
}
65+
```
66+
67+
###Amazon session
68+
* Method 1: DP + Set
69+
```Java
70+
class Solution {
71+
private Set<String> set;
72+
public List<String> findAllConcatenatedWordsInADict(String[] words) {
73+
List<String> result = new ArrayList<>();
74+
if(words == null || words.length <= 1) return result;
75+
Arrays.sort(words, (a, b)->{
76+
return a.length() - b.length();
77+
});
78+
this.set = new HashSet<>();
79+
set.add(words[0]);
80+
for(int i = 1; i < words.length; i++){
81+
if(dfs(words[i])) result.add(words[i]);
82+
set.add(words[i]);
83+
}
84+
return result;
85+
}
86+
private boolean dfs(String cur){
87+
if(set.isEmpty()) return false;
88+
int len = cur.length();
89+
boolean[] dp = new boolean[len + 1];
90+
dp[0] = true;
91+
for(int end = 1; end <= len; end++){
92+
for(int start = 0; start < end; start++){
93+
String sub = cur.substring(start, end);
94+
if(set.contains(sub)){
95+
dp[end] |= dp[start];
96+
}
97+
if(dp[end]) break;
98+
}
99+
}
100+
return dp[len];
101+
}
102+
}
65103
```

‎leetcode/895. Maximum Frequency Stack.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,45 @@ Note:
8787
*/
8888
```
8989

90+
###Amazon Session
91+
* Method 1: two maps + deque
92+
* Fix some bugs in first try, since all frequencies are countinuous, we don't have to use while loop to find next maxCount.
93+
```Java
94+
class FreqStack {
95+
private int maxCount;
96+
private Map<Integer, Integer> countMap;
97+
private Map<Integer, Deque<Integer>> freqMap;
98+
public FreqStack() {
99+
this.countMap = new HashMap<>();
100+
this.freqMap = new HashMap<>();
101+
}
102+
103+
public void push(int x) {
104+
countMap.put(x, countMap.getOrDefault(x, 0) + 1);
105+
int freq = countMap.get(x);
106+
maxCount = Math.max(maxCount, freq);
107+
Deque<Integer> deque = freqMap.getOrDefault(freq, new ArrayDeque<>());
108+
deque.push(x);
109+
freqMap.put(freq, deque);
110+
}
111+
112+
public int pop() {
113+
int res = freqMap.get(maxCount).pop();
114+
countMap.put(res, countMap.get(res) - 1);
115+
if(freqMap.get(maxCount).size() == 0){
116+
freqMap.remove(maxCount);
117+
maxCount--;
118+
}
119+
return res;
120+
}
121+
}
90122

123+
/**
124+
* Your FreqStack object will be instantiated and called as such:
125+
* FreqStack obj = new FreqStack();
126+
* obj.push(x);
127+
* int param_2 = obj.pop();
128+
*/
129+
```
91130
###Reference
92131
1.[Java - intuition and thought process](https://leetcode.com/problems/maximum-frequency-stack/discuss/289916/Java-intuition-and-thought-process)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp