LeetCode 3278: Find Candidates for Data Scientist Position II
Problem Description
Explanation:
To solve this problem, we can use a hashmap to store the count of skills each candidate possesses. Then, we iterate through the list of candidates and calculate the similarity score with the given skills. We keep track of the candidates with the highest similarity score and return them as the result.
- Create a hashmap to store the count of skills for each candidate.
- Iterate through the list of candidates and calculate the similarity score.
- Keep track of candidates with the highest similarity score.
- Return the candidates with the highest similarity score.
Time Complexity: O(n * m) where n is the number of candidates and m is the average number of skills each candidate possesses. Space Complexity: O(n) where n is the number of candidates.
:
Solutions
import java.util.*;
class Solution {
public List<String> findCandidates(String[] candidates, String[] skills) {
Map<String, Set<String>> map = new HashMap<>();
for (String candidate : candidates) {
map.put(candidate, new HashSet<>());
}
for (String skill : skills) {
for (String candidate : candidates) {
if (candidate.contains(skill)) {
map.get(candidate).add(skill);
}
}
}
int maxScore = 0;
List<String> result = new ArrayList<>();
for (Map.Entry<String, Set<String>> entry : map.entrySet()) {
int score = entry.getValue().size();
if (score > maxScore) {
maxScore = score;
result.clear();
result.add(entry.getKey());
} else if (score == maxScore) {
result.add(entry.getKey());
}
}
return result;
}
}
Loading editor...