mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish bst min challenge and solution (#76)
Update constraints, algorithm discussion, and code.
This commit is contained in:
parent
c24a628329
commit
1321723eb9
|
@ -39,6 +39,8 @@
|
||||||
"* Are the array elements unique?\n",
|
"* Are the array elements unique?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Can we assume we already have a Node class with an insert method?\n",
|
"* Can we assume we already have a Node class with an insert method?\n",
|
||||||
|
" * Yes\n",
|
||||||
|
"* Can we assume this fits memory?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -90,11 +92,11 @@
|
||||||
"source": [
|
"source": [
|
||||||
"def create_min_bst(array):\n",
|
"def create_min_bst(array):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" # Calls __create_min_bst__\n",
|
" # Calls _create_min_bst\n",
|
||||||
" pass \n",
|
" pass \n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def __create_min_bst__(array, start, end):\n",
|
"def _create_min_bst(array, start, end):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass"
|
" pass"
|
||||||
]
|
]
|
||||||
|
@ -171,21 +173,21 @@
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
|
@ -38,6 +38,8 @@
|
||||||
"* Are the array elements unique?\n",
|
"* Are the array elements unique?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Can we assume we already have a Node class with an insert method?\n",
|
"* Can we assume we already have a Node class with an insert method?\n",
|
||||||
|
" * Yes\n",
|
||||||
|
"* Can we assume this fits memory?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -103,16 +105,16 @@
|
||||||
"def create_min_bst(array):\n",
|
"def create_min_bst(array):\n",
|
||||||
" if array is None:\n",
|
" if array is None:\n",
|
||||||
" return\n",
|
" return\n",
|
||||||
" return __create_min_bst__(array, 0, len(array)-1)\n",
|
" return _create_min_bst(array, 0, len(array)-1)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def __create_min_bst__(array, start, end):\n",
|
"def _create_min_bst(array, start, end):\n",
|
||||||
" if end < start:\n",
|
" if end < start:\n",
|
||||||
" return\n",
|
" return None\n",
|
||||||
" mid = (start + end) // 2\n",
|
" mid = (start + end) // 2\n",
|
||||||
" node = Node(array[mid])\n",
|
" node = Node(array[mid])\n",
|
||||||
" node.left = __create_min_bst__(array, start, mid-1)\n",
|
" node.left = _create_min_bst(array, start, mid-1)\n",
|
||||||
" node.right = __create_min_bst__(array, mid+1, end)\n",
|
" node.right = _create_min_bst(array, mid+1, end)\n",
|
||||||
" return node"
|
" return node"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -199,21 +201,21 @@
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user