✔️ 100% Fastest Swift Solution, 0(n) + O(n).
\nclass Solution {\n\t// - Complexity:\n    //   - time: 0(n) + O(n)\n\n    func candy(_ ratings: [Int]) -> Int {\n\t\tvar indexs: [Int] = []\n\t\tvar count = 0
```\nclass Solution {\n\t// - Complexity:\n    //   - time: 0(n) + O(n)\n\n    func candy(_ ratings: [Int]) -> Int {\n\t\tvar indexs: [Int] = []\n\t\tvar count = 0\n        \n        for i in 0..<ratings.count {\n\t\t\tlet left = i > 0 ? ratings[i - 1] : Int.max\n\t\t\tlet right = i < ratings.count - 1 ? ratings[i + 1] : Int.max\n\t\t\t\n            if ratings[i] <= left && ratings[i] <= right {\n\t\t\t\tindexs.append(i)\n\t\t\t\tcount += 1\n\t\t\t}\n\t\t}\n\t\t\n        indexs.append(ratings.count)\n\t\t\n        var prev = -1\n        for index in indexs {\n\t\t\tlet start = prev + 1\n\t\t\tlet end = index\n\t\t\t\n            if end > start {\n\t\t\t\tvar maxRating = Int.min\n\t\t\t\tvar maxIndex = Int.min\n\t\t\t\t\n                for i in start..<end {\n\t\t\t\t\tif ratings[i] >= maxRating {\n\t\t\t\t\t\tmaxRating = ratings[i]\n\t\t\t\t\t\tmaxIndex = i\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n                var leftCount = maxIndex - start\n\t\t\t\tvar rightCount = end - maxIndex - 1\n\t\t\t\t\n                if maxIndex - 1 >= start && ratings[maxIndex - 1] == maxRating {\n\t\t\t\t\trightCount += 1\n\t\t\t\t\n                } else {\n\t\t\t\t\tif leftCount > rightCount {\n\t\t\t\t\t\tleftCount += 1\n\t\t\t\t\t\n                    } else {\n\t\t\t\t\t\trightCount += 1\n\t\t\t\t\t}\n\t\t\t\t}\n                \n\t\t\t\tcount += (2 + leftCount + 1) * leftCount / 2 + (2 + rightCount + 1) * rightCount / 2\n\t\t\t}\n\t\t\t\n            prev = index\n\t\t}\n        \n\t\treturn count\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']