티스토리 뷰

 

 

코딩테스트 연습 - 수식 최대화

IT 벤처 회사를 운영하고 있는 라이언은 매년 사내 해커톤 대회를 개최하여 우승자에게 상금을 지급하고 있습니다. 이번 대회에서는 우승자에게 지급되는 상금을 이전 대회와는 다르게 다음과

programmers.co.kr

이 문제는 사실 +, -, *의 우선순위에 따라 최대값을 구하는 문제여서 6개의 경우의수를 모두 구해서 최대값을 리턴해주었다.

이 문제는 다른 문제가 좀 더 레퍼런스로 좋을 것 같다!

import java.util.Deque;
import java.util.LinkedList;

class Solution {
    long answer = Integer.MIN_VALUE;
    public long solution(String expression) {
        answer = Math.max(answer,minusMulPlus(expression));
        answer = Math.max(answer,minusPlusMul(expression));
        answer = Math.max(answer,mulPlusMinus(expression));
        answer = Math.max(answer,mulMinusPlus(expression));
        answer = Math.max(answer,plusMulMinus(expression));
        answer = Math.max(answer,plusMinusMul(expression));

        System.out.println(answer);
        return answer;
    }
    private long minusPlusMul(String expression) {
        String[] numberStringArr = expression.split("[-*+]");
        Deque<String> queue = new LinkedList<>();
        String[] signArr = expression.replaceAll("[0-9]", "").split("");

        // expression 초기화
        for(int i = 0 ;i<signArr.length;i++) {
            queue.add(numberStringArr[i]);
            queue.add(signArr[i]);
        }
        queue.add(numberStringArr[signArr.length]);

        Deque<String> firstMinus = minus(queue);

        // - > + > *
        Deque<String> MinusPlusMul = plus(firstMinus);
        MinusPlusMul = multiply(MinusPlusMul);
        long aa = Long.valueOf(MinusPlusMul.poll());
        if(Math.abs(aa) > answer){
            answer = Math.abs(aa);
        }
        return answer;

    }

    private long minusMulPlus(String expression) {
        String[] numberStringArr = expression.split("[-*+]");
        Deque<String> queue = new LinkedList<>();
        String[] signArr = expression.replaceAll("[0-9]", "").split("");

        // expression 초기화
        for(int i = 0 ;i<signArr.length;i++) {
            queue.add(numberStringArr[i]);
            queue.add(signArr[i]);
        }
        queue.add(numberStringArr[signArr.length]);

        Deque<String> firstMinus = minus(queue);

        // - > * > +
        Deque<String> MinusMulPlus = multiply(firstMinus);
        MinusMulPlus = plus(MinusMulPlus);
        long aa = Long.valueOf(MinusMulPlus.poll());
        if(Math.abs(aa) > answer){
            answer = Math.abs(aa);
        }

        return answer;

    }

    private long mulPlusMinus(String expression) {
        String[] numberStringArr = expression.split("[-*+]");
        Deque<String> queue = new LinkedList<>();
        String[] signArr = expression.replaceAll("[0-9]", "").split("");

        // expression 초기화
        for(int i = 0 ;i<signArr.length;i++) {
            queue.add(numberStringArr[i]);
            queue.add(signArr[i]);
        }
        queue.add(numberStringArr[signArr.length]);

        Deque<String> firstMul = multiply(queue);

        // * > + > -
        Deque<String> MulPlusMinus = plus(firstMul);
        MulPlusMinus = minus(MulPlusMinus);
        long aa = Long.valueOf(MulPlusMinus.poll());
        if(Math.abs(aa) > answer){
            answer = Math.abs(aa);
        }
        return answer;
    }

    private long mulMinusPlus(String expression) {
        String[] numberStringArr = expression.split("[-*+]");
        Deque<String> queue = new LinkedList<>();
        String[] signArr = expression.replaceAll("[0-9]", "").split("");

        // expression 초기화
        for(int i = 0 ;i<signArr.length;i++) {
            queue.add(numberStringArr[i]);
            queue.add(signArr[i]);
        }
        queue.add(numberStringArr[signArr.length]);

        Deque<String> firstMul = multiply(queue);

        // * > - > +
        Deque<String> MulMinusPlus = minus(firstMul);
        MulMinusPlus = plus(MulMinusPlus);
        long aa = Long.valueOf(MulMinusPlus.poll());
        if(Math.abs(aa) > answer){
            answer = Math.abs(aa);
        }
        return answer;

    }

    private long plusMulMinus(String expression) {
        String[] numberStringArr = expression.split("[-*+]");
        Deque<String> queue = new LinkedList<>();
        String[] signArr = expression.replaceAll("[0-9]", "").split("");

        // expression 초기화
        for(int i = 0 ;i<signArr.length;i++) {
            queue.add(numberStringArr[i]);
            queue.add(signArr[i]);
        }
        queue.add(numberStringArr[signArr.length]);

        Deque<String> firstPlus = plus(queue);

        // + > * > -
        Deque<String> PlusMulMinus = multiply(firstPlus);
        PlusMulMinus = minus(PlusMulMinus);
        long aa = Long.valueOf(PlusMulMinus.poll());
        if(Math.abs(aa) > answer){
            answer = Math.abs(aa);
        }
        return answer;

    }

    private long plusMinusMul(String expression) {
        String[] numberStringArr = expression.split("[-*+]");
        Deque<String> queue = new LinkedList<>();
        String[] signArr = expression.replaceAll("[0-9]", "").split("");

        // expression 초기화
        for(int i = 0 ;i<signArr.length;i++) {
            queue.add(numberStringArr[i]);
            queue.add(signArr[i]);
        }
        queue.add(numberStringArr[signArr.length]);

        Deque<String> firstPlus = plus(queue);

        // + > - > *
        Deque<String> PlusMinusMul = minus(firstPlus);
        PlusMinusMul = multiply(PlusMinusMul);
        long aa = Long.valueOf(PlusMinusMul.poll());
        if(Math.abs(aa) > answer){
            answer = Math.abs(aa);
        }
        return answer;

    }

    private Deque<String> multiply(Deque<String> queue) {
        Deque<String> answer = new LinkedList<>();
        String peek;
        while(!queue.isEmpty()) {
            String element =queue.poll();
            if(element.equals("*")) {
                long a = Long.valueOf(answer.pollLast());
                long b = Long.valueOf(queue.poll());
                peek = String.valueOf(a*b);
            } else {
                peek = element;
            }
            answer.add(peek);
        }
        return answer;
    }

    private Deque<String> plus(Deque<String> queue) {
        Deque<String> answer = new LinkedList<>();
        String peek;
        while(!queue.isEmpty()) {
            String element =queue.poll();
            if(element.equals("+")) {
                long a = Long.valueOf(answer.pollLast());
                long b = Long.valueOf(queue.poll());
                peek = String.valueOf(a+b);
            } else {
                peek = element;
            }
            answer.add(peek);
        }
        return answer;
    }

    private Deque<String> minus(Deque<String> queue) {
        Deque<String> answer = new LinkedList<>();
        String peek;
        while(!queue.isEmpty()) {
            String element =queue.poll();
            if(element.equals("-")) {
                long a = Long.valueOf(answer.pollLast());
                long b = Long.valueOf(queue.poll());
                peek = String.valueOf(a-b);
            } else {
                peek = element;
            }
            answer.add(peek);
        }
        return answer;
    }

}

public class code {
    public static void main(String[] args) {
        Solution s = new Solution();
        String str = "100-200*300-500+20";
        s.solution(str);
    }
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
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
글 보관함