Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- simulation
- two pointers
- 코테
- geometry
- Math
- Method
- 코딩테스트
- array
- 구현
- Stack
- Binary Tree
- Binary Search
- Counting
- Number Theory
- Tree
- hash table
- Data Structure
- Class
- java
- greedy
- dynamic programming
- database
- SQL
- string
- sorting
- Matrix
- 자바
- implement
- bit manipulation
- 파이썬
Archives
- Today
- Total
코린이의 소소한 공부노트
[프로그래머스/Lv.1] 푸드 파이트 대회 본문
1. Input
1) 정수 배열 food
- food[0]은 준비한 물의 양이며, 항상 1이다.
- food[i]는 i번째의 음식의 수이다.
2. Output
1) 두 선수가 먹게 될 음식의 배치를 담은 문자열
- 두 선수는 양쪽 끝에서부터 음식을 다 먹는다.
- 두 선수가 먹는 음식의 종류와 수, 순서는 모두 같다.
- 가장 가운데에 위치한 물을 먼저 마시는 선수가 이긴다. 그러므로 배치에서 물은 가장 가운데에 위치하게 된다.
3. Constraint
1) 2 ≤ food의 길이 ≤ 9
2) 1 ≤ food의 각 원소 ≤ 1,000
3) food에는 칼로리가 적은 순서대로 음식의 양이 담겨 있다.
4) 정답의 길이가 3 이상인 경우만 입력으로 주어진다.
4. Example
Input: [1, 3, 4, 6] -> Output: "1223330333221“
설명:
- food[0]==1: 물 1개 -> “0”
- food[1]==3: 1번 음식 3개. 두 선수가 동일하게 먹어야하므로 2개만 배치 -> “101”
- food[2]=4: 2번 음식 4개 -> “1220221”
- food[3]=6: 3번 음식 6개 -> “1223330333221”
5. Code
1) 첫 코드(2023/01/11)
int len = 1;
for(int i=1 ; i<food.length ; i++)
len += food[i]/2 *2;
char[] c = new char[len];
c[len/2] = '0';
int ci = 0;
for(int i=1 ; i<food.length ; i++){
for(int j=ci; j<ci+food[i]/2 ; j++){
c[j] = (char)(i+'0');
c[c.length-1-j] = c[j];
}
ci += food[i]/2;
}
return new String(c);
2) 다른 사람들의 풀이 중 가장 간단해보이는 코드(2023/01/11)
String answer = "0";
for (int i = food.length - 1; i > 0; i--) {
for (int j = 0; j < food[i] / 2; j++) {
answer = i + answer + i;
}
}
return answer;
- 가장 직관적인 코드였지만, 너무 느리다. 1번 코드는 평균 1ms도 안된 것에 반해 2번 코드는 10ms는 가볍게 넘어갔다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2057. Smallest Index With Equal Value (0) | 2023.01.12 |
---|---|
[LeetCode/Easy] 2042. Check if Numbers Are Ascending in a Sentence (0) | 2023.01.12 |
[LeetCode/Easy] 100. Same Tree (0) | 2023.01.11 |
[LeetCode/Easy] 2037. Minimum Number of Moves to Seat Everyone (0) | 2023.01.11 |
[LeetCode/Easy] 2022. Convert 1D Array Into 2D Array (0) | 2023.01.11 |