Determine the Widest Valley
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
Imagine we get a slice of some mountainous region, this would result in a shape similar to this:
4 _
3 _ _ __/
2 / __/ _/ _ /
1 / / _/
0 /
12322223210012233343221112
As we can see, we can represent this (to a certain degree) with a sequence of integers.
For the purpose of this challenge we define a valley as a contiguous subsequence where the values initially are decreasing and from some point on they are increasing. More formally for a sequence $(a_i)_i=1^n$ a valley will be indices $1 leq s < r < t leq n$ for which the following holds:
- the valley's start and endpoint are the same: $a_s = a_t$
- the valley starts and ends once the region gets lower: $a_s > a_s+1 land a_t-1 < a_t$
- the valley is not flat: $a_s neq a_r land a_r neq a_t$
- the valley initially decreases: $forall i in [s,r): a_i geq a_i+1$
- the valley will at some point increase: $forall j in [r,t): a_j leq a_j+1$
Now we define the width of such a valley as the size of the indices $[s,t]$, ie. $t-s+1$.
Challenge
Given a height-profile (sequence of non-negative integers), your task is to determine the width of the widest valley.
Example
Given the height-profile [1,2,3,2,2,2,2,3,2,1,0,0,1,2,2,3,3,3,4,3,2,2,1,1,1,2]
, we can visualize it as before:
4 _
3 _ _ __/
2 / __/ _/ _ /
1 / / _/
0 /
12322223210012233343221112
aaaaaa ccccc
bbbbbbbbb
Note how the second valley [3,2,1,0,0,1,2,2,3]
does not extend further to the right because the left-most point is $3$ and not $4$. Furthermore we don't add the remaining two $3$s because we require that the endpoint is higher up than the second-last point.
Therefore the width of the widest valley is $9$.
Rules
- Input will be a sequence of non-negative (sorry Dutch people) integers
- you can assume that there is always at least one valley
- Output will be the size of the widest valley as defined above
Testcases
[4,0,4] -> 3
[1,0,1,0,1] -> 3
[1,0,2,0,1,2] -> 4
[13,13,13,2,2,1,0,1,14,2,13,14] -> 4
[1,2,3,2,2,2,2,3,2,1,0,0,1,2,2,3,3,3,4,3,2,2,1,1,1,2] -> 9
code-golf array-manipulation
add a comment |Â
up vote
5
down vote
favorite
Imagine we get a slice of some mountainous region, this would result in a shape similar to this:
4 _
3 _ _ __/
2 / __/ _/ _ /
1 / / _/
0 /
12322223210012233343221112
As we can see, we can represent this (to a certain degree) with a sequence of integers.
For the purpose of this challenge we define a valley as a contiguous subsequence where the values initially are decreasing and from some point on they are increasing. More formally for a sequence $(a_i)_i=1^n$ a valley will be indices $1 leq s < r < t leq n$ for which the following holds:
- the valley's start and endpoint are the same: $a_s = a_t$
- the valley starts and ends once the region gets lower: $a_s > a_s+1 land a_t-1 < a_t$
- the valley is not flat: $a_s neq a_r land a_r neq a_t$
- the valley initially decreases: $forall i in [s,r): a_i geq a_i+1$
- the valley will at some point increase: $forall j in [r,t): a_j leq a_j+1$
Now we define the width of such a valley as the size of the indices $[s,t]$, ie. $t-s+1$.
Challenge
Given a height-profile (sequence of non-negative integers), your task is to determine the width of the widest valley.
Example
Given the height-profile [1,2,3,2,2,2,2,3,2,1,0,0,1,2,2,3,3,3,4,3,2,2,1,1,1,2]
, we can visualize it as before:
4 _
3 _ _ __/
2 / __/ _/ _ /
1 / / _/
0 /
12322223210012233343221112
aaaaaa ccccc
bbbbbbbbb
Note how the second valley [3,2,1,0,0,1,2,2,3]
does not extend further to the right because the left-most point is $3$ and not $4$. Furthermore we don't add the remaining two $3$s because we require that the endpoint is higher up than the second-last point.
Therefore the width of the widest valley is $9$.
Rules
- Input will be a sequence of non-negative (sorry Dutch people) integers
- you can assume that there is always at least one valley
- Output will be the size of the widest valley as defined above
Testcases
[4,0,4] -> 3
[1,0,1,0,1] -> 3
[1,0,2,0,1,2] -> 4
[13,13,13,2,2,1,0,1,14,2,13,14] -> 4
[1,2,3,2,2,2,2,3,2,1,0,0,1,2,2,3,3,3,4,3,2,2,1,1,1,2] -> 9
code-golf array-manipulation
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
Imagine we get a slice of some mountainous region, this would result in a shape similar to this:
4 _
3 _ _ __/
2 / __/ _/ _ /
1 / / _/
0 /
12322223210012233343221112
As we can see, we can represent this (to a certain degree) with a sequence of integers.
For the purpose of this challenge we define a valley as a contiguous subsequence where the values initially are decreasing and from some point on they are increasing. More formally for a sequence $(a_i)_i=1^n$ a valley will be indices $1 leq s < r < t leq n$ for which the following holds:
- the valley's start and endpoint are the same: $a_s = a_t$
- the valley starts and ends once the region gets lower: $a_s > a_s+1 land a_t-1 < a_t$
- the valley is not flat: $a_s neq a_r land a_r neq a_t$
- the valley initially decreases: $forall i in [s,r): a_i geq a_i+1$
- the valley will at some point increase: $forall j in [r,t): a_j leq a_j+1$
Now we define the width of such a valley as the size of the indices $[s,t]$, ie. $t-s+1$.
Challenge
Given a height-profile (sequence of non-negative integers), your task is to determine the width of the widest valley.
Example
Given the height-profile [1,2,3,2,2,2,2,3,2,1,0,0,1,2,2,3,3,3,4,3,2,2,1,1,1,2]
, we can visualize it as before:
4 _
3 _ _ __/
2 / __/ _/ _ /
1 / / _/
0 /
12322223210012233343221112
aaaaaa ccccc
bbbbbbbbb
Note how the second valley [3,2,1,0,0,1,2,2,3]
does not extend further to the right because the left-most point is $3$ and not $4$. Furthermore we don't add the remaining two $3$s because we require that the endpoint is higher up than the second-last point.
Therefore the width of the widest valley is $9$.
Rules
- Input will be a sequence of non-negative (sorry Dutch people) integers
- you can assume that there is always at least one valley
- Output will be the size of the widest valley as defined above
Testcases
[4,0,4] -> 3
[1,0,1,0,1] -> 3
[1,0,2,0,1,2] -> 4
[13,13,13,2,2,1,0,1,14,2,13,14] -> 4
[1,2,3,2,2,2,2,3,2,1,0,0,1,2,2,3,3,3,4,3,2,2,1,1,1,2] -> 9
code-golf array-manipulation
Imagine we get a slice of some mountainous region, this would result in a shape similar to this:
4 _
3 _ _ __/
2 / __/ _/ _ /
1 / / _/
0 /
12322223210012233343221112
As we can see, we can represent this (to a certain degree) with a sequence of integers.
For the purpose of this challenge we define a valley as a contiguous subsequence where the values initially are decreasing and from some point on they are increasing. More formally for a sequence $(a_i)_i=1^n$ a valley will be indices $1 leq s < r < t leq n$ for which the following holds:
- the valley's start and endpoint are the same: $a_s = a_t$
- the valley starts and ends once the region gets lower: $a_s > a_s+1 land a_t-1 < a_t$
- the valley is not flat: $a_s neq a_r land a_r neq a_t$
- the valley initially decreases: $forall i in [s,r): a_i geq a_i+1$
- the valley will at some point increase: $forall j in [r,t): a_j leq a_j+1$
Now we define the width of such a valley as the size of the indices $[s,t]$, ie. $t-s+1$.
Challenge
Given a height-profile (sequence of non-negative integers), your task is to determine the width of the widest valley.
Example
Given the height-profile [1,2,3,2,2,2,2,3,2,1,0,0,1,2,2,3,3,3,4,3,2,2,1,1,1,2]
, we can visualize it as before:
4 _
3 _ _ __/
2 / __/ _/ _ /
1 / / _/
0 /
12322223210012233343221112
aaaaaa ccccc
bbbbbbbbb
Note how the second valley [3,2,1,0,0,1,2,2,3]
does not extend further to the right because the left-most point is $3$ and not $4$. Furthermore we don't add the remaining two $3$s because we require that the endpoint is higher up than the second-last point.
Therefore the width of the widest valley is $9$.
Rules
- Input will be a sequence of non-negative (sorry Dutch people) integers
- you can assume that there is always at least one valley
- Output will be the size of the widest valley as defined above
Testcases
[4,0,4] -> 3
[1,0,1,0,1] -> 3
[1,0,2,0,1,2] -> 4
[13,13,13,2,2,1,0,1,14,2,13,14] -> 4
[1,2,3,2,2,2,2,3,2,1,0,0,1,2,2,3,3,3,4,3,2,2,1,1,1,2] -> 9
code-golf array-manipulation
code-golf array-manipulation
asked 1 hour ago
BMO
10.4k21879
10.4k21879
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
Python 2, 120 115 89 87 86 bytes
lambda a:max(r-l+1for l,v in e(a)for r,w in e(a)if v==w>max(a[l+1:r]+[0]))
e=enumerate
Try it online!
-1 byte, thanks to BMO
Does replacing[-1]
with[0]
work?
â BMO
1 hour ago
@BMO Yeah, I guess it does. A valley must always start withn>0
right?
â TFeld
1 hour ago
Yes, there can only be non-negative integers and as such a valley has to start withn>0
.
â BMO
1 hour ago
add a comment |Â
up vote
1
down vote
Haskell, 66 bytes
f(a:b)|(_:h,i:_)<-span(<a)b,i==a=max(length h+3)$f b|1<2=f b
f _=0
Try it online!
add a comment |Â
up vote
1
down vote
Jelly, 17 16 bytes
áºÂ>þá»Â@â¬.ìÃÂ.ææòÃÂáºÂá¹Â
Try it online!
áºÂá¹ Maximum of lengths ofâ¦
ẠàAll sublists satisfying:
ò This funky 4-link monad:
>þ á»Â@â¬. ìÃÂ.æ æ Is a valley.
First, >þ
makes a table of $a[x]>a[y]$ for our valley candidate. For $[6, 2, 3, 1, 4, 6]$ it's:
beginbmatrix
color#0000&0&0&0&0&colorred0\
colorblue1&color#0000&1&0&1&colorblue1\
colorblue1&0&color#0000&0&1&colorblue1\
colorblue1&1&1&color#0000&1&colorblue1\
colorblue1&0&0&0&color#0000&colorblue1\
colorred0&0&0&0&0&color#0000
endbmatrix
We want the $colorredtextred$ elements to be both zero, representing that the start and end are equal.
We want the $colorbluetextblue$ elements to be all ones, representing that the endpoints are > everything in the middle.
We apply á»Â@â¬.
: index each by 0.5, i.e. get the start and end of every row. This gets us
beginbmatrix
color#0000&colorred0\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorred0&color#0000
endbmatrix
Then ìÃÂ.æ
negates the first and last rows, and æ
checks that the matrix is all ones.
(The top-left and bottom-right corners are guaranteed to be 0s in the table we started from, as they're on the diagonal, and $ x not> x $. So we don't need to check them at all.)
Also,Lâ¬
âÂÂáºÂ
.
â Erik the Outgolfer
7 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Python 2, 120 115 89 87 86 bytes
lambda a:max(r-l+1for l,v in e(a)for r,w in e(a)if v==w>max(a[l+1:r]+[0]))
e=enumerate
Try it online!
-1 byte, thanks to BMO
Does replacing[-1]
with[0]
work?
â BMO
1 hour ago
@BMO Yeah, I guess it does. A valley must always start withn>0
right?
â TFeld
1 hour ago
Yes, there can only be non-negative integers and as such a valley has to start withn>0
.
â BMO
1 hour ago
add a comment |Â
up vote
1
down vote
Python 2, 120 115 89 87 86 bytes
lambda a:max(r-l+1for l,v in e(a)for r,w in e(a)if v==w>max(a[l+1:r]+[0]))
e=enumerate
Try it online!
-1 byte, thanks to BMO
Does replacing[-1]
with[0]
work?
â BMO
1 hour ago
@BMO Yeah, I guess it does. A valley must always start withn>0
right?
â TFeld
1 hour ago
Yes, there can only be non-negative integers and as such a valley has to start withn>0
.
â BMO
1 hour ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Python 2, 120 115 89 87 86 bytes
lambda a:max(r-l+1for l,v in e(a)for r,w in e(a)if v==w>max(a[l+1:r]+[0]))
e=enumerate
Try it online!
-1 byte, thanks to BMO
Python 2, 120 115 89 87 86 bytes
lambda a:max(r-l+1for l,v in e(a)for r,w in e(a)if v==w>max(a[l+1:r]+[0]))
e=enumerate
Try it online!
-1 byte, thanks to BMO
edited 1 hour ago
answered 1 hour ago
TFeld
12.9k2836
12.9k2836
Does replacing[-1]
with[0]
work?
â BMO
1 hour ago
@BMO Yeah, I guess it does. A valley must always start withn>0
right?
â TFeld
1 hour ago
Yes, there can only be non-negative integers and as such a valley has to start withn>0
.
â BMO
1 hour ago
add a comment |Â
Does replacing[-1]
with[0]
work?
â BMO
1 hour ago
@BMO Yeah, I guess it does. A valley must always start withn>0
right?
â TFeld
1 hour ago
Yes, there can only be non-negative integers and as such a valley has to start withn>0
.
â BMO
1 hour ago
Does replacing
[-1]
with [0]
work?â BMO
1 hour ago
Does replacing
[-1]
with [0]
work?â BMO
1 hour ago
@BMO Yeah, I guess it does. A valley must always start with
n>0
right?â TFeld
1 hour ago
@BMO Yeah, I guess it does. A valley must always start with
n>0
right?â TFeld
1 hour ago
Yes, there can only be non-negative integers and as such a valley has to start with
n>0
.â BMO
1 hour ago
Yes, there can only be non-negative integers and as such a valley has to start with
n>0
.â BMO
1 hour ago
add a comment |Â
up vote
1
down vote
Haskell, 66 bytes
f(a:b)|(_:h,i:_)<-span(<a)b,i==a=max(length h+3)$f b|1<2=f b
f _=0
Try it online!
add a comment |Â
up vote
1
down vote
Haskell, 66 bytes
f(a:b)|(_:h,i:_)<-span(<a)b,i==a=max(length h+3)$f b|1<2=f b
f _=0
Try it online!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Haskell, 66 bytes
f(a:b)|(_:h,i:_)<-span(<a)b,i==a=max(length h+3)$f b|1<2=f b
f _=0
Try it online!
Haskell, 66 bytes
f(a:b)|(_:h,i:_)<-span(<a)b,i==a=max(length h+3)$f b|1<2=f b
f _=0
Try it online!
answered 35 mins ago
nimi
30.5k31985
30.5k31985
add a comment |Â
add a comment |Â
up vote
1
down vote
Jelly, 17 16 bytes
áºÂ>þá»Â@â¬.ìÃÂ.ææòÃÂáºÂá¹Â
Try it online!
áºÂá¹ Maximum of lengths ofâ¦
ẠàAll sublists satisfying:
ò This funky 4-link monad:
>þ á»Â@â¬. ìÃÂ.æ æ Is a valley.
First, >þ
makes a table of $a[x]>a[y]$ for our valley candidate. For $[6, 2, 3, 1, 4, 6]$ it's:
beginbmatrix
color#0000&0&0&0&0&colorred0\
colorblue1&color#0000&1&0&1&colorblue1\
colorblue1&0&color#0000&0&1&colorblue1\
colorblue1&1&1&color#0000&1&colorblue1\
colorblue1&0&0&0&color#0000&colorblue1\
colorred0&0&0&0&0&color#0000
endbmatrix
We want the $colorredtextred$ elements to be both zero, representing that the start and end are equal.
We want the $colorbluetextblue$ elements to be all ones, representing that the endpoints are > everything in the middle.
We apply á»Â@â¬.
: index each by 0.5, i.e. get the start and end of every row. This gets us
beginbmatrix
color#0000&colorred0\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorred0&color#0000
endbmatrix
Then ìÃÂ.æ
negates the first and last rows, and æ
checks that the matrix is all ones.
(The top-left and bottom-right corners are guaranteed to be 0s in the table we started from, as they're on the diagonal, and $ x not> x $. So we don't need to check them at all.)
Also,Lâ¬
âÂÂáºÂ
.
â Erik the Outgolfer
7 mins ago
add a comment |Â
up vote
1
down vote
Jelly, 17 16 bytes
áºÂ>þá»Â@â¬.ìÃÂ.ææòÃÂáºÂá¹Â
Try it online!
áºÂá¹ Maximum of lengths ofâ¦
ẠàAll sublists satisfying:
ò This funky 4-link monad:
>þ á»Â@â¬. ìÃÂ.æ æ Is a valley.
First, >þ
makes a table of $a[x]>a[y]$ for our valley candidate. For $[6, 2, 3, 1, 4, 6]$ it's:
beginbmatrix
color#0000&0&0&0&0&colorred0\
colorblue1&color#0000&1&0&1&colorblue1\
colorblue1&0&color#0000&0&1&colorblue1\
colorblue1&1&1&color#0000&1&colorblue1\
colorblue1&0&0&0&color#0000&colorblue1\
colorred0&0&0&0&0&color#0000
endbmatrix
We want the $colorredtextred$ elements to be both zero, representing that the start and end are equal.
We want the $colorbluetextblue$ elements to be all ones, representing that the endpoints are > everything in the middle.
We apply á»Â@â¬.
: index each by 0.5, i.e. get the start and end of every row. This gets us
beginbmatrix
color#0000&colorred0\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorred0&color#0000
endbmatrix
Then ìÃÂ.æ
negates the first and last rows, and æ
checks that the matrix is all ones.
(The top-left and bottom-right corners are guaranteed to be 0s in the table we started from, as they're on the diagonal, and $ x not> x $. So we don't need to check them at all.)
Also,Lâ¬
âÂÂáºÂ
.
â Erik the Outgolfer
7 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Jelly, 17 16 bytes
áºÂ>þá»Â@â¬.ìÃÂ.ææòÃÂáºÂá¹Â
Try it online!
áºÂá¹ Maximum of lengths ofâ¦
ẠàAll sublists satisfying:
ò This funky 4-link monad:
>þ á»Â@â¬. ìÃÂ.æ æ Is a valley.
First, >þ
makes a table of $a[x]>a[y]$ for our valley candidate. For $[6, 2, 3, 1, 4, 6]$ it's:
beginbmatrix
color#0000&0&0&0&0&colorred0\
colorblue1&color#0000&1&0&1&colorblue1\
colorblue1&0&color#0000&0&1&colorblue1\
colorblue1&1&1&color#0000&1&colorblue1\
colorblue1&0&0&0&color#0000&colorblue1\
colorred0&0&0&0&0&color#0000
endbmatrix
We want the $colorredtextred$ elements to be both zero, representing that the start and end are equal.
We want the $colorbluetextblue$ elements to be all ones, representing that the endpoints are > everything in the middle.
We apply á»Â@â¬.
: index each by 0.5, i.e. get the start and end of every row. This gets us
beginbmatrix
color#0000&colorred0\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorred0&color#0000
endbmatrix
Then ìÃÂ.æ
negates the first and last rows, and æ
checks that the matrix is all ones.
(The top-left and bottom-right corners are guaranteed to be 0s in the table we started from, as they're on the diagonal, and $ x not> x $. So we don't need to check them at all.)
Jelly, 17 16 bytes
áºÂ>þá»Â@â¬.ìÃÂ.ææòÃÂáºÂá¹Â
Try it online!
áºÂá¹ Maximum of lengths ofâ¦
ẠàAll sublists satisfying:
ò This funky 4-link monad:
>þ á»Â@â¬. ìÃÂ.æ æ Is a valley.
First, >þ
makes a table of $a[x]>a[y]$ for our valley candidate. For $[6, 2, 3, 1, 4, 6]$ it's:
beginbmatrix
color#0000&0&0&0&0&colorred0\
colorblue1&color#0000&1&0&1&colorblue1\
colorblue1&0&color#0000&0&1&colorblue1\
colorblue1&1&1&color#0000&1&colorblue1\
colorblue1&0&0&0&color#0000&colorblue1\
colorred0&0&0&0&0&color#0000
endbmatrix
We want the $colorredtextred$ elements to be both zero, representing that the start and end are equal.
We want the $colorbluetextblue$ elements to be all ones, representing that the endpoints are > everything in the middle.
We apply á»Â@â¬.
: index each by 0.5, i.e. get the start and end of every row. This gets us
beginbmatrix
color#0000&colorred0\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorblue1&colorblue1\
colorred0&color#0000
endbmatrix
Then ìÃÂ.æ
negates the first and last rows, and æ
checks that the matrix is all ones.
(The top-left and bottom-right corners are guaranteed to be 0s in the table we started from, as they're on the diagonal, and $ x not> x $. So we don't need to check them at all.)
edited 3 mins ago
answered 12 mins ago
Lynn
49k694223
49k694223
Also,Lâ¬
âÂÂáºÂ
.
â Erik the Outgolfer
7 mins ago
add a comment |Â
Also,Lâ¬
âÂÂáºÂ
.
â Erik the Outgolfer
7 mins ago
Also,
Lâ¬
â áºÂ
.â Erik the Outgolfer
7 mins ago
Also,
Lâ¬
â áºÂ
.â Erik the Outgolfer
7 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175321%2fdetermine-the-widest-valley%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password