Created
January 13, 2018 07:58
-
-
Save pixelknitter/962848553ade616d94a77c0b19fe843a to your computer and use it in GitHub Desktop.
CodePath Pre-Work
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func longestSequence(_ A: [Int]) -> Int { | |
var adjacentCounts = [Int: Int]() | |
var max = 0 | |
for index in 0..<A.count { | |
if adjacentCounts[index] != nil { | |
continue | |
} | |
var leftSide = adjacentCounts[index - 1] ?? 0 | |
var rightSide = adjacentCounts[index + 1] ?? 0 | |
adjacentCounts[index] = leftSide + rightSide + 1 | |
// length of left side consecutive sequence + right side consecutive sequence + itself | |
var passIndex = index - 1 | |
while leftSide >= 0 { | |
adjacentCounts[passIndex] = adjacentCounts[index] | |
print(adjacentCounts[index]!) | |
passIndex -= 1 | |
leftSide = adjacentCounts[passIndex]! | |
} | |
passIndex = index + 1 | |
while rightSide >= 0 { | |
adjacentCounts[passIndex] = adjacentCounts[index] | |
passIndex += 1 | |
rightSide = adjacentCounts[passIndex]! | |
} | |
adjacentCounts[A[index]] = leftSide + rightSide + 1 | |
if(adjacentCounts[A[index]]! > max) { | |
max = adjacentCounts[A[index]]! | |
} | |
} | |
return max | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func nextGreater(_ A: inout [Int]) -> [Int] { | |
var greatest = Array(repeating: -1, count: A.count) | |
var seen = [Int]() | |
for indexA in stride(from: A.count - 1, to: -1, by: -1) { | |
seen.append(A[indexA]) | |
for indexB in 0..<seen.count { | |
if seen[indexB] > A[indexA] { | |
greatest[indexA] = seen[indexB] | |
} | |
} | |
} | |
return greatest | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func numRange(_ A: inout [Int], _ B: inout Int, _ C: inout Int) -> Int { | |
func findSum(_ sequence: [Int], _ start: Int, _ end: Int) -> Int { | |
var total = 0 | |
for index in start..<end+1 { | |
total += A[index] | |
} | |
return total | |
} | |
var count = 0 | |
for start in 0..<A.count { | |
var outerbreak = false | |
for end in start..<A.count { | |
let sum = findSum(A, start, end) | |
if(sum >= B && sum <= C) { | |
count+=1 | |
} else if(sum > C) { | |
break | |
} else if(sum < B && end == A.count-1) { | |
outerbreak = true | |
} | |
} | |
if(outerbreak) { | |
break | |
} | |
} | |
return count | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func prettyPrint(_ A: inout Int) -> [[Int]] { | |
let dimension = A+(A-1) | |
let center = A-1 | |
var matrix = [[Int]]() | |
for row in 0..<dimension { | |
matrix.append([Int]()) | |
for column in 0..<dimension { | |
matrix[row].append(max(abs(row-center), abs(column-center)) + 1) | |
} | |
} | |
return matrix | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment