Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions leetcode3/최원준/1130. Minimum Cost Tree From Leaf Values.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions leetcode3/최원준/4078. Three Fibonacci Sum.py
Original file line number Diff line number Diff line change
@@ -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
Loading