
with salary_rank as (
select
d.name as department
, e.name as employee
, e.salary
, rank() over (partition by departmentId order by e.salary desc) as rnk
from
Employee e
left join Department d
on e.departmentId = d.id
)
select
department
, employee
, salary
from
salary_rank
where
rnk = 1
;
with table1 as (
select name, salary, departmentId,
dense_rank() over(partition by departmentId order by salary desc) as ranking
from Employee
)
select t2.name as Department, t1.name as Employee, t1.salary as Salary
from table1 t1
left join Department t2
on t1.departmentId = t2.id
where t1.ranking = 1;
def solution(s):
answer = 0
s = list(s)
for _ in range(len(s)):
stack = []
for i in range(len(s)):
if len(stack) > 0:
if stack[-1] == '[' and s[i] == ']':
stack.pop()
elif stack[-1] == '{' and s[i] == '}':
stack.pop()
elif stack[-1] == '(' and s[i] == ')':
stack.pop()
else:
stack.append(s[i])
else:
stack.append(s[i])
if len(stack) == 0:
answer += 1
s.append(s.pop(0))
return answer
스택을 이용한 문제로 이중 반복문으로 s를 검사
1. 만약 스택이 비어있다면, 해당 글자를 스택에 저장
2. 스택이 비어있지 않다면, 스택에 마지막으로 저장되어 있는 괄호와 현재 검사하는 s 글자를 매치하여 매칭되면 스택에서 pop
3. s 검사 후, 스택이 비어있다면 +1
4. s 첫 글자를 마지막 글자로 옮김
from collections import deque
def solution(s):
flag = False # 올바른 문자열 확인 플래그
count = 0 # 올바른 문자열 카운트
stack = [] # 올바른 문자열 확인을 위한 스택
queue = deque(s) # 문자열 큐로 변환
# 문자열 큐 길이 만큼 for loop
for i in range(len(queue)):
# 괄호 문자열 하나씩 확인하는 for loop
for el in queue:
# 괄호 여는 문자열이면 스택에 저장
if el == '[' or el == '{' or el == '(':
stack.append(el)
# 괄호 닫는 문자열이면 스택 마지막 값과 확인
else:
# 스택에 값이 있고 괄호 문자열이 완성되면 제거 후 플래그 True
if stack:
if stack[-1] == '[' and el == ']':
stack.pop()
flag = True
elif stack[-1] == '{' and el == '}':
stack.pop()
flag = True
elif stack[-1] == '(' and el == ')':
stack.pop()
flag = True
# 올바른 문자열이 만들어졌고 스택이 비어있다면 카운트 1씩 증가
if flag and not stack: count += 1
# 스택, 플래그 초기화, 문자열 회전
stack = []
flag = False
queue.append(queue.popleft())
# 올바른 문자열 카운트 반환
return count
해결 과정
(1) 올바른 문자열 확인을 위한 플래그, 반환할 올바른 문자열 카운트 그리고 스택과 큐를 선언해 줍니다.
(2) 선언한 문자열 큐의 길이만큼 for loop을 돌려 회전해 줍니다.
(3) 회전된 문자열을 for loop을 돌려 하나씩 확인해 줍니다.
(4) 괄호를 여는 문자열이면 스택에 저장, 괄호를 닫는 문자열이면 스택의 마지막 값과 확인합니다.
(5) 스택에 값이 있고 괄호 문자열이 완성되면 제거 후 플래그를 True로 업데이트해 줍니다.
(6) 올바른 문자열이 만들어졌고 스택이 비어있다면 반환할 카운트를 1씩 증가시켜 줍니다.
(7) 스택과 플래그를 초기화해 주고 문자열을 회전이 끝날 때까지 3번부터 반복해 줍니다.
# 리팩토링 코드
from collections import deque
def check(s):
while True:
if "()" in s: s = s.replace("()","")
elif "{}" in s: s = s.replace("{}","")
elif "[]" in s: s = s.replace("[]","")
else: return False if s else True
def solution(s):
count = 0
queue = deque(s)
for i in range(len(s)):
if check(''.join(queue)): count+=1
queue.rotate(-1)
return count
def bracket(s):
stack = []
for i in s:
if len(stack) == 0: stack.append(i)
else:
if i == ")" and stack[-1] == "(": stack.pop()
elif i == "]" and stack[-1] == "[": stack.pop()
elif i == "}" and stack[-1] == "{": stack.pop()
else: stack.append(i)
return 1 if len(stack) == 0 else 0
def solution(s):
answer = 0
for i in range(len(s)):
if bracket(s): answer +=1
s = s[1:] + s[:1]
return answer
def solution(test):
answer= 0
n=len(test)
for i in range(n):
stack = [ ]
for j in range(n): # test를 회전하기 위해 이중 for문사용
c = test[(i+j)%n]
if c == "(" or c == "{" or c == "[":
stack.append(c) #스택에 푸시
else:
if not stack: #짝이 맞지 않는 경우는 break
break
if c == ")" and stack[-1] == "(":
stack.pop()
elif c == "]" and stack[-1] == "[":
stack.pop()
elif c == "}" and stack[-1] == "{":
stack.pop()
else:
break
else: # break에 걸리지 않고 끝까지 수행한 경우
if not stack:
answer +=1
return answer

