Skip to content

Commit 842d5d1

Browse files
author
bitoollearner
committed
LeetCode PySpark Solution
1 parent 6cfc90b commit 842d5d1

4 files changed

+953
-28
lines changed

Solved/3060. User Activities within Time Bounds (Hard)-(Solved).ipynb

Lines changed: 215 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,
@@ -108,15 +117,27 @@
108117
"execution_count": 0,
109118
"metadata": {
110119
"application/vnd.databricks.v1+cell": {
111-
"cellMetadata": {},
120+
"cellMetadata": {
121+
"byteLimit": 2048000,
122+
"rowLimit": 10000
123+
},
112124
"inputWidgets": {},
113125
"nuid": "a2368434-0191-416c-aa1d-12cd44cf48e6",
114126
"showTitle": false,
115127
"tableResultSettingsMap": {},
116128
"title": ""
117129
}
118130
},
119-
"outputs": [],
131+
"outputs": [
132+
{
133+
"output_type": "stream",
134+
"name": "stdout",
135+
"output_type": "stream",
136+
"text": [
137+
"+-------+-------------------+-------------------+----------+------------+\n|user_id| session_start| session_end|session_id|session_type|\n+-------+-------------------+-------------------+----------+------------+\n| 101|2023-11-01 08:00:00|2023-11-01 09:00:00| 1| Viewer|\n| 101|2023-11-01 10:00:00|2023-11-01 11:00:00| 2| Streamer|\n| 102|2023-11-01 13:00:00|2023-11-01 14:00:00| 3| Viewer|\n| 102|2023-11-01 15:00:00|2023-11-01 16:00:00| 4| Viewer|\n| 101|2023-11-02 09:00:00|2023-11-02 10:00:00| 5| Viewer|\n| 102|2023-11-02 12:00:00|2023-11-02 13:00:00| 6| Streamer|\n| 101|2023-11-02 13:00:00|2023-11-02 14:00:00| 7| Streamer|\n| 102|2023-11-02 16:00:00|2023-11-02 17:00:00| 8| Viewer|\n| 103|2023-11-01 08:00:00|2023-11-01 09:00:00| 9| Viewer|\n| 103|2023-11-02 20:00:00|2023-11-02 23:00:00| 10| Viewer|\n| 103|2023-11-03 09:00:00|2023-11-03 10:00:00| 11| Viewer|\n+-------+-------------------+-------------------+----------+------------+\n\n"
138+
]
139+
}
140+
],
120141
"source": [
121142
"sessions_data_3060 = [\n",
122143
" (101, \"2023-11-01 08:00:00\", \"2023-11-01 09:00:00\", 1, \"Viewer\"),\n",
@@ -136,15 +157,202 @@
136157
"sessions_df_3060 = spark.createDataFrame(sessions_data_3060, sessions_columns_3060)\n",
137158
"sessions_df_3060.show()"
138159
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": 0,
164+
"metadata": {
165+
"application/vnd.databricks.v1+cell": {
166+
"cellMetadata": {
167+
"byteLimit": 2048000,
168+
"rowLimit": 10000
169+
},
170+
"inputWidgets": {},
171+
"nuid": "cbed387c-a844-4d77-a6ba-e898b49508ca",
172+
"showTitle": false,
173+
"tableResultSettingsMap": {},
174+
"title": ""
175+
}
176+
},
177+
"outputs": [],
178+
"source": [
179+
"sessions_df_3060 = sessions_df_3060\\\n",
180+
" .withColumn(\"session_start\", col(\"session_start\").cast(\"timestamp\")) \\\n",
181+
" .withColumn(\"session_end\", col(\"session_end\").cast(\"timestamp\"))"
182+
]
183+
},
184+
{
185+
"cell_type": "code",
186+
"execution_count": 0,
187+
"metadata": {
188+
"application/vnd.databricks.v1+cell": {
189+
"cellMetadata": {
190+
"byteLimit": 2048000,
191+
"rowLimit": 10000
192+
},
193+
"inputWidgets": {},
194+
"nuid": "726ca09a-f2a4-4df3-83ba-bcadd8c9c44e",
195+
"showTitle": false,
196+
"tableResultSettingsMap": {},
197+
"title": ""
198+
}
199+
},
200+
"outputs": [],
201+
"source": [
202+
"windowSpec = Window.partitionBy(\"user_id\").orderBy(\"session_start\")"
203+
]
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": "547345a8-db64-495a-8175-f47e5e284574",
216+
"showTitle": false,
217+
"tableResultSettingsMap": {},
218+
"title": ""
219+
}
220+
},
221+
"outputs": [],
222+
"source": [
223+
"sessions_df_3060 = sessions_df_3060\\\n",
224+
" .withColumn(\"prev_end\", lag(\"session_end\").over(windowSpec)) \\\n",
225+
" .withColumn(\"prev_type\", lag(\"session_type\").over(windowSpec))"
226+
]
227+
},
228+
{
229+
"cell_type": "code",
230+
"execution_count": 0,
231+
"metadata": {
232+
"application/vnd.databricks.v1+cell": {
233+
"cellMetadata": {
234+
"byteLimit": 2048000,
235+
"rowLimit": 10000
236+
},
237+
"inputWidgets": {},
238+
"nuid": "b3af788b-33db-4006-8e79-9f62c465133f",
239+
"showTitle": false,
240+
"tableResultSettingsMap": {},
241+
"title": ""
242+
}
243+
},
244+
"outputs": [],
245+
"source": [
246+
"sessions_df_3060 = sessions_df_3060\\\n",
247+
" .withColumn(\"gap_hours\", (unix_timestamp(col(\"session_start\")) - unix_timestamp(col(\"prev_end\"))) / 3600)"
248+
]
249+
},
250+
{
251+
"cell_type": "code",
252+
"execution_count": 0,
253+
"metadata": {
254+
"application/vnd.databricks.v1+cell": {
255+
"cellMetadata": {
256+
"byteLimit": 2048000,
257+
"rowLimit": 10000
258+
},
259+
"inputWidgets": {},
260+
"nuid": "28314437-b74b-427a-957d-695e11a8da4e",
261+
"showTitle": false,
262+
"tableResultSettingsMap": {},
263+
"title": ""
264+
}
265+
},
266+
"outputs": [
267+
{
268+
"output_type": "display_data",
269+
"data": {
270+
"text/html": [
271+
"<style scoped>\n",
272+
" .table-result-container {\n",
273+
" max-height: 300px;\n",
274+
" overflow: auto;\n",
275+
" }\n",
276+
" table, th, td {\n",
277+
" border: 1px solid black;\n",
278+
" border-collapse: collapse;\n",
279+
" }\n",
280+
" th, td {\n",
281+
" padding: 5px;\n",
282+
" }\n",
283+
" th {\n",
284+
" text-align: left;\n",
285+
" }\n",
286+
"</style><div class='table-result-container'><table class='table-result'><thead style='background-color: white'><tr><th>user_id</th></tr></thead><tbody><tr><td>102</td></tr><tr><td>103</td></tr></tbody></table></div>"
287+
]
288+
},
289+
"metadata": {
290+
"application/vnd.databricks.v1+output": {
291+
"addedWidgets": {},
292+
"aggData": [],
293+
"aggError": "",
294+
"aggOverflow": false,
295+
"aggSchema": [],
296+
"aggSeriesLimitReached": false,
297+
"aggType": "",
298+
"arguments": {},
299+
"columnCustomDisplayInfos": {},
300+
"data": [
301+
[
302+
102
303+
],
304+
[
305+
103
306+
]
307+
],
308+
"datasetInfos": [],
309+
"dbfsResultPath": null,
310+
"isJsonSchema": true,
311+
"metadata": {},
312+
"overflow": false,
313+
"plotOptions": {
314+
"customPlotOptions": {},
315+
"displayType": "table",
316+
"pivotAggregation": null,
317+
"pivotColumns": null,
318+
"xColumns": null,
319+
"yColumns": null
320+
},
321+
"removedWidgets": [],
322+
"schema": [
323+
{
324+
"metadata": "{}",
325+
"name": "user_id",
326+
"type": "\"long\""
327+
}
328+
],
329+
"type": "table"
330+
}
331+
},
332+
"output_type": "display_data"
333+
}
334+
],
335+
"source": [
336+
"sessions_df_3060\\\n",
337+
" .filter((col(\"session_type\") == col(\"prev_type\")) & (col(\"gap_hours\") <= 12)) \\\n",
338+
" .select(\"user_id\").distinct() \\\n",
339+
" .orderBy(\"user_id\").display()"
340+
]
139341
}
140342
],
141343
"metadata": {
142344
"application/vnd.databricks.v1+notebook": {
143-
"computePreferences": null,
345+
"computePreferences": {
346+
"hardware": {
347+
"accelerator": null,
348+
"gpuPoolId": null,
349+
"memory": null
350+
}
351+
},
144352
"dashboards": [],
145353
"environmentMetadata": {
146354
"base_environment": "",
147-
"environment_version": "1"
355+
"environment_version": "2"
148356
},
149357
"inputWidgetPreferences": null,
150358
"language": "python",

0 commit comments

Comments
 (0)