Algorithm

greedy

겨르 2024. 11. 18. 18:13
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] <= nums[right]){
	       if(nums[left] + nums[right] <= m){
	           answer++;
	           left++;
	           right--;
	       }
	       else{
	           answer++;
	           right--;
	       }
	   }

		
		return answer;
	}

	public static void main(String[] args){
		Solution T = new Solution();
		System.out.println(T.solution(new int[]{90, 50, 70, 100, 60}, 140));
		System.out.println(T.solution(new int[]{10, 20, 30, 40, 50, 60, 70, 80, 90}, 100));
		System.out.println(T.solution(new int[]{68, 72, 30, 105, 55, 115, 36, 67, 119, 111, 95, 24, 25, 80, 55, 85, 75, 83, 21, 81}, 120));
	}
}

'Algorithm' 카테고리의 다른 글

sort  (0) 2024.11.18
자료구조 활용 : HashSet  (0) 2024.11.18
해시 : 한 번 사용한 최초문자  (0) 2024.11.16