1393. Capital Gain/Loss LeetCode Solution

In this guide, you will get 1393. Capital Gain/Loss LeetCode Solution with the best time and space complexity. The solution to Capital Gain/Loss problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Capital Gain/Loss solution in C++
  4. Capital Gain/Loss solution in Java
  5. Capital Gain/Loss solution in Python
  6. Additional Resources
1393. Capital Gain/Loss LeetCode Solution image

Problem Statement of Capital Gain/Loss

Table: Stocks

+—————+———+
| Column Name | Type |
+—————+———+
| stock_name | varchar |
| operation | enum |
| operation_day | int |
| price | int |
+—————+———+
(stock_name, operation_day) is the primary key (combination of columns with unique values) for this table.
The operation column is an ENUM (category) of type (‘Sell’, ‘Buy’)
Each row of this table indicates that the stock which has stock_name had an operation on the day operation_day with the price.
It 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.

Write a solution to report the Capital gain/loss for each stock.
The Capital gain/loss of a stock is the total gain or loss after buying and selling the stock one or many times.
Return the result table in any order.
The result format is in the following example.

Example not found

Constraints not found

Complexity Analysis

  • Time Complexity: Google AdSense
  • Space Complexity: Google Analytics
See also  1422. Maximum Score After Splitting a String LeetCode Solution

1393. Capital Gain/Loss LeetCode Solution in C++

SELECT
  stock_name,
  SUM(IF(operation = 'Buy', -price, price)) AS capital_gain_loss
FROM Stocks
GROUP BY 1;
/* code provided by PROGIEZ */

1393. Capital Gain/Loss LeetCode Solution in Java

N/A
// code provided by PROGIEZ

1393. Capital Gain/Loss LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.