Gary Lai
  • Home
  • About
登入 訂閱
Dynamic Programming

70. Climbing Stairs

Gary Lai

Jan 28, 2023

70. Climbing Stairs

等同於 509. Fibonacci Number

class Solution:
    def fib(self, n: int):
        p1, p2 = 0, 1
        for i in range(2, n + 1):
            p1, p2 = p2, p1 + p2
        return p2

    def climbStairs(self, n: int) -> int:
        return self.fib(n + 1)

閱讀下一篇

210. Course Schedule II

210. Course Schedule II Check 207. Course Schedule
Gary Lai Jan 30, 2023

207. Course Schedule

207. Course Schedule 這一個題目的設計滿巧秒的,我個人覺得這個題目綜合了樹的遍歷(遞迴)、回溯法、動態規劃以及資料與圖形轉換的設計,外加上題目可以套用在各種現實生活問題中。所以整體而言 LeetCode 給出了中等難度,但是細節很多很需要小心的想通。 先看看題目可以怎麼想,所有的課程與其先修課程基本上就是很一顆或是很多棵樹,也就是說如果一棵樹裡面沒有任何的環,我們就知道不會有一個課程他的先修課是另外一門課,而那門課的先修課又是該堂課的情況。 目標就變得很明確,我們要找出此棵樹沒有環,但是就要回到題目看看給定的課程與先修課程的關係,這是一個數列,但是並沒有特定的排列順序,所以我們需要一個可以快速查找的課程有哪些先修課的表: table = defaultdict(list) for prerequisite in prerequisites: course, prerequisiteCourse = prerequisite table[course].append(prerequisiteCourse) 這張表就是前言一開始提到的資料與圖
Gary Lai Jan 30, 2023

1676. Lowest Common Ancestor of a Binary Tree IV

1676. Lowest Common Ancestor of a Binary Tree IV Check 235. Lowest Common Ancestor of a Binary Search Tree
Gary Lai Jan 30, 2023

訂閱Gary Lai

Don't miss out on the latest news. Sign up now to get access to the library of members-only articles.
  • Sign up
Gary Lai © 2023. Powered by Ghost