greedy import java.util.*;public class Solution { public int solution(int[] nums, int m){ int answer = 0; //1.오름차순 정렬 Arrays.sort(nums); //2. two pointer int left = 0; int right = nums.length-1; while(nums[left] Algorithm 2024.11.18
sort import java.util.*;public class Solution { public int[] solution(int[] nums){ int[] answer = new int[nums.length]; int[][] res = new int[nums.length][2]; //1. 10진수를 2진수로 변환한 값 저장 for(int i=0; i0){ cnt += (tmp%2); tmp /=2; } res[i][0] = nums[i]; res[i][1] = cnt; } //2. 정렬 Arrays.sort(res, (a,b) -> a[1] == b[1] ? a[0] - b[0]: a[1].. Algorithm 2024.11.18
자료구조 활용 : HashSet import java.util.*;public class Solution { public int solution(int[] nums){ int answer = 0; //set으로 중복된 원소 제거 HashSet set = new HashSet(); for(int i:nums) set.add(i); //set을 돌면서 하나 작은거 있는지 찾고 없으면 그다음숫자 있는지 확인 for(int i : set){ if(set.contains(i-1)) continue; int cnt = 0; while(set.contains(i)){ cnt++; i++; } answer = Math.max(answer, cnt); } .. Algorithm 2024.11.18