Skip to content

Commit a186b8f

Browse files
author
bitoollearner
committed
LeetCode Pyspark Solution
1 parent b6f62a6 commit a186b8f

6 files changed

+456
-42
lines changed

Solved/1731. The Number of Employees Which Report to Each Employee (Easy)-(Solved).ipynb

Lines changed: 71 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": "fadf709d-de3d-4bfc-b69f-3b489de68bef",
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": "d4a1fef1-aa99-469c-b35f-9b3f3e81c9f7",
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": "78daa073-80d8-4f7e-9cfa-10f6eb568909",
4655
"showTitle": false,
@@ -118,15 +127,27 @@
118127
"execution_count": 0,
119128
"metadata": {
120129
"application/vnd.databricks.v1+cell": {
121-
"cellMetadata": {},
130+
"cellMetadata": {
131+
"byteLimit": 2048000,
132+
"rowLimit": 10000
133+
},
122134
"inputWidgets": {},
123135
"nuid": "7b78bc27-26cd-4523-8646-7369baf68fcb",
124136
"showTitle": false,
125137
"tableResultSettingsMap": {},
126138
"title": ""
127139
}
128140
},
129-
"outputs": [],
141+
"outputs": [
142+
{
143+
"output_type": "stream",
144+
"name": "stdout",
145+
"output_type": "stream",
146+
"text": [
147+
"+-----------+-------+----------+---+\n|employee_id| name|reports_to|age|\n+-----------+-------+----------+---+\n| 1|Michael| NULL| 45|\n| 2| Alice| 1| 38|\n| 3| Bob| 1| 42|\n| 4|Charlie| 2| 34|\n| 5| David| 2| 40|\n| 6| Eve| 3| 37|\n| 7| Frank| NULL| 50|\n| 8| Grace| NULL| 48|\n+-----------+-------+----------+---+\n\n"
148+
]
149+
}
150+
],
130151
"source": [
131152
"employees_data_1731 = [\n",
132153
" (1, \"Michael\", None, 45),\n",
@@ -143,15 +164,58 @@
143164
"employees_df_1731 = spark.createDataFrame(employees_data_1731, employees_columns_1731)\n",
144165
"employees_df_1731.show()"
145166
]
167+
},
168+
{
169+
"cell_type": "code",
170+
"execution_count": 0,
171+
"metadata": {
172+
"application/vnd.databricks.v1+cell": {
173+
"cellMetadata": {
174+
"byteLimit": 2048000,
175+
"rowLimit": 10000
176+
},
177+
"inputWidgets": {},
178+
"nuid": "6bc6fe2e-cbb0-4398-9961-ddefe996fbf1",
179+
"showTitle": false,
180+
"tableResultSettingsMap": {},
181+
"title": ""
182+
}
183+
},
184+
"outputs": [
185+
{
186+
"output_type": "stream",
187+
"name": "stdout",
188+
"output_type": "stream",
189+
"text": [
190+
"+-----------+-------+-------------+-----------+\n|employee_id| name|reports_count|average_age|\n+-----------+-------+-------------+-----------+\n| 1|Michael| 2| 40|\n| 2| Alice| 2| 37|\n| 3| Bob| 1| 37|\n+-----------+-------+-------------+-----------+\n\n"
191+
]
192+
}
193+
],
194+
"source": [
195+
"employees_df_1731.alias(\"e\")\\\n",
196+
" .join( employees_df_1731.alias(\"m\"), col(\"e.reports_to\") == col(\"m.employee_id\"),\"inner\")\\\n",
197+
" .groupBy(\"m.employee_id\", \"m.name\") \\\n",
198+
" .agg(\n",
199+
" count(\"e.employee_id\").alias(\"reports_count\"),\n",
200+
" round(avg(\"e.age\"), 0).cast(\"int\").alias(\"average_age\")\n",
201+
" ) \\\n",
202+
" .orderBy(\"m.employee_id\").show()"
203+
]
146204
}
147205
],
148206
"metadata": {
149207
"application/vnd.databricks.v1+notebook": {
150-
"computePreferences": null,
208+
"computePreferences": {
209+
"hardware": {
210+
"accelerator": null,
211+
"gpuPoolId": null,
212+
"memory": null
213+
}
214+
},
151215
"dashboards": [],
152216
"environmentMetadata": {
153217
"base_environment": "",
154-
"environment_version": "1"
218+
"environment_version": "2"
155219
},
156220
"inputWidgetPreferences": null,
157221
"language": "python",

Solved/1741. Find Total Time Spent by Each Employee (Easy)-(Solved).ipynb

Lines changed: 89 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": "fadf709d-de3d-4bfc-b69f-3b489de68bef",
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": "d4a1fef1-aa99-469c-b35f-9b3f3e81c9f7",
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": "78daa073-80d8-4f7e-9cfa-10f6eb568909",
4655
"showTitle": false,
@@ -102,15 +111,27 @@
102111
"execution_count": 0,
103112
"metadata": {
104113
"application/vnd.databricks.v1+cell": {
105-
"cellMetadata": {},
114+
"cellMetadata": {
115+
"byteLimit": 2048000,
116+
"rowLimit": 10000
117+
},
106118
"inputWidgets": {},
107119
"nuid": "7b78bc27-26cd-4523-8646-7369baf68fcb",
108120
"showTitle": false,
109121
"tableResultSettingsMap": {},
110122
"title": ""
111123
}
112124
},
113-
"outputs": [],
125+
"outputs": [
126+
{
127+
"output_type": "stream",
128+
"name": "stdout",
129+
"output_type": "stream",
130+
"text": [
131+
"+------+----------+-------+--------+\n|emp_id| event_day|in_time|out_time|\n+------+----------+-------+--------+\n| 1|2020-11-28| 4| 32|\n| 1|2020-11-28| 55| 200|\n| 1|2020-12-03| 1| 42|\n| 2|2020-11-28| 3| 33|\n| 2|2020-12-09| 47| 74|\n+------+----------+-------+--------+\n\n"
132+
]
133+
}
134+
],
114135
"source": [
115136
"employees_data_1741 = [\n",
116137
" (1, \"2020-11-28\", 4, 32),\n",
@@ -123,15 +144,76 @@
123144
"employees_df_1741 = spark.createDataFrame(employees_data_1741, employees_columns_1741)\n",
124145
"employees_df_1741.show()"
125146
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": 0,
151+
"metadata": {
152+
"application/vnd.databricks.v1+cell": {
153+
"cellMetadata": {
154+
"byteLimit": 2048000,
155+
"rowLimit": 10000
156+
},
157+
"inputWidgets": {},
158+
"nuid": "2d13bbd3-0bcd-4274-a0ce-45589d22846b",
159+
"showTitle": false,
160+
"tableResultSettingsMap": {},
161+
"title": ""
162+
}
163+
},
164+
"outputs": [],
165+
"source": [
166+
"employees_df_1741 = employees_df_1741\\\n",
167+
" .withColumn(\"time_spent\", col(\"out_time\") - col(\"in_time\"))"
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": 0,
173+
"metadata": {
174+
"application/vnd.databricks.v1+cell": {
175+
"cellMetadata": {
176+
"byteLimit": 2048000,
177+
"rowLimit": 10000
178+
},
179+
"inputWidgets": {},
180+
"nuid": "b0d8958a-7aca-4592-8a6e-8887b8000c2c",
181+
"showTitle": false,
182+
"tableResultSettingsMap": {},
183+
"title": ""
184+
}
185+
},
186+
"outputs": [
187+
{
188+
"output_type": "stream",
189+
"name": "stdout",
190+
"output_type": "stream",
191+
"text": [
192+
"+----------+------+----------+\n| day|emp_id|total_time|\n+----------+------+----------+\n|2020-11-28| 1| 173|\n|2020-12-03| 1| 41|\n|2020-11-28| 2| 30|\n|2020-12-09| 2| 27|\n+----------+------+----------+\n\n"
193+
]
194+
}
195+
],
196+
"source": [
197+
"employees_df_1741\\\n",
198+
" .groupBy(\"event_day\", \"emp_id\") \\\n",
199+
" .agg(sum(\"time_spent\").alias(\"total_time\")) \\\n",
200+
" .withColumnRenamed(\"event_day\", \"day\").show()"
201+
]
126202
}
127203
],
128204
"metadata": {
129205
"application/vnd.databricks.v1+notebook": {
130-
"computePreferences": null,
206+
"computePreferences": {
207+
"hardware": {
208+
"accelerator": null,
209+
"gpuPoolId": null,
210+
"memory": null
211+
}
212+
},
131213
"dashboards": [],
132214
"environmentMetadata": {
133215
"base_environment": "",
134-
"environment_version": "1"
216+
"environment_version": "2"
135217
},
136218
"inputWidgetPreferences": null,
137219
"language": "python",

0 commit comments

Comments
 (0)