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 :)
A good way to refactor, if you didn't want repeated code would be, instead of returning a boolean from the isOK function, we could return first index which causes an issue, and SZ+1 or some sentinel if good.
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 😊
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.
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
Great solutions!!
A good way to refactor, if you didn't want repeated code would be, instead of returning a boolean from the isOK function, we could return first index which causes an issue, and SZ+1 or some sentinel if good.
HE IS BACK
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 😊
legendary grandmaster
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.
hey u are back...
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