Skip to content

Commit 58c9673

Browse files
author
bitoollearner
committed
LeetCode Question Solved Pyspark-Batch-3
1 parent fd61b9e commit 58c9673

9 files changed

+948
-72
lines changed

Solved/601. Human Traffic of Stadium (Hard)-(Solved).ipynb

Lines changed: 100 additions & 8 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": "5ab27f1b-7c08-44cb-8766-b16c3470b90d",
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": "0d316502-e6b4-4432-b3fd-6159096e4951",
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": "2e4d3e31-be24-427a-bc7e-362b6765c4fa",
4655
"showTitle": false,
@@ -102,7 +111,10 @@
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": "f6f11a82-8828-41ca-828a-e88f8586b97a",
108120
"showTitle": false,
@@ -127,25 +139,105 @@
127139
"stadium_df_601 = spark.createDataFrame(stadium_data_601, stadium_columns_601)\n",
128140
"stadium_df_601.show()"
129141
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": 0,
146+
"metadata": {
147+
"application/vnd.databricks.v1+cell": {
148+
"cellMetadata": {
149+
"byteLimit": 2048000,
150+
"rowLimit": 10000
151+
},
152+
"inputWidgets": {},
153+
"nuid": "3782a28a-5f25-4030-97c5-84c6d999d2c2",
154+
"showTitle": false,
155+
"tableResultSettingsMap": {},
156+
"title": ""
157+
}
158+
},
159+
"outputs": [],
160+
"source": [
161+
"windowSpec = Window.orderBy(\"id\")\n",
162+
"\n",
163+
"stadium_df_601 = stadium_df_601.withColumn(\"prev_id\", lag(\"id\", 1).over(windowSpec))\n",
164+
"stadium_df_601 = stadium_df_601.withColumn(\"next_id\", lead(\"id\", 1).over(windowSpec))"
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": 0,
170+
"metadata": {
171+
"application/vnd.databricks.v1+cell": {
172+
"cellMetadata": {
173+
"byteLimit": 2048000,
174+
"rowLimit": 10000
175+
},
176+
"inputWidgets": {},
177+
"nuid": "48279fbe-7227-4057-9509-07c44ba86d85",
178+
"showTitle": false,
179+
"tableResultSettingsMap": {},
180+
"title": ""
181+
}
182+
},
183+
"outputs": [],
184+
"source": [
185+
"stadium_df_601 = stadium_df_601.withColumn(\"consecutive_group\", \n",
186+
" when((col(\"people\") >= 100) & \n",
187+
" (col(\"prev_id\") == col(\"id\") - 1) & \n",
188+
" (col(\"next_id\") == col(\"id\") + 1), 1).otherwise(0))"
189+
]
190+
},
191+
{
192+
"cell_type": "code",
193+
"execution_count": 0,
194+
"metadata": {
195+
"application/vnd.databricks.v1+cell": {
196+
"cellMetadata": {
197+
"byteLimit": 2048000,
198+
"rowLimit": 10000
199+
},
200+
"inputWidgets": {},
201+
"nuid": "b3ee790b-06dc-4ce5-983f-8ed3dae2b8b4",
202+
"showTitle": false,
203+
"tableResultSettingsMap": {},
204+
"title": ""
205+
}
206+
},
207+
"outputs": [],
208+
"source": [
209+
"stadium_df_601\\\n",
210+
" .withColumn(\"group_sum\", sum(\"consecutive_group\").over(windowSpec))\\\n",
211+
" .filter(col(\"group_sum\") >= 3).select(\"id\", \"visit_date\", \"people\").orderBy(\"visit_date\").show()"
212+
]
130213
}
131214
],
132215
"metadata": {
133216
"application/vnd.databricks.v1+notebook": {
134-
"computePreferences": null,
217+
"computePreferences": {
218+
"hardware": {
219+
"accelerator": null,
220+
"gpuPoolId": null,
221+
"memory": null
222+
}
223+
},
135224
"dashboards": [],
136225
"environmentMetadata": {
137226
"base_environment": "",
138-
"environment_version": "1"
227+
"environment_version": "2"
139228
},
140229
"inputWidgetPreferences": null,
141230
"language": "python",
142231
"notebookMetadata": {
143232
"pythonIndentUnit": 4
144233
},
145-
"notebookName": "601. Human Traffic of Stadium (Hard)",
234+
"notebookName": "601. Human Traffic of Stadium (Hard)-(Solved)",
146235
"widgets": {}
236+
},
237+
"language_info": {
238+
"name": "python"
147239
}
148240
},
149241
"nbformat": 4,
150242
"nbformat_minor": 0
151-
}
243+
}

Solved/602. Friend Requests II_ Who Has the Most Friends (Medium)-(Solved).ipynb

Lines changed: 143 additions & 8 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": "5ab27f1b-7c08-44cb-8766-b16c3470b90d",
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": "0d316502-e6b4-4432-b3fd-6159096e4951",
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": "2e4d3e31-be24-427a-bc7e-362b6765c4fa",
4655
"showTitle": false,
@@ -96,7 +105,10 @@
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": "f6f11a82-8828-41ca-828a-e88f8586b97a",
102114
"showTitle": false,
@@ -117,25 +129,148 @@
117129
"request_accepted_df_602 = spark.createDataFrame(request_accepted_data_602, request_accepted_columns_602)\n",
118130
"request_accepted_df_602.show()"
119131
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"execution_count": 0,
136+
"metadata": {
137+
"application/vnd.databricks.v1+cell": {
138+
"cellMetadata": {
139+
"byteLimit": 2048000,
140+
"rowLimit": 10000
141+
},
142+
"inputWidgets": {},
143+
"nuid": "9b1f3cc0-9f0c-4f06-8dae-ab399d787883",
144+
"showTitle": false,
145+
"tableResultSettingsMap": {},
146+
"title": ""
147+
}
148+
},
149+
"outputs": [],
150+
"source": [
151+
"request_accepted_df_602_1 = request_accepted_df_602.select(col(\"requester_id\").alias(\"id\"),\n",
152+
" col(\"accepter_id\").alias(\"friend_id\"))\n",
153+
"\n",
154+
"request_accepted_df_602_2 = request_accepted_df_602.select(col(\"accepter_id\").alias(\"id\"),\n",
155+
" col(\"requester_id\").alias(\"friend_id\"))"
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": "4db7c485-5425-4514-93f3-e9f58cb33643",
169+
"showTitle": false,
170+
"tableResultSettingsMap": {},
171+
"title": ""
172+
}
173+
},
174+
"outputs": [],
175+
"source": [
176+
"friendships_df_602 = request_accepted_df_602_1.union(request_accepted_df_602_2)"
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": "27256137-8305-4b9a-9e89-cfc45044b3da",
190+
"showTitle": false,
191+
"tableResultSettingsMap": {},
192+
"title": ""
193+
}
194+
},
195+
"outputs": [],
196+
"source": [
197+
"friend_counts_602 = friendships_df_602.groupBy(\"id\").agg(\n",
198+
" countDistinct(\"friend_id\").alias(\"num\")\n",
199+
")"
200+
]
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": 0,
205+
"metadata": {
206+
"application/vnd.databricks.v1+cell": {
207+
"cellMetadata": {
208+
"byteLimit": 2048000,
209+
"rowLimit": 10000
210+
},
211+
"inputWidgets": {},
212+
"nuid": "d5b957f1-c332-4d99-b8c3-dc266f232806",
213+
"showTitle": false,
214+
"tableResultSettingsMap": {},
215+
"title": ""
216+
}
217+
},
218+
"outputs": [],
219+
"source": [
220+
"max_friends_602 = friend_counts_602.agg(\n",
221+
" max(\"num\").alias(\"max_num\")\n",
222+
").collect()[0][\"max_num\"]"
223+
]
224+
},
225+
{
226+
"cell_type": "code",
227+
"execution_count": 0,
228+
"metadata": {
229+
"application/vnd.databricks.v1+cell": {
230+
"cellMetadata": {
231+
"byteLimit": 2048000,
232+
"rowLimit": 10000
233+
},
234+
"inputWidgets": {},
235+
"nuid": "3b85ed15-6485-465b-8a08-47da673a2ce1",
236+
"showTitle": false,
237+
"tableResultSettingsMap": {},
238+
"title": ""
239+
}
240+
},
241+
"outputs": [],
242+
"source": [
243+
"friend_counts_602\\\n",
244+
" .filter(col(\"num\") == max_friends_602).show()"
245+
]
120246
}
121247
],
122248
"metadata": {
123249
"application/vnd.databricks.v1+notebook": {
124-
"computePreferences": null,
250+
"computePreferences": {
251+
"hardware": {
252+
"accelerator": null,
253+
"gpuPoolId": null,
254+
"memory": null
255+
}
256+
},
125257
"dashboards": [],
126258
"environmentMetadata": {
127259
"base_environment": "",
128-
"environment_version": "1"
260+
"environment_version": "2"
129261
},
130262
"inputWidgetPreferences": null,
131263
"language": "python",
132264
"notebookMetadata": {
133265
"pythonIndentUnit": 4
134266
},
135-
"notebookName": "602. Friend Requests II: Who Has the Most Friends (Medium)",
267+
"notebookName": "602. Friend Requests II_ Who Has the Most Friends (Medium)-(Solved)",
136268
"widgets": {}
269+
},
270+
"language_info": {
271+
"name": "python"
137272
}
138273
},
139274
"nbformat": 4,
140275
"nbformat_minor": 0
141-
}
276+
}

0 commit comments

Comments
 (0)