kauvii
    • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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
    • Make a copy
    • 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 Make a copy 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    # maximum expression by adding parentheses ## problem: Describe and analyze an algorithm to compute, given a list of integers separated by + and − signs, the maximum possible value the expression can take by adding parentheses. Parentheses must be used only to group additions and subtractions; in particular, do not use them to create implicit multiplication. ## algorithm: Similar to matrix chain multiplication, we can utilize interval dynamic programming. Define nums[i] as the ith number and ops[i] as the operator (either + or -) between nums[i] and nums[i + 1]. Then define two DP tables, maxVal[i][j] as the maximum value we can get the subexpression nums[i] to nums[j] and minVal[i][j] as the minimum value from the subexpression. Two tables are necessary as the subtraction operator can turn a large negative into a large positive and must be considered. The base case when there is only a single element in the subexpression is maxVal[i][i] = minVal[i][i] = nums[i]. For every possible split point k between i and j for each larger range [i...j], subexpression = (nums[i] ... nums[k]) op[k] (nums[k + 1] ... nums[k]). Then, combine according to op[k]. If op[k] == '+', maxVal[i][j] = max(maxVal[i][j], maxVal[i][k] + maxVal[k+1][j]) and minVal[i][j] = min(minVal[i][j], minVal[i][k] + minVal[k+1][j]), else if op[k] == '-' maxVal[i][j] = max(maxVal[i][j], maxVal[i][k] - minVal[k+1][j]) and minVal[i][j] = min(minVal[i][j], minVal[i][k] - maxVal[k+1][j]). "" ```javascript= // input: array of numbers and array of operators between those numbers // output: the maximum value of the expression through addition of parentheses int maxExpressionValue(vector<int> nums, vector<char> ops) { int n = nums.size(); // create 2d arrays vector<vector<int>> maxVal(n, vector<int>(n)); vector<vector<int>> minVal(n, vector<int>(n)); // base cases for (int i = 0; i < n; i++) { maxVal[i][i] = minVal[i][i] = nums[i]; } // length of subexpression for (int length = 2; length <= n; length++) { for (int i = 0; i + length - 1 < n; i++) { int j = i + length - 1; // initalize maximum and minimums maxVal[i][j] = (int minimum); minVal[i][j] = (int maximum); for (int k = i; k < j; k++) { if (ops[k] == '+') { maxVal[i][j] = max(maxVal[i][j], maxVal[i][k] + maxVal[k+1][j]); minVal[i][j] = min(minVal[i][j], minVal[i][k] + minVal[k+1][j]); } // else ops[k] == '-' else { maxVal[i][j] = max(maxVal[i][j], maxVal[i][k] - minVal[k+1][j]); minVal[i][j] = min(minVal[i][j], minVal[i][k] - maxVal[k+1][j]); } } } } return maxVal[0][n-1]; } // sample call // standard input: 1 + 3 - 2 - 5 + 1 - 6 + 7 // separate into two arrays (not relevant to scope of problem): // vector<int> nums = {1, 3, 2, 5, 1, 6, 7} // vector<char> ops = {+, -, -, +, -, +} // int maxExpressionValue(nums, ops) // expect 9 ``` ## correctness: #### Theorem: The algorithm correctly identifies both the true maximum and minimum value after parenthesization for any input. #### Proof: Proof by induction on length l = j - i + 1. Base case (l = 1): When i = j, the subexpression is a single number, meaning that the only parenthesization trivially yields the value nums[i]. The algorithm accounts for this in this line by setting maxVal[i][i] = minVal[i][i] = nums[i], which means it correctly identifies the true maximum and minimum values. Inductive hypothesis: Assume for all intervals of length < l, the algorithm correctly computes maxVal and minVal. Inductive step (prove for all intervals of length ≥ 2): Fix an interval [i, j] with j - i + 1 = l. Consider any full parenthesization of the subexpression. Any such one has a top-level binary split: there exists some k with i ≤ k < j s.t. the topmost operation combines a left subexpression over [i...k] and a right one over [k + 1...j], and that top operator is op[k]. Therefore every element/value v of E(i, j) can be written as either v = x + y, where x is an element of E(i, k), y is an element of E(k + 1, j) when op[k] = '+', or v = x - y where x is an element of E(i, k), y is an element of E(k + 1, j) when op[k] = '-'. Thus, the set of all possible values E(i,j) is the union of the results you can form by taking any possbile result from the left part [i, k] and combining it (with '+' or '-') with any possible result from the right. We want the max and min of that union By the inductive hypothesis, the sets E(i, k) and E(k + 1, j) correctly represent their minimums and maximums. Consider two cases: - Case 1: op[k] = '+' For fixed k, the set {x + y : x ∈ E(i,k), y ∈ E(k+1,j)} has maximum maxVal[i][k] + maxVal[k + 1][j] and minimum minVal[i][k] + minVal[k + 1][j] because, in each argument, addition is monotone. Thus, the largest value obtainable using split k is maxVal[i][k] + maxVal[k+1][j] and the smallest is minVal[i][k] + minVal[k+1][j]. - Case 2: op[k] = '-' For fixed k, the set {x − y : x ∈ E(i,k), y ∈ E(k+1,j)} has maximum maxVal[i][k] − minVal[k+1][j] and minimum minVal[i][k] − maxVal[k+1][j]. This continues to follow the monotonicity and the observation that x - y increases with x and decreases with y. Combining over all k, the global maximum over E(i, j) is the maximum of the k-specific maxima and the minimum is the minimum of the k-specific minima. If op[k] = '+', candidateMax = maxVal[i][k] + maxVal[k + 1][j], candidateMin = minVal[i][k] + minVal[k + 1][j], and if op[k] = '-', candidateMax = maxVal[i][k] − minVal[k + 1][j], candidateMin = minVal[i][k] − maxVal [k + 1][j]. Taking the respective maximum/minimum of all candidateMax/candidateMin over k yields the true maximum/minimum for i, j. Thus, the DP correctly comptues maxVal[i][j] and minVal[i][j] for length l, completing the inductive step. By induction, the algorithim is correctl for all intervals, in which maxVal[1][n] is the maximum value of the whole expression. QED. ## complexity: The time complexity is the same as standard optimal parenthesization dynamic programming problem, O(n^3). This is because there are O(n^2) subproblems and each subproblem checks O(n) splits.

    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
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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