Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • CompanyInterviews >
      • InvestmentBanks >
        • ECS
        • Bank Of America
        • WesternUnion
        • WellsFargo
      • ProductBasedCompanies >
        • CA Technologies
        • Model N India
        • Verizon Media
        • Oracle & GoJek
        • IVY Computec
        • Nvidia
        • ClearWaterAnalytics
        • ADP
        • ServiceNow
        • Pubmatic
        • Expedia
        • Amphora
        • CDK Global
        • CDK Global
        • Epic
        • Sincro-Pune
        • Whiz.AI
        • ChargePoint
      • ServiceBasedCompanies >
        • Altimetrik
        • ASG World Wide Pvt Ltd
        • Paraxel International & Pramati Technologies Pvt Ltd
        • MitraTech
        • Intelizest Coding Round
        • EPAM
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • TemplatePattern
        • Adapter Design Pattern
        • Decorator
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • RequestChaining
        • Singleton >
          • Singletons
  • Frameworks
    • Apache Velocity
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • ApacheSpark Installation
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Messaging Formats
      • Design Patterns
    • AWS >
      • Honeycode
    • Dockers >
      • GitBash
      • Issues
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
  • Your Views

How far can you Go

7/4/2021

0 Comments

 
 You are given an array of integers, where every value denotes the height of the building.
 You are also given some Ladders & Bricks
 If the height of the next building is more than current building, then you can use either Ladder
 or Bricks but if next building height is less, then you can jump to next building directly.

 So if next building height is more you need Ladder/Builder else you can move to next building without consuming any resource.
 
Conditions :
 1) Ladder can be used for any height, can assume length of Ladder is infinite.
 2) For height difference of e.g. 5, you will need 5 bricks, so 1 Brick covers 1 unit of height only.
 3) If you use any Brick or Ladder to move ahead then it can't be used again.

 Now you need to find the farthest index to which you can move using the given resources optimally. You will start from index 0.
 e.g. heights : 4,12,2,7,3,18,20,3,19
        Bricks : 10 & Ladders : 2
 
Answer: One can reach to index 7 with height 3, as either all resources are consumed to
               reach here or don't have sufficient resources remaining.

 E
xplanation : 
                     You can use 8 Bricks to move from Building 4 to Building 12
                     Jump to building with height 2.

                     Use Ladder to move from Building 2 to Building 7
                     Jump to building with height 3.

                     Use Ladder to move from Building 3 to Building 18
                     Use 2 Bricks to move from Building 18 to Building 20
                     Jump to building with height 3.
 So answer will be 7, that is last index in the given array to which you can move to, using the
 given resources.
​
 Approach : First consume all the ladders & keep the minimum gap where you have used the
 ladder, one can use the sorted list or Priority Queue to keep the minimum gap at the top.
 PriorityQueue concept has been suggested by one another reader & it will be performance
 efficient.
 After consuming all the ladders, if get any gap which is greater than the minimum gap used for ladder, then remove minimum gap to use for bricks & use ladder for new bigger gap. Also put this new in the collection being mantained above for ladders.

Solution : 
Picture
0 Comments



Leave a Reply.

    Author

    Nitin Agrawal

    Archives

    July 2021

    Categories

    All

Powered by Create your own unique website with customizable templates.