Swift solution - Runtime Error


  • 0
    G

    Below is my Swift solution. It works and compiles on my local machine via Swift Playgrounds (Xcode 8.3). However, when I submitted this code I got an error: "Variable column size in 2d array is not supported. Column size expected to be constant size (5) but got size (4)."

    class Solution {
        func smallestRange(_ nums: [[Int]]) -> [Int] {
            var numLists = [[Num]]()
            for (index, list) in nums.enumerated() {
                numLists.append(Num.initNumArray(list, forListIndex: index))
            }
            let numList = numLists.flatMap { $0 }.sorted()
            guard numList.count > 0 else { return [Int]() }
            
            var lowBound = Index(val: numList.first!.val, listIndex: numList.first!.listIndex)
            var highBound = Index(val: numList.last!.val, listIndex: numList.last!.listIndex)
            var minRangeValue = highBound.val - lowBound.val
            
            var range = [Index]()
            
            for num in numList {
                
                let indexNum = Index(val: num.val, listIndex: num.listIndex)
                if let index = range.index(of: indexNum) {
                    range.remove(at: index)
                }
                range.append(indexNum)
                
                // if the sub-range contains elements from all lists
                if range.count == nums.count {
                    let rangeVal = range.last!.val - range.first!.val
                    print("rangeVal: \(rangeVal) low: \(lowBound.val) high: \(highBound.val)")
                    if (rangeVal < minRangeValue) {
                        lowBound = range.first!
                        highBound = range.last!
                        minRangeValue = rangeVal
                        print("after modification: minRangeValue: \(minRangeValue) low: \(lowBound.val) high: \(highBound.val)")
                    }
                }
            }
            
            var rangeArray = [Int]()
            rangeArray.append(lowBound.val)
            rangeArray.append(highBound.val)
            return rangeArray
        }
    }
    
    struct Index: Hashable, Equatable {
        var val: Int
        var listIndex: Int
    
        init(val: Int, listIndex: Int) {
            self.val = val
            self.listIndex = listIndex
        }
    
        static func == (lhs: Index, rhs: Index) -> Bool {
            return lhs.listIndex == rhs.listIndex
        }
        
        var hashValue: Int {
            return listIndex
        }
    
    }
    
    struct Num: Comparable {
        var val: Int
        var listIndex: Int
        
        init(val: Int, listIndex: Int) {
            self.val = val
            self.listIndex = listIndex
        }
        
        static func initNumArray(_ array: [Int], forListIndex listIndex: Int) -> [Num] {
            var output = [Num]()
            for i in array {
                output.append(Num(val:i, listIndex: listIndex))
            }
            
            return output
        }
        
        static func < (lhs: Num, rhs: Num) -> Bool {
            return lhs.val < rhs.val
        }
        
        static func == (lhs: Num, rhs: Num) -> Bool {
            return lhs.val == rhs.val
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.