본문 바로가기
알고리즘 & 자료구조/프로그래머스

수식 최대화

by 신재권 2023. 3. 1.
package programmers;

import java.util.ArrayList;
import java.util.List;

public class 수식_최대화 {

   // 우선순위 경우의 수 6
   // * + -
   // * - +
   // + * -
   // + - *
   // - * +
   // - + *

   public static long solution(String expression) {

      List<Long> num = new ArrayList<>();
      List<String> operator = new ArrayList<>();
      init(expression, num, operator);

      long ans = 0;
      ans = max(new ArrayList<>(num), new ArrayList<>(operator), "*", "+", "-", ans);
      ans = max(new ArrayList<>(num), new ArrayList<>(operator), "*", "-", "+", ans);
      ans = max(new ArrayList<>(num), new ArrayList<>(operator), "+", "*", "-", ans);
      ans = max(new ArrayList<>(num), new ArrayList<>(operator), "+", "-", "*", ans);
      ans = max(new ArrayList<>(num), new ArrayList<>(operator), "-", "*", "+", ans);
      ans = max(new ArrayList<>(num), new ArrayList<>(operator), "-", "+", "*", ans);

      return ans;
   }

   private static long max(List<Long> num, List<String> operator, String operator1, String operator2, String operator3,
      long ans) {
      long result = 0;
      int idx = 0;

      while (!operator.isEmpty()) {
         if (operator.contains(operator1)) {
            idx = operator.indexOf(operator1);
         } else if (operator.contains(operator2)) {
            idx = operator.indexOf(operator2);
         } else {
            idx = operator.indexOf(operator3);
         }

         if (idx == -1) {
            break;
         }

         long f = num.remove(idx);
         long e = num.remove(idx);
         String c = operator.remove(idx);

         if (c.equals("*")) {
            result = f * e;
         } else if (c.equals("+")) {
            result = f + e;
         } else {
            result = f - e;
         }
         num.add(idx, result);
      }

      return Math.max(ans, Math.abs(result));
   }

   private static void init(String expression, List<Long> num, List<String> operator) {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < expression.length(); i++) {
         char c = expression.charAt(i);
         if (c >= '0' && c <= '9') {
            sb.append(c - '0');
         } else {
            num.add(Long.parseLong(sb.toString()));
            sb = new StringBuilder();
            operator.add(String.valueOf(c));
         }
      }
      num.add(Long.parseLong(sb.toString()));
   }

   public static void main(String[] args) {
      System.out.println(solution("100-200*300-500+20"));
      System.out.println(solution("50*6-3*2"));
   }

}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

행렬 테두리 회전하기  (0) 2023.03.03
숫자 변환하기  (0) 2023.03.02
방금그곡  (0) 2023.02.28
뒤에 있는 큰 수 찾기  (0) 2023.02.27
두 큐 합 같게 만들기  (0) 2023.02.26