最小二乘法解决什么问题_如何解决最小操作问题

最小二乘法解决什么问题

Hello, everyone!

大家好!

It’s been quite some time since I last posted, but I am back with another JavaScript algorithm tutorial. This time we’ll be solving the Minimum Operations problem, which tasks you with finding the minimum number of operations needed to make each element of an array equal. There are a few more specific guidelines to the problem, which are outlined below.

自上次发布以来已经有一段时间了,但是我又回来了另一篇JavaScript算法教程。 这次,我们将解决“ 最小操作数”问题,该任务将为您找到使数组的每个元素相等所需的最小操作数。 有一些针对此问题的更具体的准则,概述如下。

You have an array arr of length n where arr[i] = (2 * i) + 1 for all valid values of i (i.e. 0 <= i < n).In one operation, you can select two indices x and y where 0 <= x, y < n and subtract 1 from arr[x] and add 1 to arr[y] (i.e. perform arr[x] -= 1 arr[y] += 1). The goal is to make all the elements of the array equal. It is guaranteed that all the elements of the array can be made equal using some operations.Given an integer n, the length of the array. Return the minimum number of operations needed to make all the elements of arr equal.

After reading the problem description, I immediately noticed that although we are meant to iterate over an array, there is no array being passed into the function as an argument. This means that our first course of action is to create and populate an array where each element follows the formula outlined in the instructions, arr[i] = (2 * i) + 1. Let’s declare a variable called arr and set it equal to an empty array.

阅读问题描述后,我立即注意到,尽管我们打算遍历数组,但没有数组作为参数传递给函数。 这意味着我们的第一个操作步骤是创建并填充一个数组,其中每个元素都遵循指令中概述的公式arr[i] = (2 * i) + 1 。 让我们声明一个名为arr的变量,并将其设置为一个空数组。

Next we’ll create a for loop that loops from 0 to n. Within each iteration of the for loop we’ll run the formula to populate the array with the intended values.

接下来,我们将创建一个从0n循环的for loop 。 在for loop每次迭代中for loop我们将运行公式以使用预期值填充数组。

Image for post
Just to visualize: If the value of n is 3 then arr would equal [1, 3, 5] after the loop was completed.
只是想像一下:如果n的值为3,则循环完成后arr等于[1、3、5]。

Now that our array is populated, we can begin solving the problem.

现在已经填充了数组,我们可以开始解决问题了。

Our next step will be to create a couple of variables. Our first variable, which we’ll call counter will keep track of the number of operations our function has made, and two additional variables, front and end, will act as pointers that will be initialized to represent the first and last indices of our array. Note: These pointer variables represent the x and y variables in the problem description.

我们的下一步将是创建几个变量。 我们的第一个变量(称为counter将跟踪函数执行的操作数,另外两个变量frontend将用作指针,将初始化这些指针以表示数组的第一个索引和最后一个索引。 注意: 这些指针变量代表 问题描述中 x y 变量。

Image for post

Next we’re going to create a second for loop to iterate over arr and compare the elements at the pointer indices, checking if they are equal. If they are not equal, we must operate on the values, incrementing the element at front’s index and decrementing the element at end’s index. Additionally, we’ll increment counter here as well because we just operated on the array. Since we want to increment and decrement until the two elements are equal, we will use a while loop as our conditional.

接下来,我们将创建第二个for loop以遍历arr并比较指针索引处的元素,检查它们是否相等。 如果它们不相等,则必须对这些值进行运算,使front的索引处的元素递增,而end的索引处的元素递减。 另外,我们也将在此处递增counter ,因为我们只是在数组上进行操作。 由于我们要递增和递减直到两个元素相等,因此我们将使用while loop作为条件。

Image for post
Note: Our for loop will run while i < n / 2 as we are comparing both ends of the array each iteration. This eliminates the need to loop through arr’s full length.
注意:我们的for循环将在i <n / 2时运行,因为我们在每次迭代时都比较数组的两端。 这样就无需遍历arr的全长。

Once the two elements are equal, our function will exit the while loop and we’ll need to check the next two elements. To do this, we will increment front and decrement end by one.

一旦两个元素相等,我们的函数将退出while loop ,我们需要检查接下来的两个元素。 要做到这一点,我们将增加front和递减end的一个。

Image for post

The next iteration of our for loop will begin and if the two elements are not equal, then the while loop and corresponding code will run again. This process will continue until the for loop has completed the last iteration and all elements have been made equal. All that’s left to do after this is return counter outside of the for loop to output the total number of operations the function had to make in order to achieve total equality.

for loop的下一个迭代将开始,如果两个元素不相等,则while loop和相应的代码将再次运行。 该过程将继续进行,直到for loop完成最后一次迭代且所有元素均相等为止。 此后剩下要做的就是在for loop外的return counter ,以输出该函数为实现完全相等而必须执行的操作总数。

Image for post

结论 (Conclusion)

When I first encountered the Minimum Operations problem, I found the description to be a bit difficult to understand. While reading it a second time, I began to identify and break the problem down into parts (creating and populating an array, comparing elements, adjusting values, etc.) and noticed similarities between this problem and others I’ve solved before.

当我第一次遇到最低操作问题时,我发现描述有些难以理解。 在第二遍阅读时,我开始将问题识别并分解为多个部分(创建和填充数组,比较元素,调整值等),并注意到该问题与我之前解决的其他问题之间的相似之处。

Hopefully when you first read the problem description you were able to identify a few possible ways to approach the solution, such as the use of pointers to compare two variables at once. If you did not come up with that strategy initially, that’s alright! You now are equipped to handle problems like this in the future.

希望当您初次阅读问题描述时,您能够找到几种解决方案的可能方法,例如使用指针一次比较两个变量。 如果您最初没有提出该策略,那没关系! 您现在已准备好将来处理此类问题。

翻译自: https://medium.com/@briandsalemi/how-to-solving-the-minimum-operations-question-3a6d43edf54a

最小二乘法解决什么问题

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值