SELECT
n
, CASE
WHEN p IS NULL THEN 'Root'
WHEN n NOT IN (
SELECT
DISTINCT p
FROM
bst
WHERE
p IS NOT NULL
) THEN 'Leaf'
ELSE 'Inner'
END AS node_type
FROM
bst
ORDER BY
n
;
SELECT DISTINCT NOW.N,
CASE
WHEN NOW.P IS NULL THEN 'Root'
WHEN CHILD.P IS NULL THEN 'Leaf'
ELSE 'Inner'
END
FROM BST NOW
LEFT JOIN BST CHILD ON NOW.N = CHILD.P
LEFT JOIN BST PARENT ON NOW.P = PARENT.N
ORDER BY NOW.N
→ 출력해야 하는 것
1. the company_code
2. founder name
3. total number of lead managers
4. total number of senior managers
5. total number of managers
6. total number of employees
→ company_code 가준 오름차순으로 정렬
→ conglomerate?
여러 업종의 회사를 차례로 매수·합병하여 거대한 기업으로 팽창한 회사. 흔히 수익성(收益性)이 높고 장래성이 있는 회사를 주식 매점 등의 수단으로 흡수함. 복합 기업.
SELECT
c.company_code
, founder
, COUNT(DISTINCT lead_manager_code)
, COUNT(DISTINCT senior_manager_code)
, COUNT(DISTINCT manager_code)
, COUNT(DISTINCT employee_code)
FROM
employee e
LEFT JOIN company c
USING(company_code)
GROUP BY
c.company_code
, founder
ORDER BY
c.company_code
;
def solution(s):
answer = 0
cnt_x = 0
cnt_not_x = 0
for i in s:
if cnt_x == 0:
x = i
cnt_x += 1
continue
if i == x:
cnt_x += 1
else:
cnt_not_x += 1
if cnt_x == cnt_not_x:
answer += 1
cnt_x = 0
cnt_not_x = 0
if cnt_x != 0:
answer += 1
return answer
from collections import deque
def solution(s):
ans = 0
q = deque(s)
while q:
a, b = 1, 0
x = q.popleft()
while q:
n = q.popleft()
if n == x:
a += 1
else:
b += 1
if a == b:
ans += 1
break
if a != b:
ans += 1
return ans
큐(queue)는 선입선출, FIFO(First In First Out) 기반의 매우 유명한 자료구조입니다. 큐를 사용하면 데이터를 추가한 순서대로 제거할 수 있기 때문에 비동기 메세징(asynchronous messaging), 스트리밍(streaming), 너비 우선 탐색(breath first search) 등 소프트웨어 개발에서 널리 응용되고 있습니다.
collections 모듈의 deque는 double-ended queue의 약자로 데이터를 양방향에서 추가하고 제거할 수 있는 자료구조입니다.
# 매번 x 값이 바뀜
def solution(s):
answer = 0
is_x , not_x = 0, 0
for i in range(len(s)): # s만큼 반복문 반복
if is_x == not_x: # 두 개가 같으면(answer + 1) / 처음은 무조건 같음
answer += 1
x = s[i]
is_x, not_x = 0, 0
if s[i] == x:
is_x += 1
else:
not_x += 1
return answer
이해하기 위해 abracadabra 0-10 적어보기
: ab - ra - ca - da - br - a와 같이 분해됨
i [is_x, not_x, answer]
0 [0 0 1] s[i] = a, x = a
0 [1 0 1] s[i] = a, x = a
1 [1 1 1] s[i] = b, x = a
2 [0 0 2] s[i] = r, x = r
2 [1 0 2] s[i] = r, x = r
3 [1 1 2] s[i] = a, x = r
4 [0 0 3] s[i] = c, x = c
4 [1 0 3] s[i] = c, x = c
5 [1 1 3] s[i] = a, x = c
6 [0 0 4] s[i] = d, x = d
6 [1 0 4] s[i] = d, x = d
7 [1 1 4] s[i] = a, x = d
8 [0 0 5] s[i] = b, x = b
8 [1 0 5] s[i] = b, x = b
9 [1 1 5] s[i] = r, x = b
10 [0 0 6] s[i] = a, x = a