Skip to content

Practice Capital Gain/Loss

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1393
LeetCode Problem

Capital Gain/Loss

Problem Statement

Tables: Stocks(stock_name VARCHAR, operation ENUM, operation_day INT, price INT). Stocks has key (stock_name, operation_day). operation is Buy or Sell. Every buy has a later matching sell, and every sell has an earlier matching buy. Return stock_name and capital_gain_loss, calculated as total selling prices minus total buying prices for that stock. Output order is unrestricted.

Source: Capital Gain/Loss on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
Stocks table: +---------------+-----------+---------------+--------+ | stock_name    | operation | operation_day | price  | +---------------+-----------+---------------+--------+ | 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  | +---------------+-----------+---------------+--------+
Output
+---------------+-------------------+ | stock_name    | capital_gain_loss | +---------------+-------------------+ | Corona Masks  | 9500              | | Leetcode      | 8000              | | Handbags      | -23000            | +---------------+-------------------+

Related LeetCode Problems

Practice more problems similar to Capital Gain/Loss.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.