본문 바로가기

언어64

Manhattan Distance -- p1 at (x1, y1) and p2 at (x2, y2), it is |x1 - x2| + |y1 - y2|select round(abs(min(lat_n)-max(lat_n)) + abs(min(long_w)-max(long_w)),4)from station 2024. 6. 11.
수입 1등은 몇 명인가 select months * salary, count(distinct name)from employeegroup by months * salaryorder by months * salary desclimit 1 2024. 6. 11.
round, ceil, floor, truncate /*round: 반올림ceil: 올림 (to integer)floor: 버림 (to integer)truncate: 내림*/select ceil(avg(Salary) - avg(replace(Salary, 0, '')))from employees; 2024. 6. 11.
full outer join 불가능 new companies/*select e.company_code, c.founder, count(distinct lead_manager_code), count(distinct senior_manager_code), count(distinct manager_code), count(distinct employee_code)from employee eleft join company con e.company_code=c.company_codegroup by company_code, founderorder by company_code*/-- company: 100-- lead_manager_code: 100-- senior_manager_code: 144-- m.. 2024. 6. 10.
조건문 case when binary tree nodesselect N, case when P is null then 'Root' when N in (select distinct P from BST) then 'Inner' else 'Leaf' endfrom BSTorder by N 2024. 6. 10.
last three characters Query the Name of any student in STUDENTS who scored higher than  Marks. Order your output by the last three characters of each name. If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.select namefrom studentswhere marks > 75order by right(name,3), id 2024. 6. 5.
정규표현식 필터링 Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.select distinct cityfrom stationwhere city regexp '^[aeiou].*[aeiou]$'표현식의미 ^x 문자열의 시작을 표현하며 x 문자로 시작됨을 의미한다.x$ 문자열의 종료를 표현하며 x 문자로 종료됨을 의미한다. .x임의의 한 문자의 자리수를 표현하며 문자열이 x 로 끝난다는 것을 의미한다. x*반복여부를 표현하며 x 문자가 0번 또는 그 이상 반복됨을 의미한다. x+반복.. 2024. 6. 5.
체육복 여분이 있어도 도난을 당하다니 def solution(n, lost, reserve): # +1, -1의 학생에게만 빌려줄 수 있음 # 최대한 많은 학생이 빌려야 함 # 5, [2,4], [3] -> 한 명만 빌릴 수 있음 # reserve 학생들을 대상으로 -> -1, +1 번호에 lost 학생이 있는지 find -> lost에서 제거 (빌려줌!) # 빌려줄 수 있는데 도난당한 학생은, 자기가 써야하므로 빌려줄 수 없음! lost = set(lost) reserve = set(reserve) # 교집합 찾기 rem_set = lost & reserve lost = lost - rem_set reserve = reserve - rem_set for reserveNum in reserve: if reserveNum-1 in lost: .. 2024. 2. 22.
ct 정리 hash() 하나만 찾아야 할 때 유용 ex: 마라톤 미완주자 from collections import Counter Counter: 딕셔너리 형태로 key가 몇 개 (value) 있는지 자동처리 sorted(옵션): reverse=True, key=(lambda x: x[1]) str startswith zfill upper lower isupper islower ascii 알파벳 26개 map(func, iter) dict[key] = value value가 list가 들어가면 +로 append 가능 for key in dict for k,v in dict.items() -> tuple [x for x in list if x > 2] [[False] * n for _ in range(m)] ex: .. 2024. 2. 21.
티켓으로 여행 경로 만들기: BFS 모든 공항은 알파벳 대문자 3글자로 이루어집니다. 주어진 공항 수는 3개 이상 10,000개 이하입니다. tickets의 각 행 [a, b]는 a 공항에서 b 공항으로 가는 항공권이 있다는 의미입니다. 주어진 항공권은 모두 사용해야 합니다. 만일 가능한 경로가 2개 이상일 경우 알파벳 순서가 앞서는 경로를 return 합니다. 모든 도시를 방문할 수 없는 경우는 주어지지 않습니다. from collections import deque def solution(tickets): answer = [] graph = dict() # 출발 공항: 도착 가능한 공항 # 방문 가능한 공항 그래프 생성 for d, a in tickets: graph[d] = graph.get(d, []) + [a] # key=d에 아.. 2024. 2. 16.