Programmers / Pokemon

Programmers / Pokemon

Problem

Solution 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import java.util.Set;
import java.util.HashSet;

class Solution {
    public int solution(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();

        // Create existing phonecatmon set
        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
        }
        
        // Return
        if (set.size() > (nums.length / 2)) {
            return nums.length / 2;
        }
        return set.size();
    }
}
Solution 1
  • Description
    • Use Set to check the number of Pokemon types
    • If the number of Pokemon types is greater than or equal to half of the total Pokemon count, return half of the total count
    • If the number of Pokemon types is less than or equal to half of the total Pokemon count, return the number of Pokemon types
  • Time Complexity
    • O(len(nums))
    • Operations proportional to nums length
  • Space Complexity
    • O(1)
    • Function parameters and local variables