score:1
You can see my suggestion for iterative & recursive solutions here -
score:3
I don't know much about DP but I have a few observations that I hope will contribute to the conversation.
First, your example Scala code doesn't appear to solve the LIS problem. I plugged in the Van der Corput sequence as found on the Wikipedia page and did not get the designated result.
Working through the problem this is the solution I came up with.
def lis(a: Seq[Int], acc: Seq[Int] = Seq.empty[Int]): Int =
if (a.isEmpty) acc.length
else if (acc.isEmpty) lis(a.tail, Seq(a.head)) max lis(a.tail)
else if (a.head > acc.last) lis(a.tail, acc :+ a.head) max lis(a.tail, acc)
else lis(a.tail, acc)
lis(Seq(0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15)) // res0: Int = 6
This can be adjusted to return the subsequence itself, and I'm sure it can be tweaked for better performance.
As for memoization, it's not hard to roll-your-own on an as-needed basis. Here's the basic outline to memoize any arity-2 function.
def memo[A,B,R](f: (A,B)=>R): ((A,B)=>R) = {
val cache = new collection.mutable.WeakHashMap[(A,B),R]
(a:A,b:B) => cache.getOrElseUpdate((a,b),f(a,b))
}
With this I can create a memoized version of some often called method/function.
val myfunc = memo{(s:String, i:Int) => s.length > i}
myfunc("bsxxlakjs",7) // res0: Boolean = true
Note: It used to be that WeakHashMap
was recommended so that the cache could drop lesser used elements in memory-challenged environments. I don't know if that's still the case.
Source: stackoverflow.com
Related Query
- Solving dynamic programming problems using functional programming
- Dynamic programming in the functional paradigm
- Generate a DAG from a poset using stricly functional programming
- How to rewrite this code in Scala using a Functional Programming approach
- Scala: dynamic programming recursion using iterators
- Scala way of merging tuples in a list using functional programming techniques
- Solving project Euler 25 in a functional manner using Scala
- How to manage DB connection in Scala using functional programming style?
- Using dynamic programming with Scala to solve the Mixture from CodeChef
- Recursively Generate Hierarchy Data set using spark scala functional programming
- How would I alter a list within a list in Scala using functional programming
- How to feed JSON to CASE CLASS directly using functional programming in scala?
- how can i refactor this using functional programming ideas
- Solve Coin Change Problems with both functional programming and tail recursion?
- How to extract immutable from Map(String, List(String)) list using Functional programming in scala
- Functional programming - is immutability expensive?
- Difference between reduce and foldLeft/fold in functional programming (particularly Scala and Scala APIs)?
- Functional Programming - Lots of emphasis on recursion, why?
- Functional Reactive Programming in Scala
- Is Scala functional programming slower than traditional coding?
- Object-oriented programming in a purely functional programming context?
- Mixing object-oriented and functional programming
- Real World Functional Programming in Scala
- What does the "world" mean in functional programming world?
- What classes of problems is Clojure good/bad at solving vis-a-vis Scala?
- Is Scala a Functional Programming Language?
- Functional Programming + Domain-Driven Design
- Will using Scala in a more functional way (scalaz) incur a performance/maintainability penalty?
- How to return all positives and the first negative number in a list using functional programming?
- Problems on migrating from functional to OO
More Query from same tag
- how to pass in array into udf spark
- Spark(scala): converting a JSON string to dataframe
- Parameterized type converts Char to Int
- How does the initialization of classes in Scala work?
- ScalaQuery many to many
- Json reader with different type in an array
- Testing MultipartForm with multiple file inputs.
- Spark Scala read text file into DataFrame
- for-comprehension: type mismatch - required Option[?]
- Scala HashMap with typed entries
- Understanding the implementation of Y-Combinator
- Different rlike behavior in Spark 1.6 and Spark 2.2
- Remote deploy of [akka://Test/user/simpleActor] is not allowed, falling back to local
- Using JSON Path to update value in json with a json object
- Debugging a filter operation in scala through `in-code variable inspection`
- Get indexes from one array and update elements on same indexes in second array
- How to get all distinct elements per key in DataFrame?
- Encoding singleton objects as lazy vals
- Proof by induction with multiple lists
- Cast values of a Spark dataframe using a defined StructType
- AKKA- how to block the creation of an actor if its name is not uniqe in the cluster
- Omit input data of map function in Scala
- Overriding Scala Enumeration Value
- What does Keep in akka stream mean?
- regex, http link which is not url to image
- sbt 0.13.8 -- what is the difference between buildSettings and projectSettings?
- org.apache.avro.SchemaParseException: Undefined name
- How to convert a String-represented ByteBuffer into a byte array in Java
- Accessing nested fields in AVRO GenericRecord (Java/Scala)
- Reduce Map values concurrently