목록코딩테스트/프로그래머스 (53)
Jam's story
최대로 몇개를 지원해야지 알아야하니까, 작은값부터 빼주기 위해서 오름차순으로 입력들어온 배열을 정렬해주고, 배열의 해당값이 budget 이상이라면 answer++ 해주고 budget에는 해당값만큼 빼주기 import java.util.Arrays; class Solution { public static int solution(int[] d, int budget) { int answer = 0; Arrays.sort(d); for(int i=0; i=d[i]) answer++; budget-=d[i]; } return answer; } }
package soltest; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; public class Soiution7 { public static int solution(int[] nums) { int max=nums.length/2; int result=0; //뽑은카드수 //중복제거 HashSet nSet=new HashSet(); for(int num: nums) { nSet.add(num); } //HashSet-> 배열만듬 Object[] objArr=nSet.toArray(); //System.out.println(Arrays.toString(objArr)); for(int i=0; i
class Solution { public int solution(int n, int[] lost, int[] reserve) { // 1. student 배열 생성 //매번 앞뒤로 검색해야하는데, 1일때와 마지막 숫자일때 if문으로 거르는 처리를 두려면 //코드가 지저분해지니, 0번 인덱스와 마지막인덱스+1는 0으로 초기화해서 //사용하지 않는 배열로 냅둔다. //인덱스가 0= 자기꺼만있음 1=여분있음 -1는 빌려야함 int[] student = new int[n + 2]; int answer = 0; // 2. reserve / lost 정보 반영 for (int l : lost) student[l]--; for (int r : reserve) student[r]++; // 3. 여분을 기준으로 앞뒤..
package soltest; class Solution5 { public int[] solution(int[] answers) { int[] answer = {}; //반복되는 수로만 배열을 생성 int[] a= { 1, 2, 3, 4, 5}; int[] b= {2, 1, 2, 3, 2, 4, 2, 5}; int[] c= {3, 3, 1, 1, 2, 2, 4, 4, 5, 5}; //맞춘 갯수를 저장하는 배열 int[] score= {0,0,0}; //얼마나 맞췄는지 for (int i = 0; i < answers.length; i++) { if(answers[i]==a[i%5]) score[0]++; else if(answers[i]==b[i%8]) score[1]++; else if(answers[..
import java.util.ArrayList; import java.util.Collections; class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = new int[commands.length]; ArrayList arr=new ArrayList(); for (int i = 0; i < commands.length; i++) { int start=commands[i][0]-1; int end=commands[i][1]; int pick=commands[i][2]-1; for (int j = start; j
completion가 participant에 있는 것으로만 이루어진거니까 두 배열을 비교해서 없는것을 찾으면 된다고 생각 우선 두 배열을 정렬을 한다. completion 길이가 더 짧음 항상 completion 은 participation에 있는 똑같은 멤버들중에서 빠진 한명을 찾으면 되기때문에 completion길이만큼 for문을 돌려준다 i를 for문 밖에서도 사용해야해서 선언하는 부분 빼줬고 package prac; import java.util.Arrays; class Solution { public String solution(String[] participant, String[] completion) { Arrays.sort(participant); Arrays.sort(completion)..
package soltest; import java.util.Iterator; //case문으로 L=1,4,7,* / R= 3,6,9,# 나누기 //처음에는 쉽게 값-값으로 구하려고 햇으나 //왼손위치 :4 , 오른손위치:2 -> 눌러야할것 5 //이 예제를 보니 안될 것같아 자리 배열을 만들어주기 //2,5,8,0은 거리를 계산해서, 거리가 같다면 --손잡이로 //정답은 string 문자열에 추가 public class Soltest4 { public String solution(int[] numbers, String hand) { //어떤 손잡이인지,넣어주기 String hands= (hand.equals("left"))? "L":"R"; String answer = ""; //오른손 왼손 초치 위치..
for문 안에 if문이 없어도 실행이 된다... -> 존재하던말던 검사를 해서 그런가보다 replaceAll은 바꿀 문자열에 정규식을 넣을 수 있다는 점이 replace와 다르다. class Solution { public int solution(String s) { String[] mat= {"zero", "one", "two", "three","four","five", "six","seven","eight","nine"}; for (int i = 0; i < mat.length; i++) { if(s.contains(mat[i])){ s=s.replace(mat[i], Integer.toString(i)); } } int answer=Integer.parseInt(s); return answer; }..