Skip to content

Commit f6fce3b

Browse files
committed
Weekly Contest 170 V1.0
1 parent 8d39184 commit f6fce3b

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @author changleamazing
3+
* @date 2020/1/5 10:32
4+
* source: https://leetcode-cn.com/contest/weekly-contest-170/problems/decrypt-string-from-alphabet-to-integer-mapping/
5+
**/
6+
public class Solution {
7+
8+
public String freqAlphabets(String s) {
9+
String res = "";
10+
int index = 0;
11+
int endPoint;
12+
while (s.indexOf('#', index) != -1) {
13+
endPoint = s.indexOf('#', index) - 2;
14+
while (index < endPoint) {
15+
res = res + ((char) (s.charAt(index) - '1' + 'a'));
16+
index++;
17+
}
18+
char c = (char) (Integer.parseInt(s.substring(index, index + 2)) - 1 + 'a');
19+
res = res + c;
20+
index = index + 3;
21+
}
22+
23+
while (index < s.length()) {
24+
res = res + ((char) (s.charAt(index) - '1' + 'a'));
25+
index++;
26+
}
27+
return res;
28+
}
29+
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @author changleamazing
3+
* @date 2020/1/5 11:12
4+
* source: https://leetcode-cn.com/contest/weekly-contest-170/problems/xor-queries-of-a-subarray/
5+
**/
6+
public class Solution {
7+
8+
public int[] xorQueries(int[] arr, int[][] queries) {
9+
int[] result = new int[queries.length];
10+
for (int i = 0; i < queries.length; i++) {
11+
int l = queries[i][0];
12+
int r = queries[i][1];
13+
if(l < 0 || r > arr.length){
14+
result[i] = 0;
15+
continue;
16+
}
17+
int res = arr[l];
18+
for (int j = l+1; j <= r; j++) {
19+
res = res ^ arr[j ];
20+
}
21+
result[i] = res;
22+
}
23+
return result;
24+
}
25+
26+
public static void main(String[] args) {
27+
int[] queries = new Solution()
28+
.xorQueries(new int[]{1,3,4,8}, new int[][]{{0, 1}, {1, 2}, {0, 3}, {3, 3}});
29+
for (int i = 0; i < queries.length; i++) {
30+
System.out.println(queries[i]);
31+
}
32+
33+
}
34+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.HashMap;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.PriorityQueue;
7+
import java.util.Queue;
8+
import javafx.util.Pair;
9+
10+
/**
11+
* @author changleamazing
12+
* @date 2020/1/5 11:31
13+
* source: https://leetcode-cn.com/contest/weekly-contest-170/problems/get-watched-videos-by-your-friends/
14+
**/
15+
public class Solution {
16+
17+
// 存储 id 的 level
18+
public List<String> watchedVideosByFriends(List<List<String>> watchedVideos, int[][] friends,
19+
int id, int level) {
20+
int[] visited = new int[watchedVideos.size()];
21+
Arrays.fill(visited, -1);
22+
// 存放当前 level 中的元素
23+
Queue<Integer> queue = new LinkedList<>();
24+
visited[id] = 1;
25+
queue.add(id);
26+
int depth = 0;
27+
while (!queue.isEmpty() && depth < level) {
28+
int size = queue.size();
29+
for (int i = 0; i < size; i++) {
30+
int curFriendId = queue.poll();
31+
for (int j = 0; j < friends[curFriendId].length; j++) {
32+
int friendId = friends[curFriendId][j];
33+
if (visited[friendId] == -1) {
34+
queue.add(friendId);
35+
visited[friendId] = 1;
36+
}
37+
}
38+
}
39+
depth++;
40+
}
41+
42+
HashMap<String, Integer> map = new HashMap<>();
43+
while (!queue.isEmpty()) {
44+
List<String> videoList = watchedVideos.get(queue.poll());
45+
videoList.forEach(o -> map.put(o, map.getOrDefault(o, 0) + 1));
46+
}
47+
PriorityQueue<Pair<String, Integer>> priorityQueue = new PriorityQueue<>((t1, t2) -> {
48+
if (t1.getValue().equals(t2.getValue())) {
49+
return t1.getKey().compareTo(t2.getKey());
50+
} else {
51+
return t1.getValue() - t2.getValue();
52+
}
53+
});
54+
55+
map.forEach((key, value) -> priorityQueue.offer(new Pair<>(key, value)));
56+
List<String> res = new ArrayList<>();
57+
while (!priorityQueue.isEmpty()) {
58+
res.add(priorityQueue.poll().getKey());
59+
}
60+
return res;
61+
}
62+
63+
64+
public static void main(String[] args) {
65+
List<List<String>> lists = new ArrayList<>();
66+
List<String> list1 = new ArrayList<>();
67+
List<String> list2 = new ArrayList<>();
68+
List<String> list3 = new ArrayList<>();
69+
List<String> list4 = new ArrayList<>();
70+
list1.add("A");
71+
list1.add("B");
72+
list2.add("C");
73+
list3.add("B");
74+
list3.add("C");
75+
list4.add("D");
76+
lists.add(list1);
77+
lists.add(list2);
78+
lists.add(list3);
79+
lists.add(list4);
80+
int[][] friends = new int[][]{{1, 2}, {0, 3}, {0, 3}, {1, 2}};
81+
new Solution().watchedVideosByFriends(lists, friends, 0, 1);
82+
}
83+
}

0 commit comments

Comments
 (0)