Skip to content

Commit 80be51e

Browse files
author
Junyan
committed
2020 change
1 parent 4d85261 commit 80be51e

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

src/main/java/org/zjy/learn/code/amazon/FindSongs.java

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,104 @@
22

33
import org.zjy.learn.util.Application;
44

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")
69
public class FindSongs implements Runnable {
710
/**
811
* Two sum. 要求找到sum是target-30,并且其中一首歌曲是符合要求的pairs里单首最长的pair
912
* 一个坑,随时update结果pair,保存在一个variable里。因为如果两首歌一样长并且这两首诗最终答案,如果只keep合格的最长的歌曲,最后再retrieve index,会导致这种test case出错。
1013
*/
1114
@Override
1215
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);
1424
}
1525

1626
private int[] findSongs(int[] songs) {
1727
return null;
1828
}
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+
}
19105
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.zjy.learn.code.google;
2+
3+
import org.zjy.learn.util.Application;
4+
5+
@Application(time = "06/04/2020 13:15")
6+
public class MatrixWiredIterator implements Runnable {
7+
@Override
8+
public void run() {
9+
int[][] matrix = new int[][]{
10+
{1,2,3,4},
11+
{5,1,2,3},
12+
{6,5,1,2},
13+
{7,6,5,1},
14+
};
15+
matrixIterator(matrix);
16+
}
17+
// 矩阵斜着遍历
18+
private void matrixIterator(int[][] matrix) {
19+
int n = matrix[0].length;
20+
int m = matrix.length;
21+
for (int d = 1; d < n; d++) {
22+
for (int l = 0; l < m - d; l++) {
23+
int r = l + d;
24+
System.out.println(matrix[l][r]);
25+
}
26+
}
27+
// for (int d = 1; d < n; d++) {
28+
// for (int r = m - d; r > 0; r--) {
29+
// int l = r - d + 1;
30+
// System.out.println(matrix[l][r]);
31+
// }
32+
// }
33+
}
34+
}

0 commit comments

Comments
 (0)