본문 바로가기

프로그래머스

프로그래머스 - 모의고사 import java.util.*;class Solution { public int[] solution(int[] answers) { // 초기값 세팅 int [] first = {1,2,3,4,5}; int [] second = {2,1,2,3,2,4,2,5}; int [] third = {3,3,1,1,2,2,4,4,5,5}; int firstCount = 0; int secondCount = 0; int thirdCount = 0; for (int i=0; i savePot = new ArrayList(); max = Math.max(Math.max(firstCount,secondC.. 더보기
프로그래머스_Level1_콜라 문제 풀이 class Solution { public int solution(int a, int b, int n) { int answer = 0; while(n>=a){ int tmp=n/a; int tmp2=tmp*b; n=n-tmp*a+tmp*b; //현재 보유한 콜라 병 수 answer+=tmp2; } return answer; } } 간단한 문제다. 다만 주의할 점은 역시나 콜라를 몇 병 당 몇 병으로 다시 페이백해주는지에 대한 처리를 해줘야 된다는 점이다. 그것만 제외한다면 나머지는 while문에서 돌아가는 간단한 문제이다. 더보기
프로그래머스_Level1_푸드 파이트 대회 class Solution { public String solution(int[] food) { String answer = ""; int length=food.length; for(int i=1; i 더보기
프로그래머스_Level1_과일장수 풀이 import java.util.*; class Solution { public int solution(int k, int m, int[] score) { int answer = 0; int boxSize = score.length/m; int startCount=score.length-1; Arrays.sort(score); for(int i=0; i0){ answer+=score[startCount-m+1]*m; } startCount=startCount-m; } return answer; } } 생각을 해보자. 사실 맨 처음에는 리스트를 사용해서 데이터들을 분류한뒤 분류된 데이터들에서 최소값들을 Math.min과 같은 내장함수를 이용하여 끄집어 낼 생각을 했지만 굳이 그렇게 하지않고도 Array.. 더보기
프로그래머스_Level1_카드뭉치 풀이: class Solution { public String solution(String[] cards1, String[] cards2, String[] goal) { String answer = "Yes"; int firstL=0; int secondL=0; for(int i=0; i 더보기
프로그래머스_LEVEL1_삼총사 풀이: class Solution { public int solution(int[] number) { int answer = 0; for(int i =0; i 더보기
프로그래머스_Level1_크기가 작은 부분 문자열 풀이 class Solution { public int solution(String t, String p) { int length = t.length(); int plength = p.length(); int count = 0; long testNum = Long.parseLong(p); for (int i = 0; i 더보기
프로그래머스_Level1_가장 가까운 같은 글자 풀이 class Solution { public int[] solution(String s) { int length=s.length(); int[] answer = new int[length]; answer[0]=-1; int count; for(int i=1; i=0; j--){ if(s.charAt(i)==s.charAt(j)){ count=(i-j); break; } } answer[i]=count; } return answer; } } 음 일단 풀이를 하기 전에 앞서서 생각을 해보자. 맨 처음으로 나오는 문자(aib는 각각 처음 세상에 나온 문자들이므로 모두 -1의 값을 지닌다)는 -1을 지닌다. 특히나 단어 구조상 첫번쨰 문자는 '반드시' 처음 나온 문자이다.따라서 answer[0]=-1; 처음.. 더보기