本文介绍了"如何给我的编码打动面试官?对于为打动面试官而提出的问题,我在编写的代码中可以采用哪些实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说有一个int向量.现在我们要进行合并,我们选择2个相邻元素v [I]和v [I + 1](对于每个有效I),然后做v [I] = v [I + 1] + v [I].并擦除v [I + 1].继续执行此操作,直到在向量中只剩下一个元素为止(注意I = 0和I = v.size()-1也被视为相邻元素).因此我们需要尝试所有可能的组合(即,我们首先采用哪对并合并的事项,如果需要进一步说明,请在评论中让我知道)

Say there is a vector of int. Now we want to merge such that , we select 2 adjacent element v[I] and v[I+1] ( for each valid I ) and do v[I] = v[I+1] + v[I] . And erase v[I+1]. Keep on doing this until you're just left with one element in the vector.(Note I=0 & I=v.size()-1 are also considered as adjacent ). so we need to try all such possible combination(i.e which pair we took first and merged matters if further clarification required please let me know in the comment)

每次合并时,我们都会执行cost + = v [I] + v [I + 1].目标是使成本最小化.以向量为1 2 3为例.合并[1 2 3]-> [3 ,3]& cost = 3-> [6]& cost = 9另一种方式[1 2 3]-> [1,5]& cost = 5-> [6]&费用= 11.那么他们的任何算法都可以生成具有给定约束的所有置换吗?

where every time we merge we do cost+= v[I] + v[I+1].Goal is to minimise cost.Take an example say vector is 1 2 3. merging [1 2 3]-> [3,3] & cost=3 -> [6] & cost=9 another way [1 2 3]-> [1,5] & cost=5 -> [6] & cost=11 . So is their any algorithm to generate all permutation with given constrain ?

#include<bits/stdc++.h>
using namespace std;
int mn =INT_MAX;
void r(vector<int > v, int sum)
{
    if(v.size()==1){if( mn >sum) mn=sum; return ;}

    for(int i=0;i<v.size();i++)
    {
        sum+=v[i]+v[(i+1)%v.size()];
        v[i]=v[i]+v[(i+1)%v.size()];
        v.erase(v.begin()+(i+1)%v.size());
        r(v,sum);
    }
}
int main()
{
   vector<int> v;//suppose we gave some input to our vector

   r(v,0);
   cout<<mn;
return 0;

}
#if you have a better solution, do state it, thankyou!

推荐答案

您的目标是打动面试官.
他们正在寻找可以在团队中工作并且可以创建可重用代码的人员,这些人员可以在需要切换到其他主题进行工作时将其移交给同事.

Your goal is to impress interviewers.
They are looking for people who can work in teams and can make reusable code, which can be handed over to colleagues when needing to be switched to a different topic to work on.

为此,学习

  • 养成解释代码的习惯,即编写有用的注释
    • 根据如果代码和注释不匹配,可能都存在错误"来写注释
    • 根据我在没有计算机的岛上度过三个月的假期后,我的评论可帮助我理解自己的代码"
    • get into the habit of explaing your code, i.e. write useful comments
      • write comments according to "if code and comment do not match, both are probably in error"
      • write comments according to "my comments help me to understand my own code after I return from a three month vacation on an island without computers"

      这篇关于&quot;如何给我的编码打动面试官?对于为打动面试官而提出的问题,我在编写的代码中可以采用哪些实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 14:22