|
20 | 20 |
|
21 | 21 | public class Test2 {
|
22 | 22 |
|
23 |
| - public class AutocompleteSystem { |
24 |
| - |
25 |
| - class TrieNode { |
26 |
| - Map<Character, TrieNode> children; |
27 |
| - Map<String, Integer> counts; |
28 |
| - boolean isWord; |
29 |
| - public TrieNode() { |
30 |
| - children = new HashMap<Character, TrieNode>(); |
31 |
| - counts = new HashMap<String, Integer>(); |
32 |
| - isWord = false; |
33 |
| - } |
34 |
| - } |
35 |
| - |
36 |
| - class Pair { |
37 |
| - String s; |
38 |
| - int c; |
39 |
| - public Pair(String s, int c) { |
40 |
| - this.s = s; this.c = c; |
41 |
| - } |
| 23 | + public int[] searchRange(int[] nums, int target) { |
| 24 | + if (nums.length == 0) { |
| 25 | + return new int[]{-1, -1}; |
42 | 26 | }
|
| 27 | + return new int[] { |
| 28 | + lowerBound(nums, target), |
| 29 | + upperBound(nums, target) |
| 30 | + }; |
| 31 | + } |
43 | 32 |
|
44 |
| - TrieNode root; |
45 |
| - String prefix; |
46 |
| - |
| 33 | + public static int lowerBound(int[] nums, int target) { |
| 34 | + int left = 0, right = nums.length - 1; |
47 | 35 |
|
48 |
| - public AutocompleteSystem(String[] sentences, int[] times) { |
49 |
| - root = new TrieNode(); |
50 |
| - prefix = ""; |
| 36 | + while (left < right) { |
| 37 | + int mid = left + ((right - left) >> 1); |
51 | 38 |
|
52 |
| - for (int i = 0; i < sentences.length; i++) { |
53 |
| - add(sentences[i], times[i]); |
| 39 | + if (target > nums[mid]) { |
| 40 | + left = mid + 1; |
| 41 | + } else { |
| 42 | + right = mid; |
54 | 43 | }
|
55 | 44 | }
|
56 | 45 |
|
57 |
| - private void add(String s, int count) { |
58 |
| - TrieNode curr = root; |
59 |
| - for (char c : s.toCharArray()) { |
60 |
| - TrieNode next = curr.children.get(c); |
61 |
| - if (next == null) { |
62 |
| - next = new TrieNode(); |
63 |
| - curr.children.put(c, next); |
64 |
| - } |
65 |
| - curr = next; |
66 |
| - curr.counts.put(s, curr.counts.getOrDefault(s, 0) + count); |
67 |
| - } |
68 |
| - curr.isWord = true; |
69 |
| - } |
| 46 | + return nums[left] == target ? left : -1; |
| 47 | + } |
70 | 48 |
|
71 |
| - public List<String> input(char c) { |
72 |
| - if (c == '#') { |
73 |
| - add(prefix, 1); |
74 |
| - prefix = ""; |
75 |
| - return new ArrayList<String>(); |
76 |
| - } |
| 49 | + public static int upperBound(int[] nums, int target) { |
| 50 | + int left = 0, right = nums.length - 1; |
77 | 51 |
|
78 |
| - prefix = prefix + c; |
79 |
| - TrieNode curr = root; |
80 |
| - for (char cc : prefix.toCharArray()) { |
81 |
| - TrieNode next = curr.children.get(cc); |
82 |
| - if (next == null) { |
83 |
| - return new ArrayList<String>(); |
84 |
| - } |
85 |
| - curr = next; |
86 |
| - } |
| 52 | + while (left < right) { |
| 53 | + int mid = left + ((right - left) >> 1) + 1; |
87 | 54 |
|
88 |
| - PriorityQueue<Pair> pq = new PriorityQueue<>((a, b) -> (a.c == b.c ? a.s.compareTo(b.s) : b.c - a.c)); |
89 |
| - for (String s : curr.counts.keySet()) { |
90 |
| - pq.add(new Pair(s, curr.counts.get(s))); |
| 55 | + if (target < nums[mid]) { |
| 56 | + right = mid - 1; |
| 57 | + } else { |
| 58 | + left = mid; |
91 | 59 | }
|
92 |
| - |
93 |
| - List<String> res = new ArrayList<String>(); |
94 |
| - for (int i = 0; i < 3 && !pq.isEmpty(); i++) { |
95 |
| - res.add(pq.poll().s); |
96 |
| - } |
97 |
| - return res; |
98 | 60 | }
|
| 61 | + |
| 62 | + return nums[right] == target ? right : -1; |
99 | 63 | }
|
100 | 64 | }
|
0 commit comments