Victor Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- ###### tags: `資訊科技產業專案設計` --- # info2022-homework2 > 貢獻者: 霸佛羅|Buffalo :man:: interviewer :baby::interviewee ## [LeetCode#198 House Robber](https://leetcode.com/problems/house-robber/) > 影片連結:[第二次作業(漢)](https://youtu.be/ZzLqaPX529k) #### 模擬面試過程的討論 :man::你好,今天由我來主持這場面試。在這一輪的題目中,會給你一個裝整數型態的 vector,在這個 vector 裡面我們想要加總不相鄰的數字和是最大的,以舉例講解會更清楚 ``` example 1 // Input: nums = [1, 2, 3, 1] // Output: 4 (因為 1 + 3 = 4) ``` ``` example 2 // Input: nums = [2, 7, 9, 3, 1] // Output: 12 (因為 2 + 9 + 1 = 12) ``` :baby::好,大致上了解,我想請問一下如果是 ``` // Input: nums = [2, 1, 1, 5] // Output: 7? ``` :man::是的,沒有錯 :baby::那目前我的想法是如果我們以第一個範例做舉例,我們可以選擇選 1 或不選 1,如果選 1 的話,因為我們不能選 2,所以我們會選 3 或是後面的 1,那如果我們不選 1 的話,因為後面的 3 跟他相鄰,所以我們只能選最後面的 1,不過因為這只有 4 個數字,如果今天數字變多,這個方法會變得複雜並且有許多重複的 ``` / \ 1 2 / \ \ 3 1 1 ``` :baby::所以目前我的想法是說他可以選擇要選或不選,如果是選的話,那我們就會是以選 `max((arr[0] + arr[2:]), a[1:])` :man::那可以請你實作看看嗎 ```cpp= class Solution{ public: int rob(vector<int>& nums){ if (nums.empty()) return 0; if (nums.size() <= 1) return num[0]; // 創建 dp 所需要的陣列 vector<int> dp(nums.size(), 0); dp[0] = nums[0]; dp[1] = max(dp[0], nums[1]); for (int i = 2; i < nums.size(); ++i) { // 選取前一項或自己跟前兩項相加之較大者 dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]); } return dp.back(); } }; ``` :baby::由於我們只需要前兩項的數字,不需要用 vector 將其他群部儲存取來,因此可以以兩個變數代替 ```cpp= class Solution{ public: int rob(vector<int>& nums){ if (nums.empty()) return 0; if (nums.size() <= 1) return num[0]; int a = nums[0]; int b = max(a, nums[1]); for (int i = 2; i < nums.size(); ++i) { int temp = b; b = max(a + nums[i], b); a = temp } return b; } }; ``` :baby::這樣的話我們可以降低我們空間的利用,從 _O(n)_ 到 _O(1)_ :man::那如果我們現在將題目改成有 cycle,像是 _[1, 2, 3, 1]_ 為 ```mermaid graph LR 1. --> 2 --> 3 --> 1 --> 1. ``` :man::那你覺得你該怎麼做呢? :baby::如果這樣的話我會分成兩個方面討論,分別是有選第一個和沒有第一個,也就是第一個到倒數第二的的陣列比第二個到最後一個的陣列 ```cpp= class Solution { public: int rob(vector<int>& nums) { if (nums.empty()) return 0; if (nums.size() == 1) return nums[0]; // with the first one int a = nums[0]; int b = max(nums[0], nums[1]); for (int i = 2; i < nums.size() - 1; ++i){ int temp = b; b = max(a + nums[i], b); a = temp } // without the first one int c = 0; int d = num[1]; for (int i = 2; i < nums.size(); ++i) { int temp = d; d = max(a + nums[i], d); c = temp } // 比較兩個陣列並回傳較大者 return max(b, d); } } ``` :baby::以這個來說,時間複雜度一樣是 _O(n)_,空間複雜度為 _O(1)_ :man::那我們今天的面試就到這邊,謝謝 ### 延伸閱讀 [213. House Robber II](https://leetcode.com/problems/house-robber-ii/) [337. House Robber III](https://leetcode.com/problems/house-robber-iii/) --- ## 共筆 ### 對其他同學的批評 [時光-Bryan](https://hackmd.io/@sysprog/BJhz9Ylfs) [西尼-Ssini](https://hackmd.io/@sysprog/HJ1oYFlzi) [雜菜煲-Vegetable HotPot(V.H.P.)](https://hackmd.io/@sysprog/BJpBqFgGj) [達摩-Daruma](https://hackmd.io/@sysprog/SyyDRMZfi) [咕咕雞-Chicken](https://hackmd.io/@sysprog/HkzGaFxzj) ### 從中學到什麼 - 面試真的需要練習,很多時候會發現生活中覺得沒什麼的話在面試時就會變的可能不尊重,像是建議將「我是你今天的面試官」改成「今天由我來主持這場面試」,如果沒經過這些練習不會注意到的細節,並且確實一邊講一邊打沒想像中容易 - 題目的演算法會發現有不同解法,經過這幾周也看了一些之前沒看過的題目 - 經過上課有別於以往認為就只是把題目練好,演算法到最佳解,現在會開始思考題目的應用與相關延伸 - 更有危機意識 ### 對授課教師許願 由於上課可以給一點壓力,或許可以要求同學完成像是 Linkedin 或是履歷,讓大家有初版履歷並且知道之後要加強哪部分的能力或經驗。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully