Skip to content

Commit f4ad990

Browse files
author
bitoollearner
committed
LeetCode PySpark Solution
1 parent 77e9e9a commit f4ad990

10 files changed

+2293
-70
lines changed

Solved/3268. Find Overlapping Shifts II (Hard)-(Solved).ipynb

Lines changed: 372 additions & 7 deletions
Large diffs are not rendered by default.

Solved/3278. Find Candidates for Data Scientist Position II (Medium)-(Solved).ipynb

Lines changed: 302 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": "7d1791bd-9552-4116-90b2-daa678bbffb8",
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": "2901fbc9-49db-4285-8b94-c460f34d4c4f",
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": "655120e7-6b25-417c-a08a-c6925feaa425",
4655
"showTitle": false,
@@ -138,15 +147,27 @@
138147
"execution_count": 0,
139148
"metadata": {
140149
"application/vnd.databricks.v1+cell": {
141-
"cellMetadata": {},
150+
"cellMetadata": {
151+
"byteLimit": 2048000,
152+
"rowLimit": 10000
153+
},
142154
"inputWidgets": {},
143155
"nuid": "a2368434-0191-416c-aa1d-12cd44cf48e6",
144156
"showTitle": false,
145157
"tableResultSettingsMap": {},
146158
"title": ""
147159
}
148160
},
149-
"outputs": [],
161+
"outputs": [
162+
{
163+
"output_type": "stream",
164+
"name": "stdout",
165+
"output_type": "stream",
166+
"text": [
167+
"+------------+----------+-----------+\n|candidate_id| skill|proficiency|\n+------------+----------+-----------+\n| 101| Python| 5|\n| 101| Tableau| 3|\n| 101|PostgreSQL| 4|\n| 101|TensorFlow| 2|\n| 102| Python| 4|\n| 102| Tableau| 5|\n| 102|PostgreSQL| 4|\n| 102| R| 4|\n| 103| Python| 3|\n| 103| Tableau| 5|\n| 103|PostgreSQL| 5|\n| 103| Spark| 4|\n+------------+----------+-----------+\n\n+----------+----------+----------+\n|project_id| skill|importance|\n+----------+----------+----------+\n| 501| Python| 4|\n| 501| Tableau| 3|\n| 501|PostgreSQL| 5|\n| 502| Python| 3|\n| 502| Tableau| 4|\n| 502| R| 2|\n+----------+----------+----------+\n\n"
168+
]
169+
}
170+
],
150171
"source": [
151172
"candidates_data_3278 = [\n",
152173
" (101, \"Python\", 5),\n",
@@ -180,15 +201,289 @@
180201
"projects_df_3278 = spark.createDataFrame(projects_data_3278, projects_columns_3278)\n",
181202
"projects_df_3278.show()"
182203
]
204+
},
205+
{
206+
"cell_type": "code",
207+
"execution_count": 0,
208+
"metadata": {
209+
"application/vnd.databricks.v1+cell": {
210+
"cellMetadata": {
211+
"byteLimit": 2048000,
212+
"rowLimit": 10000
213+
},
214+
"inputWidgets": {},
215+
"nuid": "5af41a82-21b0-41d1-9335-006df581b382",
216+
"showTitle": false,
217+
"tableResultSettingsMap": {},
218+
"title": ""
219+
}
220+
},
221+
"outputs": [],
222+
"source": [
223+
"joined_3278 = candidates_df_3278.join(projects_df_3278, \"skill\")"
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": 0,
229+
"metadata": {
230+
"application/vnd.databricks.v1+cell": {
231+
"cellMetadata": {
232+
"byteLimit": 2048000,
233+
"rowLimit": 10000
234+
},
235+
"inputWidgets": {},
236+
"nuid": "f1e94844-7eda-4150-a62c-b8b491374729",
237+
"showTitle": false,
238+
"tableResultSettingsMap": {},
239+
"title": ""
240+
}
241+
},
242+
"outputs": [],
243+
"source": [
244+
"joined_3278 = joined_3278\\\n",
245+
" .withColumn( \"score_change\",\n",
246+
" when(col(\"proficiency\") > col(\"importance\"), lit(10))\n",
247+
" .when(col(\"proficiency\") < col(\"importance\"), lit(-5))\n",
248+
" .otherwise(lit(0))\n",
249+
" )"
250+
]
251+
},
252+
{
253+
"cell_type": "code",
254+
"execution_count": 0,
255+
"metadata": {
256+
"application/vnd.databricks.v1+cell": {
257+
"cellMetadata": {
258+
"byteLimit": 2048000,
259+
"rowLimit": 10000
260+
},
261+
"inputWidgets": {},
262+
"nuid": "39f79f5a-b5fb-4dc6-88c8-57bb754484e4",
263+
"showTitle": false,
264+
"tableResultSettingsMap": {},
265+
"title": ""
266+
}
267+
},
268+
"outputs": [],
269+
"source": [
270+
"score_summary_3278 = joined_3278\\\n",
271+
" .groupBy(\"project_id\", \"candidate_id\")\\\n",
272+
" .agg(\n",
273+
" sum(\"score_change\").alias(\"score_change_total\"),\n",
274+
" countDistinct(\"skill\").alias(\"matched_skills\")\n",
275+
" )"
276+
]
277+
},
278+
{
279+
"cell_type": "code",
280+
"execution_count": 0,
281+
"metadata": {
282+
"application/vnd.databricks.v1+cell": {
283+
"cellMetadata": {
284+
"byteLimit": 2048000,
285+
"rowLimit": 10000
286+
},
287+
"inputWidgets": {},
288+
"nuid": "463aeaed-1a2c-4723-abeb-417b32467ef5",
289+
"showTitle": false,
290+
"tableResultSettingsMap": {},
291+
"title": ""
292+
}
293+
},
294+
"outputs": [],
295+
"source": [
296+
"project_requirements_3278 = projects_df_3278\\\n",
297+
" .groupBy(\"project_id\")\\\n",
298+
" .agg(countDistinct(\"skill\").alias(\"required_skills\"))"
299+
]
300+
},
301+
{
302+
"cell_type": "code",
303+
"execution_count": 0,
304+
"metadata": {
305+
"application/vnd.databricks.v1+cell": {
306+
"cellMetadata": {
307+
"byteLimit": 2048000,
308+
"rowLimit": 10000
309+
},
310+
"inputWidgets": {},
311+
"nuid": "cd0d0402-64e6-44a1-9a38-1f8af78fcff8",
312+
"showTitle": false,
313+
"tableResultSettingsMap": {},
314+
"title": ""
315+
}
316+
},
317+
"outputs": [],
318+
"source": [
319+
"window = Window.partitionBy(\"project_id\").orderBy(col(\"score\").desc(), col(\"candidate_id\").asc())"
320+
]
321+
},
322+
{
323+
"cell_type": "code",
324+
"execution_count": 0,
325+
"metadata": {
326+
"application/vnd.databricks.v1+cell": {
327+
"cellMetadata": {
328+
"byteLimit": 2048000,
329+
"rowLimit": 10000
330+
},
331+
"inputWidgets": {},
332+
"nuid": "1e0d9881-f8af-4b11-b613-ed3fca19ace7",
333+
"showTitle": false,
334+
"tableResultSettingsMap": {},
335+
"title": ""
336+
}
337+
},
338+
"outputs": [],
339+
"source": [
340+
"qualified_3278 = score_summary_3278\\\n",
341+
" .join(project_requirements_3278, \"project_id\")\\\n",
342+
" .filter(col(\"matched_skills\") == col(\"required_skills\"))"
343+
]
344+
},
345+
{
346+
"cell_type": "code",
347+
"execution_count": 0,
348+
"metadata": {
349+
"application/vnd.databricks.v1+cell": {
350+
"cellMetadata": {
351+
"byteLimit": 2048000,
352+
"rowLimit": 10000
353+
},
354+
"inputWidgets": {},
355+
"nuid": "79657fd7-40ba-448b-a2e8-998624cbb4cb",
356+
"showTitle": false,
357+
"tableResultSettingsMap": {},
358+
"title": ""
359+
}
360+
},
361+
"outputs": [],
362+
"source": [
363+
"qualified_3278 =qualified_3278\\\n",
364+
" .withColumn(\"score\", 100 + col(\"score_change_total\"))\\\n",
365+
" .withColumn(\"rn\", row_number().over(window))"
366+
]
367+
},
368+
{
369+
"cell_type": "code",
370+
"execution_count": 0,
371+
"metadata": {
372+
"application/vnd.databricks.v1+cell": {
373+
"cellMetadata": {
374+
"byteLimit": 2048000,
375+
"rowLimit": 10000
376+
},
377+
"inputWidgets": {},
378+
"nuid": "74130461-66df-4ec0-b84f-4a31e9a0d5dd",
379+
"showTitle": false,
380+
"tableResultSettingsMap": {},
381+
"title": ""
382+
}
383+
},
384+
"outputs": [
385+
{
386+
"output_type": "display_data",
387+
"data": {
388+
"text/html": [
389+
"<style scoped>\n",
390+
" .table-result-container {\n",
391+
" max-height: 300px;\n",
392+
" overflow: auto;\n",
393+
" }\n",
394+
" table, th, td {\n",
395+
" border: 1px solid black;\n",
396+
" border-collapse: collapse;\n",
397+
" }\n",
398+
" th, td {\n",
399+
" padding: 5px;\n",
400+
" }\n",
401+
" th {\n",
402+
" text-align: left;\n",
403+
" }\n",
404+
"</style><div class='table-result-container'><table class='table-result'><thead style='background-color: white'><tr><th>project_id</th><th>candidate_id</th><th>score</th></tr></thead><tbody><tr><td>501</td><td>101</td><td>105</td></tr><tr><td>502</td><td>102</td><td>130</td></tr></tbody></table></div>"
405+
]
406+
},
407+
"metadata": {
408+
"application/vnd.databricks.v1+output": {
409+
"addedWidgets": {},
410+
"aggData": [],
411+
"aggError": "",
412+
"aggOverflow": false,
413+
"aggSchema": [],
414+
"aggSeriesLimitReached": false,
415+
"aggType": "",
416+
"arguments": {},
417+
"columnCustomDisplayInfos": {},
418+
"data": [
419+
[
420+
501,
421+
101,
422+
105
423+
],
424+
[
425+
502,
426+
102,
427+
130
428+
]
429+
],
430+
"datasetInfos": [],
431+
"dbfsResultPath": null,
432+
"isJsonSchema": true,
433+
"metadata": {},
434+
"overflow": false,
435+
"plotOptions": {
436+
"customPlotOptions": {},
437+
"displayType": "table",
438+
"pivotAggregation": null,
439+
"pivotColumns": null,
440+
"xColumns": null,
441+
"yColumns": null
442+
},
443+
"removedWidgets": [],
444+
"schema": [
445+
{
446+
"metadata": "{}",
447+
"name": "project_id",
448+
"type": "\"long\""
449+
},
450+
{
451+
"metadata": "{}",
452+
"name": "candidate_id",
453+
"type": "\"long\""
454+
},
455+
{
456+
"metadata": "{}",
457+
"name": "score",
458+
"type": "\"long\""
459+
}
460+
],
461+
"type": "table"
462+
}
463+
},
464+
"output_type": "display_data"
465+
}
466+
],
467+
"source": [
468+
"qualified_3278\\\n",
469+
" .filter(col(\"rn\") == 1)\\\n",
470+
" .select(\"project_id\", \"candidate_id\", \"score\").orderBy(\"project_id\").display()"
471+
]
183472
}
184473
],
185474
"metadata": {
186475
"application/vnd.databricks.v1+notebook": {
187-
"computePreferences": null,
476+
"computePreferences": {
477+
"hardware": {
478+
"accelerator": null,
479+
"gpuPoolId": null,
480+
"memory": null
481+
}
482+
},
188483
"dashboards": [],
189484
"environmentMetadata": {
190485
"base_environment": "",
191-
"environment_version": "1"
486+
"environment_version": "2"
192487
},
193488
"inputWidgetPreferences": null,
194489
"language": "python",

0 commit comments

Comments
 (0)