Skip to content

Instantly share code, notes, and snippets.

@ihainan
Created July 18, 2018 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihainan/1d73a472243b4e954e589518c3d34287 to your computer and use it in GitHub Desktop.
Save ihainan/1d73a472243b4e954e589518c3d34287 to your computer and use it in GitHub Desktop.
object Solution498 {
def findDiagonalOrder(matrix: Array[Array[Int]]): Array[Int] = {
if (matrix.length == 0 || matrix(0).length == 0) Array.empty[Int]
else {
val (row, column) = (matrix.length - 1, matrix(0).length - 1)
var flag = true
(0 to row + column).flatMap { sum =>
val (low, high) = (0 max (sum - column), row min sum)
val range = if (flag) (high to low by -1) else (low to high)
flag = !flag
range.map(r => matrix(r)(sum - r))
}.toArray
}
}
}
object Solution121 {
def maxProfit(prices: Array[Int]): Int = {
if (prices.length == 0 || prices.length == 1) 0
else {
var (ans, minValue) = (0, prices(0))
(1 until prices.length).foreach { i =>
ans = ans max (prices(i) - minValue)
minValue = minValue min prices(i)
}
ans
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment