Skip to content

Commit 4f75a0c

Browse files
author
bitoollearner
committed
LeetCode Pyspark Solution
1 parent 3dd748c commit 4f75a0c

5 files changed

+685
-35
lines changed

Solved/1613. Find the Missing IDs (Medium)-(Solved).ipynb

Lines changed: 109 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": "f6ac9266-658c-4507-8a37-ecc81334b06d",
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": "d6194993-9312-4b46-8365-d6a73d8cb0b0",
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": "23f6a680-8f01-4976-83fa-88103e49c6b8",
4655
"showTitle": false,
@@ -96,31 +105,124 @@
96105
"execution_count": 0,
97106
"metadata": {
98107
"application/vnd.databricks.v1+cell": {
99-
"cellMetadata": {},
108+
"cellMetadata": {
109+
"byteLimit": 2048000,
110+
"rowLimit": 10000
111+
},
100112
"inputWidgets": {},
101113
"nuid": "a762f84d-f622-4403-b695-4066e5bdbaf0",
102114
"showTitle": false,
103115
"tableResultSettingsMap": {},
104116
"title": ""
105117
}
106118
},
107-
"outputs": [],
119+
"outputs": [
120+
{
121+
"output_type": "stream",
122+
"name": "stdout",
123+
"output_type": "stream",
124+
"text": [
125+
"+-----------+-------------+\n|customer_id|customer_name|\n+-----------+-------------+\n| 1| Alice|\n| 4| Bob|\n| 5| Charlie|\n+-----------+-------------+\n\n"
126+
]
127+
}
128+
],
108129
"source": [
109130
"customers_data_1613 = [(1, \"Alice\"), (4, \"Bob\"), (5, \"Charlie\")]\n",
110131
"\n",
111132
"customers_columns_1613 = [\"customer_id\", \"customer_name\"]\n",
112133
"customers_df_1613 = spark.createDataFrame(customers_data_1613, customers_columns_1613)\n",
113134
"customers_df_1613.show()"
114135
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": 0,
140+
"metadata": {
141+
"application/vnd.databricks.v1+cell": {
142+
"cellMetadata": {
143+
"byteLimit": 2048000,
144+
"rowLimit": 10000
145+
},
146+
"inputWidgets": {},
147+
"nuid": "5578a3a0-1042-4671-ae1c-e432556bf4fa",
148+
"showTitle": false,
149+
"tableResultSettingsMap": {},
150+
"title": ""
151+
}
152+
},
153+
"outputs": [],
154+
"source": [
155+
"max_id_1613 = customers_df_1613.agg(max(\"customer_id\")).collect()[0][0]"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": 0,
161+
"metadata": {
162+
"application/vnd.databricks.v1+cell": {
163+
"cellMetadata": {
164+
"byteLimit": 2048000,
165+
"rowLimit": 10000
166+
},
167+
"inputWidgets": {},
168+
"nuid": "64c56a28-74cf-453d-8b87-02ff956f7647",
169+
"showTitle": false,
170+
"tableResultSettingsMap": {},
171+
"title": ""
172+
}
173+
},
174+
"outputs": [],
175+
"source": [
176+
"all_ids_1613 = spark.range(1, max_id_1613 + 1).withColumnRenamed(\"id\", \"ids\")"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": 0,
182+
"metadata": {
183+
"application/vnd.databricks.v1+cell": {
184+
"cellMetadata": {
185+
"byteLimit": 2048000,
186+
"rowLimit": 10000
187+
},
188+
"inputWidgets": {},
189+
"nuid": "fbad7c41-75bf-4f7a-a9ab-141e1b4b4b69",
190+
"showTitle": false,
191+
"tableResultSettingsMap": {},
192+
"title": ""
193+
}
194+
},
195+
"outputs": [
196+
{
197+
"output_type": "stream",
198+
"name": "stdout",
199+
"output_type": "stream",
200+
"text": [
201+
"+---+\n|ids|\n+---+\n| 2|\n| 3|\n+---+\n\n"
202+
]
203+
}
204+
],
205+
"source": [
206+
"all_ids_1613\\\n",
207+
" .join(customers_df_1613, all_ids_1613.ids == customers_df_1613.customer_id, \"left_anti\") \\\n",
208+
" .select(\"ids\") \\\n",
209+
" .orderBy(\"ids\").show()"
210+
]
115211
}
116212
],
117213
"metadata": {
118214
"application/vnd.databricks.v1+notebook": {
119-
"computePreferences": null,
215+
"computePreferences": {
216+
"hardware": {
217+
"accelerator": null,
218+
"gpuPoolId": null,
219+
"memory": null
220+
}
221+
},
120222
"dashboards": [],
121223
"environmentMetadata": {
122224
"base_environment": "",
123-
"environment_version": "1"
225+
"environment_version": "2"
124226
},
125227
"inputWidgetPreferences": null,
126228
"language": "python",

Solved/1623. All Valid Triplets That Can Represent a Country (Easy)-(Solved).ipynb

Lines changed: 125 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": "f6ac9266-658c-4507-8a37-ecc81334b06d",
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": "d6194993-9312-4b46-8365-d6a73d8cb0b0",
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": "23f6a680-8f01-4976-83fa-88103e49c6b8",
4655
"showTitle": false,
@@ -139,15 +148,27 @@
139148
"execution_count": 0,
140149
"metadata": {
141150
"application/vnd.databricks.v1+cell": {
142-
"cellMetadata": {},
151+
"cellMetadata": {
152+
"byteLimit": 2048000,
153+
"rowLimit": 10000
154+
},
143155
"inputWidgets": {},
144156
"nuid": "a762f84d-f622-4403-b695-4066e5bdbaf0",
145157
"showTitle": false,
146158
"tableResultSettingsMap": {},
147159
"title": ""
148160
}
149161
},
150-
"outputs": [],
162+
"outputs": [
163+
{
164+
"output_type": "stream",
165+
"name": "stdout",
166+
"output_type": "stream",
167+
"text": [
168+
"+----------+------------+\n|student_id|student_name|\n+----------+------------+\n| 1| Alice|\n| 2| Bob|\n+----------+------------+\n\n+----------+------------+\n|student_id|student_name|\n+----------+------------+\n| 3| Tom|\n+----------+------------+\n\n+----------+------------+\n|student_id|student_name|\n+----------+------------+\n| 3| Tom|\n| 2| Jerry|\n| 10| Alice|\n+----------+------------+\n\n"
169+
]
170+
}
171+
],
151172
"source": [
152173
"schoolA_data_1623 = [(1, \"Alice\"), (2, \"Bob\")]\n",
153174
"\n",
@@ -167,15 +188,112 @@
167188
"schoolC_df_1623 = spark.createDataFrame(schoolC_data_1623, schoolC_columns_1623)\n",
168189
"schoolC_df_1623.show()"
169190
]
191+
},
192+
{
193+
"cell_type": "code",
194+
"execution_count": 0,
195+
"metadata": {
196+
"application/vnd.databricks.v1+cell": {
197+
"cellMetadata": {
198+
"byteLimit": 2048000,
199+
"rowLimit": 10000
200+
},
201+
"inputWidgets": {},
202+
"nuid": "0cf73bc0-f144-422f-aaa6-f6192d24cdcb",
203+
"showTitle": false,
204+
"tableResultSettingsMap": {},
205+
"title": ""
206+
}
207+
},
208+
"outputs": [],
209+
"source": [
210+
"A = schoolA_df_1623.alias(\"A\")\n",
211+
"B = schoolB_df_1623.alias(\"B\")\n",
212+
"C = schoolC_df_1623.alias(\"C\")"
213+
]
214+
},
215+
{
216+
"cell_type": "code",
217+
"execution_count": 0,
218+
"metadata": {
219+
"application/vnd.databricks.v1+cell": {
220+
"cellMetadata": {
221+
"byteLimit": 2048000,
222+
"rowLimit": 10000
223+
},
224+
"inputWidgets": {},
225+
"nuid": "fd5c5e79-9609-4fab-8009-5adbe0731079",
226+
"showTitle": false,
227+
"tableResultSettingsMap": {},
228+
"title": ""
229+
}
230+
},
231+
"outputs": [],
232+
"source": [
233+
"triplets_df_1623 = A.crossJoin(B).crossJoin(C) \\\n",
234+
" .select(\n",
235+
" col(\"A.student_id\").alias(\"id_A\"),\n",
236+
" col(\"A.student_name\").alias(\"member_A\"),\n",
237+
" col(\"B.student_id\").alias(\"id_B\"),\n",
238+
" col(\"B.student_name\").alias(\"member_B\"),\n",
239+
" col(\"C.student_id\").alias(\"id_C\"),\n",
240+
" col(\"C.student_name\").alias(\"member_C\")\n",
241+
" )"
242+
]
243+
},
244+
{
245+
"cell_type": "code",
246+
"execution_count": 0,
247+
"metadata": {
248+
"application/vnd.databricks.v1+cell": {
249+
"cellMetadata": {
250+
"byteLimit": 2048000,
251+
"rowLimit": 10000
252+
},
253+
"inputWidgets": {},
254+
"nuid": "1de95fdd-d2f4-4436-beaa-793b60cfacbc",
255+
"showTitle": false,
256+
"tableResultSettingsMap": {},
257+
"title": ""
258+
}
259+
},
260+
"outputs": [
261+
{
262+
"output_type": "stream",
263+
"name": "stdout",
264+
"output_type": "stream",
265+
"text": [
266+
"+--------+--------+--------+\n|member_A|member_B|member_C|\n+--------+--------+--------+\n| Alice| Tom| Jerry|\n| Bob| Tom| Alice|\n+--------+--------+--------+\n\n"
267+
]
268+
}
269+
],
270+
"source": [
271+
"triplets_df_1623\\\n",
272+
" .filter(\n",
273+
" (col(\"id_A\") != col(\"id_B\")) &\n",
274+
" (col(\"id_A\") != col(\"id_C\")) &\n",
275+
" (col(\"id_B\") != col(\"id_C\")) &\n",
276+
" (col(\"member_A\") != col(\"member_B\")) &\n",
277+
" (col(\"member_A\") != col(\"member_C\")) &\n",
278+
" (col(\"member_B\") != col(\"member_C\"))\n",
279+
" )\\\n",
280+
" .select(\"member_A\", \"member_B\", \"member_C\").show()"
281+
]
170282
}
171283
],
172284
"metadata": {
173285
"application/vnd.databricks.v1+notebook": {
174-
"computePreferences": null,
286+
"computePreferences": {
287+
"hardware": {
288+
"accelerator": null,
289+
"gpuPoolId": null,
290+
"memory": null
291+
}
292+
},
175293
"dashboards": [],
176294
"environmentMetadata": {
177295
"base_environment": "",
178-
"environment_version": "1"
296+
"environment_version": "2"
179297
},
180298
"inputWidgetPreferences": null,
181299
"language": "python",

0 commit comments

Comments
 (0)