Skip to content

Commit 308bb5f

Browse files
author
bitoollearner
committed
LeetCode Pyspark Solution
1 parent 6e994f6 commit 308bb5f

10 files changed

+1384
-70
lines changed

Solved/1939. Users That Actively Request Confirmation Messages (Easy)-(Solved).ipynb

Lines changed: 131 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": "e405a61e-83e0-4a83-8234-4a55e28c9c87",
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": "bb35cd35-f82a-443f-90d7-d161591cdb61",
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": "29ed3054-4879-4c88-8f36-f775ffecdfe0",
4655
"showTitle": false,
@@ -124,15 +133,27 @@
124133
"execution_count": 0,
125134
"metadata": {
126135
"application/vnd.databricks.v1+cell": {
127-
"cellMetadata": {},
136+
"cellMetadata": {
137+
"byteLimit": 2048000,
138+
"rowLimit": 10000
139+
},
128140
"inputWidgets": {},
129141
"nuid": "64a3f754-3c0a-4ca1-af02-4d6f736ea2e9",
130142
"showTitle": false,
131143
"tableResultSettingsMap": {},
132144
"title": ""
133145
}
134146
},
135-
"outputs": [],
147+
"outputs": [
148+
{
149+
"output_type": "stream",
150+
"name": "stdout",
151+
"output_type": "stream",
152+
"text": [
153+
"+-------+-------------------+\n|user_id| time_stamp|\n+-------+-------------------+\n| 3|2020-03-21 10:16:13|\n| 7|2020-01-04 13:57:59|\n| 2|2020-07-29 23:09:44|\n| 6|2020-12-09 10:39:37|\n+-------+-------------------+\n\n+-------+-------------------+---------+\n|user_id| time_stamp| action|\n+-------+-------------------+---------+\n| 3|2021-01-06 03:30:46| timeout|\n| 3|2021-01-06 03:37:45| timeout|\n| 7|2021-06-12 11:57:29|confirmed|\n| 7|2021-06-13 11:57:30|confirmed|\n| 2|2021-01-22 00:00:00|confirmed|\n| 2|2021-01-23 00:00:00| timeout|\n| 6|2021-10-23 14:14:14|confirmed|\n| 6|2021-10-24 14:14:13| timeout|\n+-------+-------------------+---------+\n\n"
154+
]
155+
}
156+
],
136157
"source": [
137158
"signups_data_1939 = [\n",
138159
" (3, \"2020-03-21 10:16:13\"),\n",
@@ -160,15 +181,118 @@
160181
"confirmations_df_1939 = spark.createDataFrame(confirmations_data_1939, confirmations_columns_1939)\n",
161182
"confirmations_df_1939.show()"
162183
]
184+
},
185+
{
186+
"cell_type": "code",
187+
"execution_count": 0,
188+
"metadata": {
189+
"application/vnd.databricks.v1+cell": {
190+
"cellMetadata": {
191+
"byteLimit": 2048000,
192+
"rowLimit": 10000
193+
},
194+
"inputWidgets": {},
195+
"nuid": "e2ef8356-ad90-4706-99b6-840fb2f987e0",
196+
"showTitle": false,
197+
"tableResultSettingsMap": {},
198+
"title": ""
199+
}
200+
},
201+
"outputs": [],
202+
"source": [
203+
"windowSpec = Window.partitionBy(\"user_id\").orderBy(\"time_stamp\")"
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": 0,
209+
"metadata": {
210+
"application/vnd.databricks.v1+cell": {
211+
"cellMetadata": {
212+
"byteLimit": 2048000,
213+
"rowLimit": 10000
214+
},
215+
"inputWidgets": {},
216+
"nuid": "7c3ed02b-efc7-4d97-be2f-289194091c45",
217+
"showTitle": false,
218+
"tableResultSettingsMap": {},
219+
"title": ""
220+
}
221+
},
222+
"outputs": [],
223+
"source": [
224+
"confirmations_df_1939 = confirmations_df_1939\\\n",
225+
" .withColumn(\"prev_time\", lag(\"time_stamp\").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": "1c06265e-e16d-47e2-96f2-f7523c487c32",
239+
"showTitle": false,
240+
"tableResultSettingsMap": {},
241+
"title": ""
242+
}
243+
},
244+
"outputs": [],
245+
"source": [
246+
"confirmations_with_diff_1939 = confirmations_df_1939\\\n",
247+
" .withColumn(\"time_diff\", unix_timestamp(\"time_stamp\") - unix_timestamp(\"prev_time\"))"
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": "ae4da9f4-f9fe-4868-91b5-5f3f03f064be",
261+
"showTitle": false,
262+
"tableResultSettingsMap": {},
263+
"title": ""
264+
}
265+
},
266+
"outputs": [
267+
{
268+
"output_type": "stream",
269+
"name": "stdout",
270+
"output_type": "stream",
271+
"text": [
272+
"+-------+\n|user_id|\n+-------+\n| 2|\n| 3|\n| 6|\n+-------+\n\n"
273+
]
274+
}
275+
],
276+
"source": [
277+
"confirmations_with_diff_1939\\\n",
278+
" .filter((col(\"time_diff\").isNotNull()) & (col(\"time_diff\") <= 86400))\\\n",
279+
" .select(\"user_id\").distinct().show()"
280+
]
163281
}
164282
],
165283
"metadata": {
166284
"application/vnd.databricks.v1+notebook": {
167-
"computePreferences": null,
285+
"computePreferences": {
286+
"hardware": {
287+
"accelerator": null,
288+
"gpuPoolId": null,
289+
"memory": null
290+
}
291+
},
168292
"dashboards": [],
169293
"environmentMetadata": {
170294
"base_environment": "",
171-
"environment_version": "1"
295+
"environment_version": "2"
172296
},
173297
"inputWidgetPreferences": null,
174298
"language": "python",

Solved/1949. Strong Friendship (Medium)-(Solved).ipynb

Lines changed: 118 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": "e405a61e-83e0-4a83-8234-4a55e28c9c87",
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": "bb35cd35-f82a-443f-90d7-d161591cdb61",
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": "29ed3054-4879-4c88-8f36-f775ffecdfe0",
4655
"showTitle": false,
@@ -111,15 +120,27 @@
111120
"execution_count": 0,
112121
"metadata": {
113122
"application/vnd.databricks.v1+cell": {
114-
"cellMetadata": {},
123+
"cellMetadata": {
124+
"byteLimit": 2048000,
125+
"rowLimit": 10000
126+
},
115127
"inputWidgets": {},
116128
"nuid": "64a3f754-3c0a-4ca1-af02-4d6f736ea2e9",
117129
"showTitle": false,
118130
"tableResultSettingsMap": {},
119131
"title": ""
120132
}
121133
},
122-
"outputs": [],
134+
"outputs": [
135+
{
136+
"output_type": "stream",
137+
"name": "stdout",
138+
"output_type": "stream",
139+
"text": [
140+
"+--------+--------+\n|user1_id|user2_id|\n+--------+--------+\n| 1| 2|\n| 1| 3|\n| 2| 3|\n| 1| 4|\n| 2| 4|\n| 1| 5|\n| 2| 5|\n| 1| 7|\n| 3| 7|\n| 1| 6|\n| 3| 6|\n| 2| 6|\n+--------+--------+\n\n"
141+
]
142+
}
143+
],
123144
"source": [
124145
"friendship_data_1949 = [\n",
125146
" (1, 2), (1, 3), (2, 3), (1, 4), (2, 4),\n",
@@ -131,15 +152,105 @@
131152
"friendship_df_1949 = spark.createDataFrame(friendship_data_1949, friendship_columns_1949)\n",
132153
"friendship_df_1949.show()"
133154
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 0,
159+
"metadata": {
160+
"application/vnd.databricks.v1+cell": {
161+
"cellMetadata": {
162+
"byteLimit": 2048000,
163+
"rowLimit": 10000
164+
},
165+
"inputWidgets": {},
166+
"nuid": "a323eb25-d903-4c8b-807e-a2199f7d1d43",
167+
"showTitle": false,
168+
"tableResultSettingsMap": {},
169+
"title": ""
170+
}
171+
},
172+
"outputs": [],
173+
"source": [
174+
"edges_df_1949 = friendship_df_1949\\\n",
175+
" .select(col(\"user1_id\").alias(\"user\"), col(\"user2_id\").alias(\"friend\"))\\\n",
176+
" .union(friendship_df_1949.select(col(\"user2_id\").alias(\"user\"), col(\"user1_id\").alias(\"friend\")))"
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": "7b17bd7f-8d1f-4a32-a206-a188e163d438",
190+
"showTitle": false,
191+
"tableResultSettingsMap": {},
192+
"title": ""
193+
}
194+
},
195+
"outputs": [],
196+
"source": [
197+
"pairs_df_1949 = edges_df_1949.alias(\"a\")\\\n",
198+
" .join(edges_df_1949.alias(\"b\"), col(\"a.friend\") == col(\"b.friend\"))\\\n",
199+
" .filter(col(\"a.user\") < col(\"b.user\"))\\\n",
200+
" .select(\n",
201+
" col(\"a.user\").alias(\"user1_id\"),\n",
202+
" col(\"b.user\").alias(\"user2_id\"),\n",
203+
" col(\"a.friend\").alias(\"common_friend\")\n",
204+
" )"
205+
]
206+
},
207+
{
208+
"cell_type": "code",
209+
"execution_count": 0,
210+
"metadata": {
211+
"application/vnd.databricks.v1+cell": {
212+
"cellMetadata": {
213+
"byteLimit": 2048000,
214+
"rowLimit": 10000
215+
},
216+
"inputWidgets": {},
217+
"nuid": "cd6e8a49-b12f-4acb-83b8-894f5ed39711",
218+
"showTitle": false,
219+
"tableResultSettingsMap": {},
220+
"title": ""
221+
}
222+
},
223+
"outputs": [
224+
{
225+
"output_type": "stream",
226+
"name": "stdout",
227+
"output_type": "stream",
228+
"text": [
229+
"+--------+--------+-------------+\n|user1_id|user2_id|common_friend|\n+--------+--------+-------------+\n| 1| 3| 3|\n| 1| 2| 4|\n+--------+--------+-------------+\n\n"
230+
]
231+
}
232+
],
233+
"source": [
234+
"pairs_df_1949\\\n",
235+
" .groupBy(\"user1_id\", \"user2_id\")\\\n",
236+
" .agg(countDistinct(\"common_friend\").alias(\"common_friend\"))\\\n",
237+
" .filter(col(\"common_friend\") >= 3).show()"
238+
]
134239
}
135240
],
136241
"metadata": {
137242
"application/vnd.databricks.v1+notebook": {
138-
"computePreferences": null,
243+
"computePreferences": {
244+
"hardware": {
245+
"accelerator": null,
246+
"gpuPoolId": null,
247+
"memory": null
248+
}
249+
},
139250
"dashboards": [],
140251
"environmentMetadata": {
141252
"base_environment": "",
142-
"environment_version": "1"
253+
"environment_version": "2"
143254
},
144255
"inputWidgetPreferences": null,
145256
"language": "python",

0 commit comments

Comments
 (0)