데브코스 TIL/자료구조, 알고리즘

문제 풀이

예니ㅣ 2023. 10. 18. 17:48

lv1_예산_소팅

def solution(d, budget):
    d.sort(reverse = True)
    
    cnt = 0
    
    while budget > 0 and d != []:
        budget -= d.pop()
        cnt += 1
        
        if budget < 0:
            cnt -= 1
            break
        
    return cnt

 

 

lv2_기능개발

⭐ 큐를 이용하는 문제! 이지만 포인터로 풀어본 문제!

import math

def solution(progresses, speeds):
    n = len(speeds)
    
    days = [0] * n
    
    for i in range(n):
        days[i] = math.ceil((100 - progresses[i]) / speeds[i])
        
    print(days)
    
    dist = []
    
    start = 0
    end = 0
    
    while start < n:
        if end > n-1:
            cnt = n - start
            dist.append(cnt)
            break
            
        if days[start] >= days[end]:
            end += 1
        else:           
            cnt = end - start
            dist.append(cnt)
            
            start = end
    
    return dist

 

 

lv2_가장 큰 수

⭐ 이렇게 푸는게 맞나 싶지만 정렬 알고리즘 커스텀해서 푸는 문제!

from functools import cmp_to_key

def custom(a, b):
    if a + b > b + a:
        return -1
    elif a + b < b + a:
        return 1
    else:
        return 0

def solution(numbers):
    numbers = list(map(str, numbers))
    sort_numbers = sorted(numbers,
                          key = cmp_to_key(custom))
    
    if len(sort_numbers) == sort_numbers.count("0"):
        sort_numbers = ["0"]
            
    ans = "".join(sort_numbers)
            
    return ans

 

 

lv2_최솟값 만들기

def solution(A,B):
    A.sort()
    B.sort(reverse=True)
    
    answer =0
    
    for i in range(len(A)):
        answer += A[i]*B[i]
        
    return answer

 

 

lv1_나머지 한 점

def solution(v):
    x = []
    y = []
        
    for i in range(len(v)):
        if v[i][0] not in x:
            x.append(v[i][0])
        else:
            x.remove(v[i][0])
        
        if v[i][1] not in y:
            y.append(v[i][1])
        else:
            y.remove(v[i][1])
            
    return [x[0], y[0]]

 

 

lv1_운송 트럭

def solution(max_weight, specs, names):
    specs_dict = {specs[i][0]:int(specs[i][1]) for i in range(len(specs))}
    
    cnt = 1
    
    truck = 0
    
    for name in names:       
        truck += specs_dict[name]
        
        if truck > max_weight:
            truck = specs_dict[name]
            cnt += 1
            
    return cnt

 

 

lv2_카펫

import math

def solution(brown, yellow):
    total = brown+yellow
    
    for i in range(3, int(math.sqrt(total))+1):
        if total%i == 0:
            if yellow%(i-2) == 0:
                w = i
                l = total//w
                
                if total%l == 0:
                    if yellow%(l-2) == 0:
                        answer = [l, w]
    return answer

 

 

lv2_사탕 담기

from itertools import combinations

def solution(m, weights):
    cnt = 0
    
    for i in range(1, len(weights)):
        for comb in combinations(weights, i):
            if sum(comb) == m:
                cnt += 1
                
    return cnt