2024/11/16 灵茶题单 二分查找
This commit is contained in:
parent
59f449b22a
commit
b5c1126d7d
|
@ -0,0 +1,34 @@
|
||||||
|
package com.cool.ling_cha_mount.binary_search;
|
||||||
|
|
||||||
|
public class Num1170 {
|
||||||
|
public int[] numSmallerByFrequency(String[] queries, String[] words) {
|
||||||
|
int[] count = new int[12];
|
||||||
|
for (String str : words) {
|
||||||
|
count[f(str)]++;
|
||||||
|
}
|
||||||
|
for (int i = 9; i > 0; i--) {
|
||||||
|
count[i] += count[i + 1];
|
||||||
|
}
|
||||||
|
int[] res = new int[queries.length];
|
||||||
|
for (int i = 0; i < queries.length; i++) {
|
||||||
|
int c = f(queries[i]);
|
||||||
|
res[i] = count[c + 1];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int f(String s) {
|
||||||
|
char current = 'z';
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
char cur = s.charAt(i);
|
||||||
|
if (cur < current) {
|
||||||
|
current = cur;
|
||||||
|
count = 1;
|
||||||
|
} else if (cur == current) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue