#๐Ÿ”’ Help with CP question

9 messages ยท Page 1 of 1 (latest)

tropic notch
#

Hello, i am trying to solve this simple CP question, my approach works on the sample testcase but fails on the first hidden testcase
https://open.kattis.com/problems/upsanddownsofinvesting

my solution:
x, n, m = list(map(int, input().split()))
n-=1
m-=1
l=list(map(int, input().split()))
upordown=[1 if ((l[i+1]-l[i])>0) else -1 if ((l[i+1]-l[i])<0) else 0 for i in range(len(l)-1)]
if (x==1) or (x==2):
print("0 0")
else:
collapsed=[]
length=0
for i in range(len(upordown)-1):
if upordown[i]!=upordown[i+1]:
length+=upordown[i]
collapsed.append(length)
length=0
else:
length+=upordown[i]
collapsed.append(length+1 if length>0 else length-1)
ind=0
h=0
l=0
for i in range(len(collapsed)-1):
ind+=abs(collapsed[i])
if ((collapsed[i]>=n) and (abs(collapsed[i+1])>=n)):
h+=1
elif ((abs(collapsed[i])>=m) and (collapsed[i+1]>=m)):
l+=1
print(h,l)

i try to collapse the graph into a series of ups and downs, for example if its 1 3 5 2 that would be collapsed to [2,-1] in my solution to denote that there is 2 ups followed by one down. After i have the series of ups and downs i just need to check every i and i+1 to make sure its greater than the m or n that is given. it is guranteed that no point is the same so i dont really have to worry about 0s. im sure there is a much easier solution to this problem, but i can't wrap my head around what testcase im failing and whats wrong with my approach.

dapper masonBOT
#

@tropic notch

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tropic notch
#

my collapse code seems to work for every testcase i feed it, for example the sample testcase

#

collapsed = [2, -3, 1, -2, 3, -2]

#

which is correct

#

if anyone wants more info on my approach just ask

dapper masonBOT
#

@tropic notch

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.