Post

[Programmers] 181934번 - 조건 문자열 [Java][C++]

[Programmers] 181934번 - 조건 문자열 [Java][C++]

문제 링크


1. 아이디어

ineqeq에 따라 다른 연산을 하는 문제로 조건문을 활용해 분기 처리를 해주는 방식으로 해결했다.


2. 복잡도

시간복잡도공간복잡도
$O(1)$$O(1)$

3. 코드

풀이 [Java][C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
    public int solution(String ineq, String eq, int n, int m) {
        if (ineq.equals(">")) {
            if (eq.equals("=")) {
                return n >= m ? 1 : 0;
            } else {
                return n > m ? 1 : 0;
            }
        } else {
            if (eq.equals("=")) {
                return n <= m ? 1 : 0;
            } else {
                return n < m ? 1 : 0;
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;

int solution(string ineq, string eq, int n, int m) {
    if (ineq == ">") {
        if (eq == "=") {
            return n >= m;
        } else {
            return n > m;
        }
    } else {
        if (eq == "=") {
            return n <= m;
        } else {
            return n < m;
        }
    }
}

This post is licensed under CC BY 4.0 by the author.