Git Product home page Git Product logo

syntax-it-flights's Introduction

syntax-it-flights

An assesment from SyntaxIT that requested the maximum duration sum of exactly two movies, from a list of movies, with the following constraint:

flightDuration - 30 <= sum <= flightDuration

I approached it the following way:

  1. I found a naive solution, using a nested for-loop that took O(n2)
  2. I thought that it would help if my list was ordered, but then the original indexing would be lost
  3. I created a HashMap(key -> movie duration, value -> original index), which has complexity of O(n) and then I sorted the list, with complexity O(nlogn) (I thinkJava uses TimSort) . That way I could hold on to the index, and find them in O(1).
  4. I then kept two pointers, one that starts at the beginning of my list (smallest duration) and one that starts on the rightmost element.
  5. At each iteration, the method calculates the combined length of the two movies pointed to by the pointers. If this combined length is less than the flight length minus a buffer of 30 minutes, the method increments the left pointer. If the combined length is greater than the flight length, the method decrements the right pointer. If the combined length is between the flight length and the flight length minus the buffer, the method checks if this combined length is greater than the current maximum combined length. If it is, the method updates the maxRuntime, smallerMovieLength, and longestMovieLength variables accordingly.

Run instructions:

Java 17
javac Main.java
java Main 

syntax-it-flights's People

Contributors

zabatistas 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.