Day 2 of my own Hackerrank Challenge. Today I solved the Jumping On Clouds problem. I will publish my Day 1 takeaways soon, it’s still a work in progress.
Level : Easy
- There’s a consecutive number of clouds
- There’s two types of clouds; cumulus & thunderheads
- You can jump on a cumulus cloud if :
- The destination cumulus cloud is equal to the number of cloud you are on plus 1 or 2.
- You must avoid the thunderheads
The minimum number of jumps from start to finish.
- Input will be an array of binary integers where :
- 0 represents a cumulus cloud and
- 1 represents a thunderhead
- Output will be an integer of the number of minimum jumps.
As I’m brushing up my Swift programming skills. I’ll talk about the things about Swift that I didn’t really know previously as well.
So, my first initial thought was that I had to loop it of course, but what I didn’t know and just recently discovered was that you cannot increment the index directly in a for loop in Swift. Well, you can, but you must use the
stride function where you will determine the start index, finish index, and how much you want to increment your index by. I still need to learn more about that. So, I was kinda stuck when I tried to increment my index inside the for loop, I got an error message saying the index was immutable. Thus, I must resolve to a different form of loops, my old friend,
while. It's been awhile since I used it. Get it? hehe. Bad pun I know. And this is my way of solving it.
The first thing that came to mind, because we want the minimum number of jumps, and we can jump once or twice from our current position, I want to check whether we can double jump from our current position
And because of that, the maximum index where we can check double jumps so that we don’t get an Index out of range in this case would be index number
4 or simply the length of the array - 2.
But when we’re on second to last index (index
5) we also want to be able to check if the last index (index
6) is a safe columbus cloud. And thats why we can't directly check the case above first.
After the two
ifs conditions are met, we can now check on the double jumps. I simply checked whether
i+2is a cumulus cloud (has the value of 0), if yes, then we can jump there and increment the index by 2. If not, since there's only two possibilities, it must be a thunderheads. Then we take a step back and check whether the cloud at
i+1 is a cumulus cloud (by logic, it has to be a cumulus cloud, or else we can't proceed) and increment the index by 1.
Both single and double jumps are considered as 1 jump, and that’s why at the end, we increment the
numberOfJumps. This is what we will return.
Oh, don’t forget to exit the loop once the condition is not fulfilled, simple but common mistake.
- I timed myself and it took me around 45 minutes to solve this problem. I had understand the logic but was struggling with the for loop implementation in Swift. And I spent some time researching about that first.
- Compared to my first day, I’m less intimidated and now I know what to do. Divide and conquer. Scribble the problems on a piece of paper or a tab. Don’t rely 100% on typing out your logic. Sometimes it makes more sense to draw it.