meyr543
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Leetcode刷題學習筆記 -- Line Sweep ## Intrudoction Line Sweep顧名思義,就是針對x或是y軸依序掃描過去。 例如下圖,針對不同的interval,對x軸掃描。從最左邊掃到最右邊。 ![Line Sweep](https://www.researchgate.net/profile/Vincent_Chu5/publication/338940336/figure/fig2/AS:870108046045184@1584461332356/Illustrative-example-of-a-sweep-line-algorithm-that-can-determine-intersections-of-1-D.ppm) 因為,需要從最小掃到最大。所以interval必須要拆成起點和終點,分別給不同的flag,放進一個vector之後==遞增排序==。 ### Code Snippet 給你一個vector<vector<int>> intervals來代表區間,vector<int> queries來查詢在x = queries[i] 的時候有多少個重複interval,使用interval和queries來建立line sweep的準備資料。 ```cpp enum state{start, end}; vector<vector<int>> intervals; vector<vector<int>> db; for(auto& itv : intervals) { db.push_back({itv[0], start}); db.push_back({itv[1], end, itv[0]}); } for(int i = 0; i < queries.size(); ++i) db.push_back({queries[i], query, i}); sort(db.begin(), db.end()); ``` 建立好準備資料,就可以根據題目依序拿出item來做處理。 ```cpp vector<int> ans(queries.size()); unordered_multiset<int> ms; for(auto& item : db) { switch(item[1]) { case start: ms.insert(item[0]); break; case query: ans[item[2]] = ms.empty() ? -1 : ms.size(); break; case end : ms.erase(ms.find(item[2])); break; } } return ans; ``` ## Problem ### [1851. Minimum Interval to Include Each Query](https://leetcode.com/problems/minimum-interval-to-include-each-query/description/) 給定vector<vector<int>> interval,找出每個query的x值,長度最小的interval。如果沒有則回傳-1。 > 1. 因為是interval可以使用line sweep。 > 2. 將query當成第二個,因為這邊的interval定義是前後都有包含。所以先查詢之後再刪除。 > 3. 第三個欄位放每個interval的size。 > 4. 因為要找出最小的interval所以使用multiset來對遇到的interval進行排序。 ```cpp class Solution { enum state {start, query, end}; public: vector<int> minInterval(vector<vector<int>>& intervals, vector<int>& queries) { vector<vector<int>> db; for(auto& itv : intervals) { int sz = itv[1] - itv[0] + 1; db.push_back({itv[0], start, sz}); db.push_back({itv[1], end, sz}); } for(int i = 0; i < queries.size(); ++i) { db.push_back({queries[i], query, i}); } sort(db.begin(), db.end()); vector<int> ans(queries.size()); multiset<int> ms; for(auto& item : db) { if(item[1] == start) ms.insert(item[2]); else if(item[1] == query) { if(ms.empty()) ans[item[2]] = -1; else ans[item[2]] = *ms.begin(); } else ms.erase(ms.find(item[2])); } return ans; } }; ``` > + 2023/07/18 重做一次還是沒想到使用line sweep。 > + line sweep可以想成是走訪整個時間軸。或是依序走訪x軸。 > + 先建立掃描的順序。 > 1. interval的起始點 (把length放進multiset) > 2. query點 (從multiset拿出最小值) > 3. interval的終點 (把length從multiset移除) > + 依序走訪得出答案。 ### [218. The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/description/) 找出重疊矩形的邊緣。如下圖所示。 ![example](https://assets.leetcode.com/uploads/2020/12/01/merged.jpg) > 一開始對這種題目完全沒想法。如果是面試應該當場傻掉。 > 不過後來看答案之後,原來是使用Line Sweep。另外當沒有想法的時候,如果不擠點東西出來,在面試場合應該也不好看。 > 1. 先觀察給的example的答案。 > 2. 解答都是出現在高度有變化的地方 ==> 判斷高度改變就儲存答案。 > 3. 判斷的地方都是每個方塊的x起點和x終點。 ==> line sweep > 4. ==**重點**== 因為我們必須排列x從小到大,如果x一樣則y從大到小。 ==> 所以start-point {b[0], -b[2]}, end-point {b[1], b[2]},因為當x一樣,最大的y就會排在前面。 > 為什麼要這樣排列? > 當矩形重疊時,{1, 2, 1}, {1, 2, 2}, {1, 2, 3} > 存入height之後 ![](https://i.imgur.com/QmCQHO8.png) > 這邊也是個重點,把成對的vector排列成像竹筍的樣子。 ```cpp class Solution { public: vector<vector<int>> getSkyline(vector<vector<int>>& buildings) { vector<pair<int, int>> height; // x, y multiset<int> hlist; for(auto& b : buildings) { height.emplace_back(b[0], -b[2]); // start height.emplace_back(b[1], b[2]); // end } sort(begin(height), end(height)); hlist.insert(0); // 從高度0開始 int cur{0}, prev{0}; vector<vector<int>> ans; for(auto& [x, y] : height) { if(y < 0) hlist.insert(-y); // 矩形起點,儲存高度。 else hlist.erase(hlist.find(y)); // 矩形終點,所以把這個矩形的高度刪掉 cur = *hlist.rbegin(); // 取最高的高度 if(cur != prev) { // 高度改變 ans.push_back({x, cur}); prev = cur; } } return ans; } }; ``` ###### tags: `leetcode` `刷題`

    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