File tree Expand file tree Collapse file tree 2 files changed +16
-20
lines changed
leetcode.com/swift/DS_ALGO_PRAC.playground Expand file tree Collapse file tree 2 files changed +16
-20
lines changed Original file line number Diff line number Diff line change 1
1
import Foundation
2
2
3
- class Solution {
4
- func slowestKey( _ releaseTimes: [ Int ] , _ keysPressed: String ) -> Character {
5
- let keys = Array ( keysPressed)
6
- var times = [ Character: Int] ( )
7
- for i in 0 ..< keys. count {
8
- if i == 0 {
9
- times [ keys [ i] , default: 0 ] = releaseTimes [ i]
10
- } else {
11
- times [ keys [ i] , default: 0 ] = releaseTimes [ i] - releaseTimes[ i - 1 ]
12
- }
13
- }
14
-
15
- var timeCharMap = [ Int: Character] ( )
16
- for (key, value) in times {
17
- timeCharMap [ value] = key
18
- }
19
-
20
- return timeCharMap [ timeCharMap. keys. sorted ( ) . first]
21
- }
22
- }
Original file line number Diff line number Diff line change
1
+ import Foundation
2
+
3
+ class Solution {
4
+ func slowestKey( _ releaseTimes: [ Int ] , _ keysPressed: String ) -> Character {
5
+ let keys = Array ( keysPressed)
6
+ var ( maxTime, maxTimeChar) = ( releaseTimes. first!, keys. first!)
7
+ for i in 1 ..< keys. count {
8
+ let time = releaseTimes [ i] - releaseTimes[ i - 1 ]
9
+ if time > maxTime || ( time == maxTime && keys [ i] > maxTimeChar) {
10
+ maxTimeChar = keys [ i]
11
+ maxTime = time
12
+ }
13
+ }
14
+ return maxTimeChar
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments