Memory management for a programming puzzle in java -


i trying solve interesting programming puzzle

there several visitors movie rental store , want rent movies. each person can rent upto 2 movies. rental agency can rent 1 copy of each movie.to solve this, rental agency asks each person give wish list of movies want rent. each person can provide upto 10 wishes. depending on wish list provided people, rental agency wants come solution each person can give unique movie rental.

input:
text file containingg of 2 columns
column1 has perosn id , column2 has movie id wish rent.
there can upto 10 entries each person , file may not sorted.

solution

  1. read entire file line line , create map of person vs list of movies requested.
  2. sort entries in map based on no of movies requested.
  3. traverse sorted list , assign upto 2 movies person list if particular movie not assigned else (using hashset purpose).

this algorithm works reasonable size of file. if input file large out of memory error. specific, while trying store data in map. there other approach can take? away reading entire file in memory.

probably away reading entire file in memory.

yeah, of course. read line line.

bufferedreader br = new bufferedreader(new filereader(file)); while(br.ready()) {     string line = br.readline();     // line } br.close(); 

now, if map won't fit in memory, have very different issue. didn't that's case, i'll assume it's not.

edit: if can't store entire map in memory, use lightweight , local database sqlite. that's they're for.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -