✔️ 100% Fastest Swift Solution, time: O(n), space: O(1).
\nclass Solution {\n    // - Complexity:\n    //   - time: O(n), where n is the number of prices.\n    //   - space: O(1), only constant space is used.\n    \n 
```\nclass Solution {\n    // - Complexity:\n    //   - time: O(n), where n is the number of prices.\n    //   - space: O(1), only constant space is used.\n    \n    func maxProfit(_ prices: [Int]) -> Int {\n        var minPrice = Int.max\n        var ans = 0\n        \n        for price in prices {\n            minPrice = min(minPrice, price)\n            ans = max(ans, price - minPrice)\n        }\n        \n        return ans        \n    }\n\n}\n```\n\nLet me know in comments if you have any doubts. I will be happy to answer.\n\nPlease upvote if you found the solution useful.
['Swift']