Skip to content

Commit 14bac48

Browse files
author
bitoollearner
committed
LeetCode Pyspark Solution
1 parent 122772a commit 14bac48

12 files changed

+1674
-81
lines changed

Solved/2142. The Number of Passengers in Each Bus I (Medium)-(Solved).ipynb

Lines changed: 157 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"cell_type": "markdown",
55
"metadata": {
66
"application/vnd.databricks.v1+cell": {
7-
"cellMetadata": {},
7+
"cellMetadata": {
8+
"byteLimit": 2048000,
9+
"rowLimit": 10000
10+
},
811
"inputWidgets": {},
912
"nuid": "a23f6d56-6d8d-44bc-b796-cedda9e17c21",
1013
"showTitle": false,
@@ -21,7 +24,10 @@
2124
"execution_count": 0,
2225
"metadata": {
2326
"application/vnd.databricks.v1+cell": {
24-
"cellMetadata": {},
27+
"cellMetadata": {
28+
"byteLimit": 2048000,
29+
"rowLimit": 10000
30+
},
2531
"inputWidgets": {},
2632
"nuid": "0a69d78b-43d7-4f9e-a05d-026985c2539f",
2733
"showTitle": false,
@@ -40,7 +46,10 @@
4046
"cell_type": "markdown",
4147
"metadata": {
4248
"application/vnd.databricks.v1+cell": {
43-
"cellMetadata": {},
49+
"cellMetadata": {
50+
"byteLimit": 2048000,
51+
"rowLimit": 10000
52+
},
4453
"inputWidgets": {},
4554
"nuid": "2a54b88b-06bd-43e4-978a-c0c5d33ca5bb",
4655
"showTitle": false,
@@ -122,15 +131,27 @@
122131
"execution_count": 0,
123132
"metadata": {
124133
"application/vnd.databricks.v1+cell": {
125-
"cellMetadata": {},
134+
"cellMetadata": {
135+
"byteLimit": 2048000,
136+
"rowLimit": 10000
137+
},
126138
"inputWidgets": {},
127139
"nuid": "c260f510-542b-4067-a6f4-ffed86995056",
128140
"showTitle": false,
129141
"tableResultSettingsMap": {},
130142
"title": ""
131143
}
132144
},
133-
"outputs": [],
145+
"outputs": [
146+
{
147+
"output_type": "stream",
148+
"name": "stdout",
149+
"output_type": "stream",
150+
"text": [
151+
"+------+------------+\n|bus_id|arrival_time|\n+------+------------+\n| 1| 2|\n| 2| 4|\n| 3| 7|\n+------+------------+\n\n+------------+------------+\n|passenger_id|arrival_time|\n+------------+------------+\n| 11| 1|\n| 12| 5|\n| 13| 6|\n| 14| 7|\n+------------+------------+\n\n"
152+
]
153+
}
154+
],
134155
"source": [
135156
"buses_data_2142 = [\n",
136157
" (1, 2),\n",
@@ -153,15 +174,144 @@
153174
"passengers_df_2142 = spark.createDataFrame(passengers_data_2142, passengers_columns_2142)\n",
154175
"passengers_df_2142.show()"
155176
]
177+
},
178+
{
179+
"cell_type": "code",
180+
"execution_count": 0,
181+
"metadata": {
182+
"application/vnd.databricks.v1+cell": {
183+
"cellMetadata": {
184+
"byteLimit": 2048000,
185+
"rowLimit": 10000
186+
},
187+
"inputWidgets": {},
188+
"nuid": "afaca1d0-79e0-4ffc-9c77-79c6fc7e38f4",
189+
"showTitle": false,
190+
"tableResultSettingsMap": {},
191+
"title": ""
192+
}
193+
},
194+
"outputs": [],
195+
"source": [
196+
"joined_df_2142 = passengers_df_2142.alias(\"p\")\\\n",
197+
" .join( buses_df_2142.alias(\"b\"), col(\"p.arrival_time\") <= col(\"b.arrival_time\"))"
198+
]
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": 0,
203+
"metadata": {
204+
"application/vnd.databricks.v1+cell": {
205+
"cellMetadata": {
206+
"byteLimit": 2048000,
207+
"rowLimit": 10000
208+
},
209+
"inputWidgets": {},
210+
"nuid": "25a51bb8-bf85-4f6b-b942-84c2d4dbc22d",
211+
"showTitle": false,
212+
"tableResultSettingsMap": {},
213+
"title": ""
214+
}
215+
},
216+
"outputs": [],
217+
"source": [
218+
"windowSpec = Window.partitionBy(\"p.passenger_id\")"
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"execution_count": 0,
224+
"metadata": {
225+
"application/vnd.databricks.v1+cell": {
226+
"cellMetadata": {
227+
"byteLimit": 2048000,
228+
"rowLimit": 10000
229+
},
230+
"inputWidgets": {},
231+
"nuid": "9dfccb64-9516-4036-afce-8b228a9a05e6",
232+
"showTitle": false,
233+
"tableResultSettingsMap": {},
234+
"title": ""
235+
}
236+
},
237+
"outputs": [],
238+
"source": [
239+
"passenger_bus_df_2142 = joined_df_2142\\\n",
240+
" .withColumn( \"min_bus_time\", min(\"b.arrival_time\").over(windowSpec))\\\n",
241+
" .where(col(\"b.arrival_time\") == col(\"min_bus_time\")) \\\n",
242+
" .select(\"p.passenger_id\", \"b.bus_id\")"
243+
]
244+
},
245+
{
246+
"cell_type": "code",
247+
"execution_count": 0,
248+
"metadata": {
249+
"application/vnd.databricks.v1+cell": {
250+
"cellMetadata": {
251+
"byteLimit": 2048000,
252+
"rowLimit": 10000
253+
},
254+
"inputWidgets": {},
255+
"nuid": "45a272fb-7400-4f5a-acc5-43e18e8745fc",
256+
"showTitle": false,
257+
"tableResultSettingsMap": {},
258+
"title": ""
259+
}
260+
},
261+
"outputs": [],
262+
"source": [
263+
"bus_counts_df_2142 = passenger_bus_df_2142\\\n",
264+
" .groupBy(\"bus_id\")\\\n",
265+
" .agg(count(\"passenger_id\").alias(\"passengers_cnt\")\n",
266+
" )"
267+
]
268+
},
269+
{
270+
"cell_type": "code",
271+
"execution_count": 0,
272+
"metadata": {
273+
"application/vnd.databricks.v1+cell": {
274+
"cellMetadata": {
275+
"byteLimit": 2048000,
276+
"rowLimit": 10000
277+
},
278+
"inputWidgets": {},
279+
"nuid": "860f8f74-09f1-4067-8df0-45d9fcf6c3b5",
280+
"showTitle": false,
281+
"tableResultSettingsMap": {},
282+
"title": ""
283+
}
284+
},
285+
"outputs": [
286+
{
287+
"output_type": "stream",
288+
"name": "stdout",
289+
"output_type": "stream",
290+
"text": [
291+
"+------+--------------+\n|bus_id|passengers_cnt|\n+------+--------------+\n| 1| 1|\n| 2| 0|\n| 3| 3|\n+------+--------------+\n\n"
292+
]
293+
}
294+
],
295+
"source": [
296+
"buses_df_2142\\\n",
297+
" .join(bus_counts_df_2142, \"bus_id\", \"left\").fillna(0).orderBy(\"bus_id\")\\\n",
298+
" .select('bus_id','passengers_cnt').show()"
299+
]
156300
}
157301
],
158302
"metadata": {
159303
"application/vnd.databricks.v1+notebook": {
160-
"computePreferences": null,
304+
"computePreferences": {
305+
"hardware": {
306+
"accelerator": null,
307+
"gpuPoolId": null,
308+
"memory": null
309+
}
310+
},
161311
"dashboards": [],
162312
"environmentMetadata": {
163313
"base_environment": "",
164-
"environment_version": "1"
314+
"environment_version": "2"
165315
},
166316
"inputWidgetPreferences": null,
167317
"language": "python",

0 commit comments

Comments
 (0)