[Programmers] #181934 - 조건 문자열 [Java][C++][Python]
[Programmers] #181934 - 조건 문자열 [Java][C++][Python]
1. 아이디어
두 수 n, m에 대해 문자열 ineq와 eq의 조합에 따라 아래와 같은 조건을 비교만 하면 된다.
ineq = ">",eq = "="→n >= mineq = "<",eq = "="→n <= mineq = ">",eq = "!"→n > mineq = "<",eq = "!"→n < m
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(1)$ | $O(1)$ |
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public int solution(String ineq, String eq, int n, int m) {
if (ineq.equals(">")) {
if (eq.equals("=")) return n >= m ? 1 : 0;
return n > m ? 1 : 0;
} else {
if (eq.equals("=")) return n <= m ? 1 : 0;
return n < m ? 1 : 0;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
#include <bits/stdc++.h>
using namespace std;
int solution(string ineq, string eq, int n, int m) {
if (ineq == ">") {
if (eq == "=") return n >= m;
return n > m;
} else {
if (eq == "=") return n <= m;
return n < m;
}
}
1
2
3
4
5
6
7
8
9
def solution(ineq, eq, n, m):
if ineq == ">":
if eq == "=":
return int(n >= m)
return int(n > m)
else:
if eq == "=":
return int(n <= m)
return int(n < m)
This post is licensed under CC BY 4.0 by the author.