[태그:] java

  • 프로그래머스 디스크컨트롤러

    웹에 올린 결과는 틀림. 그러나 내가 맞다 생각한다.

    문제는 아래와 같다.

    1. 작업이 [시작 시각, 작업 길이] 2차원 배열로 들어온다.
    2. 작업이 연속될 경우, 이 안에서 작업 길이를 짧은 순으로 정렬하여 실행한다.
    3. 각 작업에 대한 기다린 시간 + 작업 시간을 계산한다.
    4. 작업 개수를 총 시간으로 나눠준다.

    여기에서 배열을 한번에 읽는다. 첫번째 작업 순서를 읽을 때, 모든 작업 순서를 결정할 수 있다. 예제 [[0, 3], [1, 9], [2, 6]] 순서에 마지막 2개를 더 들어온다고 하자. [[0, 3], [1, 9], [2, 6], [24,4], [20,7]].

    엑셀 Calc로 그림을 그리면 아래와 같다.

    작업A부터 작업E까지 읽을 때, 필요한 작업 시간(checkBar)을 정해야 한다. 이 작업 시간안에 들어오면 작업 시간을 적은 순서대로 정렬한다. 작업 시간을 넘어가면 다음 턴에 정렬해야 한다.

    입력을 읽을 때, 24초에 들어온 작업과 20초에 들어온 작업을 결정할 수 있다. 다음과 같이 조정할 수 있다. 24초에 들어온 작업을 20초에 먼저 실행하면 작업D, 작업E를 하는데 총 11초 걸린다.

    만약 들어온 순서대로 하면 작업D, 작업E를 14초에 한다. jobs을 읽을 때 뒤쪽 배열에 시간이 빠른 작업을 어떻게 처리할까? 이런 경우를 보면 jobs을 순서대로 읽으면 안된다. 모두 읽어서 빠른 순서대로 정리해야 한다.

    하드 디스크가 놀고 있을 때 먼저 들어온 순서대로 한다는 제한이 있다. jobs[][]이 대기열이다. 현재 시각이 0초이고, jobs[0] = [1,2], jobs[1] = [2,5], jobs[2] = [0,4]이라 하자. 위 제한에 따르면 jobs[2]는 조정할 수 없어 실핼할 수 없다. 문제 조건에 jobs[i]는 항상 시작 시각이 늘어난다는 조건을 추가해야 한다. 이 조건이 있었으면 내가 이런 개고생 안했을 수 있었다. 웹은 틀렸다지만 난 맞았다고 정당화시키고 끝낸다.

    문제는 이 코드를 웹에 올리면 runtime error로 검증할 수 없다. 우선순위 큐 문제에 너무 많은 시간을 보냈다.

    
    import java.util.Collections;
    import java.util.PriorityQueue;
    
    public class Main {
    	public static void main(String[] args) {
    
    		//		PriorityQueue<Student> ptrTestQ = new PriorityQueue<>();
    
    		//		while (!sol.getQ().isEmpty())
    		//			System.out.println(ptrTestQ.remove());
    
    		// 시작..
    		int[][] jobs = new int[5][2];
    		jobs[0][0] = 0;
    		jobs[0][1] = 3;
    		jobs[1][0] = 1;
    		jobs[1][1] = 9;
    		jobs[2][0] = 2;
    		jobs[2][1] = 6;
    		jobs[3][0] = 24;
    		jobs[3][1] = 4;
    		jobs[4][0] = 20;
    		jobs[4][1] = 7;
    
    		/*
    		 * System.out.println("Jobs type은"+jobs.getClass().getTypeName());
    		 * 
    		 * 
    		 */
    		Solution sol = new Solution();
    		int answer = sol.solution(jobs);
    
    		System.out.println("여기");
    		System.out.println("답은  " + answer);
    
    		/*
    		 * while (!sol.getQue().isEmpty()) System.out.println("sol.getQue내용은 " +
    		 * sol.getQue().poll()); while (!sol.getQueOffset().isEmpty())
    		 * System.out.println("sol.getQueOffset내용은 " + sol.getQueOffset().poll());
    		 */
    
    	}// void main
    
    } // Main
    
    class Solution {
    
    	public int solution(int[][] jobs) {
    
    		// 요청 대기시간이 보정된 큐.
    		PriorityQueue<Job> priqueJobsReversed = new PriorityQueue<>();
    		PriorityQueue<Job> priqueCheckReversed = new PriorityQueue<>();
    
    		// jobs을 class로 입력
    		for (int i = 0; i < jobs.length; i++) {
    			Job tmpJob = new Job(jobs[i][0], jobs[i][1]);
    			// timeStamp 기준 정렬
    			// setFlag와 세트로 같이 움직여야 됨.
    			priqueJobsReversed.add(tmpJob);
    		}
    		/*
    		 * while (!priqueJobsReversed.isEmpty()) { System.out.println("timeStamp:" +
    		 * priqueJobsReversed.peek().timeStamp + ", Duration: " +
    		 * priqueJobsReversed.peek().duration); priqueJobsReversed.poll(); }
    		 */
    
    		PriorityQueue<Job> priqueJobs = new PriorityQueue<>(priqueJobsReversed.size(), Collections.reverseOrder());
    		priqueJobs.addAll(priqueJobsReversed);
    
    		// 복사..
    		PriorityQueue<Job> priqueJobsTmp = new PriorityQueue<>(priqueJobs);
    
    		boolean overlapped = false;
    		int startTime = 0;
    		int endTime = 0;
    		int preDuration = 0;
    		int preTimeStamp = 0;
    		int PolledDuration = 0;
    		int PolledTimeStamp = 0;
    		while (!priqueJobsTmp.isEmpty()) {
    			if (!overlapped) {
    				startTime = priqueJobsTmp.peek().timeStamp;
    				endTime = startTime + priqueJobsTmp.peek().duration;
    
    			}
    			preDuration = priqueJobsTmp.peek().duration;
    			preTimeStamp = priqueJobsTmp.peek().timeStamp;
    			priqueJobsTmp.poll();
    			if (!priqueJobsTmp.isEmpty()) {
    				PolledDuration = priqueJobsTmp.peek().duration;
    				PolledTimeStamp = priqueJobsTmp.peek().timeStamp;
    			} //if
    			else {
    				PolledTimeStamp = 0;
    				Job checkBar = new Job(startTime, endTime);
    				// 시간순으로 정렬
    				priqueCheckReversed.add(checkBar);
    
    			}
    			if (startTime + preDuration >= PolledTimeStamp) {
    				// overlapped를 true로 바꾸고, endTime을 연장
    				overlapped = true;
    				endTime = endTime + PolledDuration;
    
    			} //if
    			else {
    				// endTime 연장하지 않음.
    				// overlapped flag를 다시 false로 변경.
    				overlapped = false;
    				// 여기에서 priorityQueue에 넣음.
    				Job checkBar = new Job(startTime, endTime-1);
    				// 시간순으로 정렬
    				priqueCheckReversed.add(checkBar);
    
    			} // else
    				//			System.out.println("현재 startTime" + startTime);
    				//			System.out.println("현재 endTime" + endTime);
    				//			Object[] testArry = priqueCheckReversed.toArray();
    
    		} // while
    		/*
    		 * System.out.println("*****여기 확인*******"); while (!priqueJobsTmp.isEmpty())
    		 * System.out.println(priqueJobsTmp.poll());
    		 */
    
    		// 거꾸로 만든 큐 순서를 뒤집음.
    
    		PriorityQueue<Job> priqueCheck = new PriorityQueue<>(priqueCheckReversed.size(), Collections.reverseOrder());
    		priqueCheck.addAll(priqueCheckReversed);
    		//		System.out.println("큐 크기" + priqueCheckReversed.size());
    
    		int answer = 0;
    
    		PriorityQueue<Job> priqueJobsOder = new PriorityQueue<Job>();
    
    		int i = 1;
    		while (!priqueCheck.isEmpty()) {
    			// 시작시각 확인.
    			int startTimeCheck = priqueCheck.peek().timeStamp;
    			int endTimeCheck = priqueCheck.peek().duration;
    			int presentTime = priqueCheck.peek().timeStamp;
    			//check구간의 Jobs을 꺼낸 후, 우선순위 변경하여 다시 넣어줌.
    			while (!priqueJobs.isEmpty()) {
    				int tmpTimeStamp = priqueJobs.peek().timeStamp;
    				int tmpDuration = priqueJobs.peek().duration;
    				if (startTimeCheck <= tmpTimeStamp && tmpTimeStamp <= endTimeCheck) {
    					Job orderJob = new Job(tmpTimeStamp, tmpDuration);
    					orderJob.setFlag(true);
    					priqueJobsOder.add(orderJob);
    					priqueJobs.poll();
    				} // if
    				else
    					break;
    
    			} //while priqueJobs
    
    			//한턴 끝난 후 reverse order로 변경.
    			PriorityQueue<Job> priqueJobsOderNext = new PriorityQueue<>(priqueJobsOder.size(),
    					Collections.reverseOrder());
    			priqueJobsOderNext.addAll(priqueJobsOder);
    
    			while (!priqueJobsOderNext.isEmpty()) {
    
    				int delay = presentTime - priqueJobsOderNext.peek().timeStamp;
    				int tmpAnswer = delay + priqueJobsOderNext.peek().duration;
    				answer = answer + tmpAnswer;
    				System.out.println("answer = " + answer);
    				System.out.println("현재시각 = " + presentTime);
    				System.out.println("duration = " + priqueJobsOderNext.peek().duration);
    				System.out.println("delay = " + delay);
    
    				i++;
    				presentTime = presentTime + priqueJobsOderNext.peek().duration;
    				priqueJobsOderNext.poll();
    				priqueJobsOder.poll();
    			} //while (priqueHobsOrderNext)
    
    			priqueCheck.poll();
    
    		} //while priqueCheck
    
    		answer = answer / (i - 1);
    		return answer;
    	}// int solutions(int[][] jobs)
    
    }
    
    class Job implements Comparable<Job> {
    	int duration;
    	int timeStamp;
    	boolean flag;
    
    	// 기본 생성자.
    	Job() {
    		this.duration = 0;
    		this.timeStamp = 0;
    		this.flag = false;
    	}
    
    	// 생성자 작성.
    	Job(int timeStamp, int duration) {
    		this.timeStamp = timeStamp;
    		this.duration = duration;
    		this.flag = false;
    
    	}
    
    	@Override
    	public int compareTo(Job arg0) {
    		// TODO Auto-generated method stub
    		if (!this.flag)
    			// TimeStamp가 큰 순서대로 poll;
    			return this.timeStamp < arg0.timeStamp ? 1 : -1;
    		else
    			// Duration이 큰 순서대로 poll
    			return this.duration < arg0.duration ? 1 : -1;
    
    	}
    
    	@Override
    	public String toString() { // TODO Auto-generated method stub
    		return "시각" + this.timeStamp + ", 기간" + this.duration;
    	}
    
    	public void setFlag(boolean flag) {
    		this.flag = flag;
    
    	}
    
    } // Job
  • 프로그래머스 카펫

    점점 속도가 붙고 있다. 무식한게 딱 내스타일이다.

    class Solution {
        public int[] solution(int brown, int red) {
            int[] answer = new int[2];
            answer[0]=1;answer[1]=1;
            for(int i=3;i<10000;i++)
                for (int j=3; j<10000;j++)
                    if(i*j==(brown+red)){
                        if((i-2)*(j-2)==red){
                            System.out.println("i is "+i);
                            System.out.println("j is "+j);
                            answer[0]=i;
                            answer[1]=j;
                            break;
                        }
                        
                    }
            return answer;
        }
    }
  • 프로그래머스 네트워크

    프로그래머스 네트워크

    DFS인지 BFS인지 헷갈린다. 배우면서 하려니 힘드네. Level3까지만 해야겠다. 남이 작성한 코드 그대로 쓰니 개판되는 듯 하고. 한 문제당 30분이라는데 도저히 못 풀듯.

    20년전 배운 linked list, stack, queue를 다시 보니 내가 참 힘들게 살았다. 애고 모가지야.

    
    import java.util.Stack;
    import java.util.Arrays;
    import java.util.TreeSet;
    
    public class Main {
    	public static void main(String[] args) {
    		Solution sol = new Solution();
    		int[][] nodeMap = new int[3][3];
    		nodeMap[0][0] = 1;
    		nodeMap[0][1] = 1;
    		nodeMap[0][2] = 0;
    		nodeMap[1][0] = 1;
    		nodeMap[1][1] = 1;
    		nodeMap[1][2] = 0;
    		nodeMap[2][0] = 0;
    		nodeMap[2][1] = 0;
    		nodeMap[2][2] = 1;
    
    		sol.solution(3, nodeMap);
    
    		System.out.println("여기");
    	}// void main
    
    } // Main
    
    class Solution {
    	public int solution(int n, int[][] nodeMap) {
    
    		/*
    		 * boolean[] nodeVisited = new boolean[3]; nodeVisited[0] = false;
    		 * nodeVisited[1] = false; nodeVisited[2] = false;
    		 * 
    		 * int[] nodeVisitedInd = new int[3]; nodeVisitedInd[0] = 0; nodeVisitedInd[1] =
    		 * 0; nodeVisitedInd[2] = 0; //테스트를 위해 임시로.. //기존 nodeMap을 보고 만들어줘야 됨
    		 */
    		int[] nodeVisitedInd = new int[nodeMap.length];
    		for (int i = 0; i < nodeMap.length; i++)
    			nodeVisitedInd[i] = 0;
    
    		// 중복을 삭제하기 위해 만듦.
    		TreeSet trimedNodeVisitedInd = new TreeSet();
    
    		int answer = 0;
    		boolean flag = false;
    		int visitIndex = 1;
    		for (int i = 0; i < nodeMap.length; i++) {
    
    //		dfs(nodeMap, nodeVisited, 0, flag);
    			System.out.println("**********************");
    			System.out.println("제일 바깥쪽 외부 루프");
    			System.out.println("visitIndex = " + visitIndex);
    			System.out.println("**********************");
    
    			dfs(nodeMap, nodeVisitedInd, i, flag, visitIndex);
    
    			visitIndex++;
    
    		}
    
    		System.out.println("nodeVisitedInd는" + nodeVisitedInd[0] + nodeVisitedInd[1] + nodeVisitedInd[2]);
    		for (int i = 0; i < nodeVisitedInd.length; i++) {
    			trimedNodeVisitedInd.add(nodeVisitedInd[i]);
    		}
    		System.out.println("TreeSet 크기는" + trimedNodeVisitedInd.size());
    		answer = trimedNodeVisitedInd.size();
    
    		return answer;
    	} // solutions
    
    	public static void dfs(int[][] nodeMap, boolean[] nodeVisited, int startNode, boolean flag) {
    		Stack<Integer> dfsStack = new Stack<>();
    		dfsStack.push(startNode);
    		nodeVisited[startNode] = true;
    		System.out.println("node " + startNode + " is visited");
    
    		while (!dfsStack.empty()) {
    			flag = false;
    			int tempNode = dfsStack.peek();
    			for (int i = 0; i < nodeMap.length; i++) {
    				System.out.println(tempNode + "에서 " + i + " 노드로 검색 시작");
    				// 왼쪽에서 오른쪽으로 훑음..1이면 링크가 있음.
    				if (nodeMap[tempNode][i] == 1 && !nodeVisited[i]) {
    					dfsStack.push(i);
    					nodeVisited[i] = true;
    					System.out.println("node " + nodeMap[tempNode][i] + " is visited");
    					System.out.println("dsfStack has " + Arrays.toString(dfsStack.toArray()));
    					flag = true;
    					break;
    
    				}
    
    				System.out.println(tempNode + "노드 에서 " + i + "확인");
    				System.out.println("길이 없거나 이미 방문하여 생략");
    				System.out.println("노드 " + i + "방문 결과는" + nodeVisited[i]);
    
    			} // for loop
    			if (!flag) {
    				dfsStack.pop();
    				System.out.println("pop 실행됨");
    
    			} // if(!flag)
    
    		} // while
    
    	}// dsf..
    
    	public static void dfs(int[][] nodeMap, int[] nodeVisited, int startNode, boolean flag, int visitIndex) {
    		Stack<Integer> dfsStack = new Stack<>();
    		// for 루프를 돌릴 경우, 재방문을 방지.
    		// 조건에 따라 Stack에 push.
    		if (nodeVisited[startNode] == 0) {
    			dfsStack.push(startNode);
    			nodeVisited[startNode] = visitIndex;
    			System.out.println("\t****노드 방문****");
    			System.out.println("\tnode " + startNode + " is visited");
    			System.out.println("\t****노드 방문****");
    		}
    
    		while (!dfsStack.empty()) {
    			flag = false;
    			int tempNode = dfsStack.peek();
    			for (int i = 0; i < nodeMap.length; i++) {
    				System.out.println(tempNode + "에서 " + i + " 노드로 검색 시작");
    				// 왼쪽에서 오른쪽으로 훑음..1이면 링크가 있음.
    				if (nodeMap[tempNode][i] == 1 && nodeVisited[i] == 0) {
    					dfsStack.push(i);
    					nodeVisited[i] = visitIndex;
    					System.out.println("\t****노드 방문****");
    					System.out.println("\tnode " + i + " is visited");
    					System.out.println("\t****노드 방문****");
    					System.out.println("node " + nodeMap[tempNode][i] + " is visited");
    					System.out.println("dsfStack has " + Arrays.toString(dfsStack.toArray()));
    					System.out.println("nodeVisited[" + i + "]는 " + nodeVisited[i]);
    					flag = true;
    					break;
    
    				}
    
    				System.out.println(tempNode + "노드 에서 " + i + "확인");
    				System.out.println("길이 없거나 이미 방문하여 생략");
    				System.out.println("노드 " + i + "방문 결과는" + nodeVisited[i]);
    
    			} // for loop
    			if (!flag) {
    				dfsStack.pop();
    				System.out.println("pop 실행됨");
    
    			} // if(!flag)
    
    		} // while
    
    	}// dsf..
    
    }
  • 프로그래머스 주식가격

    stack을 만들어서 바로 int[]로 변경하려 했으나 안되었다.다름 사람의 풀이 역시 바로 바꾸지 않았다. stack으로 넣은 다음 하나씩 읽어 새로 만든 int[]에 집어 넣었다.

    
    public class Main {
    	public static void main(String[] args) {
    		Solution sol = new Solution();
    		int[] prices = new int[5];
    		prices[0] = 1;
    		prices[1] = 2;
    		prices[2] = 3;
    		prices[3] = 2;
    		prices[4] = 3;
    
    		sol.solution(prices);
    
    		System.out.println("여기");
    	}// void main
    
    } // Main
    
    class Solution {
    	public int[] solution(int[] prices) {
    		int[] answer = new int[prices.length];
    
    		int targetTime = 0;
    		for (int i = 0; i < prices.length-1; i++) {
    			// 현재 목표값.prices[i]
    			targetTime = 0;
    //			System.out.println("현재 i는" + i + ",값은" + prices[i]);
    
    			for (int j = 1; j < prices.length - i; j++) {
    //				System.out.println("현재 i+j는" + (i + j) + ",값은" + prices[i + j]);
    
    				if (prices[i] > prices[i + j]) {
    					targetTime = targetTime + 1;
    					break;
    				} else
    					targetTime = targetTime + 1;
    			}
    
    //			System.out.println("목표값=" + targetTime);
    			answer[i] = targetTime;
    
    		}
    //		for (int i = 0; i < prices.length; i++)
    //			System.out.println("answer=" + answer[i]);
    
    		return answer;
    	} // solutions
    }
  • 프로그래머스 쇠막대기

    문제를 보고 stack을 적용해야 생각해야 상식이다. 그러나 이 문제를 stack을 쓰기위해 만들었다.

    
    import java.util.Stack;
    
    public class Main {
    	public static void main(String[] args) {
    		Solution sol = new Solution();
    		String arrangement = "(())";
    
    		sol.solution(arrangement);
    
    		System.out.println("여기");
    	}// void main
    
    } // Main
    
    class Solution {
    	public int solution(String arrangement) {
    		Stack<String> stackOfWork = new Stack<>();
    		String pushChar = "";
    		int answer = 0;
    		boolean trigUp = false;
    		boolean trigDown = false;
    		// arrangement의 처음을 push.
    		for (int i = 0; i < arrangement.length(); i++) {
    			// 현재 넣을 문자 확인
    			pushChar = String.valueOf(arrangement.charAt(i));
    //			System.out.println(pushChar);
    
    			if (pushChar.equals("(")) {
    				// 레이저 발사 확인
    				// 다음턴에 )가 있으면 레이저 동작
    				stackOfWork.push(pushChar);
    				trigUp = true;
    				trigDown = false;
    
    			} else if (pushChar.equals(")")) {
    				stackOfWork.pop();
    				trigDown = true;
    				if (trigUp == true && trigDown == true)
    					answer = answer + stackOfWork.size();
    				else
    					answer = answer + 1;
    				System.out.println(stackOfWork);
    				System.out.println(answer);
    				trigUp = false;
    
    			}
    
    		}
    //		stackOfWork.push(arrangement.substring(0,1));
    //		System.out.println(arrangement.substring(0,1));
    
    		System.out.println(answer);
    		return answer;
    	} // solutions
    }