forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1170.java
113 lines (102 loc) · 3.92 KB
/
_1170.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.fishercoder.solutions;
import java.util.Arrays;
public class _1170 {
public static class Solution1 {
/**
* Use simple iteration when finding counts
* Time: O(n^m) where m is the size of queries and n is the size of words
* Space: O(max(m, n) where m is the size of queries and n is the size of words)
*/
public int[] numSmallerByFrequency(String[] queries, String[] words) {
int[] queriesMinFrequecies = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
queriesMinFrequecies[i] = computeLowestFrequency(queries[i]);
}
int[] wordsMinFrequecies = new int[words.length];
for (int i = 0; i < words.length; i++) {
wordsMinFrequecies[i] = computeLowestFrequency(words[i]);
}
Arrays.sort(wordsMinFrequecies);
int[] result = new int[queries.length];
for (int i = 0; i < result.length; i++) {
result[i] = search(wordsMinFrequecies, queriesMinFrequecies[i]);
}
return result;
}
private int search(int[] nums, int target) {
int count = 0;
for (int i = nums.length - 1; i >= 0; i--) {
if (nums[i] > target) {
count++;
} else {
break;
}
}
return count;
}
private int computeLowestFrequency(String string) {
char[] str = string.toCharArray();
Arrays.sort(str);
String sortedString = new String(str);
int frequency = 1;
for (int i = 1; i < sortedString.length(); i++) {
if (sortedString.charAt(i) == sortedString.charAt(0)) {
frequency++;
} else {
break;
}
}
return frequency;
}
}
public static class Solution2 {
/**
* Use binary search when finding counts
* Time: O(n^logn) where m is the size of queries and n is the size of words
* Space: O(max(m, n) where m is the size of queries and n is the size of words)
*/
public int[] numSmallerByFrequency(String[] queries, String[] words) {
int[] queriesMinFrequecies = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
queriesMinFrequecies[i] = computeLowestFrequency(queries[i]);
}
int[] wordsMinFrequecies = new int[words.length];
for (int i = 0; i < words.length; i++) {
wordsMinFrequecies[i] = computeLowestFrequency(words[i]);
}
Arrays.sort(wordsMinFrequecies);
int[] result = new int[queries.length];
for (int i = 0; i < result.length; i++) {
result[i] = binarySearch(wordsMinFrequecies, queriesMinFrequecies[i]);
}
return result;
}
private int binarySearch(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] <= target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return nums.length - left;
}
private int computeLowestFrequency(String string) {
char[] str = string.toCharArray();
Arrays.sort(str);
String sortedString = new String(str);
int frequency = 1;
for (int i = 1; i < sortedString.length(); i++) {
if (sortedString.charAt(i) == sortedString.charAt(0)) {
frequency++;
} else {
break;
}
}
return frequency;
}
}
}