Download code/train/C++/0007941_solution.cpp from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 503 Bytes
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/code/train/C%2B%2B/0007941_solution.cpp
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/code/train/C++/0007941_solution.cpp
-
curl -L -o 0007941_solution.cpp https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/code/train/C%2B%2B/0007941_solution.cpp
503 Bytes
| class Solution | |
| { | |
| public: | |
| int maxProfit(const vector<int> &prices) | |
| { | |
| if (prices.size() <= 1) | |
| { | |
| return 0; | |
| } | |
| int result = prices[1] - prices[0]; | |
| int last = result; | |
| for (int i = 2; i < prices.size(); i++) | |
| { | |
| int diff = prices[i] - prices[i - 1]; | |
| last = last > 0 ? last + diff : diff; | |
| result = max(result, last); | |
| } | |
| return result > 0 ? result : 0; | |
| } | |
| }; |