Fetch the repository succeeded.
{
"data": {
"question": {
"questionId": "1523",
"questionFrontendId": "1393",
"boundTopicId": null,
"title": "Capital Gain/Loss",
"titleSlug": "capital-gainloss",
"content": "<p>Table: <code>Stocks</code></p>\n\n<pre>\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| stock_name | varchar |\n| operation | enum |\n| operation_day | int |\n| price | int |\n+---------------+---------+\n(stock_name, operation_day) is the primary key (combination of columns with unique values) for this table.\nThe operation column is an ENUM (category) of type ('Sell', 'Buy')\nEach row of this table indicates that the stock which has stock_name had an operation on the day operation_day with the price.\nIt is guaranteed that each 'Sell' operation for a stock has a corresponding 'Buy' operation in a previous day. It is also guaranteed that each 'Buy' operation for a stock has a corresponding 'Sell' operation in an upcoming day.\n</pre>\n\n<p> </p>\n\n<p>Write a solution to report the <strong>Capital gain/loss</strong> for each stock.</p>\n\n<p>The <strong>Capital gain/loss</strong> of a stock is the total gain or loss after buying and selling the stock one or many times.</p>\n\n<p>Return the result table in <strong>any order</strong>.</p>\n\n<p>The result format is in the following example.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nStocks table:\n+---------------+-----------+---------------+--------+\n| stock_name | operation | operation_day | price |\n+---------------+-----------+---------------+--------+\n| Leetcode | Buy | 1 | 1000 |\n| Corona Masks | Buy | 2 | 10 |\n| Leetcode | Sell | 5 | 9000 |\n| Handbags | Buy | 17 | 30000 |\n| Corona Masks | Sell | 3 | 1010 |\n| Corona Masks | Buy | 4 | 1000 |\n| Corona Masks | Sell | 5 | 500 |\n| Corona Masks | Buy | 6 | 1000 |\n| Handbags | Sell | 29 | 7000 |\n| Corona Masks | Sell | 10 | 10000 |\n+---------------+-----------+---------------+--------+\n<strong>Output:</strong> \n+---------------+-------------------+\n| stock_name | capital_gain_loss |\n+---------------+-------------------+\n| Corona Masks | 9500 |\n| Leetcode | 8000 |\n| Handbags | -23000 |\n+---------------+-------------------+\n<strong>Explanation:</strong> \nLeetcode stock was bought at day 1 for 1000$ and was sold at day 5 for 9000$. Capital gain = 9000 - 1000 = 8000$.\nHandbags stock was bought at day 17 for 30000$ and was sold at day 29 for 7000$. Capital loss = 7000 - 30000 = -23000$.\nCorona Masks stock was bought at day 1 for 10$ and was sold at day 3 for 1010$. It was bought again at day 4 for 1000$ and was sold at day 5 for 500$. At last, it was bought at day 6 for 1000$ and was sold at day 10 for 10000$. Capital gain/loss is the sum of capital gains/losses for each ('Buy' --> 'Sell') operation = (1010 - 10) + (500 - 1000) + (10000 - 1000) = 1000 - 500 + 9000 = 9500$.\n</pre>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 772,
"dislikes": 43,
"isLiked": null,
"similarQuestions": "[]",
"exampleTestcases": "{\"headers\":{\"Stocks\":[\"stock_name\",\"operation\",\"operation_day\",\"price\"]},\"rows\":{\"Stocks\":[[\"Leetcode\",\"Buy\",1,1000],[\"Corona Masks\",\"Buy\",2,10],[\"Leetcode\",\"Sell\",5,9000],[\"Handbags\",\"Buy\",17,30000],[\"Corona Masks\",\"Sell\",3,1010],[\"Corona Masks\",\"Buy\",4,1000],[\"Corona Masks\",\"Sell\",5,500],[\"Corona Masks\",\"Buy\",6,1000],[\"Handbags\",\"Sell\",29,7000],[\"Corona Masks\",\"Sell\",10,10000]]}}",
"categoryTitle": "Database",
"contributors": [],
"topicTags": [
{
"name": "Database",
"slug": "database",
"translatedName": null,
"__typename": "TopicTagNode"
}
],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "MySQL",
"langSlug": "mysql",
"code": "# Write your MySQL query statement below\n",
"__typename": "CodeSnippetNode"
},
{
"lang": "MS SQL Server",
"langSlug": "mssql",
"code": "/* Write your T-SQL query statement below */\n",
"__typename": "CodeSnippetNode"
},
{
"lang": "Oracle",
"langSlug": "oraclesql",
"code": "/* Write your PL/SQL query statement below */\n",
"__typename": "CodeSnippetNode"
},
{
"lang": "Pandas",
"langSlug": "pythondata",
"code": "import pandas as pd\n\ndef capital_gainloss(stocks: pd.DataFrame) -> pd.DataFrame:\n ",
"__typename": "CodeSnippetNode"
},
{
"lang": "PostgreSQL",
"langSlug": "postgresql",
"code": "-- Write your PostgreSQL query statement below\n",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"98K\", \"totalSubmission\": \"114.6K\", \"totalAcceptedRaw\": 98012, \"totalSubmissionRaw\": 114592, \"acRate\": \"85.5%\"}",
"hints": [],
"solution": {
"id": "2030",
"canSeeDetail": true,
"paidOnly": false,
"hasVideoSolution": false,
"paidOnlyVideo": true,
"__typename": "ArticleNode"
},
"status": null,
"sampleTestCase": "{\"headers\":{\"Stocks\":[\"stock_name\",\"operation\",\"operation_day\",\"price\"]},\"rows\":{\"Stocks\":[[\"Leetcode\",\"Buy\",1,1000],[\"Corona Masks\",\"Buy\",2,10],[\"Leetcode\",\"Sell\",5,9000],[\"Handbags\",\"Buy\",17,30000],[\"Corona Masks\",\"Sell\",3,1010],[\"Corona Masks\",\"Buy\",4,1000],[\"Corona Masks\",\"Sell\",5,500],[\"Corona Masks\",\"Buy\",6,1000],[\"Handbags\",\"Sell\",29,7000],[\"Corona Masks\",\"Sell\",10,10000]]}}",
"metaData": "{\"mysql\": [\"Create Table If Not Exists Stocks (stock_name varchar(15), operation ENUM('Sell', 'Buy'), operation_day int, price int)\"], \"mssql\": [\"Create Table Stocks (stock_name varchar(15), operation VARCHAR(10) NOT NULL CHECK (operation IN ('Sell', 'Buy')), operation_day int, price int)\"], \"oraclesql\": [\"Create Table Stocks (stock_name varchar(15), operation VARCHAR(10) NOT NULL CHECK (operation IN ('Sell', 'Buy')), operation_day int, price int)\"], \"database\": true, \"name\": \"capital_gainloss\", \"pythondata\": [\"Stocks = pd.DataFrame([], columns=['stock_name', 'operation', 'operation_day', 'price']).astype({'stock_name':'object', 'operation':'object', 'operation_day':'Int64', 'price':'Int64'})\"], \"postgresql\": [\"Create Table If Not Exists Stocks (stock_name varchar(15), operation VARCHAR(30) CHECK (operation IN ('Sell', 'Buy')), operation_day int, price int)\\n\"], \"database_schema\": {\"Stocks\": {\"stock_name\": \"VARCHAR(15)\", \"operation\": \"ENUM('Sell', 'Buy')\", \"operation_day\": \"INT\", \"price\": \"INT\"}}}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [
"Create Table If Not Exists Stocks (stock_name varchar(15), operation ENUM('Sell', 'Buy'), operation_day int, price int)",
"Truncate table Stocks",
"insert into Stocks (stock_name, operation, operation_day, price) values ('Leetcode', 'Buy', '1', '1000')",
"insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Buy', '2', '10')",
"insert into Stocks (stock_name, operation, operation_day, price) values ('Leetcode', 'Sell', '5', '9000')",
"insert into Stocks (stock_name, operation, operation_day, price) values ('Handbags', 'Buy', '17', '30000')",
"insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Sell', '3', '1010')",
"insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Buy', '4', '1000')",
"insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Sell', '5', '500')",
"insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Buy', '6', '1000')",
"insert into Stocks (stock_name, operation, operation_day, price) values ('Handbags', 'Sell', '29', '7000')",
"insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Sell', '10', '10000')"
],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": false,
"envInfo": "{\"mysql\": [\"MySQL\", \"<p><code>MySQL 8.0</code>.</p>\"], \"mssql\": [\"MS SQL Server\", \"<p><code>mssql server 2019</code>.</p>\"], \"oraclesql\": [\"Oracle\", \"<p><code>Oracle Sql 11.2</code>.</p>\"], \"pythondata\": [\"Pandas\", \"<p>Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0</p>\"], \"postgresql\": [\"PostgreSQL\", \"<p>PostgreSQL 16</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。