Algorithms/- 프로그래머스
-
프로그래머스 - 약수의 개수와 덧셈 [자바]Algorithms/- 프로그래머스 2022. 1. 31. 01:47
문제 링크 : 약수의 개수와 덧셈 코딩테스트 연습 - 약수의 개수와 덧셈 두 정수 left와 right가 매개변수로 주어집니다. left부터 right까지의 모든 수들 중에서, 약수의 개수가 짝수인 수는 더하고, 약수의 개수가 홀수인 수는 뺀 수를 return 하도록 solution 함수를 완성해주 programmers.co.kr 정답 import java.util.*; class Solution { public int solution(int left, int right) { int answer = 0; /* left 부터 right 까지 약수의 개수가 짝수 + : true 약수의 개수 홀수 - : false */ for(int i=left; i
-
프로그래머스 - 실패율 [자바]Algorithms/- 프로그래머스 2022. 1. 31. 01:41
문제 링크 : 실패율 코딩테스트 연습 - 실패율 실패율 슈퍼 게임 개발자 오렐리는 큰 고민에 빠졌다. 그녀가 만든 프랜즈 오천성이 대성공을 거뒀지만, 요즘 신규 사용자의 수가 급감한 것이다. 원인은 신규 사용자와 기존 사용자 사이에 스 programmers.co.kr 정답 import java.util.*; class Solution { public int[] solution(int N, int[] stages) { /* 실패율 = 스테이지에 도달했으나 아직 클리어하지 못한 플레이어의 수 / 스테이지에 도달한 플레이어 수 전체 스테이지 수 N 멈춰 있는 stages 총 플레이어수 stages.length 모두 클리어한 유저가 있어서 N+1이 됨 실패율 높은 스테이지부터 내림차순 return */ int ..
-
프로그래머스 - 포켓몬 [자바]Algorithms/- 프로그래머스 2022. 1. 31. 01:38
문제 링크 : 폰켓몬 코딩테스트 연습 - 폰켓몬 당신은 폰켓몬을 잡기 위한 오랜 여행 끝에, 홍 박사님의 연구실에 도착했습니다. 홍 박사님은 당신에게 자신의 연구실에 있는 총 N 마리의 폰켓몬 중에서 N/2마리를 가져가도 좋다고 했습니다. programmers.co.kr 정답 import java.util.*; class Solution { public int solution(int[] nums) { int answer = 0; /* n -> n/2 선택 종류에 따라 번호 최대한 다양한 폰켓몬(숫자) 가장 많은 번호의 개수 return */ int pick = nums.length / 2; //nums가 항상 짝수 //set을 이용하면 중복을 제거할 수 있다. Set numsSet = new HashSe..
-
프로그래머스 - 체육복 [자바]Algorithms/- 프로그래머스 2022. 1. 30. 22:44
문제 링크 : 체육복 코딩테스트 연습 - 체육복 점심시간에 도둑이 들어, 일부 학생이 체육복을 도난당했습니다. 다행히 여벌 체육복이 있는 학생이 이들에게 체육복을 빌려주려 합니다. 학생들의 번호는 체격 순으로 매겨져 있어, 바로 앞번 programmers.co.kr 정답 import java.util.*; import java.util.stream.*; class Solution { public int solution(int n, int[] lost, int[] reserve) { /* 체격순 번호의 앞 뒤만 가능 e.g. 53 하나 차이만 가능 최대한 많은 학생 전체 학생 수 : n 도난당한 학생 번호 : lost 여벌 학생 번호 : reserve */ int finalLost = lost.length..
-
프로그래머스 - 소수 만들기 [자바]Algorithms/- 프로그래머스 2022. 1. 30. 22:40
문제 링크 : 소수 만들기 코딩테스트 연습 - 소수 만들기 주어진 숫자 중 3개의 수를 더했을 때 소수가 되는 경우의 개수를 구하려고 합니다. 숫자들이 들어있는 배열 nums가 매개변수로 주어질 때, nums에 있는 숫자들 중 서로 다른 3개를 골라 더했을 때 programmers.co.kr 정답 import java.util.*; import java.lang.*; class Solution { public int solution(int[] nums) { int count = 0; //3개의 수를 더했을 때 소수가 되는 경우의 수 //3개를 골라 소수가 되는 경우의 수 int length = nums.length; int number = 0; for(int i=0; i
-
프로그래머스 - 내적 [자바]Algorithms/- 프로그래머스 2022. 1. 30. 22:37
문제 링크 : 내적 코딩테스트 연습 - 내적 길이가 같은 두 1차원 정수 배열 a, b가 매개변수로 주어집니다. a와 b의 내적을 return 하도록 solution 함수를 완성해주세요. 이때, a와 b의 내적은 a[0]*b[0] + a[1]*b[1] + ... + a[n-1]*b[n-1] 입니다. (n은 a, b의 programmers.co.kr 정답 class Solution { public int solution(int[] a, int[] b) { int answer = 0; int length = a.length; for(int i=0; i
-
프로그래머스 - 음양 더하기 [자바]Algorithms/- 프로그래머스 2022. 1. 30. 22:36
문제 링크 : 음양 더하기 코딩테스트 연습 - 음양 더하기 어떤 정수들이 있습니다. 이 정수들의 절댓값을 차례대로 담은 정수 배열 absolutes와 이 정수들의 부호를 차례대로 담은 불리언 배열 signs가 매개변수로 주어집니다. 실제 정수들의 합을 구하여 re programmers.co.kr 정답 class Solution { public int solution(int[] absolutes, boolean[] signs) { int answer = 0; int length = absolutes.length; for(int i = 0; i
-
프로그래머스 - 없는 숫자 더하기 [자바]Algorithms/- 프로그래머스 2022. 1. 30. 22:23
문제 링크 : 없는 숫자 더하기 코딩테스트 연습 - 없는 숫자 더하기 0부터 9까지의 숫자 중 일부가 들어있는 정수 배열 numbers가 매개변수로 주어집니다. numbers에서 찾을 수 없는 0부터 9까지의 숫자를 모두 찾아 더한 수를 return 하도록 solution 함수를 완성해주세요. programmers.co.kr 정답 import java.util.*; class Solution { public int solution(int[] numbers) { int answer = 0; /* numbers에 없는 숫자 모두 더해서 리턴 */ int[] pickNumbers = {0,1,2,3,4,5,6,7,8,9}; //정렬하고 Arrays.sort(numbers); for(int pick : pick..