Skip to content

Commit 155f661

Browse files
committed
weekly contest 303
1 parent 7f2a033 commit 155f661

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Record leetcode contest and ideas every week, and encourage yourself to think mo
55
leetcode url: <https://leetcode.cn/u/cctest/>
66

77

8+
9+
* ☁️ [weekly contest 303](src/main/java/weekly/wk303.java) Array | Simulation | Heap (Priority Queue)|Binary Search
810
* ☀️ [weekly contest 302](src/main/java/weekly/wk302.java) Array | Heap | Heap| Math GCD
911
* ☀️ [weekly contest 301](src/main/java/weekly/wk301.java) Greegy | Hash Table | Two Pointers|Dynamic Programming+Combinatorics
1012
* ☀️ [weekly contest 300](src/main/java/weekly/wk300.java) Hash Table | Simulation | DP | DP

src/main/java/weekly/wk303.java

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package weekly;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.PriorityQueue;
10+
import java.util.Set;
11+
import java.util.TreeMap;
12+
import java.util.TreeSet;
13+
import java.util.function.IntFunction;
14+
15+
public class wk303 {
16+
17+
18+
//ranking:800 / 7032
19+
20+
//简单题,计数即可
21+
public char repeatedCharacter(String s) {
22+
int[] count = new int[26];
23+
for (char c : s.toCharArray()) {
24+
count[c - 'a']++;
25+
if (count[c - 'a'] >= 2) {
26+
return c;
27+
}
28+
}
29+
return '1';
30+
}
31+
32+
33+
//中等题,模拟
34+
public int equalPairs(int[][] grid) {
35+
List<List<Integer>> row = new ArrayList<>();
36+
List<List<Integer>> col = new ArrayList<>();
37+
for (int i = 0; i < grid.length; i++) {
38+
List<Integer> r = new ArrayList<>();
39+
List<Integer> c = new ArrayList<>();
40+
for (int j = 0; j < grid[0].length; j++) {
41+
r.add(grid[i][j]);
42+
c.add(grid[j][i]);
43+
}
44+
row.add(r);
45+
col.add(c);
46+
}
47+
int res = 0;
48+
for (List<Integer> r : row) {
49+
for (List<Integer> c : col) {
50+
boolean flag = true;
51+
for (int i = 0; i < r.size(); i++) {
52+
if (!r.get(i).equals(c.get(i))) {
53+
flag = false;
54+
break;
55+
}
56+
}
57+
if (flag) res++;
58+
59+
}
60+
}
61+
return res;
62+
63+
}
64+
65+
66+
67+
//中等题,hashmap+PriorityQueue,懒修改,一时没想明白怎么映射
68+
class FoodRatings {
69+
70+
class pac {
71+
int rate;
72+
String name;
73+
74+
public pac(int rate, String name) {
75+
this.name = name;
76+
this.rate = rate;
77+
}
78+
}
79+
80+
Map<String, PriorityQueue<pac>> map = new HashMap<>();
81+
Map<String, Integer> rate = new HashMap<>();
82+
Map<String, String> type = new HashMap<>();
83+
84+
85+
public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
86+
87+
for (int i = 0; i < foods.length; i++) {
88+
String food = foods[i];
89+
String cuisine = cuisines[i];
90+
int rating = ratings[i];
91+
type.put(food, cuisine);
92+
if (!map.containsKey(cuisine)) {
93+
map.put(cuisine, new PriorityQueue<>((a, b) -> a.rate == b.rate ? a.name.compareTo(b.name) : b.rate - a.rate));
94+
}
95+
rate.put(food, rating);
96+
map.get(cuisine).add(new pac(rating, food));
97+
}
98+
}
99+
100+
public void changeRating(String food, int newRating) {
101+
map.get(type.get(food)).add(new pac(newRating, food));
102+
rate.put(food, newRating);
103+
}
104+
105+
public String highestRated(String cuisine) {
106+
PriorityQueue<pac> pacs = map.get(cuisine);
107+
while (!pacs.isEmpty()) {
108+
pac poll = pacs.poll();
109+
if (!rate.get(poll.name).equals(poll.rate)) {
110+
continue;
111+
} else {
112+
pacs.add(poll);
113+
return poll.name;
114+
}
115+
}
116+
return "";
117+
}
118+
}
119+
120+
121+
122+
//困难提,找规律+二分查找
123+
/* public long countExcellentPairs(int[] nums, int k) {
124+
125+
Map<Integer, Integer> set = new HashMap<>();
126+
127+
long res = 0;
128+
for (int i = 0; i < nums.length; i++) {
129+
set.put(nums[i], set.getOrDefault(nums[i], 0) + 1);
130+
}
131+
132+
133+
nums = new int[set.size()];
134+
int j = 0;
135+
for (Map.Entry<Integer, Integer> entry : set.entrySet()) {
136+
Integer num = entry.getKey();
137+
if (Integer.bitCount(num ) + Integer.bitCount(num) >= k) {
138+
res++;
139+
}
140+
nums[j++] = Integer.bitCount(num);
141+
}
142+
143+
Arrays.sort(nums);
144+
145+
for (int i = 0; i < nums.length; i++) {
146+
res += ((long) help(nums, k - nums[i], i+1)) * 2;
147+
}
148+
return res;
149+
150+
}
151+
152+
int help(int[] nums, int k, int left) {
153+
154+
int right = nums.length - 1;
155+
if(left>right) return 0;
156+
while (left < right) {
157+
int mid = (left + right) / 2;
158+
if (nums[mid] < k) {
159+
left = mid + 1;
160+
} else {
161+
right = mid;
162+
}
163+
}
164+
if (nums[left] < k) return 0;
165+
return nums.length - left;
166+
}*/
167+
168+
169+
//统计次数+前缀和
170+
public long countExcellentPairs(int[] nums, int k) {
171+
int[] count=new int[33];
172+
Set<Integer> set=new HashSet<>();
173+
for (int num : nums) {
174+
if(!set.contains(num)){
175+
count[Integer.bitCount(num)]++;
176+
set.add(num);
177+
}
178+
}
179+
long ans=0;
180+
int sum=0;
181+
for(int i=k;i<count.length;i++){
182+
sum+=count[i];
183+
}
184+
185+
for(int i=0;i<count.length;i++){
186+
ans+=sum*(long)count[i];
187+
int j=k-1-i;
188+
if(j>=0&&j<count.length) sum+=count[j];
189+
}
190+
return ans;
191+
}
192+
}

0 commit comments

Comments
 (0)