SSAFY/SWEA

[SWEA] 1208 : Flatten

믕비 2023. 5. 8. 10:55
반응형

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV139KOaABgCFAYh 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

최소랑 최대만 쓰면 돼서 최소값이랑 최대값을 갱신해주며 계산했다.

모든 상자의 크기가 같을 때( = 최소랑 최대의 차가 0)와 최소와 최대의 차가 1일 때  바로 중지한 후 출력해줌.

 

내 코드

메모리 : 18336 KB

시간 : 104 ms

코드길이 : 1468 B

 

[내 코드]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Solution {
	static int dump;
	static int[] box;
	
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		StringTokenizer st;
		
		for(int t = 1; t <= 10; t++) {
			sb.append('#').append(t).append(' ');
			dump = Integer.parseInt(br.readLine());
			box = new int[100];
			st = new StringTokenizer(br.readLine());
			for(int i = 0; i < 100; i++) {
				box[i] = Integer.parseInt(st.nextToken());
			}
			Arrays.sort(box);
			
			int height = box[99] - box[0];
			if(height == 0 || height == 1) {
				sb.append(height).append('\n');
				continue;
			}
			
			while(dump-- > 0) {
				box[0]++;
				box[99]--;
				sortBox();
				height = box[99] - box[0];
				if(height == 0 || height == 1) {
					break;
				}
			}
			sb.append(height).append('\n');
		}
		System.out.print(sb);
	}
	static void sortBox() {
		for(int i = 99; i > 0; i--) {
			if(box[i] < box[i - 1]) {
				int temp = box[i];
				box[i] = box[i - 1];
				box[i - 1] = temp;
			}
			else
				break;
		}
		for(int i = 0; i < 99; i++) {
			if(box[i] > box[i + 1]) {
				int temp = box[i];
				box[i] = box[i + 1];
				box[i + 1] = temp;
			}
			else
				break;
		}
		return;
	}

}
반응형

'SSAFY > SWEA' 카테고리의 다른 글

[SWEA] 1493 : 수의 새로운 연산  (0) 2023.05.08
[SWEA] 1209 : Sum  (0) 2023.05.08
[SWEA] 3499 : 퍼펙트셔플  (0) 2023.05.07
[SWEA] 9658 : 유효숫자 표기  (0) 2023.05.07
[SWEA] 3431 : 준환이의 운동관리  (0) 2023.05.07