본문 바로가기

코딩테스트/2024 코딩 테스트65

[ 프로그래머스 ] 최댓값 만들기(2) 문제 풀이 1) numbers를 정렬시킨다. 2) 양수일때 가장 큰 두 수의 곱과 3) 음수일때 가장 큰 두 수의 곱을 구한 후 4) Math.max()로 가장 큰 값을 반환시킨다. 결과 import java.util.Arrays; class Solution { public int solution(int[] numbers) { int len = numbers.length; Arrays.sort(numbers); //정렬 int max1 = numbers[len-1]*numbers[len-2]; int max2 = numbers[0]*numbers[1]; //음수일때 return Math.max(max1,max2); } } 2024. 3. 9.
[ 프로그래머스 ] 삼각형의 완성조건(1) 문제 풀이 1) sides를 순차적으로 정렬해준다. 2) sides[0] + sides[1]이 sides[2]보다 크면 1을 반환하고 그렇지 않으면 2를 반환한다. 결과 import java.util.Arrays; class Solution{ public int solution(int[] slide){ Arrays.sort(slide); return slide[0] + slide[1] > slide[2] ? 1 : 2; } } 2024. 3. 6.
[ 프로그래머스 ] 짝수는 싫어요 문제 풀이 1) 1~n까지 수중에서 홀수 인 수만 가져와 배열로 만들어준다. 2) IntStream.rangeClosed() : 1~n까지의 수를 가지고 온다. 3) filter()를 통해 홀수인 것만 가져온다. 4) toArray()를 이용해 배열로 만들어준다. 결과 import java.util.stream.IntStream; class Solution { public int[] solution(int n) { return IntStream.rangeClosed(1,n).filter(i -> i%2==1).toArray(); } } 2024. 3. 2.
[ 프로그래머스 ] 아이스 아메리카노 문제 풀이 answer[0]번 방에는 최대로 마실 수 있는 아메리카노의 잔 수를 구해야하고 answer[1]에는 최대로 마시고 선 남은 돈을 구해야한다. 최대로 마실 수 있는 아메리카노의 잔 수 : 현재 가지고 있는 돈에서 5500을 나눈 몫 남은 돈 : moey를 5500으로 나눈 나머지 결과 class Solution { public int[] solution(int money) { int[] answer = new int[2]; answer[0]= money/5500; answer[1]= money%5500; return answer; } } 2024. 3. 2.