Skip to content

Commit 89c55d1

Browse files
add binary search solution for 1170
1 parent ac140b8 commit 89c55d1

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/main/java/com/fishercoder/solutions/_1170.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,59 @@ private int computeLowestFrequency(String string) {
8181
return frequency;
8282
}
8383
}
84+
85+
public static class Solution2 {
86+
/**
87+
* Use binary search when finding counts
88+
* Time: O(n^logn) where m is the size of queries and n is the size of words
89+
* Space: O(max(m, n) where m is the size of queries and n is the size of words)
90+
* */
91+
public int[] numSmallerByFrequency(String[] queries, String[] words) {
92+
int[] queriesMinFrequecies = new int[queries.length];
93+
for (int i = 0; i < queries.length; i++) {
94+
queriesMinFrequecies[i] = computeLowestFrequency(queries[i]);
95+
}
96+
97+
int[] wordsMinFrequecies = new int[words.length];
98+
for (int i = 0; i < words.length; i++) {
99+
wordsMinFrequecies[i] = computeLowestFrequency(words[i]);
100+
}
101+
Arrays.sort(wordsMinFrequecies);
102+
103+
int[] result = new int[queries.length];
104+
for (int i = 0; i < result.length; i++) {
105+
result[i] = binarySearch(wordsMinFrequecies, queriesMinFrequecies[i]);
106+
}
107+
return result;
108+
}
109+
110+
private int binarySearch(int[] nums, int target) {
111+
int left = 0;
112+
int right = nums.length - 1;
113+
while (left <= right) {
114+
int mid = (left + right) / 2;
115+
if (nums[mid] <= target) {
116+
left = mid + 1;
117+
} else {
118+
right = mid - 1;
119+
}
120+
}
121+
return nums.length - left;
122+
}
123+
124+
private int computeLowestFrequency(String string) {
125+
char[] str = string.toCharArray();
126+
Arrays.sort(str);
127+
String sortedString = new String(str);
128+
int frequency = 1;
129+
for (int i = 1; i < sortedString.length(); i++) {
130+
if (sortedString.charAt(i) == sortedString.charAt(0)) {
131+
frequency++;
132+
} else {
133+
break;
134+
}
135+
}
136+
return frequency;
137+
}
138+
}
84139
}

src/test/java/com/fishercoder/_1170Test.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
public class _1170Test {
1010
private static _1170.Solution1 solution1;
11+
private static _1170.Solution2 solution2;
1112
private static String[] queries;
1213
private static String[] words;
1314

1415
@BeforeClass
1516
public static void setup() {
1617
solution1 = new _1170.Solution1();
18+
solution2 = new _1170.Solution2();
1719
}
1820

1921
@Test
@@ -30,4 +32,18 @@ public void test2() {
3032
assertArrayEquals(new int[]{1, 2}, solution1.numSmallerByFrequency(queries, words));
3133
}
3234

35+
@Test
36+
public void test3() {
37+
queries = new String[]{"cbd"};
38+
words = new String[]{"zaaaz"};
39+
assertArrayEquals(new int[]{1}, solution2.numSmallerByFrequency(queries, words));
40+
}
41+
42+
@Test
43+
public void test4() {
44+
queries = new String[]{"bbb", "cc"};
45+
words = new String[]{"a", "aa", "aaa", "aaaa"};
46+
assertArrayEquals(new int[]{1, 2}, solution2.numSmallerByFrequency(queries, words));
47+
}
48+
3349
}

0 commit comments

Comments
 (0)