Highest possible pyramid
I tried everything, but I still can't solve this problem without brute force:
I get N blocks with a known height and width. I can rotate them (height become width and width become height) and I have to build the tallest possible pyramid from them (of course I can change the order of blocks).
The problem is that you can't put a block of width X onto a block with width smaller than X.
EDIT:
The problem is, that you can't put a block onto a block of the same width.
Any ideas?
algorithm logic 2d
|
show 7 more comments
I tried everything, but I still can't solve this problem without brute force:
I get N blocks with a known height and width. I can rotate them (height become width and width become height) and I have to build the tallest possible pyramid from them (of course I can change the order of blocks).
The problem is that you can't put a block of width X onto a block with width smaller than X.
EDIT:
The problem is, that you can't put a block onto a block of the same width.
Any ideas?
algorithm logic 2d
1
If the blocks can directly be stacked on each other (without any constraints), then iterate over them and sum(max(block.height, block.width)) and it will be the maximum possible height. Which is a linear algorithm, you can't do any better since you need to inspect every block... or your question is something else entirely?
– Aleksei Maide
Nov 22 at 10:22
@AlekseiMaide The problem is that you can't put a block of width X onto a block with a width smaller than X.
– georgeel
Nov 22 at 10:29
5
Your question seems unclear. Would you please show brute force code you did and provide some testcases for expected input/output.
– Bishal Gautam
Nov 22 at 10:30
Could you provide the definition of pyramid, how could we know that the pyramid is valid? Any constraints for N?
– Pham Trung
Nov 22 at 10:42
2
This question, as asked, is too easy. I think you've probably made a mistake in transcribing it.
– Matt Timmermans
Nov 22 at 13:16
|
show 7 more comments
I tried everything, but I still can't solve this problem without brute force:
I get N blocks with a known height and width. I can rotate them (height become width and width become height) and I have to build the tallest possible pyramid from them (of course I can change the order of blocks).
The problem is that you can't put a block of width X onto a block with width smaller than X.
EDIT:
The problem is, that you can't put a block onto a block of the same width.
Any ideas?
algorithm logic 2d
I tried everything, but I still can't solve this problem without brute force:
I get N blocks with a known height and width. I can rotate them (height become width and width become height) and I have to build the tallest possible pyramid from them (of course I can change the order of blocks).
The problem is that you can't put a block of width X onto a block with width smaller than X.
EDIT:
The problem is, that you can't put a block onto a block of the same width.
Any ideas?
algorithm logic 2d
algorithm logic 2d
edited Nov 24 at 16:37
asked Nov 22 at 10:18
georgeel
24
24
1
If the blocks can directly be stacked on each other (without any constraints), then iterate over them and sum(max(block.height, block.width)) and it will be the maximum possible height. Which is a linear algorithm, you can't do any better since you need to inspect every block... or your question is something else entirely?
– Aleksei Maide
Nov 22 at 10:22
@AlekseiMaide The problem is that you can't put a block of width X onto a block with a width smaller than X.
– georgeel
Nov 22 at 10:29
5
Your question seems unclear. Would you please show brute force code you did and provide some testcases for expected input/output.
– Bishal Gautam
Nov 22 at 10:30
Could you provide the definition of pyramid, how could we know that the pyramid is valid? Any constraints for N?
– Pham Trung
Nov 22 at 10:42
2
This question, as asked, is too easy. I think you've probably made a mistake in transcribing it.
– Matt Timmermans
Nov 22 at 13:16
|
show 7 more comments
1
If the blocks can directly be stacked on each other (without any constraints), then iterate over them and sum(max(block.height, block.width)) and it will be the maximum possible height. Which is a linear algorithm, you can't do any better since you need to inspect every block... or your question is something else entirely?
– Aleksei Maide
Nov 22 at 10:22
@AlekseiMaide The problem is that you can't put a block of width X onto a block with a width smaller than X.
– georgeel
Nov 22 at 10:29
5
Your question seems unclear. Would you please show brute force code you did and provide some testcases for expected input/output.
– Bishal Gautam
Nov 22 at 10:30
Could you provide the definition of pyramid, how could we know that the pyramid is valid? Any constraints for N?
– Pham Trung
Nov 22 at 10:42
2
This question, as asked, is too easy. I think you've probably made a mistake in transcribing it.
– Matt Timmermans
Nov 22 at 13:16
1
1
If the blocks can directly be stacked on each other (without any constraints), then iterate over them and sum(max(block.height, block.width)) and it will be the maximum possible height. Which is a linear algorithm, you can't do any better since you need to inspect every block... or your question is something else entirely?
– Aleksei Maide
Nov 22 at 10:22
If the blocks can directly be stacked on each other (without any constraints), then iterate over them and sum(max(block.height, block.width)) and it will be the maximum possible height. Which is a linear algorithm, you can't do any better since you need to inspect every block... or your question is something else entirely?
– Aleksei Maide
Nov 22 at 10:22
@AlekseiMaide The problem is that you can't put a block of width X onto a block with a width smaller than X.
– georgeel
Nov 22 at 10:29
@AlekseiMaide The problem is that you can't put a block of width X onto a block with a width smaller than X.
– georgeel
Nov 22 at 10:29
5
5
Your question seems unclear. Would you please show brute force code you did and provide some testcases for expected input/output.
– Bishal Gautam
Nov 22 at 10:30
Your question seems unclear. Would you please show brute force code you did and provide some testcases for expected input/output.
– Bishal Gautam
Nov 22 at 10:30
Could you provide the definition of pyramid, how could we know that the pyramid is valid? Any constraints for N?
– Pham Trung
Nov 22 at 10:42
Could you provide the definition of pyramid, how could we know that the pyramid is valid? Any constraints for N?
– Pham Trung
Nov 22 at 10:42
2
2
This question, as asked, is too easy. I think you've probably made a mistake in transcribing it.
– Matt Timmermans
Nov 22 at 13:16
This question, as asked, is too easy. I think you've probably made a mistake in transcribing it.
– Matt Timmermans
Nov 22 at 13:16
|
show 7 more comments
1 Answer
1
active
oldest
votes
What I understand reading your problem statement and comments is that you want to build tallest pyramid with width
from bottom to top in decreasing order.
If this is the case, then what we can do is simply the following steps:
- Loop over blocks and swap
width
andheight
only ifwidth > height
. Now, sort the array of blocks in decreasing order of width which is the order used for stacking blocks from bottom to top in pyramid.
Answer is summation of all heights.
Note: step -2 is only needed if you want to display order of blocks
from bottom to top in pyramid.
So the first stage is to rotate blocks making them "higher than wider" ??
– MBo
Nov 22 at 11:19
@MBo, yes. First step ensure taller pyramid.
– Bishal Gautam
Nov 22 at 11:26
@BishalGautam The problem is, that you can't put a block onto a block of the same width.
– georgeel
Nov 24 at 16:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428682%2fhighest-possible-pyramid%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What I understand reading your problem statement and comments is that you want to build tallest pyramid with width
from bottom to top in decreasing order.
If this is the case, then what we can do is simply the following steps:
- Loop over blocks and swap
width
andheight
only ifwidth > height
. Now, sort the array of blocks in decreasing order of width which is the order used for stacking blocks from bottom to top in pyramid.
Answer is summation of all heights.
Note: step -2 is only needed if you want to display order of blocks
from bottom to top in pyramid.
So the first stage is to rotate blocks making them "higher than wider" ??
– MBo
Nov 22 at 11:19
@MBo, yes. First step ensure taller pyramid.
– Bishal Gautam
Nov 22 at 11:26
@BishalGautam The problem is, that you can't put a block onto a block of the same width.
– georgeel
Nov 24 at 16:35
add a comment |
What I understand reading your problem statement and comments is that you want to build tallest pyramid with width
from bottom to top in decreasing order.
If this is the case, then what we can do is simply the following steps:
- Loop over blocks and swap
width
andheight
only ifwidth > height
. Now, sort the array of blocks in decreasing order of width which is the order used for stacking blocks from bottom to top in pyramid.
Answer is summation of all heights.
Note: step -2 is only needed if you want to display order of blocks
from bottom to top in pyramid.
So the first stage is to rotate blocks making them "higher than wider" ??
– MBo
Nov 22 at 11:19
@MBo, yes. First step ensure taller pyramid.
– Bishal Gautam
Nov 22 at 11:26
@BishalGautam The problem is, that you can't put a block onto a block of the same width.
– georgeel
Nov 24 at 16:35
add a comment |
What I understand reading your problem statement and comments is that you want to build tallest pyramid with width
from bottom to top in decreasing order.
If this is the case, then what we can do is simply the following steps:
- Loop over blocks and swap
width
andheight
only ifwidth > height
. Now, sort the array of blocks in decreasing order of width which is the order used for stacking blocks from bottom to top in pyramid.
Answer is summation of all heights.
Note: step -2 is only needed if you want to display order of blocks
from bottom to top in pyramid.
What I understand reading your problem statement and comments is that you want to build tallest pyramid with width
from bottom to top in decreasing order.
If this is the case, then what we can do is simply the following steps:
- Loop over blocks and swap
width
andheight
only ifwidth > height
. Now, sort the array of blocks in decreasing order of width which is the order used for stacking blocks from bottom to top in pyramid.
Answer is summation of all heights.
Note: step -2 is only needed if you want to display order of blocks
from bottom to top in pyramid.
answered Nov 22 at 10:55
Bishal Gautam
774517
774517
So the first stage is to rotate blocks making them "higher than wider" ??
– MBo
Nov 22 at 11:19
@MBo, yes. First step ensure taller pyramid.
– Bishal Gautam
Nov 22 at 11:26
@BishalGautam The problem is, that you can't put a block onto a block of the same width.
– georgeel
Nov 24 at 16:35
add a comment |
So the first stage is to rotate blocks making them "higher than wider" ??
– MBo
Nov 22 at 11:19
@MBo, yes. First step ensure taller pyramid.
– Bishal Gautam
Nov 22 at 11:26
@BishalGautam The problem is, that you can't put a block onto a block of the same width.
– georgeel
Nov 24 at 16:35
So the first stage is to rotate blocks making them "higher than wider" ??
– MBo
Nov 22 at 11:19
So the first stage is to rotate blocks making them "higher than wider" ??
– MBo
Nov 22 at 11:19
@MBo, yes. First step ensure taller pyramid.
– Bishal Gautam
Nov 22 at 11:26
@MBo, yes. First step ensure taller pyramid.
– Bishal Gautam
Nov 22 at 11:26
@BishalGautam The problem is, that you can't put a block onto a block of the same width.
– georgeel
Nov 24 at 16:35
@BishalGautam The problem is, that you can't put a block onto a block of the same width.
– georgeel
Nov 24 at 16:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428682%2fhighest-possible-pyramid%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
If the blocks can directly be stacked on each other (without any constraints), then iterate over them and sum(max(block.height, block.width)) and it will be the maximum possible height. Which is a linear algorithm, you can't do any better since you need to inspect every block... or your question is something else entirely?
– Aleksei Maide
Nov 22 at 10:22
@AlekseiMaide The problem is that you can't put a block of width X onto a block with a width smaller than X.
– georgeel
Nov 22 at 10:29
5
Your question seems unclear. Would you please show brute force code you did and provide some testcases for expected input/output.
– Bishal Gautam
Nov 22 at 10:30
Could you provide the definition of pyramid, how could we know that the pyramid is valid? Any constraints for N?
– Pham Trung
Nov 22 at 10:42
2
This question, as asked, is too easy. I think you've probably made a mistake in transcribing it.
– Matt Timmermans
Nov 22 at 13:16