Skip to content

Commit fad41c7

Browse files
author
bitoollearner
committed
LeetCode Questions Solution
LeetCode Questions Solution
1 parent c260745 commit fad41c7

11 files changed

+1146
-99
lines changed

Solved/1159. Market Analysis II (Hard)-(Solved).ipynb

Lines changed: 138 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": "c8535576-d746-40ee-8199-8ccd3e39577f",
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": "09da8fc1-d579-4949-bbc7-9132909993c8",
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": "4567f050-e6f4-4b8b-b899-db2f17021c1d",
4655
"showTitle": false,
@@ -142,7 +151,10 @@
142151
"execution_count": 0,
143152
"metadata": {
144153
"application/vnd.databricks.v1+cell": {
145-
"cellMetadata": {},
154+
"cellMetadata": {
155+
"byteLimit": 2048000,
156+
"rowLimit": 10000
157+
},
146158
"inputWidgets": {},
147159
"nuid": "9b14d03f-22ca-4fa2-b371-32a135a82877",
148160
"showTitle": false,
@@ -187,25 +199,143 @@
187199
"items_df_1159 = spark.createDataFrame(items_data_1159, items_columns_1159)\n",
188200
"items_df_1159.show()"
189201
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": 0,
206+
"metadata": {
207+
"application/vnd.databricks.v1+cell": {
208+
"cellMetadata": {
209+
"byteLimit": 2048000,
210+
"rowLimit": 10000
211+
},
212+
"inputWidgets": {},
213+
"nuid": "51d9e878-d549-4bef-9d69-44228bc697e0",
214+
"showTitle": false,
215+
"tableResultSettingsMap": {},
216+
"title": ""
217+
}
218+
},
219+
"outputs": [],
220+
"source": [
221+
"orders_with_brand_df_1159 = orders_df_1159.join(items_df_1159, on=\"item_id\")"
222+
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": 0,
227+
"metadata": {
228+
"application/vnd.databricks.v1+cell": {
229+
"cellMetadata": {
230+
"byteLimit": 2048000,
231+
"rowLimit": 10000
232+
},
233+
"inputWidgets": {},
234+
"nuid": "5e2a0a40-556e-413b-8181-f8fd009cf719",
235+
"showTitle": false,
236+
"tableResultSettingsMap": {},
237+
"title": ""
238+
}
239+
},
240+
"outputs": [],
241+
"source": [
242+
"window_spec = Window.partitionBy(\"seller_id\").orderBy(\"order_date\")"
243+
]
244+
},
245+
{
246+
"cell_type": "code",
247+
"execution_count": 0,
248+
"metadata": {
249+
"application/vnd.databricks.v1+cell": {
250+
"cellMetadata": {
251+
"byteLimit": 2048000,
252+
"rowLimit": 10000
253+
},
254+
"inputWidgets": {},
255+
"nuid": "721b4e45-4a84-491c-a669-241815ea3244",
256+
"showTitle": false,
257+
"tableResultSettingsMap": {},
258+
"title": ""
259+
}
260+
},
261+
"outputs": [],
262+
"source": [
263+
"ranked_sales_df_1159 = orders_with_brand_df_1159.withColumn(\"rank\", row_number().over(window_spec))"
264+
]
265+
},
266+
{
267+
"cell_type": "code",
268+
"execution_count": 0,
269+
"metadata": {
270+
"application/vnd.databricks.v1+cell": {
271+
"cellMetadata": {
272+
"byteLimit": 2048000,
273+
"rowLimit": 10000
274+
},
275+
"inputWidgets": {},
276+
"nuid": "c251ebc8-4789-455a-bdd4-e0c8c54853c0",
277+
"showTitle": false,
278+
"tableResultSettingsMap": {},
279+
"title": ""
280+
}
281+
},
282+
"outputs": [],
283+
"source": [
284+
"second_sales_df_1159 = ranked_sales_df_1159.filter(col(\"rank\") == 2) \\\n",
285+
" .select(\"seller_id\", col(\"item_brand\").alias(\"second_item_brand\"))"
286+
]
287+
},
288+
{
289+
"cell_type": "code",
290+
"execution_count": 0,
291+
"metadata": {
292+
"application/vnd.databricks.v1+cell": {
293+
"cellMetadata": {
294+
"byteLimit": 2048000,
295+
"rowLimit": 10000
296+
},
297+
"inputWidgets": {},
298+
"nuid": "93d78ec5-f160-4531-9556-d483ff229fad",
299+
"showTitle": false,
300+
"tableResultSettingsMap": {},
301+
"title": ""
302+
}
303+
},
304+
"outputs": [],
305+
"source": [
306+
"users_df_1159\\\n",
307+
" .join(second_sales_df_1159, users_df_1159.user_id == second_sales_df_1159.seller_id, how=\"left\") \\\n",
308+
" .withColumn(\"2nd_item_fav_brand\", when(col(\"second_item_brand\") == col(\"favorite_brand\"), \"yes\").otherwise(\"no\")) \\\n",
309+
" .select(col(\"user_id\").alias(\"seller_id\"), \"2nd_item_fav_brand\").show()"
310+
]
190311
}
191312
],
192313
"metadata": {
193314
"application/vnd.databricks.v1+notebook": {
194-
"computePreferences": null,
315+
"computePreferences": {
316+
"hardware": {
317+
"accelerator": null,
318+
"gpuPoolId": null,
319+
"memory": null
320+
}
321+
},
195322
"dashboards": [],
196323
"environmentMetadata": {
197324
"base_environment": "",
198-
"environment_version": "1"
325+
"environment_version": "2"
199326
},
200327
"inputWidgetPreferences": null,
201328
"language": "python",
202329
"notebookMetadata": {
203330
"pythonIndentUnit": 4
204331
},
205-
"notebookName": "1159. Market Analysis II (Hard)",
332+
"notebookName": "1159. Market Analysis II (Hard)-(Solved)",
206333
"widgets": {}
334+
},
335+
"language_info": {
336+
"name": "python"
207337
}
208338
},
209339
"nbformat": 4,
210340
"nbformat_minor": 0
211-
}
341+
}

Solved/1164. Product Price at a Given Date (Medium)-(Solved).ipynb

Lines changed: 140 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": "c8535576-d746-40ee-8199-8ccd3e39577f",
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": "09da8fc1-d579-4949-bbc7-9132909993c8",
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": "4567f050-e6f4-4b8b-b899-db2f17021c1d",
4655
"showTitle": false,
@@ -95,7 +104,10 @@
95104
"execution_count": 0,
96105
"metadata": {
97106
"application/vnd.databricks.v1+cell": {
98-
"cellMetadata": {},
107+
"cellMetadata": {
108+
"byteLimit": 2048000,
109+
"rowLimit": 10000
110+
},
99111
"inputWidgets": {},
100112
"nuid": "9b14d03f-22ca-4fa2-b371-32a135a82877",
101113
"showTitle": false,
@@ -118,25 +130,145 @@
118130
"products_df_1164 = spark.createDataFrame(products_data_1164, products_columns_1164)\n",
119131
"products_df_1164.show()"
120132
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"execution_count": 0,
137+
"metadata": {
138+
"application/vnd.databricks.v1+cell": {
139+
"cellMetadata": {
140+
"byteLimit": 2048000,
141+
"rowLimit": 10000
142+
},
143+
"inputWidgets": {},
144+
"nuid": "a5ad61b8-3d8e-46d8-b91b-dff68020ed0a",
145+
"showTitle": false,
146+
"tableResultSettingsMap": {},
147+
"title": ""
148+
}
149+
},
150+
"outputs": [],
151+
"source": [
152+
"products_filtered_df_1164 = products_df_1164.filter(col(\"change_date\").cast(\"date\") <= \"2019-08-16\")"
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": 0,
158+
"metadata": {
159+
"application/vnd.databricks.v1+cell": {
160+
"cellMetadata": {
161+
"byteLimit": 2048000,
162+
"rowLimit": 10000
163+
},
164+
"inputWidgets": {},
165+
"nuid": "d0abc6ab-79e5-42be-87e0-a5f634f56bc5",
166+
"showTitle": false,
167+
"tableResultSettingsMap": {},
168+
"title": ""
169+
}
170+
},
171+
"outputs": [],
172+
"source": [
173+
"window_spec = Window.partitionBy(\"product_id\").orderBy(col(\"change_date\").desc())"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": 0,
179+
"metadata": {
180+
"application/vnd.databricks.v1+cell": {
181+
"cellMetadata": {
182+
"byteLimit": 2048000,
183+
"rowLimit": 10000
184+
},
185+
"inputWidgets": {},
186+
"nuid": "b2d9f1ef-4810-4dba-bfe0-3c3c6b2ab1c1",
187+
"showTitle": false,
188+
"tableResultSettingsMap": {},
189+
"title": ""
190+
}
191+
},
192+
"outputs": [],
193+
"source": [
194+
"latest_prices_df_1164 = products_filtered_df_1164\\\n",
195+
" .withColumn(\"rank\", row_number().over(window_spec)) \\\n",
196+
" .filter(col(\"rank\") == 1) \\\n",
197+
" .select(\"product_id\", col(\"new_price\").alias(\"price\"))"
198+
]
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": 0,
203+
"metadata": {
204+
"application/vnd.databricks.v1+cell": {
205+
"cellMetadata": {
206+
"byteLimit": 2048000,
207+
"rowLimit": 10000
208+
},
209+
"inputWidgets": {},
210+
"nuid": "94d19529-7b74-4134-a320-00105939a0b2",
211+
"showTitle": false,
212+
"tableResultSettingsMap": {},
213+
"title": ""
214+
}
215+
},
216+
"outputs": [],
217+
"source": [
218+
"all_product_ids_df_1164 = products_df_1164.select(\"product_id\").distinct()"
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"execution_count": 0,
224+
"metadata": {
225+
"application/vnd.databricks.v1+cell": {
226+
"cellMetadata": {
227+
"byteLimit": 2048000,
228+
"rowLimit": 10000
229+
},
230+
"inputWidgets": {},
231+
"nuid": "344b7686-353a-41c5-ac71-e40d0ea1944d",
232+
"showTitle": false,
233+
"tableResultSettingsMap": {},
234+
"title": ""
235+
}
236+
},
237+
"outputs": [],
238+
"source": [
239+
"all_product_ids_df_1164\\\n",
240+
" .join(latest_prices_df_1164, on=\"product_id\", how=\"left\") \\\n",
241+
" .withColumn(\"price\", coalesce(col(\"price\"), lit(10))) \\\n",
242+
" .select(\"product_id\", \"price\").show()"
243+
]
121244
}
122245
],
123246
"metadata": {
124247
"application/vnd.databricks.v1+notebook": {
125-
"computePreferences": null,
248+
"computePreferences": {
249+
"hardware": {
250+
"accelerator": null,
251+
"gpuPoolId": null,
252+
"memory": null
253+
}
254+
},
126255
"dashboards": [],
127256
"environmentMetadata": {
128257
"base_environment": "",
129-
"environment_version": "1"
258+
"environment_version": "2"
130259
},
131260
"inputWidgetPreferences": null,
132261
"language": "python",
133262
"notebookMetadata": {
134263
"pythonIndentUnit": 4
135264
},
136-
"notebookName": "1164. Product Price at a Given Date (Medium)",
265+
"notebookName": "1164. Product Price at a Given Date (Medium)-(Solved)",
137266
"widgets": {}
267+
},
268+
"language_info": {
269+
"name": "python"
138270
}
139271
},
140272
"nbformat": 4,
141273
"nbformat_minor": 0
142-
}
274+
}

0 commit comments

Comments
 (0)