Mar. 25th, 2020

juan_gandhi: (Default)
object model {
  type MovieId = Long
  type UserId  = Long
  case class View(userId: UserId, movieId: MovieId)
}
import model._

object Solution extends App {
  def diverseMovies(views: Seq[View], numMovie: Int): Set[MovieId] = {
    def choose(except: Set[UserId] = Set.empty): MovieId = {
      views.collect { case view if !except(view.userId) => view }.
            groupBy(_.movieId).mapValues(_.size).maxBy(_._2)._1
    }
    
    def viewersOf(movie: MovieId) =
      views.filter(_.movieId equals movie).map(_.userId).toSet
    
    val result: (List[MovieId], Set[UserId]) =
      ((List.empty[MovieId], Set.empty[UserId])/:(1 to numMovie)) ({
        case ((movies: List[MovieId], ignoredUsers: Set[MovieId]), _) => {
          val movie = choose(ignoredUsers)
          (movie::movies, ignoredUsers ++ viewersOf(movie))
        }
      })
    result._1.toSet
      
  }
}
juan_gandhi: (Default)
"Approximately one half of the patients at Kaiser Permanente’s San Jose hospital are confirmed or suspected cases of the new coronavirus according to a Los Angeles Times report." https://www.msn.com/en-us/news/us/coronavirus-slams-south-bay-kaiser-hospital-report/ar-BB11IeuF?ocid=hplocalnews 

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

September 2025

S M T W T F S
 1 2345 6
78 9 10 111213
14 151617 181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 18th, 2025 09:53 am
Powered by Dreamwidth Studios