ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 알고리즘
    내일배움캠프/TIL 2022. 11. 29. 02:59

    알고리즘 문제를 풀면서 하루를 보냈습니다.

    from collections import deque
    
    
    def solution(prices):
        prices = deque(prices)
        stock_sinks = []
        while len(prices):
            seconds = 0
            curr_price = prices.popleft()
    
            for val in prices:
                seconds += 1
                if curr_price > val:
                    break
            stock_sinks.append(seconds)
    
        return stock_sinks
    
    
    # prices = list(map(int, input().split()))
    # print(solution(prices))
    
    print("정답 = [2, 1, 2, 1, 0] / 현재 풀이 값 = ", solution([6, 9, 5, 7, 4]))
    print("정답 = [6, 2, 1, 3, 2, 1, 0] / 현재 풀이 값 = ",
          solution([3, 9, 9, 3, 5, 7, 2]))
    print("정답 = [6, 1, 4, 3, 1, 1, 0] / 현재 풀이 값 = ",
          solution([1, 5, 3, 6, 7, 6, 5]))

     

    def stack_sequence(n, sequence):
        top = -1
        count = 1
        stack = []
        stack_sign = []
        for num in sequence:
            while not num in stack:
                stack.append(count)
                stack_sign.append('+')
                count += 1
                top += 1
    
            if stack[top] != num:
                return print('NO')
            else:
                stack.pop()
                top -= 1
                stack_sign.append('-')
    
        for char in stack_sign:
            print(char)
    
    
    # sequence = list()
    # n = int(input())
    # for _ in range(n):
    #     sequence.append(int(input()))
    # stack_sequence(n, sequence)
    stack_sequence(8, [4, 3, 6, 8, 7, 5, 2, 1])

     

    from collections import deque
    shop_prices = [30000, 2000, 1500000]
    user_coupons = [20, 40]
    
    
    def get_max_discounted_price(prices, coupons):
        result = 0
        prices.sort(reverse=True)
        coupons.sort(reverse=True)
        while len(coupons) and len(prices):
            result += int(prices.pop(0) * (1-(coupons.pop(0) / 100)))
    
        while len(prices):
            result += prices.pop()
    
        return result
    
    
    print("정답 = 926000 / 현재 풀이 값 = ",
          get_max_discounted_price([30000, 2000, 1500000], [20, 40]))
    print("정답 = 485000 / 현재 풀이 값 = ",
          get_max_discounted_price([50000, 1500000], [10, 70, 30, 20]))
    print("정답 = 1550000 / 현재 풀이 값 = ",
          get_max_discounted_price([50000, 1500000], []))
    print("정답 = 1458000 / 현재 풀이 값 = ",
          get_max_discounted_price([20000, 100000, 1500000], [10, 10, 10]))

     

    from collections import deque
    
    
    def is_correct_parenthesis(string):
        parentheses_open = 0
        parentheses_close = 0
        for sign in string:
            if sign == '(':
                parentheses_open += 1
            else:
                parentheses_close += 1
    
            if parentheses_open < parentheses_close:
                return False
    
        if parentheses_open == parentheses_close:
            return True
        return False
    
    
    print("정답 = True / 현재 풀이 값 = ", is_correct_parenthesis("(())"))
    print("정답 = False / 현재 풀이 값 = ", is_correct_parenthesis(")"))
    print("정답 = False / 현재 풀이 값 = ", is_correct_parenthesis("((())))"))
    print("정답 = False / 현재 풀이 값 = ", is_correct_parenthesis("())()"))
    print("정답 = False / 현재 풀이 값 = ", is_correct_parenthesis("((())"))

     

    def get_melon_best_album(genre_array, play_array):
        melon_dic = {}
        melon_arr = []
        result = []
    
        # 최대 빈도수를 구하는 식
        for music in zip(genre_array, play_array):
            if music[0] in melon_dic:
                melon_dic[music[0]] += music[1]
            else:
                melon_dic[music[0]] = music[1]
            melon_arr.append(music)
        # 내림차순으로 정렬
        melon_dic = sorted(melon_dic.items(),
                           key=lambda item: item[1], reverse=True)
    
        melon_arr.sort(reverse=True)
    
        while len(melon_dic) != 0:
            genre_many = melon_dic.pop(0)[0]
            count = 0
            for pop_music in melon_arr:
                # while count != 2:
                if genre_many == pop_music[0]:
                    for i, music in enumerate(zip(genre_array, play_array)):
                        if pop_music == music:
                            result.append(i)
                            count += 1
                            if count == 2:
                                break
                if count == 2:
                    break
        return result
    
    
    print("정답 = [4, 1, 3, 0] / 현재 풀이 값 = ", get_melon_best_album(["classic",
          "pop", "classic", "classic", "pop"], [500, 600, 150, 800, 2500]))
    print("정답 = [0, 6, 5, 2, 4, 1] / 현재 풀이 값 = ", get_melon_best_album(["hiphop", "classic",
          "pop", "classic", "classic", "pop", "hiphop"], [2000, 500, 600, 150, 800, 2500, 2000]))

    3주차 내용은 숙제와 문제에 담겨있습니다.
    숙제와 문제코드를 남겨봅니다.

    '내일배움캠프 > TIL' 카테고리의 다른 글

    HTML  (0) 2022.12.02
    알고리즘  (0) 2022.11.30
    알고리즘  (0) 2022.11.25
    알고리즘  (0) 2022.11.24
    알고리즘, git  (0) 2022.11.23
Designed by Tistory.