Skip to content

Commit

Permalink
add space and time complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
gitgik committed Feb 18, 2020
1 parent 5ba0421 commit b00f559
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions graphs/single_cycle_check.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# solution\n",
"# solution: O(n) time | O(1) space\n",
"def has_single_cycle(array):\n",
" elements_visited = 0\n",
" current_index = 0\n",
Expand All @@ -46,7 +46,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand All @@ -55,7 +55,7 @@
"True"
]
},
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -64,6 +64,14 @@
"has_single_cycle([2, -1, 1, -2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The time complexity is O(n) since we have a single while loop that iterates through the entire array of length n,\n",
"while the space complexity is O(1) constant time, since we are not storing any auxilliary array, just a couple of variables. "
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
"version": "3.7.4"
}
},
"nbformat": 4,
Expand Down

0 comments on commit b00f559

Please sign in to comment.