ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

[๋ฌธ์ œ]

https://programmers.co.kr/learn/courses/30/lessons/67257

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ˆ˜์‹ ์ตœ๋Œ€ํ™”

IT ๋ฒค์ฒ˜ ํšŒ์‚ฌ๋ฅผ ์šด์˜ํ•˜๊ณ  ์žˆ๋Š” ๋ผ์ด์–ธ์€ ๋งค๋…„ ์‚ฌ๋‚ด ํ•ด์ปคํ†ค ๋Œ€ํšŒ๋ฅผ ๊ฐœ์ตœํ•˜์—ฌ ์šฐ์Šน์ž์—๊ฒŒ ์ƒ๊ธˆ์„ ์ง€๊ธ‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ๋Œ€ํšŒ์—์„œ๋Š” ์šฐ์Šน์ž์—๊ฒŒ ์ง€๊ธ‰๋˜๋Š” ์ƒ๊ธˆ์„ ์ด์ „ ๋Œ€ํšŒ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ๋‹ค์Œ๊ณผ

programmers.co.kr

[ํ’€์ด]

์กฐํ•ฉ๊ณผ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•˜์—ฌ ํ’€์ดํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

์šฐ์„  ํ•„์š”ํ•œ ์ž๋ฃŒ๋“ค์„ ์„ธํŒ…ํ•ฉ๋‹ˆ๋‹ค.

  • ArrayList<String> exp๋กœ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋ฉฐ ์—ฐ์‚ฐ์ž๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
    • (Set ์ž๋ฃŒํ˜•์„ ์ด์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜์ง€๋งŒ, ํ’€์ด ๋‹น์‹œ์—๋Š” ์ƒ๊ฐ์„ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.ใ…Žใ…Ž)
  • expression.charAt(i) < 48๋ฅผ ํ†ตํ•ด ์—ฐ์‚ฐ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
    • ( ์•„์Šคํ‚ค ์ฝ”๋“œ ์ด์šฉ )
  • ์—ฐ์‚ฐ์ž๋“ค์€ numbers์— " "์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ €์žฅํ•˜๊ณ , ์ˆซ์ž๋“ค์€ operator์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.
    • ์—ฐ์‚ฐ์ž : “*+-” , ์ˆซ์ž : “10 20 300”

๋‹ค์Œ์œผ๋กœ setNumber()๋ฅผ ํ†ตํ•ด ํ’€์ด๋ฅผ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

  • ์—ฐ์‚ฐ์ž๋“ค์˜ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋˜๋Š” ์ˆœ์„œ์Œ์„ ๊ตฌํ•ด์„œ calcuate()๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์—ฐ์‚ฐ์ž๋ฅผ ๋ชจ๋‘ ์ˆ˜ํ–‰์‹œ์ผœ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ๋ฅผ numbers์— ์ €์žฅํ•˜๊ณ  operator์— ๊ณ„์‚ฐํ•œ ์—ฐ์‚ฐ์ž ์™ธ์˜ ์—ฐ์‚ฐ์ž๋“ค์„ ์ƒˆ๋กœ ์ €์žฅํ•˜์—ฌ setNumber() ์žฌ๊ท€๋ฅผ ํ†ตํ•ด ๋ชจ๋‘ ์—ฐ์‚ฐ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค.

 

 

[์ฝ”๋“œ]

import java.util.*;
class Solution {
    static ArrayList<String> exp = new ArrayList<String>();
    static long max = Long.MIN_VALUE;
    public long solution(String expression) {
        long answer = 0;
        StringBuffer sb = new StringBuffer();
        sb.append(expression);
        
        String operator = "";

        for(int i=0;i<expression.length();i++) {
            if(expression.charAt(i) < 48) {
                String s = "" + expression.charAt(i);
                operator += s;
                if(!exp.contains(s)) {
                    exp.add(s);
                }
                sb.replace(i, i+1, " ");
            }
        }
        String numbers = sb.toString();    
        boolean[] visit = new boolean[exp.size()];
        setNumber("", visit, 0, numbers, operator);
        answer = max;
        return answer;
    }
    // ์ˆœ์„œ์Œ ๊ตฌํ•˜๊ธฐ
    public static void setNumber(String str, boolean[] visit, int cnt, String numbers, String operator) {
        if(cnt >= exp.size()) {
            Long absNum = Math.abs(Long.parseLong(numbers));
            if(max < absNum) {
                max = absNum;
            }
            return;
        }
        for(int i=0;i<exp.size();i++) {
            if(!visit[i]) {
                visit[i] = true;
                String[] nums = numbers.split(" ");
                String[] opers = operator.split("");
                
                String newNums = "";
                String newOpers = "";
                for(int j=0;j<nums.length-1;j++) {
                    if(opers[j].equals(exp.get(i))) {
                        String n = calculate(nums[j], nums[j+1], opers[j]);
                        nums[j+1] = n;
                    } else {
                        newNums = newNums + nums[j] + " ";
                        newOpers += opers[j];
                    }
                }
                newNums = newNums + nums[nums.length-1];
                setNumber(str + exp.get(i), visit, cnt+1, newNums, newOpers);
                visit[i] = false;
            }
        }
    }
    public static String calculate(String a, String b, String o) {
        long result = 0L;
        if(o.equals("+")) {
            result = Long.parseLong(a) + Long.parseLong(b);
        } else if(o.equals("-")) {
            result = Long.parseLong(a) - Long.parseLong(b);
        } else if(o.equals("*")) {
            result = Long.parseLong(a) * Long.parseLong(b);
        } else {
            result = Long.parseLong(a) / Long.parseLong(b);
        }
        return Long.toString(result);
    }
}