|
Apologies, I did not know there would be people on this forum scouting for assignment/homework answers. My bad. Hope my thread is not flagged or marked as spam.
Any rough idea/guidance on which I can build will do
|
|
|
|
|
We don't scout for them, but we do read many newly posted questions to see if we can help. And if we find something that looks like the poster has just dumped their assignment expecting to be given a working solution, we explain why it will not happen.
|
|
|
|
|
Can someone show me a framework pseudocode to indicate how a counting semaphore (not a binary semaphore) can be implemented using a Test&Set() hardware primitive?
|
|
|
|
|
Case 1:
Inputs:
[-1, 1, 2, -1, 2, -6, 1, 1, 0, 1, 1, -8]
Ouput:
5
sum of [1, 2, -1, 2] and [1, 1, 0, 1, 1] are 4 which is the max
return 5 as the LENGTH of the maximum contiguous sum
Case 2:
Input:
[-7, -4, -2]
Output:
1
-2 is the maximum contiguous sum
Case 3:
Input: []
Output: 0
|
|
|
|
|
Unclear, and probably homework. Try it yourself; you'll learn something.
|
|
|
|
|
Hi, this questions is similar to this question, but need to return the length. I tried, but still looking for solution
|
|
|
|
|
Member 11875370 wrote: I tried
Describe the algorithm you tried.
|
|
|
|
|
I tried the below code, but just get the max num, to solve the leetcode question. This is the interview question I just faced today.
public int getMaxLen(int[] nums) {
int sum=nums[0];
int max=Integer.MIN_VALUE;
int result = 0;
int cnt = 0;
for(int i=1;i<nums.length;i++){
sum=Math.max(sum+nums[i],nums[i]);
max= Math.max(max, sum);
}
return result;
}
|
|
|
|
|
Right, you'll need to track the "length" as you go, not just the max.
Personally, I would likely define a structure that holds the starting position, the length, and the sum.
I'd fill one with a sequence and then compare with my last known largest one and swap if larger.
modified 30-Jul-15 23:29pm.
|
|
|
|
|
Had you waited a few hours you could have posted this as a Friday Programming Challenge.
modified 31-Jul-15 2:11am.
|
|
|
|
|
I whipped up an implementation in C last night. About an hour's effort.
|
|
|
|
|
Sorry for my English.
_____________________
I can't find an algorithm for this special type of bin packing problem:
____________________
Consider a warehouse with a number of shelves that have number, width, height, depth and limit Weight capacity.
The objects that place in this warehouse are a number of cubes that have number, length, width, height and weight.
The algorithm must find the right place for objects that have the lowest waste of space. and objects weight not be more than shelf weight capacity.
** If a place was assigned to an object, back, front, and on it can not be allocated.
|
|
|
|
|
|
Thank you.
Yes It is 3D Bin Packing, too.
But here:
1) We have several shelves.
2) If a place for an object was assigned, we can't put any objects on it, in front of it or in back.(just left side or right side is true)
|
|
|
|
|
Then you need to go and read some of the information in those links that Google provided.
|
|
|
|
|
funny!
what is wrong with you?
simply you can don't answer to me
|
|
|
|
|
|
That's a nice way of saying "thank you".
If there was an answer readily available, we would not have to write so much code. If you can do the procedure for one shelf, you can do it for multiple shelves.
So, where is the problem?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Try to write it as an integer linear programming model, it can be solved directly (maybe not efficiently, it depends) and it helps other people understand exactly what your problem looks like so we're sure we're all talking about the same problem.
If you have that, I'll gladly take a look at it.
|
|
|
|
|
|
This is not a 3D packing problem !
If you want to optimize the warehouse usage, you need to choose if you want to minimize wasted space or wasted weight.
an algo:
sort the cubes from biggest to smallest.
for each cube
Select available places
place the cube at the place which waste minimum
next
if your problem allow to put more than 1 cube in a place which is not stated), then it is 3D packing
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Thank you very much
This is my answer
|
|
|
|
|
Finally I did it:
public static string placeItemInBin(Item itemToPlacing)
{
LinkedListNode<Bin> binNode;
bool isPlaced = false;
int allSideStates = 1;
while (allSideStates <= 2)
{
binNode = listOfBins.First;
while (isPlaced == false && binNode != null)
{
if (binNode.Value.RemainWidth >= itemToPlacing.Width)
if (binNode.Value.RemainWeight >= itemToPlacing.Weight
&& binNode.Value.Height >= itemToPlacing.Height
&& binNode.Value.Depth >= itemToPlacing.Depth)
{
LinkedListNode<Partirion> partitionNode;
LinkedListNode<Partirion> Best;
Best=null;
partitionNode = binNode.Value.parts.First;
while (partitionNode != null)
{
if (partitionNode.Value.num == -1
&& partitionNode.Value.size >= itemToPlacing.Width
&& (Best == null || partitionNode.Value.size <= Best.Value.size))
{
Best = partitionNode;
if (Best.Value.size == itemToPlacing.Width)
break;
}
partitionNode = partitionNode.Next;
}
if (Best != null)
{
Partirion newPart = new Partirion(itemToPlacing.Width, itemToPlacing.Number,
Best.Value.cmBegin);
Best.Value.cmBegin += itemToPlacing.Width;
Best.Value.size -= itemToPlacing.Width;
binNode.Value.parts.AddBefore(Best, newPart);
itemToPlacing.BinNumber = binNode.Value.Number;
binNode.Value.RemainWidth -= itemToPlacing.Width;
binNode.Value.RemainWeight -= itemToPlacing.Weight;
binNode.Value.RemainVolume -= itemToPlacing.Width
* binNode.Value.Height * binNode.Value.Depth;
isPlaced = true;
return ("item: (" + itemToPlacing + ") ====> Bin: (" + binNode.Value + ")");
}
}
binNode = binNode.Next;
}
itemToPlacing.changeSideState();
allSideStates++;
}
return "Not Found";
}
modified 23-Jul-15 1:00am.
|
|
|
|
|
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Now you investigate to see what gives the best solution
- no sort of cubes
- sort from biggest to smallest
- sort from smallest to biggest
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|