Skip to content

Maximum Nesting Depth + Julia #9

Open
@mcabbott

Description

@mcabbott

I came across this page https://github.com/codereport/array-language-comparisons/blob/main/comparisons/leetcode/P1614_Max_Paren_Depth.md

For Julia, I'm not too sure what "no row-wise minus reduction" means, but it sounds like you are looking for maximum(cumsum(..., something like this:

julia> str = "(1)+((2))+(((3)))";

julia> maximum(cumsum([(c=='(')-(c==')') for c in str]))
3

julia> maximum(Iterators.accumulate(+, (c=='(')-(c==')') for c in str))  # lazy version
3

julia> foldl(str; init=(0,0)) do (i,maxi), c
         c=='(' && return i+1, max(i+1,maxi)
         c==')' && return i-1, maxi
         i, maxi
       end[2]
3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions