English 中文(简体)
阿雷拉多因果的最低摘要
原标题:Minimum Sum of Multiple Indeces in an Array
  • 时间:2023-12-11 03:13:02
  •  标签:
  • javascript

Just BOMBED a Hacker Rank question for a work application and I m so upset,因为我感觉到,我喜欢一些事情,但可以指出:

Write a function that when given an expected_load of requests to servers, and an array of servers in which all indeces are powers of 2 ([1, 1, 2, 4, 8, etc.]), return the minimum number of servers required to equal the expected_load of requests. If there is no combination which would equal the expected_load exactly, return a -1.

我 sure诚地相信,我把问题的实际话说得bot,但这样会:

function(10, [1, 2, 4, 8, 16]) {
  some stuff
}

收益2(在此情况下,需要服务器数量为3.1和服务器[3])

i m 假设你需要一个服务器数目的计算,但我无法说明如何将服务器作为你所加一切的总和加以追踪。 我开始:

getMinServers(expected_load, server_array){
  for (let i = 0; i <= server_array.length, i++){
    for (let j = i+ 1; j <= server_array.length; j++ {
      if (server[i] + server[j] === expected_load){
        **this is where my brain craps out**
      }
    }
  }
}

我需要找到一种办法,来计算其评价联盟与预期负荷相加的阵列中的欠款数目......这样,你需要追踪目前拖欠款项的数额,以及所使用的/必要和我才智不会奏效的缺减额。

问题回答

you would just start with a very high power of two, if that is not larger than expected_load just double it until youve fouind one that is higher, then just subtract from expected_load and increment by one, next check if it is higher, if so, half it and check again until it is not anymore, subtract again and add 1. do so until you end up at 0. the counter is the amount of servers.

根据你提供的情况,我或许会稍作不同地处理这一问题。 我确信,我的执行没有几个细微的事例。

const determineNumServers = (load: number, serverCapacities: number[]) => {
    // Sort into descending order
    const sortedCaps: number[] = serverCapacities.sort((a: number, b: number) => b - a);
    // Setup some variables to help with looping
    let idx = 0;
    let matchedLoad = 0, matchedServerCount = 0;
    while (matchedLoad != load) {
        // Chip away at the matched load, trying to use larger capacities first
        const cap = sortedCaps[idx];
        if (cap + matchedLoad <= load) {
            matchedLoad += cap;
            matchedServerCount++;
        }
        idx++;
    }
    // If we matched the expected load, return the number of nodes, otherwise -1
    return matchedLoad == load ? matchedServerCount : -1;
}

You can use JavaScript Array Methods like filter(), map(), reduce(), forEach() for implementing this rather than nested for loops. My recommendation is something like this:

  1. 首先,你们必须把警示 into为 order令。

    _array.sort((a, b) => b - a)
    
  2. 在职能中确定基数(如果回归0或1)

    if (load === 0) { return 0;}
    if (load < 0 || index === _array.length) {return -1;}
    
  3. Include the current server in the solution

    const include = findMinServers(load - _array[index], index)
    
  4. 将现有服务器排除在解决方案之外

    const exclude = findMinServers(load, index + 1)
    
  5. 恢复两种选择的最低限度

    if (include === -1 && exclude === -1) {
     return -1;
    } else if (include === -1) {
     return exclude;
    } else if (exclude === -1) {
     return include + 1;
    } else {
     return Math.min(include + 1, exclude);
    }
    

可执行守则:

function getMinServers(input, _array) {
    _array.sort((a, b) => b - a);

    function findMinServers(load, index) {
        if (load === 0) {
            return 0;
        }

        if (load < 0 || index === _array.length) {
            return -1;
        }
        const include = findMinServers(load - _array[index], index);
        const exclude = findMinServers(load, index + 1);
        if (include === -1 && exclude === -1) {
            return -1;
        } else if (include === -1) {
            return exclude;
        } else if (exclude === -1) {
            return include + 1;
        } else {
            return Math.min(include + 1, exclude);
        }
    }
    return findMinServers(input, 0);
}

console.log(getMinServers(10, [1, 1, 2, 4, 8, 16]))

供参考:

• 如何在W/javascript内按批次分类

How to filter an array in javascript?

Java一阵列(各代)

The Array methods `reduce' do?





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签