Last active
July 30, 2024 05:10
-
-
Save DataKinds/8605424ab795965e6660d5731ec6aa55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NB. https://leetcode.com/problems/container-with-most-water/ | |
height=: {{ (i. # y),. y }} 1 8 6 2 5 4 8 3 7 | |
NB. call as ({: height) metric {. height | |
NB. metric=: {{ (({: y) <. {: x) * | ({. x) - {. y }} | |
metric=: ({:@] <. {:@[) * [: | {.@[ - {.@] | |
f=:>./ @ , @ (metric"1 _"_ 1~) @ {{ (i. # y),. y }} | |
NB. https://leetcode.com/problems/container-with-most-water/solutions/5139915/video-simple-two-pointer-solution/ | |
f2=:3 : 0 | |
ma=. 0 | |
l=. 0 | |
r=. -&1 # y | |
while. l < r do. | |
hl=. l { y | |
hr=. r { y | |
metricX=. l,hl | |
metricY=. r,hr | |
ma=. ma >. (metricX metric metricY) | |
if. hl < hr do. l=.l+1 else. r=.r-1 end. | |
end. | |
ma return. | |
) | |
f3=:3 : 0 | |
ma=. 0 | |
lr=. 0,-&1 # y | |
while. ({. < {:) lr do. | |
h=. lr { y | |
ma=. ma >. (({. metric {:) lr,.h) | |
lr=. lr + (0 _1,:1 0) {~ ({. < {:) h | |
end. | |
ma return. | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment