注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
virtu financial OA題目與答案,求加米,新人入站辛苦海外求職中!
1.there is a box with a capacity of 5000 grams. the box may already contain some items, reducing its capacity. You'll be adding apples to that box until it is full.
Write a function that, given a zero-indexed array A consisting of N integers, representing the weight of items already in the box and each apple's weight, returns the maximum number of apples that could fit in the box without exceding its capacity.
the input array consists of an integer K as the first element, representing the sum of the weights of items already contained in the box, followed by zero or more integers represeting individual apple weights.
You may assume that A contains between 1 and 100 elements and that every number in it is between 0 and 5000.
example
input: [4650,150,150,150]
output:2
function maxApplesInBox(A) {
const boxCapacity = 5000; // Maximum capacity of the box in grams
const initialLoad = A[0]; // Weight of items already in the box
// Calculate remaining capacity in the box
let remainingCapacity = boxCapacity - initialLoad;
// Counter for the number of apples that can fit
let appleCount = 0;
// Iterate over the apple weights in the array (starting from index 1)
for (let i = 1; i < A.length; i++) {
// Check if the current apple can fit in the remaining capacity
if (remainingCapacity >= A[i]) {
remainingCapacity -= A[i]; // Reduce the remaining capacity
appleCount++; // Increment the apple count
} else {
break; // No more apples can fit, break the loop
}
}
return appleCount;
}
2.write a function that given a string S retudents is in the range from 1 to 1000
scores are in the range from 0 to 1000
example 1
input: [1,6,3,4,3,5]
output:[1,4,4,4,4,5]
function updateScores(scores) {
// Flag to check if any score has changed in the current iteration
let changed;
do {
changed = false;
// Create a new array to store the updated scores for this iteration
let newScores = scores.slice();
for (let i = 1; i < scores.length - 1; i++) {
if (scores[i - 1] > scores[i] && scores[i + 1] > scores[i]) {
newScores[i] = scores[i] + 1;
changed = true;
} else if (scores[i - 1] < scores[i] && scores[i + 1] < scores[i]) {
newScores[i] = scores[i] - 1;
changed = true;
}
}
// Update the original scores array to the new scores after the iteration
scores = newScores;
} while (changed); // Repeat as long as there was a change in the last iteration
return scores;
} |