Skip to content

Commit 49a6033

Browse files
author
bitoollearner
committed
Leetcode Pyspark Solution
1 parent 7780899 commit 49a6033

6 files changed

+667
-42
lines changed

Solved/1407. Top Travellers (Easy)-(Solved).ipynb

Lines changed: 93 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": "24f8e8c0-d56b-4821-a900-bbe934dce4d7",
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": "243b190c-af0b-4bd1-9507-8e2be2fceca3",
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": "2d9dc49c-3f2c-4ff6-8e98-5e72a86b1bac",
4655
"showTitle": false,
@@ -131,15 +140,27 @@
131140
"execution_count": 0,
132141
"metadata": {
133142
"application/vnd.databricks.v1+cell": {
134-
"cellMetadata": {},
143+
"cellMetadata": {
144+
"byteLimit": 2048000,
145+
"rowLimit": 10000
146+
},
135147
"inputWidgets": {},
136148
"nuid": "5d66d20c-58e2-4ee9-8356-d736216372c3",
137149
"showTitle": false,
138150
"tableResultSettingsMap": {},
139151
"title": ""
140152
}
141153
},
142-
"outputs": [],
154+
"outputs": [
155+
{
156+
"output_type": "stream",
157+
"name": "stdout",
158+
"output_type": "stream",
159+
"text": [
160+
"+---+--------+\n| id| name|\n+---+--------+\n| 1| Alice|\n| 2| Bob|\n| 3| Alex|\n| 4| Donald|\n| 7| Lee|\n| 13|Jonathan|\n| 19| Elvis|\n+---+--------+\n\n+---+-------+--------+\n| id|user_id|distance|\n+---+-------+--------+\n| 1| 1| 120|\n| 2| 2| 317|\n| 3| 3| 222|\n| 4| 7| 100|\n| 5| 13| 312|\n| 6| 19| 50|\n| 7| 7| 120|\n| 8| 19| 400|\n| 9| 7| 230|\n+---+-------+--------+\n\n"
161+
]
162+
}
163+
],
143164
"source": [
144165
"users_data_1407 = [\n",
145166
" (1, \"Alice\"),\n",
@@ -171,15 +192,80 @@
171192
"rides_df_1407 = spark.createDataFrame(rides_data_1407, rides_columns_1407)\n",
172193
"rides_df_1407.show()"
173194
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": 0,
199+
"metadata": {
200+
"application/vnd.databricks.v1+cell": {
201+
"cellMetadata": {
202+
"byteLimit": 2048000,
203+
"rowLimit": 10000
204+
},
205+
"inputWidgets": {},
206+
"nuid": "b9ad7a50-3a2a-43de-8758-6270e002f969",
207+
"showTitle": false,
208+
"tableResultSettingsMap": {},
209+
"title": ""
210+
}
211+
},
212+
"outputs": [],
213+
"source": [
214+
"rides_sum_df_1407 = rides_df_1407\\\n",
215+
" .groupBy(\"user_id\") \\\n",
216+
" .agg(sum(\"distance\").alias(\"travelled_distance\"))"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": 0,
222+
"metadata": {
223+
"application/vnd.databricks.v1+cell": {
224+
"cellMetadata": {
225+
"byteLimit": 2048000,
226+
"rowLimit": 10000
227+
},
228+
"inputWidgets": {},
229+
"nuid": "0efadd04-4618-4d69-8c74-857b6277489d",
230+
"showTitle": false,
231+
"tableResultSettingsMap": {},
232+
"title": ""
233+
}
234+
},
235+
"outputs": [
236+
{
237+
"output_type": "stream",
238+
"name": "stdout",
239+
"output_type": "stream",
240+
"text": [
241+
"+--------+------------------+\n| name|travelled_distance|\n+--------+------------------+\n| Elvis| 450|\n| Lee| 450|\n| Bob| 317|\n|Jonathan| 312|\n| Alex| 222|\n| Alice| 120|\n| Donald| 0|\n+--------+------------------+\n\n"
242+
]
243+
}
244+
],
245+
"source": [
246+
"users_df_1407\\\n",
247+
" .join(rides_sum_df_1407, users_df_1407.id == rides_sum_df_1407.user_id, \"left\") \\\n",
248+
" .select(\n",
249+
" col(\"name\"),\n",
250+
" coalesce(col(\"travelled_distance\"), lit(0)).alias(\"travelled_distance\")\n",
251+
" )\\\n",
252+
" .orderBy(col(\"travelled_distance\").desc(), col(\"name\").asc()).show()"
253+
]
174254
}
175255
],
176256
"metadata": {
177257
"application/vnd.databricks.v1+notebook": {
178-
"computePreferences": null,
258+
"computePreferences": {
259+
"hardware": {
260+
"accelerator": null,
261+
"gpuPoolId": null,
262+
"memory": null
263+
}
264+
},
179265
"dashboards": [],
180266
"environmentMetadata": {
181267
"base_environment": "",
182-
"environment_version": "1"
268+
"environment_version": "2"
183269
},
184270
"inputWidgetPreferences": null,
185271
"language": "python",

Solved/1412. Find the Quiet Students in All Exam (Hard)-(Solved).ipynb

Lines changed: 158 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": "24f8e8c0-d56b-4821-a900-bbe934dce4d7",
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": "243b190c-af0b-4bd1-9507-8e2be2fceca3",
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": "2d9dc49c-3f2c-4ff6-8e98-5e72a86b1bac",
4655
"showTitle": false,
@@ -126,15 +135,27 @@
126135
"execution_count": 0,
127136
"metadata": {
128137
"application/vnd.databricks.v1+cell": {
129-
"cellMetadata": {},
138+
"cellMetadata": {
139+
"byteLimit": 2048000,
140+
"rowLimit": 10000
141+
},
130142
"inputWidgets": {},
131143
"nuid": "5d66d20c-58e2-4ee9-8356-d736216372c3",
132144
"showTitle": false,
133145
"tableResultSettingsMap": {},
134146
"title": ""
135147
}
136148
},
137-
"outputs": [],
149+
"outputs": [
150+
{
151+
"output_type": "stream",
152+
"name": "stdout",
153+
"output_type": "stream",
154+
"text": [
155+
"+----------+------------+\n|student_id|student_name|\n+----------+------------+\n| 1| Daniel|\n| 2| Jade|\n| 3| Stella|\n| 4| Jonathan|\n| 5| Will|\n+----------+------------+\n\n+-------+----------+-----+\n|exam_id|student_id|score|\n+-------+----------+-----+\n| 10| 1| 70|\n| 10| 2| 80|\n| 10| 3| 90|\n| 20| 1| 80|\n| 30| 1| 70|\n| 30| 3| 80|\n| 30| 4| 90|\n| 40| 1| 60|\n| 40| 2| 70|\n| 40| 4| 80|\n+-------+----------+-----+\n\n"
156+
]
157+
}
158+
],
138159
"source": [
139160
"students_data_1412 = [\n",
140161
" (1, \"Daniel\"),\n",
@@ -163,15 +184,145 @@
163184
"exams_df_1412 = spark.createDataFrame(exams_data_1412, exams_columns_1412)\n",
164185
"exams_df_1412.show()"
165186
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": 0,
191+
"metadata": {
192+
"application/vnd.databricks.v1+cell": {
193+
"cellMetadata": {
194+
"byteLimit": 2048000,
195+
"rowLimit": 10000
196+
},
197+
"inputWidgets": {},
198+
"nuid": "e0e45429-9fd7-4d46-b7c7-afcaebc7ccca",
199+
"showTitle": false,
200+
"tableResultSettingsMap": {},
201+
"title": ""
202+
}
203+
},
204+
"outputs": [],
205+
"source": [
206+
"exam_scores_extremes_1412 = exams_df_1412\\\n",
207+
" .groupBy(\"exam_id\").agg(\n",
208+
" max(\"score\").alias(\"max_score\"),\n",
209+
" min(\"score\").alias(\"min_score\")\n",
210+
" )"
211+
]
212+
},
213+
{
214+
"cell_type": "code",
215+
"execution_count": 0,
216+
"metadata": {
217+
"application/vnd.databricks.v1+cell": {
218+
"cellMetadata": {
219+
"byteLimit": 2048000,
220+
"rowLimit": 10000
221+
},
222+
"inputWidgets": {},
223+
"nuid": "c63c1f95-9f20-4ebd-9d6b-ab7dd241ee72",
224+
"showTitle": false,
225+
"tableResultSettingsMap": {},
226+
"title": ""
227+
}
228+
},
229+
"outputs": [],
230+
"source": [
231+
"extreme_students_1412 = exams_df_1412\\\n",
232+
" .join(exam_scores_extremes_1412, on=\"exam_id\") \\\n",
233+
" .filter((col(\"score\") == col(\"max_score\")) | (col(\"score\") == col(\"min_score\"))) \\\n",
234+
" .select(\"student_id\").distinct()"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": 0,
240+
"metadata": {
241+
"application/vnd.databricks.v1+cell": {
242+
"cellMetadata": {
243+
"byteLimit": 2048000,
244+
"rowLimit": 10000
245+
},
246+
"inputWidgets": {},
247+
"nuid": "a0cdb742-949d-42bd-8a61-e63cac842647",
248+
"showTitle": false,
249+
"tableResultSettingsMap": {},
250+
"title": ""
251+
}
252+
},
253+
"outputs": [],
254+
"source": [
255+
"students_with_exams_1412 = exams_df_1412.select(\"student_id\").distinct()"
256+
]
257+
},
258+
{
259+
"cell_type": "code",
260+
"execution_count": 0,
261+
"metadata": {
262+
"application/vnd.databricks.v1+cell": {
263+
"cellMetadata": {
264+
"byteLimit": 2048000,
265+
"rowLimit": 10000
266+
},
267+
"inputWidgets": {},
268+
"nuid": "d9468b79-d159-4871-a756-b2d37830960f",
269+
"showTitle": false,
270+
"tableResultSettingsMap": {},
271+
"title": ""
272+
}
273+
},
274+
"outputs": [],
275+
"source": [
276+
"quiet_students_1412 = students_with_exams_1412.join(extreme_students_1412, on=\"student_id\", how=\"left_anti\")"
277+
]
278+
},
279+
{
280+
"cell_type": "code",
281+
"execution_count": 0,
282+
"metadata": {
283+
"application/vnd.databricks.v1+cell": {
284+
"cellMetadata": {
285+
"byteLimit": 2048000,
286+
"rowLimit": 10000
287+
},
288+
"inputWidgets": {},
289+
"nuid": "0a2549f6-43f1-44f8-af13-7271fc6a59e6",
290+
"showTitle": false,
291+
"tableResultSettingsMap": {},
292+
"title": ""
293+
}
294+
},
295+
"outputs": [
296+
{
297+
"output_type": "stream",
298+
"name": "stdout",
299+
"output_type": "stream",
300+
"text": [
301+
"+----------+------------+\n|student_id|student_name|\n+----------+------------+\n| 2| Jade|\n+----------+------------+\n\n"
302+
]
303+
}
304+
],
305+
"source": [
306+
"quiet_students_1412\\\n",
307+
" .join(students_df_1412, on=\"student_id\") \\\n",
308+
" .select(\"student_id\", \"student_name\") \\\n",
309+
" .orderBy(\"student_id\").show()"
310+
]
166311
}
167312
],
168313
"metadata": {
169314
"application/vnd.databricks.v1+notebook": {
170-
"computePreferences": null,
315+
"computePreferences": {
316+
"hardware": {
317+
"accelerator": null,
318+
"gpuPoolId": null,
319+
"memory": null
320+
}
321+
},
171322
"dashboards": [],
172323
"environmentMetadata": {
173324
"base_environment": "",
174-
"environment_version": "1"
325+
"environment_version": "2"
175326
},
176327
"inputWidgetPreferences": null,
177328
"language": "python",

0 commit comments

Comments
 (0)