Added notebook solving the following: HackerRank Utopian Tree problem.

This commit is contained in:
Donne Martin 2015-05-21 05:58:08 -04:00
parent 6beb2282c4
commit 666fdd77a8
2 changed files with 95 additions and 1 deletions

View File

@ -30,7 +30,6 @@ Continually updated IPython Notebooks containing coding problems and solutions (
* [Find the start of a linked list loop](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/find-loop-start.ipynb) * [Find the start of a linked list loop](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/find-loop-start.ipynb)
* [Determine if a linked list is a palindrome](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/palindrome.ipynb) * [Determine if a linked list is a palindrome](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/palindrome.ipynb)
## Stacks and Queues ## Stacks and Queues
* [Implement a stack with push, pop, and peek methods using a linked list](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/stack.ipynb) * [Implement a stack with push, pop, and peek methods using a linked list](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/stack.ipynb)
@ -40,6 +39,10 @@ Continually updated IPython Notebooks containing coding problems and solutions (
* [Implement a set of stacks class that wraps a list of stacks, each bound by a capacity](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/set-of-stacks.ipynb) * [Implement a set of stacks class that wraps a list of stacks, each bound by a capacity](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/set-of-stacks.ipynb)
* [Implement the Towers of Hanoi with 3 towers and N disks](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/hanoi.ipynb) * [Implement the Towers of Hanoi with 3 towers and N disks](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/stacks-queues/hanoi.ipynb)
## Hacker Rank
* [Utopian Tree](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/hacker-rank/utopian-tree.ipynb)
## License ## License
Copyright 2015 Donne Martin Copyright 2015 Donne Martin

View File

@ -0,0 +1,91 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Utopian Tree\n",
"\n",
"From HackerRank: https://www.hackerrank.com/challenges/utopian-tree\n",
"\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"* If cycles is 0, return height of 1\n",
"* For 1 to cycles:\n",
" * If cycle is odd, double height\n",
" * Else, increment height\n",
"* Return height\n",
"\n",
"Complexity:\n",
"* Time: O(n), where n is the number of cycles, for each cycle we perform a calculation\n",
"* Space: O(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# cycles = 0, print 1: base case\n",
"# cycles = 1, print 2: i = 1: 1 * 2\n",
"# cycles = 4, print 7: i = 1: 1 * 2, i = 2: 2 + 1, i = 3: 3 * 2, i = 4: 6 + 1\n",
"def calc_utopian_tree_height(cycles):\n",
" height = 1\n",
" if cycles == 0:\n",
" return height\n",
" for i in xrange(1, cycles+1):\n",
" if i % 2 == 1:\n",
" height *= 2\n",
" else:\n",
" height += 1\n",
" return height\n",
"\n",
"num_lines = int(raw_input())\n",
"for i in range(0, num_lines):\n",
" cycles = raw_input()\n",
" cycles = int(cycles)\n",
" res = calc_utopian_tree_height(cycles)\n",
" print res"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}