코딩 스터디

momo study(23.01.16)

hunm719 2023. 1. 16. 20:51

1. 스택 문제 풀이

 

조건

  1. 새로운 페이지로 접속할 경우 prev 스택에 원래 있던 페이지를 넣고 next 스택을 비웁니다.
  2. 뒤로 가기 버튼을 누를 경우 원래 있던 페이지를 next 스택에 넣고 prev 스택의 top에 있는 페이지로 이동한 뒤 prev 스택의 값을 pop 합니다.
  3. 앞으로 가기 버튼을 누를 경우 원래 있던 페이지를 prev 스택에 넣고 next 스택의 top에 있는 페이지로 이동한 뒤 next 스택의 값을 pop 합니다.
  4. 브라우저에서 뒤로 가기, 앞으로 가기 버튼이 비활성화일 경우(클릭이 되지 않을 경우)에는 스택에 push 하지 않습니다.

입력

인자 1: actions(String 타입을 요소로 갖는 브라우저에서 행동한 순서를 차례대로 나열한 배열)

인자 2: start(String 타입의 시작 페이지를 나타내는 현재 접속해 있는 대문자 알파벳)

 

출력: (Stack타입을 인자로 가지는 ArrayList 타입을 리턴해야 합니다.

 

주의사항

  • 앞으로가기나 뒤로가기가 아닌 경우에 새로운 페이지로 취급을 한다
  • 뒤로 가기 버튼을 누른 행동은 "-1"로 표기합니다.
  • 앞으로 가기 버튼을 누른 행동은 "1"로 표기합니다.
  • 다음 방문할 페이지는 항상 현재 페이지와 다른 페이지로 접속합니다.
  • 방문한 페이지의 개수는 100개 이하입니다.
  • 반환되는 출력값 ArrayList의 첫 번째 요소 prev 스택, 두번째 요소 current 스택, 세 번째 요소 next 스택을 사용해야 합니다.

입출력 예시

1
2
3
4
5
6
7
8
9
10
11
String[] actions = new String[]{"B""C""-1""D""A""-1""1""-1""-1"};
String start = "A";
ArrayList<Stack> output = browserStack(actions, start);
 
System.out.println(output); // [["A"], ["B"], ["A", "D"]]
 
String[] actions2 = new String[]{"B""-1""B""A""C""-1""-1""D""-1""1""E""-1""-1""1"};
String start2 = "A";
ArrayList<Stack> output2 = browserStack(actions2, start2);
 
System.out.println(output2); // [["A", "B"], ["D"], ["E"]]
cs

 

문제 풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.*;
 
public class Solution { 
    public ArrayList<Stack> browserStack(String[] actions, String start) {
    Stack<String> prevStack = new Stack<>();
    Stack<String> nextStack = new Stack<>();
    Stack<String> current = new Stack<>();
    ArrayList<Stack> result = new ArrayList<>();
    // TODO:
    
    current.add(start);
    //2.prev스택이나 next스택이 clear라면
    //2-1.스택에 push하지 않는다..?
    //if (prevStack.size & nextStack.size == 0){
      
    //}
    for(int i = 0; i < actions.length; i++){
    //3.-1을 입력받은 경우
    //3-1.current스택 요소를 next스택으로 push + current 가장 나중 요소 삭제
    //3-2.prev스택 마지막 요소를 current스택으로 push
    //3-3.push한 prev스택 마지막 요소를 pop
    if (actions[i].equals("-1"&& !prevStack.empty()){           //-1이 입력되고 && prevStack이 비어있지 않아야함
      nextStack.push(current.pop());
      current.push(prevStack.pop());
    }
 
    // 4.1을 입력받은 경우
    // 4-1.current스택요소를 prev스택으로 push
    // 4-2.current스택 claer하고
    // 4-3.next스택 마지막 요소를 current스택으로 push
    // 4-4.push한 next스택 마지막 요소를 pop
    else if (actions[i].equals("1"&& !nextStack.empty()){       //1이 입력되고 and nextStack이 비어있지 않아야함
      prevStack.push(current.pop());
      current.push(nextStack.pop());
    }
    else if ((actions[i].equals("1"|| actions[i].equals("-1")) && (prevStack.empty() || nextStack.empty())){  //1이나 -1이 입력됐고 and prev나 next가 비어있을경우 아무것도 하지마라
      
     }
    //1.새로운 알파벳이 입력될 경우
    //1-1.prev스택에 그 알파벳을 push하고
    //1-2.next스택을 clear
    else {prevStack.push(current.pop());
    current.push(actions[i]);
    nextStack.clear();
    }
    }
    //최종적으로 prev current next를 리턴
    result.add(prevStack);
    result.add(current);
    result.add(nextStack);
    return result;
    } 
}
cs

 

'코딩 스터디' 카테고리의 다른 글

momo study(23.01.19)  (1) 2023.01.19
momo study(23.01.17)  (0) 2023.01.17
momo study(23.01.15)  (1) 2023.01.15
momo study(23.01.13)  (0) 2023.01.13
momo study(23.01.12)  (0) 2023.01.12