Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A QuizCourse Queries Syllabus Queries 3 years ago
User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
With DP you can do it as follows:
Here is an implementation in JavaScript which you can run here:
function maxNumberOfFittingRectangles(rectangles) {
// Storage of optimal, intermediate results (DP principle),
// keyed by the index of the first rectangle taken
let memo = new Map;
// Take a copy of rectangles, and sort it in order of decreasing width,
// and if there are ties: by decreasing height
rectangles = [...rectangles].sort( (a, b) => (b.width - a.width)
|| (b.height - a.height) );
function recurse(maxHeight, startIndex) {
for (let i = startIndex; i < rectangles.length; i++) {
if (rectangles[i].height <= maxHeight ) { // Can fit
// Try a solution that includes rectangles[i]
// - Only recurse when we did not do this before
if (!(memo.has(i))) memo.set(i, recurse(rectangles[i].height, i+1)+1);
// Also try a solution that excludes rectangles[i], and
// return best of both possibilities:
return Math.max(memo.get(i), recurse(maxHeight, i+1));
}
}
return 0; // recursion's base case
}
let result = recurse(Infinity, 0);
// Display some information for understanding the solution:
for (let i = 0; i < rectangles.length; i++) {
console.log(JSON.stringify(rectangles[i]),
'if taken as first: solution = ', memo.get(i));
}
return result;
}
// Sample data
let rectangles = [
{ width: 10, height: 8 },
{ width: 6, height: 12 },
{ width:
With DP you can do it as follows:
Here is an implementation in JavaScript which you can run here:
function maxNumberOfFittingRectangles(rectangles) {
// Storage of optimal, intermediate results (DP principle),
// keyed by the index of the first rectangle taken
let memo = new Map;
// Take a copy of rectangles, and sort it in order of decreasing width,
// and if there are ties: by decreasing height
rectangles = [...rectangles].sort( (a, b) => (b.width - a.width)
|| (b.height - a.height) );
function recurse(maxHeight, startIndex) {
for (let i = startIndex; i < rectangles.length; i++) {
if (rectangles[i].height <= maxHeight ) { // Can fit
// Try a solution that includes rectangles[i]
// - Only recurse when we did not do this before
if (!(memo.has(i))) memo.set(i, recurse(rectangles[i].height, i+1)+1);
// Also try a solution that excludes rectangles[i], and
// return best of both possibilities:
return Math.max(memo.get(i), recurse(maxHeight, i+1));
}
}
return 0; // recursion's base case
}
let result = recurse(Infinity, 0);
// Display some information for understanding the solution:
for (let i = 0; i < rectangles.length; i++) {
console.log(JSON.stringify(rectangles[i]),
'if taken as first: solution = ', memo.get(i));
}
return result;
}
// Sample data
let rectangles = [
{ width: 10, height: 8 },
{ width: 6, height: 12 },
{ width:
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
manpreet
Best Answer
3 years ago
Given a set of
nrectangles as{(L1, W1), (L2, W2), …….., (Ln, Wn)},LiandWiindicate the length and width of rectanglei, respectively. We say that rectangleifits in rectanglejifLi< LjandWi< Wj.I need help designing a
O( nˆ2 )dynamic programming algorithm that will find the maximum length of a sequence of rectangles that fit into each other.I made an attempt, but it is not working in some cases, which are as follows:
Could you please help me improve my solution or find better one?