[ 알고리즘 ]/Greedy

    [백준] 16953. A → B

    [백준] 16953. A → B

    📌문제 16953번: A → B 첫째 줄에 A, B (1 ≤ A < B ≤ 109)가 주어진다. www.acmicpc.net 🎖️난이도 ✔️풀이 a, b = map(int, input().split()) cnt = 0 while True: if b == a: print(cnt + 1) break elif b < a or (b%10 != 1 and b%10 != 2 and b%2 != 0): print(-1) break elif b%10 == 1: b //= 10 elif b%10 == 2 or b%2 == 0: b /= 2 cnt += 1 🧠노트 거꾸로 생각해서 B가 A가 될 때까지 2로 나누거나 맨 뒤에 1을 떼내어 주면 된다. 그러다가 안되는 상황이 발생하면 -1을 출력해주면 되고, B == A가 되..

    [백준] 1541. 잃어버린 괄호

    📌문제 https://www.acmicpc.net/problem/1541 🎖️난이도 ✔️풀이 sik = input() arr = sik.split('-') result = 0 for n in arr.pop(0).split('+'): # 맨 첫번째만 더하고, 이후부턴 다 빼기위해 pop result += int(n) for n in arr: for nn in n.split('+'): result -= int(nn) print(result)

    [백준] 11399. ATM

    📌문제 https://www.acmicpc.net/problem/11399 🎖️난이도 ✔️풀이 n = int(input()) p = list(map(int, input().split())) result = 0 p.sort() for i in range(len(p)): for j in range(i+1): result += p[j] print(result)

    [프로그래머스] 단속카메라

    📌문제 https://programmers.co.kr/learn/courses/30/lessons/42884 🎖️난이도 Level 3 ✔️풀이 # sol) 빠져나간 값(뒤에 값)을 기준으로 비교 & 카메라 설치 위치 갱신 import math def solution(routes): answer = 0 routes.sort(key=lambda x: x[1]) # routes를 차량이 나간 지점 (진출) 기준으로 정렬 print(routes) camera = -math.inf # math에서 가능한 것들 중 최소 값 (아주 작은 값) for route in routes: if camera < route[0]: answer += 1 camera = route[1] print(camera) return answer

    [프로그래머스] 구명보트

    📌문제 https://programmers.co.kr/learn/courses/30/lessons/42885 🎖️난이도 Level 2 ✔️풀이 # sol def solution(people, limit): people.sort() cnt = 0 l = len(people) tmp_limit = limit p = 0 # 타고 있는 사람 수 (최대 2명) for i in range(l): tmp_person = people[i] if tmp_limit >= tmp_person and p < 2: tmp_limit -= tmp_person p += 1 continue cnt += 1 tmp_limit = limit - tmp_person p = 1 return cnt + 1