Skip to content

Instantly share code, notes, and snippets.

@nuboat
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nuboat/8fe9099829686d841e87 to your computer and use it in GitHub Desktop.
Save nuboat/8fe9099829686d841e87 to your computer and use it in GitHub Desktop.
ปัญหา 2-SUM
true
Execution n = 1007377 in 1521 ms
true
Tuning Execution n = 1007377 in 1394 ms
object TwoSum {
def sum(n: Int, sorted: ArrayBuffer[Int]): Boolean = sorted.par.find(x => isMatch(x, n, sorted)).isDefined
def isMatch(x: Int, n: Int, sorted: ArrayBuffer[Int]): Boolean = sorted.find( y => (x + y) == n).isDefined
}
object TwoSumTuning {
def sum(n: Int, sorted: ArrayBuffer[Int]): Boolean = sorted.par.find(x => isMatch(x, n, sorted)).isDefined
def isMatch(x: Int, n: Int, sorted: ArrayBuffer[Int]): Boolean = {
var result = false
breakable {
var a = 0
for (a <- x until sorted.size) {
if ((x + sorted(a)) == n) {
result = true
break
}
}
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment