I am doing the euler "maximum path sum" exercice.
By starting at the top of the triangle below and moving to adjacent numbers on the row below,
the maximum total from top to bottom is 23.
3
7 4
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle.
I created a simple method which calculates for each position in the row, the maximum possible value.
If we take the previous example, it becomes:
3
10 7
12 14 13
22 19 23 16
The maximum value in the last row is 23.
Here is is the code:
function maximumPathSumI(triangle) {
let columnSum = [];
for (let row=0; row<triangle.length; row++) {
let newColumnSum = [];
for (let column=0; column<triangle[row].length; column++) {
let maxPreviousValue = getMaxPreviousValueForPosition(column, columnSum);
newColumnSum[column] = maxPreviousValue + triangle[row][column];
}
columnSum = newColumnSum;
}
return Math.max(...columnSum);
}
function getMaxPreviousValueForPosition(position, previousValues) {
let maxValue = previousValues[position] || 0;
if (position > 0 && maxValue < previousValues[position-1]) maxValue = previousValues[position-1];
if (position < previousValues.length && maxValue < previousValues[position+1]) maxValue = previousValues[position+1];
return maxValue;
}
It works with the first test, but doesn't seem to give the right result with this triangel
const testTriangle = [
[75,],
[95, 64,],
[17, 47, 82,],
[18, 35, 87, 10,],
[20, 04, 82, 47, 65],
[19, 01, 23, 75, 03, 34,],
[88, 02, 77, 73, 07, 63, 67,],
[99, 65, 04, 28, 06, 16, 70, 92,],
[41, 41, 26, 56, 83, 40, 80, 70, 33,],
[41, 48, 72, 33, 47, 32, 37, 16, 94, 29,],
[53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14,],
[70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57,],
[91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48,],
[63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31,],
[04, 62, 98, 27, 23, 09, 70, 98, 73, 93, 38, 53, 60, 04, 23,],
];
It returns 1116 instead of 1074.
When I go through the results row by row, everything seems fine to me.
Here is the intermediary calculations:
[ 75 ]
//[95, 64,],
[ 170, 139 ]
//[17, 47, 82,],
[ 187, 217, 221 ]
//[18, 35, 87, 10,],
[ 235, 256, 308, 231 ]
//[20, 04, 82, 47, 65],
[ 276, 312, 390, 355, 296 ]
//[19, 01, 23, 75, 03, 34,],
[ 331, 391, 413, 465, 358, 330 ]
//[88, 02, 77, 73, 07, 63, 67,],
[ 479, 415, 542, 538, 472, 421, 397 ]
//[99, 65, 04, 28, 06, 16, 70, 92,],
[ 578, 607, 546, 570, 544, 488, 491, 489 ]
//[41, 41, 26, 56, 83, 40, 80, 70, 33,],
[ 648, 648, 633, 626, 653, 584, 571, 561, 522 ]
//[41, 48, 72, 33, 47, 32, 37, 16, 94, 29,],
[ 689, 696, 720, 686, 700, 685, 621, 587, 655, 551 ]
//[53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14,],
[ 749, 791, 764, 785, 725, 743, 776, 707, 752, 706, 565 ]
//[70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57,],
[ 861, 802, 824, 813, 862, 849, 793, 854, 791, 820, 723, 622 ]
//[91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48,],
[ 952, 932, 876, 900, 879, 876, 945, 897, 912, 870, 847, 752, 670 ]
//[63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31,],
[ 1015, 1018, 936, 968, 989, 998, 1012, 975, 985, 928, 939, 934, 792, 701 ]
//[04, 62, 98, 27, 23, 09, 70, 98, 73, 93, 38, 53, 60, 04, 23,],
[ 1022, 1080, 1116, 1016, 1021, 1021, 1082, 1110, 1058, 1078, 977, 992, 994, 796, 724 ]
You've misunderstood the challenge. The error in your code is in this line:
maxValue = previousValues[position+1]
Your code allows to look at three different sums from the previous row, but it should only look at two (at the most): the one at position-1 (if valid) and the one at position, but not the one at position+1.
Base on your intermediate calculation, It seem to be correct to find maximum of the last row 1116.
https://codepen.io/tmixab/pen/oNWwYLd
Related
Hard to put into words but I am looking for a function that takes an array and reduces it down to a given size. The use case is specifying ticks for graph in d3.
const availableDatetimes: Date[] = [...]
const numberOfXTicks = chartWidth / 80
const tickValues = reduceArrSize(availableDatetimes, numberOfXTicks)
the result should have evenly distributed dates
I called it shrinkArray(), because reducing an array is the name of a different well-defined operation. Here it is:
const shrinkArray = (array, size) => {
const step = array.length / size
return array.filter((v, i) => Math.floor(i % step) == 0)
}
Let's shrink an array of size 100 to size 33:
const originalArray = (new Array(100).fill(0)).map((v, i) => i)
const shrunkenArray = shrinkArray(originalArray, 33)
console.log(shrunkenArray);
console.log(shrunkenArray.length == 33);
So:
[
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99
]
is shrunken to:
[
0, 4, 7, 10, 13, 16, 19, 22, 25,
28, 31, 34, 37, 40, 43, 46, 49, 53,
56, 59, 62, 65, 68, 71, 74, 77, 80,
83, 86, 89, 92, 95, 98
]
that is of size 33.
In this example the array is integers from 0 to 99, but obviously the function works for any data type.
I want to dynamically specify negative values as seen in this exact question.
Rules for unquoted JavaScript Object Literal Keys?
I want to also accomplish this using some method of dynamic bracket notation, using a string equivalent of a negative remainder (modulus operation).
bucketObj[key] reports undefined, since I have not pre set a key to store a corresponding value. How do I dynamically set a key of an object in either bracket notation, dot notation, or both?
function toDigitBuckets(array, radix) {
let bucket = JSON.parse(JSON.stringify([...Array(radix).fill([])]));
let bucketObj = Object.assign(bucket);
array.forEach(val => { let key = String.toString(val % 10); bucketObj[key].push(val) });
return bucket;
}
let array = [-7, -49, 84, 39, -31, 95, 7, -8, -13, -32, 93, 40, -81, -30, -57, -57, 49, 66, -64, 42, 35, 29, -57, 41, 93, 34, -45, -15, 51, 16, 97, -88, 52, -69, 56, -16, -91, 51, 10, -21, 80, 78, -5, 18, -20, -98, 72, -94, 11, -83, -31, 13, -21, 39, -47, 8, -98, 95, 52, -18, 77, -11, -38, -46, -98, 48, -45, -4, 76, -32, -81, 67, -82, 9, -60, -20, 0, 33, -12, 77, 65, 45, -22, 99, -47, -83, -81, 10, -99, 16, 23, 5, -57, 89, -62, 9, -16, 79, 5, -2];
let radix = 10;
console.log(JSON.stringify(toDigitBuckets(array, radix)));
You can use Array#reduce with an object as the accumulator. Each time a new key is encountered, first set that property value to be an empty array.
function toDigitBuckets(array, radix) {
return array.reduce((acc, curr) =>
((acc[curr % radix] ??= []).push(curr), acc), {});
}
let array = [-7, -49, 84, 39, -31, 95, 7, -8, -13, -32, 93, 40, -81, -30, -57, -57, 49, 66, -64, 42, 35, 29, -57, 41, 93, 34, -45, -15, 51, 16, 97, -88, 52, -69, 56, -16, -91, 51, 10, -21, 80, 78, -5, 18, -20, -98, 72, -94, 11, -83, -31, 13, -21, 39, -47, 8, -98, 95, 52, -18, 77, -11, -38, -46, -98, 48, -45, -4, 76, -32, -81, 67, -82, 9, -60, -20, 0, 33, -12, 77, 65, 45, -22, 99, -47, -83, -81, 10, -99, 16, 23, 5, -57, 89, -62, 9, -16, 79, 5, -2];
let radix = 10;
console.log(toDigitBuckets(array, radix));
.as-console-wrapper{max-height:100%!important;top:0}
const datax = [
{
hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
hrInTotal: 1106,
hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
hrTotal: 10020,
mo: 5,
time: "Thu Jun 25 18:30:00 UTC 2020",
},
{
hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
hrInTotal: 1106,
hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
hrTotal: 10120,
mo: 5,
time: "Thu Jun 26 18:30:00 UTC 2020",
}, {
hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
hrInTotal: 1106,
hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
hrTotal: 1020,
mo: 5,
time: "Thu Jun 27 18:30:00 UTC 2020",
},
{
hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
hrInTotal: 1106,
hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
hrTotal: 10110,
mo: 5,
time: "Thu Jun 28 18:30:00 UTC 2020",
}
]
const newArray = datax.foreach((element, index) => {
const labels = []
const counts = []
const idx = index
labels[idx] = index
counts[idx] = element.hrTotal
return {labels, counts}
});
Trying to achieve below object. I want iterate above array of object and get new array of object with given result below i have tryed using foreach and i am getting error as forach is not a function .
newArray = [{
hrTatal:[1020,10110,10120,10020],
labels:[0,1,2,3]
}]
Try to use map instead of forEach, like:
const newArray = datax.map((element, index) => {
const labels = []
const counts = []
const idx = index
labels[idx] = index
counts[idx] = element.hrTotal
return {labels, counts}
});
The reason is because forEach does not return anything (undefined). map returns a new modified array.
Use reduce, define the output array inside id and append the needed properties.
const datax = [{
hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
hrInTotal: 1106,
hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
hrTotal: 10020,
mo: 5,
time: "Thu Jun 25 18:30:00 UTC 2020",
},
{
hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
hrInTotal: 1106,
hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
hrTotal: 10120,
mo: 5,
time: "Thu Jun 26 18:30:00 UTC 2020",
}, {
hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
hrInTotal: 1106,
hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
hrTotal: 1020,
mo: 5,
time: "Thu Jun 27 18:30:00 UTC 2020",
},
{
hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
hrInTotal: 1106,
hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
hrTotal: 10110,
mo: 5,
time: "Thu Jun 28 18:30:00 UTC 2020",
}
]
const res = datax.reduce((acc, x, i) => {
acc[0] = {
hrTotal: [...acc[0].hrTotal, x.hrTotal],
labels: [...acc[0].labels, i]
}
return acc;
}, [{
hrTotal: [],
labels: []
}])
console.log(res)
The data (in Uint8Array format) from the minio client sometimes comes only once, but other times it comes 3-4 times.
s3Client.getObject(
bucketname,
`folder/file.jpg`,
function(err, dataStream) {
if (err) {
onError();
}
dataStream.on("data", function(data) {
console.log(data, "data");
const urlData = new Blob([data]);
}
}
)
The console is giving me the response:
Uint8Array(65536) [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 255, 219, 0, 67, 0, 8, 6, 6, 7, 6, 5, 8, 7, 7, 7, 9, 9, 8, 10, 12, 20, 13, 12, 11, 11, 12, 25, 18, 19, 15, 20, 29, 26, 31, 30, 29, 26, 28, 28, 32, 36, 46, 39, 32, 34, 44, 35, 28, 28, 40, 55, 41, 44, 48, 49, 52, 52, 52, 31, 39, 57, 61, 56, 50, 60, 46, 51, 52, 50, 255, 219, 0, 67, 1, 9, 9, 9, 12, 11, 12, …] "data"
Uint8Array(36498) [90, 44, 146, 187, 26, 132, 98, 175, 32, 178, 248, 209, 107, 36, 152, 189, 209, 166, 133, 63, 189, 12, 194, 67, 249, 16, 191, 206, 189, 7, 71, 214, 180, 253, 122, 201, 110, 244, 235, 149, 154, 18, 112, 113, 193, 83, 232, 65, 228, 26, 230, 181, 31, 133, 222, 27, 188, 129, 197, 181, 180, 150, 115, 30, 146, 67, 35, 28, 31, 247, 88, 145, 143, 202, 160, 248, 125, 225, 13, 71, 194, 218, 134, 171, 246, 199, 70, 130, 80, 139, 11, 198, 220, 73, 130, 220, 145, 212, 17, 158, …] "data"
And after refreshing the page for the same image, the console is:
Uint8Array(102034) [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 255, 219, 0, 67, 0, 8, 6, 6, 7, 6, 5, 8, 7, 7, 7, 9, 9, 8, 10, 12, 20, 13, 12, 11, 11, 12, 25, 18, 19, 15, 20, 29, 26, 31, 30, 29, 26, 28, 28, 32, 36, 46, 39, 32, 34, 44, 35, 28, 28, 40, 55, 41, 44, 48, 49, 52, 52, 52, 31, 39, 57, 61, 56, 50, 60, 46, 51, 52, 50, 255, 219, 0, 67, 1, 9, 9, 9, 12, 11, 12, …] "data"
I am already calling it in useEffect. It is hitting the minio client single time but function (err, dataStream) is not giving consistent data.
Because it appends different chunks of the file each time.
You can get the entire data from dataSteram.on("end", ...)
let size = 0
minioClient.getObject('mybucket', 'photo.jpg', function(err, dataStream) {
if (err) {
return console.log(err)
}
dataStream.on('data', function(chunk) {
size += chunk.length
})
dataStream.on('end', function() {
console.log('End. Total size = ' + size)
console.log(dataStream)
console.log(dataStream.socket._tlsOptions.session)
})
dataStream.on('error', function(err) {
console.log(err)
})
})
I have a json array like this
{Id:[1,2,4,5,8,9,14,22,10,11,12,13,20,21,28,30,31,15,23,32,33,34,35,36,37,38,41,16,24,42,43,48,49,17,25,58,59,61,62,63,64,66,67,68,18,26,69,70,74,75,19,27,82,83,85,86,87,88,89,90,91,108,109,110,111,120]}
and want to select corresponding id when an option is selected from select tag which is also dynamic i.e also a json array which is displayed in select tag option as
<select id="list">
<option>--Select--</option>`enter code here`
<option>List gets updated by the json array</option>
</select>
and I have to do it using jQuery/javascript..any help will be appreciated.
Use Array#map method to iterate and generate option element using jQuery. Attach change() event handler to listen the event.
var data = {
Id: [1, 2, 4, 5, 8, 9, 14, 22, 10, 11, 12, 13, 20, 21, 28, 30, 31, 15, 23, 32, 33, 34, 35, 36, 37, 38, 41, 16, 24, 42, 43, 48, 49, 17, 25, 58, 59, 61, 62, 63, 64, 66, 67, 68, 18, 26, 69, 70, 74, 75, 19, 27, 82, 83, 85, 86, 87, 88, 89, 90, 91, 108, 109, 110, 111, 120]
};
$('#list').append(
data.Id.map(function(v) {
return $('<option/>', {
value: v,
text: v
})
})
).change(function() {
console.log(this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="list">
<option>--Select--</option>
</select>
UPDATE : Since you need to get id based on the option selected update your code as follows. Where you can use Array#indexOf method to get the index of the selected element.
var data = {
"StoreName": ["10001 Main ST", "10002 Part1", "10004 MyStore1", "10005 M STR", "10008 Centro", "10009 MyStore 02", "1001 G", "1001 H", "10010 Store main ROAD", "10011 Central M Store", "10012 En Department", "10013 M Station", "10014 Test Center", "10015 SubStore1", "10016 AA", "10018 M part #", "10019 Test A - 26032016", "1002 B", "1002 I", "10020 Test Central B "],
Id: [1, 2, 4, 5, 8, 9, 14, 22, 10, 11, 12, 13, 20, 21, 28, 30, 31, 15, 23, 32, 3, 34, 35, 36, 37, 38, 41, 16, 24, 42, 43, 48, 49, 1, 25, 58, 59, 61, 62, 63, 4, 66, 67, 68, 18, 26, 69, 70, 74, 75, 19, 27, 82, 8, 85, 86, 87, 88, 89, 90, 1, 108, 109, 110, 111, 10]
};
$('#storenm').append(data.StoreName.map(function(v) {
// generate option with value and text content
return $('<option>', {
text: v,
value: v
})
})).change(function() {
console.log(data.Id[data.StoreName.indexOf(this.value)])
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<select id="storenm" name="name">
<option>--Select--</option>
</select>
</body>