From cc36966dd68afb25bc995988f29d545176dc4690 Mon Sep 17 00:00:00 2001 From: Yashpal Ahlawat Date: Sat, 20 Jan 2024 20:29:54 +0530 Subject: [PATCH] Solution update for Python programming problems --- .gitignore | 1 + .../Problem Solving Part 1.ipynb | 4 +- .../Problem Solving Part 2.ipynb | 148 ++++++++++++++++-- 3 files changed, 140 insertions(+), 13 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..58461f25 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.ipynb_checkpoints \ No newline at end of file diff --git a/Module 1 - Python Programming/12. Problem Solving for Interviews/Problem Solving Part 1.ipynb b/Module 1 - Python Programming/12. Problem Solving for Interviews/Problem Solving Part 1.ipynb index af95adee..ef4284e3 100644 --- a/Module 1 - Python Programming/12. Problem Solving for Interviews/Problem Solving Part 1.ipynb +++ b/Module 1 - Python Programming/12. Problem Solving for Interviews/Problem Solving Part 1.ipynb @@ -688,7 +688,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -702,7 +702,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.5" + "version": "3.12.1" } }, "nbformat": 4, diff --git a/Module 1 - Python Programming/12. Problem Solving for Interviews/Problem Solving Part 2.ipynb b/Module 1 - Python Programming/12. Problem Solving for Interviews/Problem Solving Part 2.ipynb index 2869d82b..13e8f8ad 100644 --- a/Module 1 - Python Programming/12. Problem Solving for Interviews/Problem Solving Part 2.ipynb +++ b/Module 1 - Python Programming/12. Problem Solving for Interviews/Problem Solving Part 2.ipynb @@ -21,13 +21,74 @@ "```" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Hint: Sort an array and take difference of max and min element
\n", + "Time Complexity *O(nlogn)* for sorting the array" + ] + }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def solve(arr):\n", + " arr.sort(reverse=True)\n", + " ans = arr[0]-arr[-1]\n", + " return ans\n", + "arr = [0, -1, 5, 7, 10]\n", + "solve(arr)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Can it be further optimised? Think!
\n", + "Can we do it with *O(n)* complexity" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "## Solution" + "arr = [0, -1, 5, 7, 10]\n", + "def solve(arr):\n", + " max_so_far, min_so_far = -10000007,10000007\n", + " for num in arr:\n", + " if num>max_so_far:\n", + " max_so_far=num\n", + " if num