Learn to Code by Solving Problems by Daniel Zingaro
Author:Daniel Zingaro [Zingaro, Daniel]
Language: eng
Format: azw3, pdf
ISBN: 9781718501331
Publisher: No Starch Press
Published: 2021-06-28T16:00:00+00:00
The Top Level
To begin our design, we focus on the main tasks that weâll need to solve.
Weâll certainly have to read the input, so thatâs our first task.
Now, assume that weâve read the input. What should we do to determine whether the boxes can be organized? One important thing to do is check each box on its own to make sure that its action figures have their heights in order. For example, suppose that we had the box [18, 20, 4]. This box, with heights out of order, means that we have no chance of organizing all of the boxes. We canât even organize this one!
So, thatâs our second task: determine whether each box, on its own, has its action figures in order. If any of these boxes has its action figures out of order, then we know that the boxes canât be organized. If all boxes are OK, then we have more to check.
If each box on its own is OK, the next question is whether we can organize all of the boxes. One important observation we can make here is that the only action figures we care about from now on are the ones at the left and right sides of each box. The action figures between these donât matter anymore.
Consider this example where we have three boxes:
[[9, 13, 14, 17, 25],
[32, 33, 34, 36],
[1, 6]]
The first box starts with an action figure of height 9 and ends with an action figure of height 25. Action figures placed to the left of this box must all have height 9 or less; for example, we can place the third box to the left of this box. Action figures placed to the right of this box must all have height 25 or more; for example, we can place the second box to the right of this box. The action figures of heights 13, 14, and 17 change nothing; they may as well not be there.
Thatâs our third task then: ignore all action figures except those on the ends of boxes.
Following that third task, weâll have a list that looks like this:
[[9, 25],
[32, 36],
[1, 6]]
Itâs a lot easier to tell whether we can organize these boxes if we first sort them, like this:
[[1, 6],
[9, 25],
[32, 36]]
Now itâs easy to see what the neighboring boxes of a box must be. (We used a similar approach when solving Village Neighborhood in Chapter 5.) So, our fourth task is to sort the boxes.
Our fifth and final task is to determine whether these sorted boxes are organized. They are organized if the heights of action figures are sorted from left to right. The action figures of heights 1, 6, 9, 25, 32, and 36 are appropriately sorted, so the previous boxes can be organized. But consider this example:
[[1, 6],
[9, 50],
[32, 36]]
These boxes canât be organized because of that huge action figure in the second box. That second box takes up heights 9 to 50; the third box canât go on the right of the second box because its heights are too small.
Download
Learn to Code by Solving Problems by Daniel Zingaro.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9912)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8295)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7762)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6818)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6547)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6414)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4037)
Solidity Programming Essentials by Ritesh Modi(3992)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3782)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3723)
The Ultimate iOS Interview Playbook by Avi Tsadok(3699)
