Coder Social home page Coder Social logo

akineyshen / reservations Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 19 KB

This Java program calculates the maximum profit from booking reservations for two rooms, using dynamic programming and memoization.

License: The Unlicense

Java 100.00%
hotel reservations room maxprofit java

reservations's Introduction

Reservations

This Java program is designed to calculate the maximum profit that can be obtained from booking reservations for two rooms. It does so by employing dynamic programming and memoization techniques to efficiently solve the problem.

The program defines a nested class Reserves to store the start day, end day, and price of each reservation. The main logic for calculating the maximum profit is encapsulated within the MaxProfit method, which takes the current index in the list of reservations, the current availability of each room, the list of all reservations, and a memoization hashmap to avoid recalculating profits for the same states.

The algorithm works as follows:

  • If it reaches the end of the reservations list, it returns 0 since there are no more reservations to process.
  • It generates a unique key for the current state (index and room availabilities) and checks if this state has already been computed. If so, it returns the stored result to avoid redundant calculations.
  • It recursively calls itself to calculate the profit of skipping the current reservation.
  • It also iterates through each room to check if the current reservation can be accommodated (based on start and end days). If the reservation fits, it calculates the profit for taking this reservation and updates the room's availability.
  • The maximum profit between taking or skipping the current reservation is then calculated and stored in the memoization hashmap.

The main method reads input from a file named "in.txt," which contains the reservations data. It initializes a list of Reserves objects with this data and sorts them by start day to ensure reservations are processed in chronological order. The program then invokes the MaxProfit method with initial parameters and prints the maximum profit that can be achieved.

reservations's People

Contributors

akineyshen avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.