본문 바로가기
회고/코딩테스트

2021 그렙 챌린지 JAVA 1번 회고

by 방구쟁이 2021. 8. 26.
728x90

시작

 2021 그렙 챌린지 1번 문제를 풀어보았다. 유효한 유저 정보를 가지고 입력을 순서대로 했을때 입력이 유효했던 경우를 찾아 내는 것으로 필자는 로그인 여부와 상품을 담은 여부를 변수로 생성하고 조건에 따라 유효성을 판별하였다.

 기본 구현 문제였고 배열을 다루는 문제였던 것 같다.

grepp 로고

나의 풀이

package test;

public class grepp1 {
  public static void main(String[] args) {
    String[] infos = {"kim password", "lee abc"};
    String[] actions = {"ADD 30", 
      "LOGIN kim abc", 
      "LOGIN lee password", 
      "LOGIN kim password", 
      "LOGIN kim password", 
      "LOGIN lee abc", 
      "ADD 30", 
      "ORDER",
      "ORDER",
      "ADD 40",
      "ADD 50"};
    solution(infos, actions);
  }
  
  public static boolean[] solution(String[] infos, String[] actions) {
    int actionsSize = actions.length;

    boolean[] answer = new boolean[actionsSize];
    boolean isLogin = false;
    boolean isAdd = false;

    for(int i = 0; i < actionsSize; i++) {
      if(isLogin == true) {
        if(actions[i].contains("ADD")) {
          answer[i] = true;
          isAdd = true;
        } else if(isAdd == true && actions[i].contains("ORDER")) {
          answer[i] = true;
          isAdd = false;
        }
      } else {
        if(actions[i].contains("LOGIN")) {
          String[] contents = actions[i].split(" ");
          String input = contents[1]+" "+contents[2];

          for(int j = 0; j < infos.length; j++) {
            if(infos[j].equals(input)) {
              answer[i] = true;
              isLogin = true;
            }
          }
        } 
      }
    }

    for(int i = 0 ; i < actionsSize; i++) {
      System.out.println(answer[i]);
    }

    return answer;
  }
}


728x90

댓글