[태그:] programmers

  • 프로그래머스 카펫

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

    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
    }
  • 프로그래머스 위장

    아..지겹다. 개념이 없으니 삽질하는 시간은 많아지고..

    package programmers5;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Set;
    
    public class Main {
    	public static void main(String[] args) {
    		String[][] clothes = new String[3][2];
    		clothes[0][0] = "yellow_hat";
    		clothes[0][1] = "headgear";
    
    		clothes[1][0] = "blue_sunglasses";
    		clothes[1][1] = "headgear";
    
    		clothes[2][0] = "green_turban";
    		clothes[2][1] = "headgear";
    
    		Solution sol = new Solution();
    
    		sol.solution(clothes);
    
    		System.out.println("여기");
    	}// void main
    
    } // Main
    
    class Solution {
    	public int solution(String[][] clothes) {
    
    		System.out.println("여기");
    		System.out.println(clothes.length);
    
    		HashMap<String, Integer> temp = new HashMap<>();
    		for (int i = 0; i < clothes.length; i++) {
    			// 만약 contain하면 숫자 증가.
    
    			if (temp.containsKey(clothes[i][1]))
    				temp.put(clothes[i][1],temp.get(clothes[i][1])+1);
    //			temp.computeIfPresent(clothes[i][1], arg1);
    			else
    				temp.put(clothes[i][1], 1);
    			
    		}
    
    		Set set = temp.keySet();
    		Iterator itr = set.iterator();
    		int tempNum = 1;
    
    		while (itr.hasNext()) {
    			String key = (String) itr.next();
    			System.out.println("key는" + key);
    			System.out.println("value는" + temp.get(key));
    			tempNum = tempNum * (temp.get(key) + 1);
    
    		}
    
    		System.out.println("전체숫자는" + (tempNum-1));
    //		System.out.println(temp.values());
    
    		int answer = tempNum-1;
    		return answer;
    	}
    }