From 97fecfe6140081e4cec7b3d0249a686b6fd84cea Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 2 Jun 2024 11:12:08 +0200 Subject: [PATCH] =?UTF-8?q?swift:=20add=20=C2=AB739.=20Daily=20Temperature?= =?UTF-8?q?s=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- swift/daily-temperatures.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 swift/daily-temperatures.swift diff --git a/swift/daily-temperatures.swift b/swift/daily-temperatures.swift new file mode 100644 index 0000000..319a656 --- /dev/null +++ b/swift/daily-temperatures.swift @@ -0,0 +1,17 @@ +class Solution { + func dailyTemperatures(_ temperatures: [Int]) -> [Int] { + var result = [Int](repeating: 0, count: temperatures.count) + var st: [Int] = [] + + for (i, t) in temperatures.enumerated() { + while !st.isEmpty && temperatures[st.last!] < t { + result[st.last!] = i - st.last! + st.removeLast() + } + + st.append(i) + } + + return result + } +}