05/8/19

HackerRank ‘Birthday Cake Candles’ Solution

Short Problem Definition:

You are in charge of the cake for your niece’s birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out.

Birthday Cake Candles

Complexity:

time complexity is O(N)

space complexity is O(1)

Execution:

Keep track of the tallest one along with the count.

Solution:
```#!/bin/python

import sys

n = int(raw_input().strip())
height = map(int,raw_input().strip().split(' '))

cnt = 0
running_top = 0
for candle in height:
if (candle > running_top):
cnt = 1
running_top = candle
elif candle == running_top:
cnt += 1

print cnt
```

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

05/7/19

HackerRank ‘A Very Big Sum’ Solution

Short Problem Definition:

Calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large.

A Very Big Sum

Complexity:

time complexity is O(N)

space complexity is O(1)

Execution:

Just add all of this together. No magic.

Solution:
```#!/usr/bin/py
if __name__ == '__main__':
t = input()
n = map(int, raw_input().split())
print sum(n)
```

```# RUST

use std::io;

fn get_number() -> u32 {
let mut line = String::new();
line.trim().parse::<u32>().unwrap()
}

fn get_numbers() -> Vec<u32> {
let mut line = String::new();
line.split_whitespace().map(|s| s.parse::<u32>().unwrap()).collect()
}

fn main() {
get_number();
let sum = get_numbers().iter().fold(0u64, |a, &amp;b| a + b as u64);
println!("{}", sum)
}
```

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

05/6/19

HackerRank ‘New Year Chaos’ Solution

Short Problem Definition:

It’s New Year’s Day and everyone’s in line for the Wonderland rollercoaster ride! There are a number of people queued up, and each person wears a sticker indicating their initial position in the queue. Initial positions increment by 1 from 1 at the front of the line to N at the back.

Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original places in line. One person can bribe at most two others. For example, if n = 8 and Person 5 bribes Person 4, the queue will look like this: 1, 2, 3, 5, 4, 6, 7, 8.

Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state!

New Year Chaos

Complexity:

time complexity is O(N^2)

space complexity is O(1)

Execution:

This task is solvable in O(N), but I first attempted to solve the Naive way. This same solution times if the inner loop is allowed to start at 0. Limiting the search to one position ahead of the original position passes all HR tests cases.

The moral of the story? Why write 100 lines of code, if 8 are enough.

Solution:
```def minimumBribes(q):
moves = 0
for pos, val in enumerate(q):
if (val-1) - pos > 2:
return "Too chaotic"
for j in xrange(max(0,val-2), pos):
if q[j] > val:
moves+=1
return moves
```

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

05/6/19

Posting Schedule Sprint/Summer 2019

This page has 85 HackerRank Tasks posted as of May 2019. After looking over my HR profile, I noticed that I had solved a total of 200 Tasks there. In other words, I have not posted the majority of them 🙂

I have also run out of Tasks marked as Easy. I still have around 60 Medium ones that need solving.

It is unlikely, that I can just clean all of that up in a single session. I have therefore created a posting rhythm that should allow me to get all that code up here within a reasonable time frame.

I will be posting five posts every week until the end of September. Four of those will be Tasks that I have solved in the past, and only minor cleanups are required. Additionally, I will be posting one new Medium or harder Task every week.

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.