반응형

programmers.co.kr/learn/courses/30/lessons/42583

 

코딩테스트 연습 - 다리를 지나는 트럭

트럭 여러 대가 강을 가로지르는 일 차선 다리를 정해진 순으로 건너려 합니다. 모든 트럭이 다리를 건너려면 최소 몇 초가 걸리는지 알아내야 합니다. 트럭은 1초에 1만큼 움직이며, 다리 길이

programmers.co.kr

위 문제를 푸는데 가장 어려웠던 부분은 다리에 하중 예외처리와 이동 여부 처리의 부분이었다. 

다른 많은 예제를 통해서 간신히 풀게 되었다.

import java.util.LinkedList;
import java.util.Queue;

public class Solution_42583 {

// 테스트 케이스
//   solution(2, 10, new int[] {7,4,5,6}); //8
//   solution(3, 10, new int[] {7,4,5,6}); //11
//   solution(2, 10, new int[] {4,5,4,6}); //6
//   solution(2, 10, new int[] {7,4,5,4,6}); //8
//   solution(100, 100, new int[] {10}); //101
//   solution(100, 100, new int[] {10,10,10,10,10,10,10,10,10,10}); //110
//   solution(1, 2, new int[] {1,1,1}); //4
//   solution(1, 1, new int[] {1,1,1}); //4
//   solution(4, 2, new int[] {1,1,1,1}); //10
//   solution(3, 3, new int[] {1,1,1}); //6
//   solution(3, 1, new int[] {1,1,1}); //10
//   solution(3, 1, new int[] {1,1,1,1,1}); //16
//   solution(5, 5, new int[] {1,1,1,1,1,2,2}); //14
//   solution(7, 7, new int[] {1,1,1,1,1,3,3}); //18
//   solution(5, 5, new int[] {1,1,1,1,1,2,2,2,2}); //19
//   solution(5, 5, new int[] {2,2,2,2,1,1,1,1,1}); //19

	public static void main(String[] args) {

		int bridgeLength = 3;
		int weight = 10;
		int[] truckWeight = { 7,4,5,6 };
		int result = solution(bridgeLength, weight, truckWeight);

		System.out.println(result);

	}

	public static int solution(int bridge_length, int weight, int[] truck_weights) {

		Queue<TruckVO> passingTrucks = new LinkedList<TruckVO>();

		int shiftCount = 0, idx = 0, time = 0;

		while (idx < truck_weights.length) {

			if (!passingTrucks.isEmpty() && time == passingTrucks.peek().getMoveCnt()) {

				// 다리를 지남
				TruckVO vo = passingTrucks.poll(); 

				// 트럭이 나가서 하중 증가
				weight += vo.getWeight();
			}

			// 남은 하중 보다 트럭 하중이 작으면
			if (weight >= truck_weights[idx]) {
				// 배열 {진입한 트럭의 무게, 도달시간}
				TruckVO vo = new TruckVO();
				vo.setWeight(truck_weights[idx]);
				vo.setMoveCnt(time + bridge_length);
				passingTrucks.add(vo);
				// 트럭이 올라가고 남은 하중
				weight -= truck_weights[idx++];
			}

			time++;
		}
		
		shiftCount = time + bridge_length;
		return shiftCount;
	}

}

class TruckVO {
	int weight;
	int moveCnt;

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	public int getMoveCnt() {
		return moveCnt;
	}

	public void setMoveCnt(int moveCnt) {
		this.moveCnt = moveCnt;
	}
}
728x90

'[개발관련] > 코테준비' 카테고리의 다른 글

[프로그래머스] 프린터  (0) 2021.05.07
[백준] DFS와 BFS  (0) 2021.05.01
[프로그래머스] 주식 가격  (0) 2021.04.29
[프로그래머스] 타겟 넘버  (0) 2021.04.25
[프로그래머스] 소수 찾기  (0) 2021.04.20
반응형

programmers.co.kr/learn/courses/30/lessons/42839

 

코딩테스트 연습 - 소수 찾기

한자리 숫자가 적힌 종이 조각이 흩어져있습니다. 흩어진 종이 조각을 붙여 소수를 몇 개 만들 수 있는지 알아내려 합니다. 각 종이 조각에 적힌 숫자가 적힌 문자열 numbers가 주어졌을 때, 종이

programmers.co.kr

소수 찾기의 핵심은 숫자를 입력했을 때 나올 수 있는 조합의 수를 찾아 내는 것이다.

보통 이러한 경우의 수를 추출 해야할 때는 완전 탐색 알고리즘인 DFS를 많이 쓰게된다. 

DFS(Depth First Search)로 깊이 우선 탐색이라고 한다. 

노드와 간선으로 구성된 트리이다. 

여기서 DFS는 트리를 탐색하는 방법 중에 하나인데 이와 상반대는 개념은 BFS가 있다. 

앞으로 코테를 볼경우 이와 같이 경우의 수를 추출해야하는 경우에는 DFS가 떠오르고 코드를 작성할 수 있는 연습을 많이 해야할 듯 하다. 

특히, DFS는 스택을 활용하기 때문에 점화식과 탈출식을 잘 생각해야 할 듯하다. 

DFS와 관련된 많은 문제를 통해서 문제에 익숙해지는 노력을 해야겠다.

다음은 내가 풀었던 방식이다. 

[해결]

1) 입력된 문자열이 만들수 있는 경우의 수를 구한다.

저의 경우는 dfs를 이용해서 발생할 수 있는 경우의 수를 뽑아냈습니다.

2) 소수 여부를 판단해서 count를 한다.

package com.ji.study;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class FindPrimeTest {

	@Test
	void test() {
		
		Solution_42839 test = new Solution_42839();
		
		assertEquals(3, test.solution("17"));
		assertEquals(2, test.solution("011"));
		assertEquals(1, test.solution("2"));
		assertEquals(12, test.solution("7843"));
		assertEquals(0, test.solution("9999999"));
		assertEquals(1336, test.solution("1276543"));
		
	}

}
package com.ji.study;

import java.util.ArrayList;
import java.util.List;

public class FindPrime {

	/**
	 * 소수는 1과 자기 자신으로만 나누어지는 수임
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		String input = "1276543";
		System.out.println(solution(input));

	}

	public static int solution(String numbers) {
		int answer = 0;

		// 숫자 조합 저장 리스트
		List<Integer> makeNumber = new ArrayList<Integer>();
		List<Integer> onlyNumber = new ArrayList<Integer>();

		// input 숫자 초기화
		char[] numToArr = numbers.toCharArray();
		Integer len = numToArr.length;

		// dfs를 이용한 입력된 숫자가 조합할 수 있는 모든 경우의 수 저장
		dfs(0, initNodeValue(numToArr, len), new int[len], new boolean[len], makeNumber);

		// 중복 숫자 제거
		for (Integer num : makeNumber) {
			if (!onlyNumber.contains(num))
				onlyNumber.add(num);
		}

		// 소수인 경우
		for (Integer num : onlyNumber) {
			if (isPrimeNumber(num))
				answer++;
		}

		return answer;
	}

	public static int[] initNodeValue(char[] numToArr, Integer numLength) {
		int[] result = new int[numLength];
		for (int i = 0; i < numLength; i++) {
			String numStr = String.valueOf(numToArr[i]);
			result[i] = Integer.valueOf(numStr);
		}
		return result;
	}

	public static void dfs(int n, int[] arr, int[] result, boolean[] visit, List<Integer> makeNumber) {
		if (n == arr.length) {// 입력된 숫자 길이 만큼의 경우의 숫자들

			// 모든 경우의 숫자들에서 한자리씩 감소하는 경우의 숫자 예외 처리
			int resultLength = result.length;
			while (resultLength > 0) {
				String numStr = "";
				for (int i = 0; i < resultLength; i++) {
					numStr += result[i];
				}

				if (numStr != "")
					makeNumber.add(Integer.valueOf(numStr));

				resultLength--;
			}

		} else {

			for (int i = 0; i < arr.length; i++) {
				if (!visit[i]) {
					visit[i] = true;
					result[n] = arr[i];
					dfs(n + 1, arr, result, visit, makeNumber);
					visit[i] = false;
				}
			}

		}
	}

	public static boolean isPrimeNumber(int number) {
		boolean result = true;

		if (number == 1 || number == 0)
			return false;

		for (int i = 2; i < number; i++) {
			if (number % i == 0) { // 나누어 떨어지는 지 확인
				result = false;
				break;
			}
		}
		return result;
	}

}
728x90

'[개발관련] > 코테준비' 카테고리의 다른 글

[프로그래머스] 프린터  (0) 2021.05.07
[백준] DFS와 BFS  (0) 2021.05.01
[프로그래머스] 주식 가격  (0) 2021.04.29
[프로그래머스] 타겟 넘버  (0) 2021.04.25
[프로그래머스] 다리를 지나는 트럭  (0) 2021.04.20

+ Recent posts