프로그래머스 코딩테스트/JAVA
[프로그래머스] 푸드 파이트 대회 JAVA
Coding-Su
2024. 8. 5. 10:27
728x90
문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
정답
import java.util.*;
class Solution {
public String solution(int[] food) {
String answer = "";
List<Integer> arr = new ArrayList<Integer>();
for(int i = 1; i < food.length; i++){
if(food[i] % 2 == 1) {
food[i] = (food[i] - 1)/2;
} else {
food[i] /=2;
}
for(int j = 0; j < food[i]; j++) {
arr.add(i);
answer+=i;
}
}
answer+="0";
for(int i = arr.size()-1; i >=0; i--) {
answer += arr.get(i);
}
return answer;
}
}
앞 부분과 뒤 부분이 같이 때문에 list에 추가를 하고 뒤 부분을 for문을 통해 추가해 문제를 풀었습니다.
728x90