Find maximum possible time HH:MM by permuting four given digits

前端 未结 23 2084
执念已碎
执念已碎 2020-11-30 02:44

I recently took a coding test for a promotion at work. This was one of the tasks I really struggled with and was wondering what is the best way to do this. I used a load of

23条回答
  •  旧巷少年郎
    2020-11-30 02:55

    This solution is in Swift 3.0.

    func returnValue (_ value :inout Int, tempArray : [Int] , compareValue : Int) -> Int {
    
        for i in tempArray {
    
            if value <= i && i <= compareValue {
                value = i
            }
        }
    
        return value
    }
    
    func removeValue(_ value : Int, tempArr : inout [Int]) -> Bool {
        let index = tempArr.index(of: value)
        tempArr.remove(at: index ?? 0)
        return index != nil ? true : false
    }
    
    public func solution(_ A : Int, _ B : Int, _ C : Int, _ D : Int) -> String {
    
        var tempArray = [A, B, C, D]
    
        let mainArray = [A, B, C, D]
    
        var H1 : Int = -1, H2: Int = -1, M1 : Int = -1, M2 : Int = -1;
    
        H1 = returnValue(&H1, tempArray: tempArray, compareValue: 2)
    
        if !removeValue(H1, tempArr: &tempArray) {
            return "NOT POSSIBLE"
        }
    
        for value in tempArray {
    
            if H1 < 2 {
                if H2 <= value && value <= 9 {
                    H2 = value
                }
            } else {
                if H2 <= value && value <= 3 {
                    H2 = value
                }
            }
        }
    
        if !removeValue(H2, tempArr: &tempArray) {
            return "NOT POSSIBLE"
        }
    
        M1 = returnValue(&M1, tempArray: tempArray, compareValue: 5)
    
    
        if M1 >= 0 {
    
            if !removeValue(M1, tempArr: &tempArray) {
                return "NOT POSSIBLE"
            }
        } else if mainArray.contains(0) || mainArray.contains(1) {
    
            H1 = -1
    
            H1 = returnValue(&H1, tempArray: mainArray, compareValue: 1)
    
            for value in mainArray {
    
                if H1 < 2 {
                    if H2 <= value && value <= 9 {
                        H2 = value
                    }
                } else {
                    if H2 <= value && value <= 3 {
                        H2 = value
                    }
                }
            }
    
    
            tempArray.removeAll()
    
            for value in mainArray {
                tempArray.append(value)
            }
    
    
            var index = tempArray.index(of: H1)
            tempArray.remove(at: index!)
    
            index = tempArray.index(of: H2)
            tempArray.remove(at: index!)
    
            M1 = -1
            M1 = returnValue(&M1, tempArray: tempArray, compareValue: 5)
    
            if !removeValue(M1, tempArr: &tempArray) {
                return "NOT POSSIBLE"
            }
    
        } else {
            return "NOT POSSIBLE"
        }
    
        // Now last we have M2 = temp.last
    
        if let lastValue = tempArray.last {
            M2 = lastValue
        }
    
        if M2 < 0 {
            return "NOT POSSIBLE"
        }
    
        return "\(H1)\(H2):\(M1)\(M2)"
    }
    
    
    print(solution(1,7,2,7))
    print(solution(0,0,2,9))
    print(solution(6,5,2,0))
    print(solution(3,9,5,0))
    print(solution(7,6,3,8))
    print(solution(0,0,0,0))
    print(solution(9,9,9,9))
    print(solution(1,2,3,4))
    
     17:27
     20:09
     20:56
     09:53
     NOT POSSIBLE
     00:00
     NOT POSSIBLE
     23:41
    

提交回复
热议问题