├── README.md ├── baseball.java ├── booksAssociation.java ├── checkHighestFreqWord.java ├── commonManager.java ├── distanceBSTNode.java ├── fruit.java ├── golf.java ├── k1Distinct.java ├── kDistinckChar.java ├── movieNetwork.java ├── partitionLabel.java ├── shortestSeqContinsT.java ├── sortLog.java ├── sortLogII.java ├── topKFreqWord.java └── wareHouse.java /README.md: -------------------------------------------------------------------------------- 1 | # AmazonOA 2 | code for amazon online assessment 3 | -------------------------------------------------------------------------------- /baseball.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int calPoints(String[] ops) { 3 | if (ops == null || ops.length == 0) { 4 | return 0; 5 | } 6 | 7 | Stack st = new Stack(); 8 | for (String op : ops) { 9 | if (op.equals("+")) { 10 | int top = st.pop(); 11 | int newtop = top + st.peek(); 12 | st.push(top); 13 | st.push(newtop); 14 | } else if (op.equals("D")) { 15 | st.push(2 * st.peek()); 16 | } else if (op.equals("C")) { 17 | st.pop(); 18 | } else { 19 | st.push(Integer.valueOf(op)); 20 | } 21 | } 22 | 23 | int ans = 0; 24 | while (!st.isEmpty()) { 25 | ans += st.pop(); 26 | } 27 | return ans; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /booksAssociation.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param ListA: The relation between ListB's books 4 | * @param ListB: The relation between ListA's books 5 | * @return: The answer 6 | */ 7 | public List maximumAssociationSet(String[] listA, String[] listB) { 8 | // Write your code here 9 | Map hashMap = new HashMap(); 10 | Map name = new HashMap(); 11 | 12 | int N = 0; 13 | for (int i = 0; i < listA.length; i++) { 14 | if (!hashMap.containsKey(listA[i])) { 15 | hashMap.put(listA[i], N); 16 | name.put(N, listA[i]); 17 | N++; 18 | } 19 | 20 | if (!hashMap.containsKey(listB[i])) { 21 | hashMap.put(listB[i], N); 22 | name.put(N, listB[i]); 23 | N++; 24 | } 25 | } 26 | 27 | int[] f = new int[N]; 28 | 29 | for (int i = 0; i < N; i++) { 30 | f[i] = i; 31 | } 32 | 33 | for (int i = 0; i < listA.length; i++) { 34 | int fa = find(hashMap.get(listA[i]), f); 35 | int fb = find(hashMap.get(listB[i]), f); 36 | 37 | if (fa != fb) { 38 | f[fa] = fb; 39 | } 40 | } 41 | 42 | int[] count = new int[N]; 43 | int v = 0, idx = 0; 44 | for (int i = 0; i < N; i++) { 45 | f[i] = find(i, f); 46 | count[f[i]]++; 47 | if (count[f[i]] > v) { 48 | v = count[f[i]]; 49 | idx = f[i]; 50 | } 51 | } 52 | 53 | List ans = new ArrayList<>(); 54 | for (int i = 0; i < N; i++) { 55 | if (f[i] == idx) { 56 | ans.add(name.get(i)); 57 | } 58 | } 59 | return ans; 60 | } 61 | 62 | private int find(int x, int[] father) { 63 | if (father[x] != x) { 64 | father[x] = find(father[x], father); 65 | } 66 | return father[x]; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /checkHighestFreqWord.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /* 5 | * To execute Java, please define "static void main" on a class 6 | * named Solution. 7 | * 8 | * If you need more classes, simply define them inline. 9 | */ 10 | 11 | class Solution { 12 | public static List findHighestFreqWord(String text, List exclude) { 13 | 14 | List ans = new ArrayList(); 15 | if (text == null || text.length() == 0) { 16 | return ans; 17 | } 18 | 19 | Set eWords = new HashSet<>(); 20 | for (int i = 0; i < exclude.size(); i++) { 21 | eWords.add(exclude.get(i)); 22 | } 23 | 24 | String[] textWords = text.split("[^a-zA-Z]+"); 25 | 26 | Map freq = new HashMap(); 27 | for (int i = 0; i < textWords.length; i++) { 28 | String cur = textWords[i].toLowerCase(); 29 | if (eWords.contains(cur)) { 30 | continue; 31 | } 32 | 33 | freq.put(cur, freq.getOrDefault(cur, 0) + 1); 34 | } 35 | 36 | int highestFreq = 0; 37 | for (String key : freq.keySet()) { 38 | highestFreq = Math.max(highestFreq, freq.get(key)); 39 | } 40 | 41 | for (String key : freq.keySet()) { 42 | if (freq.get(key) == highestFreq) { 43 | ans.add(key); 44 | } 45 | } 46 | 47 | return ans; 48 | } 49 | 50 | 51 | 52 | public static void main(String[] args) { 53 | String s = "This method has two variants. The first variant converts all of the characters in this String to lower case using the rules of the given Locale. This is equivalent to calling toLowerCase(Locale.getDefault()).The second variant takes locale as an argument to be used while converting into lower case."; 54 | 55 | List e = new ArrayList<>(Arrays.asList("a", "the", "an")); 56 | 57 | List ans = findHighestFreqWord(s, e); 58 | for (String x : ans) { 59 | System.out.println(x); 60 | } 61 | } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /commonManager.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Deque; 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | import java.util.Stack; 6 | class Employee { 7 | int id; 8 | List reports = new ArrayList(); 9 | public void setId(int val){ 10 | id = val; 11 | } 12 | public int getId(){ 13 | return id; 14 | } 15 | public void addReport(Employee emp){ 16 | reports.add(emp); 17 | } 18 | public List getReports(){ 19 | return reports; 20 | } 21 | 22 | 23 | 24 | 25 | public class CommonManager { 26 | 27 | public Employee comManager (Employee ceo,Employee emp1,Employee emp2){ 28 | if(ceo==null || emp1==ceo || emp2 ==ceo) return null; 29 | 30 | Deque e1 = new LinkedList(); 31 | Deque e2 = new LinkedList(); 32 | Employee root = ceo; 33 | boolean hasEmp1 = dfs(root,emp1,e1); 34 | boolean hasEmp2 = dfs(root,emp2,e2); 35 | 36 | if (hasEmp1 && hasEmp2){ 37 | int len1 = e1.size(); 38 | int len2 = e2.size(); 39 | if (len1 > len2){ 40 | moveUp(e1,len1-len2); 41 | }else { 42 | moveUp(e2,len2-len1); 43 | } 44 | 45 | //e1 and e2 are in the same size now 46 | while (e1.size()>0 && e1.peek().getId() != e2.peek().getId()){ 47 | e1.pop(); 48 | e2.pop(); 49 | } 50 | if (e1.size() > 0){ 51 | return e1.peek(); 52 | } 53 | 54 | } 55 | return null; 56 | 57 | } 58 | 59 | public void moveUp(Deque stack,int steps){ 60 | while(steps > 0 && !stack.isEmpty()){ 61 | stack.pop(); 62 | steps--; 63 | } 64 | } 65 | public boolean dfs(Employee root,Employee emp, Deque stack){ 66 | stack.offerFirst(root); 67 | if (root.getId() == emp.getId()){ 68 | return true; 69 | } 70 | for (Employee em :root.getReports()){ 71 | boolean result = dfs(em,emp,stack); 72 | if (result == true){ 73 | return true; 74 | } 75 | 76 | } 77 | stack.pollFirst(); 78 | return false; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /distanceBSTNode.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /* 5 | * To execute Java, please define "static void main" on a class 6 | * named Solution. 7 | * 8 | * If you need more classes, simply define them inline. 9 | */ 10 | 11 | class Solution { 12 | static class TreeNode { 13 | int val; 14 | TreeNode left, right; 15 | public TreeNode(int val) { 16 | this.val = val; 17 | left = right = null; 18 | } 19 | } 20 | 21 | public static TreeNode createBST(int[] input){ 22 | // 23 | TreeNode root = null; 24 | for (int x: input){ 25 | root = insert(root, x); 26 | } 27 | return root; 28 | 29 | } 30 | 31 | private static TreeNode insert(TreeNode root, int x){ 32 | if(root==null) return new TreeNode(x); 33 | if(root.val < i){ 34 | root.right = insert(root.right, i); 35 | }else{ 36 | root.left = insert(root.left, i); 37 | } 38 | 39 | return root; 40 | } 41 | 42 | 43 | public static TreeNode LCABST(TreeNode root, TreeNode p, TreeNode q) { 44 | if(root == null || p == root || q == root) return root; 45 | 46 | 47 | if(root.val > p.val && root.val > q.val) { 48 | return LCABST(root.left, p, q); 49 | } else if(root.val < p.val && root.val < q.val) { 50 | return LCABST(root.right, p, q); 51 | } else { 52 | return root; 53 | } 54 | } 55 | 56 | /** 57 | * return distance between p and q 58 | * p, q may not exit in the tree 59 | * @param lca 60 | * @param p 61 | * @param q 62 | * @return 63 | */ 64 | public static int findDistance(TreeNode root,TreeNode p, TreeNode q){ 65 | if(root==null || p==null || q==null) return -1; 66 | //p or q not exist 67 | int dis1 = getDistanceFromRoot(root, p); 68 | int dis2 = getDistanceFromRoot(root, q); 69 | if (dis1==-1 ||dis2==-1) return -1; 70 | else{ 71 | TreeNode lca = LCABST(root, p, q); 72 | int dis3 = getDistanceFromRoot(root, lca); 73 | return dis1+dis2-2*dis3; 74 | 75 | } 76 | 77 | } 78 | 79 | public static int getDistanceFromRoot(TreeNode root, TreeNode n){ 80 | if (root==null) return -1; 81 | if (root.val == n.val) return 0; 82 | 83 | int dis = -1; 84 | if (root.val>n.val) { 85 | dis = getDistanceFromRoot(root.left, n); 86 | } 87 | else if(root.val> codeList, List shoppingCart) { 8 | // Write your code here 9 | List list = new ArrayList<>(); 10 | for (List l : codeList) { 11 | for (String s : l) { 12 | list.add(s); 13 | } 14 | } 15 | 16 | if (list.size() > shoppingCart.size()) { 17 | return 0; 18 | } 19 | 20 | for (int i = 0; i + list.size() <= shoppingCart.size(); i++) { 21 | for (int j = 0; j < list.size(); j++) { 22 | if (list.get(j).equals("anything")) { 23 | if (j == list.size() - 1) { 24 | return 1; 25 | } 26 | continue; 27 | } 28 | 29 | if (!list.get(j).equals(shoppingCart.get(i + j))) { 30 | break; 31 | } 32 | 33 | if (j == list.size() - 1) { 34 | return 1; 35 | } 36 | } 37 | } 38 | 39 | return 0; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /golf.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int cutOffTree(List> forest) { 3 | List trees = new ArrayList<>(); 4 | for (int i = 0; i < forest.size(); i++) { 5 | for (int j = 0; j < forest.get(0).size(); j++) { 6 | int h = forest.get(i).get(j); 7 | if (h > 1) { 8 | trees.add(new int[]{h, i, j}); 9 | } 10 | } 11 | } 12 | 13 | Collections.sort(trees, (a, b) -> a[0] - b[0]); 14 | 15 | int sr = 0, sc = 0; 16 | int ans = 0; 17 | for (int[] tree : trees) { 18 | int d = bfs(forest, sr, sc, tree[1], tree[2]); 19 | if (d < 0) { 20 | return -1; 21 | } 22 | ans += d; 23 | sr = tree[1]; 24 | sc = tree[2]; 25 | } 26 | return ans; 27 | } 28 | 29 | private int bfs(List> forest, int sr, int sc, int tr, int tc) { 30 | int R = forest.size(), C = forest.get(0).size(); 31 | Queue q = new LinkedList<>(); 32 | boolean[][] visited = new boolean[R][C]; 33 | 34 | q.offer(new int[]{sr, sc, 0}); 35 | visited[sr][sc] = true; 36 | int[] dx = {0, -1, 0, 1}; 37 | int[] dy = {1, 0, -1, 0}; 38 | while (!q.isEmpty()) { 39 | int[] cur = q.poll(); 40 | if (cur[0] == tr && cur[1] == tc) { 41 | return cur[2]; 42 | } 43 | 44 | for (int i = 0; i < 4; i++) { 45 | int newr = cur[0] + dx[i]; 46 | int newc = cur[1] + dy[i]; 47 | 48 | if (newr >= 0 && newr < R && newc >= 0 && newc < C && !visited[newr][newc] && forest.get(newr).get(newc) > 0) { 49 | visited[newr][newc] = true; 50 | q.offer(new int[]{newr, newc, cur[2] + 1}); 51 | } 52 | } 53 | } 54 | 55 | return -1; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /k1Distinct.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /* 5 | * To execute Java, please define "static void main" on a class 6 | * named Solution. 7 | * 8 | * If you need more classes, simply define them inline. 9 | */ 10 | 11 | class Solution { 12 | public static List find(String s, int k) { 13 | 14 | if (s == null || s.length() == 0 || k == 0) { 15 | return new ArrayList(); 16 | } 17 | 18 | 19 | int[] hash = new int[26]; 20 | for (int i = 0; i < k - 1; i++) { 21 | hash[s.charAt(i) - 'a']++; 22 | } 23 | 24 | Set set = new LinkedHashSet<>(); 25 | for (int i = k - 1; i < s.length(); i++) { 26 | char c = s.charAt(i); 27 | hash[c - 'a']++; 28 | 29 | if (valid(hash)) { 30 | set.add(s.substring(i - k + 1, i + 1)); 31 | } 32 | 33 | hash[s.charAt(i - k + 1) - 'a']--; 34 | } 35 | 36 | return new ArrayList(set); 37 | } 38 | 39 | private static boolean valid(int[] hash) { 40 | 41 | boolean flag = false; 42 | for (int i = 0; i < hash.length; i++) { 43 | if (hash[i] > 1) { 44 | if (flag) { 45 | return false; 46 | } 47 | flag = true; 48 | } 49 | 50 | } 51 | if (flag) 52 | return true; 53 | 54 | return false; 55 | } 56 | 57 | 58 | public static void main(String[] args) { 59 | String s = "aabcddef"; 60 | List ans = find(s, 4); 61 | 62 | for (String x : ans) { 63 | System.out.println(x); 64 | } 65 | } 66 | } 67 | 68 | -------------------------------------------------------------------------------- /kDistinckChar.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /* 5 | * To execute Java, please define "static void main" on a class 6 | * named Solution. 7 | * 8 | * If you need more classes, simply define them inline. 9 | */ 10 | 11 | class Solution { 12 | public static List find(String s, int k) { 13 | 14 | if (s == null || s.length() == 0 || k == 0) { 15 | return new ArrayList(); 16 | } 17 | 18 | 19 | int[] hash = new int[26]; 20 | for (int i = 0; i < k - 1; i++) { 21 | hash[s.charAt(i) - 'a']++; 22 | } 23 | 24 | Set set = new LinkedHashSet<>(); 25 | for (int i = k - 1; i < s.length(); i++) { 26 | char c = s.charAt(i); 27 | hash[c - 'a']++; 28 | 29 | if (valid(hash)) { 30 | set.add(s.substring(i - k + 1, i + 1)); 31 | } 32 | 33 | hash[s.charAt(i - k + 1) - 'a']--; 34 | } 35 | 36 | return new ArrayList(set); 37 | } 38 | 39 | private static boolean valid(int[] hash) { 40 | 41 | for (int i = 0; i < hash.length; i++) { 42 | if (hash[i] > 1) { 43 | return false; 44 | } 45 | 46 | } 47 | return true; 48 | } 49 | 50 | 51 | public static void main(String[] args) { 52 | String s = "awaglknagawunagwkwagl"; 53 | List ans = find(s, 4); 54 | 55 | for (String x : ans) { 56 | System.out.println(x); 57 | } 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /movieNetwork.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param rating: the rating of the movies 4 | * @param G: the realtionship of movies 5 | * @param S: the begin movie 6 | * @param K: top K rating 7 | * @return: the top k largest rating moive which contact with S 8 | */ 9 | public class Pair { 10 | public int rating; 11 | public int index; 12 | public Pair(int rating, int index) { 13 | this.rating = rating; 14 | this.index = index; 15 | } 16 | 17 | } 18 | 19 | public static Comparator idComparator = new Comparator(){ 20 | @Override 21 | public int compare(Pair c1, Pair c2) { 22 | return c2.rating - c1.rating; 23 | } 24 | }; 25 | public void dfs(int[] rating, int[][] G,int x, int S, Queue pq, boolean[] visit) { 26 | if(visit[x] == true) { 27 | return; 28 | } 29 | visit[x] = true; 30 | if(x != S) { 31 | pq.add(new Pair(rating[x], x)); 32 | } 33 | for(int i = 0; i < G[x].length; i++) { 34 | dfs(rating, G, G[x][i], S, pq, visit); 35 | } 36 | } 37 | 38 | 39 | public int[] topKMovie(int[] rating, int[][] G, int S, int K) { 40 | // Write your code here 41 | Queue pq = new PriorityQueue(K,idComparator); 42 | boolean[] visit = new boolean[rating.length]; 43 | dfs(rating, G, S, S, pq, visit); 44 | int[] ans = new int[K]; 45 | for(int i = 0; i < K; i++) { 46 | if(!pq.isEmpty()) { 47 | Pair top = pq.poll(); 48 | ans[i] = top.index; 49 | } 50 | } 51 | return ans; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /partitionLabel.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List partitionLabels(String S) { 3 | List ans = new ArrayList<>(); 4 | if (S == null || S.length() == 0) { 5 | return ans; 6 | } 7 | 8 | int[] last = new int[26]; 9 | for (int i = 0; i < S.length(); i++) { 10 | last[S.charAt(i) - 'a'] = i; 11 | } 12 | 13 | int j = 0, anchor = 0; 14 | for (int i = 0; i < S.length(); i++) { 15 | char c = S.charAt(i); 16 | j = Math.max(j, last[c - 'a']); 17 | if (i == j) { 18 | ans.add(j - anchor + 1); 19 | anchor = i + 1; 20 | } 21 | } 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /shortestSeqContinsT.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /* 5 | * To execute Java, please define "static void main" on a class 6 | * named Solution. 7 | * 8 | * If you need more classes, simply define them inline. 9 | */ 10 | 11 | class Solution { 12 | private static int maxLen = Integer.MAX_VALUE; 13 | public static int[] shortestSeqContinsT(List s, List t) { 14 | if (s == null || s.size() == 0 || t == null || t.size() == 0) { 15 | return new int[]{0}; 16 | } 17 | 18 | 19 | Map tHash = new HashMap(); 20 | initializeT(t, tHash); 21 | int[] ans = new int[2]; 22 | Arrays.fill(ans, -1); 23 | 24 | Map sHash = new HashMap(); 25 | int j = 0; 26 | for (int i = 0; i < s.size(); i++) { 27 | while (j < s.size() && !valid(tHash, sHash)) { 28 | sHash.put(s.get(j), sHash.getOrDefault(s.get(j), 0) + 1); 29 | j++; 30 | } 31 | 32 | if (valid(tHash, sHash)) { 33 | if (maxLen > j - i) { 34 | maxLen = j - i; 35 | ans[0] = i; 36 | ans[1] = j - 1; 37 | } 38 | } 39 | 40 | int val = sHash.get(s.get(i)); 41 | sHash.put(s.get(i), val - 1); 42 | } 43 | 44 | if (ans[0] == - 1 && ans[1] == -1) { 45 | return new int[]{0}; 46 | } 47 | 48 | return ans; 49 | } 50 | 51 | private static void initializeT(List t, Map tHash) { 52 | for (String s : t) { 53 | tHash.put(s, tHash.getOrDefault(s, 0) + 1); 54 | } 55 | } 56 | 57 | private static boolean valid(Map tHash, Map sHash) { 58 | for (String key : tHash.keySet()) { 59 | if (!sHash.containsKey(key)) { 60 | return false; 61 | } 62 | 63 | int vt = tHash.get(key); 64 | int vs = sHash.get(key); 65 | if (vt > vs) { 66 | return false; 67 | } 68 | } 69 | return true; 70 | } 71 | 72 | public static void main(String[] args) { 73 | List t = new ArrayList<>(Arrays.asList("made","in","china")); 74 | List s = new ArrayList<>(Arrays.asList("made", "a","b","c","in", "china","made","b","c","d")); 75 | 76 | int[] ans = shortestSeqContinsT(s, t); 77 | System.out.println(ans[0] + ", " + ans[1]); 78 | } 79 | } 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /sortLog.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /* 5 | * To execute Java, please define "static void main" on a class 6 | * named Solution. 7 | * 8 | * If you need more classes, simply define them inline. 9 | */ 10 | 11 | class Solution { 12 | static class WordComparator implements Comparator { 13 | public int compare(String s1, String s2) { 14 | String[] a = s1.split("\\s+"); 15 | String[] b = s2.split("\\s+"); 16 | 17 | int i = 1, j = 1; 18 | while (i < a.length && j < b.length) { 19 | if (a[i].compareTo(b[j]) < 0) { 20 | return -1; 21 | } else if (a[i].compareTo(b[j]) > 0) { 22 | return 1; 23 | } 24 | i++; 25 | j++; 26 | } 27 | 28 | //tie, compare identifier a[0] and b[0] 29 | if (i == a.length && j == b.length) { 30 | int k, l; 31 | for (k = 0, l = 0; k < a[0].length() && l < b[0].length(); k++, l++) { 32 | char c1 = a[0].charAt(k); 33 | char c2 = b[0].charAt(l); 34 | 35 | if (c1 < c2) { 36 | return -1; 37 | } else if (c1 > c2) { 38 | return 1; 39 | } 40 | } 41 | if (k == a[0].length() && l == b[0].length()) { 42 | return 0; 43 | } 44 | 45 | if (k == a[0].length()) { 46 | return -1; 47 | } 48 | if (l == b[0].length()) { 49 | return 1; 50 | } 51 | } 52 | 53 | //a is shorter than b 54 | if (i == a.length) { 55 | return -1; 56 | } 57 | 58 | //a is longer than b 59 | if (j == b.length) { 60 | return 1; 61 | } 62 | 63 | return 0; 64 | } 65 | } 66 | 67 | public static List sortLog(List logs, int logSize) { 68 | List ans = new ArrayList<>(logSize); 69 | if (logs == null || logs.size() == 0) { 70 | return ans; 71 | } 72 | 73 | List words = new ArrayList<>(); 74 | List nums = new ArrayList<>(); 75 | 76 | for (String log : logs) { 77 | int index = log.indexOf(" "); //index of first white space 78 | if (Character.isDigit(log.charAt(index + 1))) { 79 | nums.add(log); 80 | } else { 81 | words.add(log); 82 | } 83 | } 84 | 85 | Collections.sort(words, new WordComparator()); 86 | 87 | ans.addAll(words); 88 | ans.addAll(nums); 89 | return ans; 90 | } 91 | 92 | public static void main(String[] args) { 93 | List logs = new ArrayList<>(); 94 | logs.add("a1 9 2 3 1"); 95 | logs.add("g1 Act car zoo"); 96 | logs.add("zo4 4 7"); 97 | logs.add("ab1 off KEY dog"); 98 | logs.add("a8 act car zoo"); 99 | 100 | List sorted = sortLog(logs, 5); 101 | for (String s : sorted) { 102 | System.out.println(s); 103 | } 104 | } 105 | } 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /sortLogII.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /* 5 | * To execute Java, please define "static void main" on a class 6 | * named Solution. 7 | * 8 | * If you need more classes, simply define them inline. 9 | */ 10 | 11 | class Solution { 12 | private static int compareWord(String s1, String s2) { 13 | int i = 0, j = 0; 14 | while (i < s1.length() && j < s2.length()) { 15 | char c1 = s1.charAt(i); 16 | char c2 = s2.charAt(j); 17 | 18 | boolean isDigC1 = Character.isDigit(c1); 19 | boolean isDigC2 = Character.isDigit(c2); 20 | 21 | if ((isDigC1 && isDigC2) ||(!isDigC1 && !isDigC2)) { 22 | if (c1 < c2) return -1; 23 | else if (c1 > c2) return 1; 24 | } else if (isDigC1) { 25 | return 1; 26 | } else if (isDigC2) { 27 | return -1; 28 | } 29 | 30 | i++; 31 | j++; 32 | } 33 | return 0; 34 | } 35 | 36 | static class WordComparator implements Comparator { 37 | public int compare(String s1, String s2) { 38 | String[] a = s1.split("\\s+"); 39 | String[] b = s2.split("\\s+"); 40 | 41 | int i = 1, j = 1; 42 | while (i < a.length && j < b.length) { 43 | if (compareWord(a[i], b[j]) < 0) { 44 | return -1; 45 | } else if (compareWord(a[i], b[j]) > 0) { 46 | return 1; 47 | } 48 | i++; 49 | j++; 50 | } 51 | 52 | //tie, compare identifier a[0] and b[0] 53 | if (i == a.length && j == b.length) { 54 | if (compareWord(a[0], b[0]) < 0) { 55 | return -1; 56 | } else if (compareWord(a[0], b[0]) > 0) { 57 | return 1; 58 | } 59 | return 0; 60 | } 61 | 62 | //a is shorter than b 63 | if (i == a.length) { 64 | return -1; 65 | } 66 | 67 | //a is longer than b 68 | if (j == b.length) { 69 | return 1; 70 | } 71 | 72 | return 0; 73 | } 74 | } 75 | 76 | public static List sortLog(List logs, int logSize) { 77 | List ans = new ArrayList<>(logSize); 78 | if (logs == null || logs.size() == 0) { 79 | return ans; 80 | } 81 | 82 | List words = new ArrayList<>(); 83 | List nums = new ArrayList<>(); 84 | 85 | for (String log : logs) { 86 | int index = log.indexOf(" "); //index of first white space 87 | if (Character.isDigit(log.charAt(index + 1))) { 88 | nums.add(log); 89 | } else { 90 | words.add(log); 91 | } 92 | } 93 | 94 | Collections.sort(words, new WordComparator()); 95 | 96 | ans.addAll(words); 97 | ans.addAll(nums); 98 | return ans; 99 | } 100 | 101 | public static void main(String[] args) { 102 | List logs = new ArrayList<>(); 103 | //logs.add("a1 9 2 3 1"); 104 | logs.add("1g act car zoo"); 105 | //logs.add("zo4 4 7"); 106 | //logs.add("ab1 off KEY dog"); 107 | logs.add("a8 1ct car zoo"); 108 | 109 | List sorted = sortLog(logs, 5); 110 | for (String s : sorted) { 111 | System.out.println(s); 112 | } 113 | } 114 | } 115 | 116 | -------------------------------------------------------------------------------- /topKFreqWord.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public List topKFrequent(String[] words, int k) { 4 | List ans = new ArrayList<>(); 5 | if (words == null || words.length == 0 || k == 0) { 6 | return ans; 7 | } 8 | 9 | Map map = new HashMap(); 10 | for (String word : words) { 11 | map.put(word, map.getOrDefault(word, 0) + 1); 12 | } 13 | 14 | PriorityQueue pq = new PriorityQueue((w1, w2)-> 15 | map.get(w1) != map.get(w2) ? map.get(w1) - map.get(w2) 16 | : w2.compareTo(w1)); 17 | for (String word : map.keySet()) { 18 | pq.offer(word); 19 | if (pq.size() > k) { 20 | pq.poll(); 21 | } 22 | } 23 | 24 | while (!pq.isEmpty()) { 25 | ans.add(pq.poll()); 26 | } 27 | Collections.reverse(ans); 28 | return ans; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /wareHouse.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /* 5 | * To execute Java, please define "static void main" on a class 6 | * named Solution. 7 | * 8 | * If you need more classes, simply define them inline. 9 | */ 10 | 11 | class Solution { 12 | private static int distance(List A) { 13 | return A.get(0) * A.get(0) + A.get(1) * A.get(1); 14 | } 15 | 16 | 17 | public static List> wareHouse(List> pos, int N, int M) { 18 | List> res = new ArrayList<>(); 19 | 20 | //max heap 21 | PriorityQueue> pq = new PriorityQueue>(M, (a, b) -> distance(b) - distance(a)); 22 | 23 | for (List p : pos) { 24 | pq.offer(p); 25 | if (pq.size() > M) { 26 | pq.poll(); 27 | } 28 | } 29 | 30 | while (!pq.isEmpty()) { 31 | res.add(pq.poll()); 32 | } 33 | 34 | Collections.reverse(res); 35 | return res; 36 | } 37 | 38 | 39 | public static void main(String[] args) { 40 | List> pos = new ArrayList<>(); 41 | 42 | pos.add(new ArrayList(Arrays.asList(1, 9))); 43 | pos.add(new ArrayList(Arrays.asList(1, 0))); 44 | 45 | pos.add(new ArrayList(Arrays.asList(2, 3))); 46 | pos.add(new ArrayList(Arrays.asList(4, 5))); 47 | 48 | List> ans = wareHouse(pos, 4, 3); 49 | 50 | for (List x : ans) { 51 | System.out.println(x.get(0) + ", " + x.get(1)); 52 | } 53 | } 54 | } 55 | 56 | --------------------------------------------------------------------------------