I just took the simple approach of erasing one element and checking each time if the sequence is safe :) I knew there was a better way! Nice to see it here :)
For Part 2 I solved it just by checking whether exactly one is bad level: - Case when exactly one inc/dec is incorrect - Case when exactly one has incorrect distance (x < 1 or x > 3) Total 5 cases need to be checked for each report, which is O(N)
@difflocktwo correct, that's why I mentioned 5 cases, 1 for the initial report and 2 for each of the cases that I mentioned. Be careful, I'm counting the direction case as one case because I'm considering only the direction which has the most correct levels. Also, when I have bad distance level Im just considering the next level for conparison, so effectively, I just store an array which tells whether from the current level away to the next there is valid distance, if not, then not safe.
For part two, when you spot an issue, removing that index plus the next is enough, except where the index is 1, then you also need to try to remove index 0, as 0..1 is the setup for asc/desc which can be wrong. After reading all the Reddit posts, was happy that I figured that out while on stream, as most people didn’t seem to get that bit 😊
I just took the simple approach of erasing one element and checking each time if the sequence is safe :)
I knew there was a better way! Nice to see it here :)
I'm so happy you're back
Yo bro you are back
HE IS BACK
For Part 2 I solved it just by checking whether exactly one is bad level:
- Case when exactly one inc/dec is incorrect
- Case when exactly one has incorrect distance (x < 1 or x > 3)
Total 5 cases need to be checked for each report, which is O(N)
if you have one bad level, couldn't that mean that you have two wrong distances? one leading to it and one leading away from it?
@difflocktwo correct, that's why I mentioned 5 cases, 1 for the initial report and 2 for each of the cases that I mentioned.
Be careful, I'm counting the direction case as one case because I'm considering only the direction which has the most correct levels.
Also, when I have bad distance level Im just considering the next level for conparison, so effectively, I just store an array which tells whether from the current level away to the next there is valid distance, if not, then not safe.
legendary grandmaster
For part two, when you spot an issue, removing that index plus the next is enough, except where the index is 1, then you also need to try to remove index 0, as 0..1 is the setup for asc/desc which can be wrong. After reading all the Reddit posts, was happy that I figured that out while on stream, as most people didn’t seem to get that bit 😊
This is the best version I could come up with for part 2(full code on my GitHub):
def check(nums):
return all(1
Oh, nice. If we focus on one direction (e.g. only increasing) then we don't need to bother with three consecutive values.
@@Errichto yeah that makes the first part even simpler to implement:
ret = 0
for l in lines:
nums = list(map(int, l.split()))
if all(1
@@animesh_chouhan This looks like O(n^2) solution
@@bladekiller2766 the check function is called only once and check_seq returns after 1 call.
@@bladekiller2766 the check function is called only once by check_seq function and then returns so O(n)
Can't you have count of increasing and decreasing number and check whether atleast one of those is 0 or 1
Erasing an element by change the number of increments by 0 or 1 or 2. So it's not that easy.
whooa