|
2 | 2 |
|
3 | 3 | import org.zjy.learn.util.Application;
|
4 | 4 |
|
5 |
| -@Application(time = "06/17/2019 15:08") |
| 5 | +import java.util.*; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +@Application(time = "05/13/2020 18:08") |
6 | 9 | public class FindSongs implements Runnable {
|
7 | 10 | /**
|
8 | 11 | * Two sum. 要求找到sum是target-30,并且其中一首歌曲是符合要求的pairs里单首最长的pair
|
9 | 12 | * 一个坑,随时update结果pair,保存在一个variable里。因为如果两首歌一样长并且这两首诗最终答案,如果只keep合格的最长的歌曲,最后再retrieve index,会导致这种test case出错。
|
10 | 13 | */
|
11 | 14 | @Override
|
12 | 15 | public void run() {
|
13 |
| - |
| 16 | +// List<String> result = popularNFeatures( |
| 17 | +// 5, |
| 18 | +// 2, |
| 19 | +// new ArrayList<>(Arrays.asList("anacell", "betacellular", "cetracular", "deltacellular", "eurocell")), |
| 20 | +// 3, |
| 21 | +// new ArrayList<>(Arrays.asList("Best services provided by anacell", "betacellular has great services", "anacell provides much better services than all other"))); |
| 22 | +// |
| 23 | +// System.out.println(result); |
14 | 24 | }
|
15 | 25 |
|
16 | 26 | private int[] findSongs(int[] songs) {
|
17 | 27 | return null;
|
18 | 28 | }
|
| 29 | + |
| 30 | + private class Node { |
| 31 | + String word; |
| 32 | + int sum; |
| 33 | + |
| 34 | + Node(String s, int size) { |
| 35 | + word = s; |
| 36 | + sum = size; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // METHOD SIGNATURE BEGINS, THIS METHOD IS REQUIRED |
| 41 | + public ArrayList<String> popularNFeatures(int numFeatures, |
| 42 | + int topFeatures, |
| 43 | + List<String> possibleFeatures, |
| 44 | + int numFeatureRequests, |
| 45 | + List<String> featureRequests) |
| 46 | + { |
| 47 | + // WRITE YOUR CODE HERE |
| 48 | + Set<String> possibleSet = new HashSet<>(possibleFeatures); |
| 49 | + Map<String, Node> map = new HashMap<>(); |
| 50 | + for (String request : featureRequests) { |
| 51 | + String[] split = request.split("\\s*[^a-zA-Z]+\\s*"); |
| 52 | + if (split.length == 0) |
| 53 | + continue; |
| 54 | + Set<String> set = new HashSet<>(Arrays.asList(split)); |
| 55 | + for (String word : set) { |
| 56 | + if (possibleSet.contains(word)) { |
| 57 | + Node node = map.getOrDefault(word, new Node(word, 0)); |
| 58 | + ++node.sum; |
| 59 | + map.put(word, node); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + PriorityQueue<Node> pq = new PriorityQueue<>(new Comparator<Node>() { |
| 64 | + public int compare(Node n1, Node n2) { |
| 65 | + if (n1.sum == n2.sum) |
| 66 | + return n2.word.compareTo(n1.word); |
| 67 | + return n1.sum - n2.sum; |
| 68 | + } |
| 69 | + }); |
| 70 | + for (String s : map.keySet()) { |
| 71 | + pq.offer(map.get(s)); |
| 72 | + if (pq.size() > topFeatures) |
| 73 | + pq.poll(); |
| 74 | + } |
| 75 | + ArrayList<String> result = new ArrayList<>(); |
| 76 | + while (!pq.isEmpty()) { |
| 77 | + Node node = pq.poll(); |
| 78 | + result.add(node.word); |
| 79 | + } |
| 80 | + Collections.reverse(result); |
| 81 | + return result.stream() |
| 82 | + .map(String::toLowerCase).collect(Collectors.toCollection(ArrayList::new)); |
| 83 | + } |
| 84 | + |
| 85 | + // METHOD SIGNATURE BEGINS, THIS METHOD IS REQUIRED |
| 86 | + public List<String> reorderLines(int logFileSize, List<String> logLines) |
| 87 | + { |
| 88 | + // WRITE YOUR CODE HERE |
| 89 | + Collections.sort(logLines, (a, b) -> { |
| 90 | + String[] split1 = a.split(" ", 2); |
| 91 | + String[] split2 = b.split(" ", 2); |
| 92 | + boolean isDigit1 = Character.isDigit(split1[1].charAt(0)); |
| 93 | + boolean isDigit2 = Character.isDigit(split2[1].charAt(0)); |
| 94 | + if(!isDigit1 && !isDigit2) { |
| 95 | + int comp = split1[1].compareTo(split2[1]); |
| 96 | + if(comp != 0) |
| 97 | + return comp; |
| 98 | + else |
| 99 | + return split1[0].compareTo(split2[0]); |
| 100 | + } |
| 101 | + return isDigit1 ? (isDigit2 ? 0 : 1) : -1; |
| 102 | + }); |
| 103 | + return logLines; |
| 104 | + } |
19 | 105 | }
|
0 commit comments