diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/1130. Minimum Cost Tree From Leaf Values.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/1130. Minimum Cost Tree From Leaf Values.py" new file mode 100644 index 00000000..6cd5c307 --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/1130. Minimum Cost Tree From Leaf Values.py" @@ -0,0 +1,46 @@ +# + +''' +1. 아이디어 : +가장 작은 숫자를 찾고, 왼쪽과 오른쪽 중 작은 숫자와 곱하여 ans에 더합니다. 그리고 작은 숫자를 제외한 리스트를 생성 + +2. 시간복잡도 : + O(n * n) + +3. 자료구조/알고리즘 : +그리디 + +''' +class Solution: + def mctFromLeafValues(self, arr: list[int]) -> int: + ans = 0 + + def get_smallest(nums): + n = len(nums) + smallest = float('inf') + index = -1 + + for i, num in enumerate(nums): + if num < smallest: + smallest = num + index = i + + new_nums = [] + for i in range(n): + if i == index: + continue + new_nums.append(nums[i]) + + if index == 0: + product = nums[0] * nums[1] + elif index == n-1: + product = nums[-1] * nums[-2] + else: + product = nums[index] * min(nums[index-1], nums[index+1]) + + return new_nums, product + + while len(arr) > 1: + arr, product = get_smallest(arr) + ans += product + return ans diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/4078. Three Fibonacci Sum.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/4078. Three Fibonacci Sum.py" new file mode 100644 index 00000000..6331d34e --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/4078. Three Fibonacci Sum.py" @@ -0,0 +1,28 @@ +# + +''' +1. 아이디어 : +fibonacci를 n보다 커질때까지 리스트를 만들고, +리스트를 순회하며 3개씩 합친다. + +2. 시간복잡도 : + O(n + n) + +3. 자료구조/알고리즘 : +- + +''' +class Solution: + def threeFibonacciSum(self, n: int) -> bool: + nums = [0, 1] + while nums[-1] <= n: + nums.append(nums[-2] + nums[-1]) + print(nums) + + if len(nums) < 3: + return False + + for i in range(len(nums)-2): + if nums[i] + nums[i+1] + nums[i+2] == n: + return True + return False