Stock Exchange Losses puzzle discussion

Never mind.
Array is optional in this simple problem. For more complex problems you may prefer to store all data before processing because you may need to reference previous or next data on-the-fly. In that case, have an array or list of data is handy.

I have no doubt it is! I will also try to resolve issues without using hints. It was a pretty good experience.

After thinking for a while I found that this is easy to do in O(n)

  • Consider your first value the PEAK
  • While you’re iterating the values if the current value is lower than the PEAK, count your CURRENT_LOSS as being the biggest difference from the peak to there
  • If the current value is higher than the PEAK then set the PEAK to that value and reset your CURRENT_LOSS
  • Everytime you reach a new peak, or there are no more values, if your CURRENT_LOSS is higher than the MAXIMUM_LOSS then that is your new MAXIMUM_LOSS
1 Like

This is a fun one. I can argue that I have one of the best solutions for this puzzle.

I have solved this in JavaScript fine however in Dart it is timing out in Test #5 even when running the below script to get the numbers it times out - this has to be an error with Dart?

void main() {
  readLineSync();
  readLineSync().split(' ').forEach((value) {});
  
  print(-1073730311);
}

Test case 5 shows a timeout issue even with the default code.
@TwoSteps Could you please take a look? Thanks.

1 Like

Thanks for your idea and I settle this puzzle by it using cpp. :smiling_face_with_three_hearts:

I’ve settled this puzzle with cpp . If you [nope].

Does your code contain any lines to print to error stream? If so, your code may pass if you comment out those lines.

i get it,thanks

in test 5 i keep getting
Found: -1073706325
Expected: -1073730311
and i have no idea why :confused: (using c++)

4246 - 1073734557 = -1073730311
I think you may consider checking how you get -1073706325 and why you have skipped the expected answer by adding some code to print to the error stream.

Well, @logicow is right.
You may need a linear algorithm like greedy algorithm, which works on this task.

This should be in easy.

Hi i wrote a O(n) algoritm to solve this puzzle. It pass all tests but test 5.
My code gives different result than expected.
I should be missing something but cant figure it out.
Any idea?

I found my mistake thanks …

1 Like

Great, you are fast ! We didn’t have even the time to help. :slight_smile:

1 Like

Is an error in test5 like
Failure
Found: -1071590351
Expected: -1073730311
means that there is actually a timeout happening behind the scenes or is there a hidden trap in test5’s dataset?
All the other tests are passing, and I optimized my code to run in a single loop reading the inputs with just 5 variables and zero data structures, so not too sure where to go from there optimization-wise.
(Using Java if it makes a difference)

I found this problem enjoyable. when I ran my program it passed every test case that was presented, except for the particular case where the input is [n1, n2, … 2, 2, 1] or [n1, n2, … 3, 2, 1] in which none of the test cases handled properly.

Hi Guys,
i have a problem with the last example (6), i need help.

import sys
import math


n,data,result,= int(input()),[],0
for i in input().split():
    v = int(i)
    data.append(v)
beforeD,maxD,minD= data[0],max(data),min(data)
if data.index(data[0]) != data.index(maxD):
    if data.index(minD) > data.index(maxD):
        result = (maxD-minD)*-1
        print(result)
    else:
        newData = data[:data.index(minD)+1]
        maxD = max(newData)
        result = (maxD-minD)*-1
        print(result)
else:
        result = (maxD-minD)*-1
        print(result)

thanks for your help