Created
July 18, 2018 01:27
-
-
Save ihainan/1d73a472243b4e954e589518c3d34287 to your computer and use it in GitHub Desktop.
This file contains 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
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