Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf414760

Browse files
committed
Added 2 solutions
1 parent22d63e2 commitf414760

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
classSolution {
2+
publicint[]numSmallerByFrequency(String[]queries,String[]words) {
3+
List<Integer>list =newArrayList<>();
4+
for (Stringword :words) {
5+
list.add(getMinWordFreq(word));
6+
}
7+
Collections.sort(list);
8+
int[]ans =newint[queries.length];
9+
for (inti =0;i <queries.length;i++) {
10+
intminCount =getMinWordFreq(queries[i]);
11+
intidx =0;
12+
ans[i] =binarySearch(minCount,list);
13+
}
14+
returnans;
15+
}
16+
17+
privateintbinarySearch(inttarget,List<Integer>list) {
18+
intleft =0;
19+
intright =list.size() -1;
20+
while (left <=right) {
21+
intmid = (left +right) /2;
22+
if (list.get(mid) <=target) {
23+
left =mid +1;
24+
}
25+
else {
26+
right =mid -1;
27+
}
28+
}
29+
30+
returnlist.size() -left;
31+
}
32+
33+
privateintgetMinWordFreq(Stringword) {
34+
int[]counter =newint[26];
35+
for (charc :word.toCharArray()) {
36+
counter[c -'a']++;
37+
}
38+
for (inti =0;i <26;i++) {
39+
if (counter[i] !=0) {
40+
returncounter[i];
41+
}
42+
}
43+
return0;
44+
}
45+
}

‎Easy/Diet Plan Performance.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
classSolution {
2+
intpoints;
3+
publicintdietPlanPerformance(int[]calories,intk,intlower,intupper) {
4+
points =0;
5+
inttotalCal =0;
6+
intstart =0;
7+
for (inti =0;i <k;i++) {
8+
totalCal +=calories[i];
9+
}
10+
for (inti =k;i <calories.length;i++) {
11+
updatePoints(totalCal,lower,upper);
12+
totalCal -=calories[start++];
13+
totalCal +=calories[i];
14+
}
15+
updatePoints(totalCal,lower,upper);
16+
returnpoints;
17+
}
18+
19+
privatevoidupdatePoints(intcurrCal,intlower,intupper) {
20+
if (currCal <lower) {
21+
points--;
22+
}
23+
if (currCal >upper) {
24+
points++;
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp