Skip to content

Commit

Permalink
feat: added trapping rain water.
Browse files Browse the repository at this point in the history
  • Loading branch information
Panadestein committed Feb 5, 2025
1 parent 94d724c commit 20a0b75
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/bqn/rollim.bqn
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ HS ← +´∨≥1+⊒˜
(HL≡HS)@‿HL 14141195511110

HLHS {𝕎_timed𝕩}¨< 1e8 •rand.Range 1e3

(+´⊢-˜`⌽⌊⌈`) [0,1,0,2,1,0,1,3,2,1,2,1]
16 changes: 16 additions & 0 deletions src/rollim.org
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,22 @@ If someone ever published that much, sorting will eventually be slower:
A testament to the idea that the simplest solution in BQN is often the most efficient:
I initially clip my citations array with ={≠¨⊔≠∘𝕩¨⌾(≥⟜≠∘𝕩⊸/)𝕩}=, which is just =/⁼≠⊸⌊=.

** Trapping rain water

This problem is a classic in the realm of algorithms, and it can be solved in linear time. Interestingly,
it admits a very elegant array solution as well:

#+begin_src bqn :tangle ./bqn/rollim.bqn :exports both
(+´⊢-˜⌈`⌾⌽⌊⌈`) [0,1,0,2,1,0,1,3,2,1,2,1]
#+end_src

#+RESULTS:
: 6

That is, we do a max scan from the left and from the right, and then take the minimum of the two,
then we subtract the height of the building at that point. The sum of all these values is the answer.
A closely related problem is [[https://leetcode.com/problems/container-with-most-water/][container with most water]], which can be solved:

[fn:1] Almost Perfect Artifacts Improve only in Small Ways: APL is more French than English,
Alan J. Perlis (1978). From [[https://www.jsoftware.com/papers/perlis78.htm][jsoftware]]'s papers collection.
[fn:2] Initially, I intended to rigorously attribute all contributions, but this
Expand Down

0 comments on commit 20a0b75

Please sign in to comment.