Skip to content

Solution update for Python programming problems #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.ipynb_checkpoints
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -702,7 +702,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,74 @@
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hint: Sort an array and take difference of max and min element <br>\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!<br>\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<min_so_far:\n",
" min_so_far=num\n",
" return (max_so_far-min_so_far)\n",
"solve(arr)"
]
},
{
Expand All @@ -49,11 +110,32 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 28,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## Solution"
"## Solution\n",
"\n",
"arr = [3,1,2,5,3]\n",
"def solve(arr):\n",
" hashmap = {}\n",
" for num in arr:\n",
" if hashmap.get(num,0)==1:\n",
" return num\n",
" break\n",
" hashmap[num]=1\n",
"solve(arr) "
]
},
{
Expand All @@ -72,17 +154,61 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 33,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## Solution"
"## Solution\n",
"# Approach to track right max and left max at each position and remove height of bar from each to get total water at each location\n",
"height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]\n",
"\n",
"def solve(height):\n",
" n = len(height)\n",
" right_max = [0]*n\n",
" left_max = [0]*n\n",
" \n",
" # calulate left max at each location\n",
" left_max[0] = height[0]\n",
" for i in range(1,n):\n",
" left_max[i]=max(left_max[i-1], height[i])\n",
" \n",
" # calulate right max at each location\n",
" right_max[n-1]= height[n-1]\n",
" for i in range(n-2,0,-1):\n",
" right_max[i]=max(right_max[i+1], height[i])\n",
"\n",
" # total water trapped min(left_max,right_max)-height of bar at location\n",
" water = 0\n",
" for i in range(n):\n",
" water+=min(right_max[i],left_max[i])-height[i]\n",
" \n",
" return water\n",
"\n",
"solve(height)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -96,7 +222,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down