You are given an integer array nums, an integer array queries, and an integer x.
For each queries[i], you need to find the index of the queries[i]th occurrence of x in the nums array. If there are fewer than queries[i] occurrences of x, the answer should be -1 for that query.
Return an integer array answer containing the answers to all queries.
Example 1:
Input: nums = [1,3,1,7], queries = [1,3,2,4], x = 1
Output: [0,-1,2,-1]
Explanation:
nums, so the answer is -1.nums, so the answer is -1.Example 2:
Input: nums = [1,2,3], queries = [10], x = 5
Output: [-1]
Explanation:
nums, so the answer is -1.Constraints:
1 <= nums.length, queries.length <= 1051 <= queries[i] <= 1051 <= nums[i], x <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The simplest way to solve this is to look at every single item in the collection one by one. We'll keep a running count of how many times we've seen the special number we're looking for, and stop as soon as our count matches the specific occurrence we want.
Here's how the algorithm would work step-by-step:
from typing import List
def occurrencesOfElement(nums: List[int], queries: List[int], target_number: int) -> List[int]:
indices_of_target = []
# First, collect all indices where the target number appears in the original list.
for index, current_number in enumerate(nums):
if current_number == target_number:
indices_of_target.append(index)
results = []
for occurrence_query in queries:
# The query is 1-based, so we must adjust it to access our 0-based list of indices.
index_in_indices_list = occurrence_query - 1
# Check if the requested occurrence is valid (i.e., we found the target that many times).
if 0 <= index_in_indices_list < len(indices_of_target):
results.append(indices_of_target[index_in_indices_list])
else:
# If the requested occurrence doesn't exist, the problem specifies we should use -1.
results.append(-1)
return resultsThe best way to solve this is to prepare ahead of time by finding all the locations of the special number first. Once we have a list of all these locations, we can quickly look up the answer for any requested occurrence without having to search the entire original collection again.
Here's how the algorithm would work step-by-step:
def find_occurrences(numbers_collection, target_number, occurrence_queries):
# Pre-calculate and store all indices where the target number appears.
locations_of_target = []
for index, current_number in enumerate(numbers_collection):
if current_number == target_number:
locations_of_target.append(index)
query_answers = []
# Process each requested occurrence to find its corresponding index.
for requested_occurrence in occurrence_queries:
# The requested occurrence is 1-based, so we must adjust it for 0-based list access.
query_index = requested_occurrence - 1
# Check if the requested occurrence is valid within the bounds of our found locations.
if 0 <= query_index < len(locations_of_target):
query_answers.append(locations_of_target[query_index])
else:
query_answers.append(-1)
return query_answers| Case | How to Handle |
|---|---|
| The `nums` array is empty or null | The preprocessing step will result in an empty map of occurrences, leading to -1 for all queries. |
| The `queries` array is empty or null | The solution should return an empty array as there are no queries to process. |
| The target `value` does not exist in the `nums` array | The map of occurrences for `value` will be empty, causing all queries to correctly return -1. |
| A query asks for the k-th occurrence, but `value` appears fewer than k times | The solution will correctly return -1 as the requested 1-based index k will be out of bounds for the stored list of occurrences. |
| A query value is zero or negative (e.g., query for the 0th or -1st occurrence) | The solution should handle this gracefully by returning -1, as queries are 1-based. |
| The `nums` array contains all identical elements matching the `value` | The map will store a single entry with a long list of indices, which is an efficient way to handle this case. |
| Large inputs for `nums` and `queries` (e.g., up to 10^5 elements each) | A preprocessing approach with a hash map ensures each query is answered in constant time, scaling efficiently for large inputs. |
| Inputs contain negative numbers or zeros for `nums` elements and the `value` | Using a hash map works correctly with any integer values, including negatives and zero, without special logic. |