파이썬 기초문제 메모

SUSU·2023년 9월 10일
0
  1. 가장 큰 이득을 얻을수 있는 날
    https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        
        profit = 0
        min_num = prices[0] #시작직후는 첫번째 숫자가 최소값 

        for i in prices[1:]:

            profit = max(profit, i-min_num)
            min_num = min(min_num,i)

        return profit
  1. 피타고라스의 삼각형
def find_pythagorean_triplet(target_sum):
    for a in range(1, target_sum // 3 + 1):
        for b in range(a + 1, target_sum // 2 + 1):
            c = target_sum - a - b
            if a**2 + b**2 == c**2:
                return a, b, c
    return None

target_sum = 1000
triplet = find_pythagorean_triplet(target_sum)

if triplet:
    a, b, c = triplet
    product = a * b * c
    print(f"a = {a}, b = {b}, c = {c}, a*b*c = {product}")
else:
    print("해당하는 피타고라스 삼각형을 찾을 수 없습니다.")
profile
기록용

0개의 댓글