IT 개발자_S

[프로그래머스] 탑 본문

IT/알고리즘_JAVA

[프로그래머스] 탑

Soso12 2020. 5. 31. 23:22
반응형

1. 스택을 이용해서 풀기

2. 맨위부터 숫자를 빼냇을때 다음 숫자가 크면 answer insert 아니면 0으로 

## 처음 스택 1개를 가지고 이용했을때, 답을 찾지 못햇을경우 로직 처리가 에러 

import java.util.Stack;
class Solution {
    public int[] solution(int[] heights) {
        

        int[] answer = new int [heights.length];
        
        Stack<Integer> st = new Stack<Integer>();
        
        int max = Integer.MIN_VALUE;
        int cnt =heights.length;
        int length = heights.length;
        int loop = 0;
        // Stack
        for (int i =0; i < heights.length ; i++){
            
            st.push(heights[i]);
            answer[i] =0;
        }
        
        int p = st.pop(); //첫번째  
        max = p; 
        while (!st.isEmpty()){
            
            p = st.pop();
            cnt -=1;
            loop +=1;
            System.out.println("Pop : " + p);
            if(max < p){
                max = p;
                System.out.println("loop : " + loop + " le " + length + " cnt "  + cnt);
                for (int i =1; i <loop+1 ; i++){
                    length = length -1;
                    System.out.println("i : " + i + " le " + length );
                    answer[length] =cnt;
                }
                loop =0;
 
            }
  

        }
      System.out.print ("@@result  ");
     for (int i =0; i < heights.length ; i++){
            
          System.out.print ( answer[i]);
   
        }

        return answer;
    }
}

 

## 스택 2개를 이용해서 해결

import java.util.Stack;
class Solution {
    public int[] solution(int[] heights) {
        

        int[] answer = new int [heights.length];
        
        Stack<Integer> st = new Stack<Integer>();

        int max = Integer.MIN_VALUE;
        int cnt =heights.length ;
        int length = heights.length;
        int result =0;
        int loop = 0;
        // Stack
        for (int i =0; i < heights.length ; i++){
            
            st.push(heights[i]);
            answer[i] =0;
        }
        
        while(!st.isEmpty()){
            int p = st.pop(); // 4
            int tempP=0;
            length -=1;
            cnt -=1; // 5->4
            Stack<Integer> tempSt = new Stack<Integer>();
            System.out.println ( "p " + p + " loop " + loop + " cnt " +cnt + " length  " + length);
            for(int i =0; i< cnt;  i++){
              System.out.println ( "i " + heights[i] );
              tempSt.push(heights[i]);
           
            }
            loop =0;
            result =0;
            
            while(!tempSt.isEmpty()){
                tempP = tempSt.pop();
                loop +=1;
                System.out.println ( "tempP " + tempP + " loop " + loop + " cnt " +cnt + " length  " + length);
                if(p < tempP){
                    result = cnt- loop +1;
                    System.out.println ( "result " + result );
                    answer[length] = result;
                    loop =0;
                    result =0;
                    break;
                }     
                
            }
            

            
        }
        
        
        System.out.print ("@@result  ");
        for (int i =0; i < heights.length ; i++){

              System.out.print ( answer[i]);

        }

        return answer;
    }
}

 

## 다른사람의 풀이 HamYoungChan

 

import java.util.*;

class Solution {
    class Tower {
        int idx;
        int height;

        public Tower(int idx, int height) {
            this.idx = idx;
            this.height = height;
        }

        @Override
        public String toString() {
            return "idx : " + idx + " height : " + height;
        }
    }

    public int[] solution(int[] heights) {
        Stack<Tower> st = new Stack<>();
        int[] answer = new int[heights.length];

        for (int i = 0; i < heights.length; i++) {
            Tower tower = new Tower(i + 1, heights[i]);
            int receiveIdx = 0;

            while (!st.isEmpty()) {
                Tower top = st.peek();

                if (top.height > tower.height) {
                    receiveIdx = top.idx;
                    break;
                }

                st.pop();
            }

            st.push(tower);
            answer[i] = receiveIdx;
        }

        return answer;
    }
}
반응형

'IT > 알고리즘_JAVA' 카테고리의 다른 글

[프로그래머스] 가장큰수  (0) 2020.06.01
[프로그래머스] k번째수  (0) 2020.06.01
[프로그래머스] 완주하지 못한 선수  (0) 2020.05.27
[코딜리티]FrogRiverOne  (0) 2020.05.11
[코딜리티] TapeEquilibrium  (0) 2020.05.10
Comments