From c712dad956f1c399eb54f5d68d43934f7dfbf42d Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Thu, 30 May 2024 07:59:43 +0100 Subject: [PATCH] Swift implementation for LCOF 63 --- .../README.md" | 18 ++++++++++++++++++ .../Solution.swift" | 13 +++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/Solution.swift" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/README.md" index 548bf95dd890a..6ab24b2393904 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/README.md" @@ -174,6 +174,24 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + func maxProfit(_ prices: [Int]) -> Int { + var mi = Int.max + var ans = 0 + + for x in prices { + ans = max(ans, x - mi) + mi = min(mi, x) + } + + return ans + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/Solution.swift" new file mode 100644 index 0000000000000..9193c85b984af --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/Solution.swift" @@ -0,0 +1,13 @@ +class Solution { + func maxProfit(_ prices: [Int]) -> Int { + var mi = Int.max + var ans = 0 + + for x in prices { + ans = max(ans, x - mi) + mi = min(mi, x) + } + + return ans + } +} \ No newline at end of file