2022 春招
题目链接:B站视频
https://www.bilibili.com/video/BV1Ri4y1D7DA?spm_id_from=333.999.0.0
输入
5
2 0 1 0 3
2
输出 (-1)
输入
6
2 1 5 6 2 3
3
输出 (2)
贪心算法,直接跳到最大值
let n = 5, M = "2 0 1 0 3", K = 2;
let n = 6, M = "2 1 5 6 2 3", K = 3;
M = M.split(" ").map((item)=>+item);
function rout(M, K) {
let step = 0;
function digui(i,step) {
if (i >= M.length - 1) {
return step;
}
if (M[i] == 0) {//并未跳转到最终台阶,而且当前台阶值为零,意味着跳不了了
return -1;
}
step++;
return digui(i + M[i], step);
}
if (digui(0, 0) != -1) {
step = digui(0, 0);
if (step > K) {
return -1;
} else {
return step;
}
} else {
return -1;
}
}
console.log(rout(M, K));
2022.3.30
题目链接:CSDN博主分享
这是我接触到的第一道华为笔试真题(当时室友做的)
//不能仅依靠AB的数量得出来,比如AAAABB和BBAAAA的数量一样,但结果不同
let coreN = 5;
let stri = "B B A B A B A";
sarr = stri.split(" ");
let len = sarr.length;
let Acnt = 0, Bcnt = 0;
let unfullIndex=1,emptyIndex=1;
//这里的unfullIndex记录的是A的情况,他的最终结果所指的那块芯片,可能是未满,也可能刚好满。
for (let i = 0; i < len; i++){
if (sarr[i] == "A") {
Acnt++;
if ( Acnt % 4 == 0) {
null
} else if (Acnt % 4 == 1) {
//说明此时是第一个A
unfullIndex=emptyIndex;//空的赋给未满
emptyIndex++;
}
}
if (sarr[i] == "B") {
Bcnt++;
emptyIndex++;
}
}
// console.log(unfullIndex + " " + emptyIndex);
if (emptyIndex - 1 > coreN) {
console.log(0);
console.log(0);
} else if (sarr[sarr.length - 1] == "A") {
console.log(unfullIndex);
console.log(Acnt % 4);
} else if (sarr[sarr.length - 1] == "B") {
console.log(emptyIndex - 1);
console.log(1);
}
2022.4.13
题目链接:CSDN一位博主的博客
https://blog.csdn.net/weixin_44052055/article/details/124221326?spm=1001.2014.3001.5502
(我的第一次笔试,当时没A出来)
输入
4
0,2,200,0,1
1,3,400,0,1
2,3,400,1,0
3,3,300,0,1
3 1 3 200 0 1
输出
2 1 3
//注意:array.sort()改变原数组,array.filter不改变原数组
//数据1
let n = 4;
let cString = "0,2,200,0,1 1,3,400,0,1 2,3,400,1,0 3,3,300,0,1";
let request = "3 1 3 200 0 1"
//数据2
// let n = 6;
// let cString = "0,2,200,0,1 1,4,330,2,1 2,3,400,3,1 3,3,310,1,1 4,3,320,8,1 5,3,330,0,1";
// let request = "3 2 3 300 9 2"
let carr = cString.split(" ").map((item) => item.split(","));
request = request.split(" ").map((item)=>+item);
//过滤不合要求的carrItem
/*要求1:cpu>=request[2] */
carr = carr.filter((item) => {
return +item[1] >= request[2];
})
/*要求2:memSize(item[2])>=request[3] */
carr = carr.filter((item) => {
return +item[2] >= request[3];
})
/*要求3:cpuArch(item[3])==request[4] ,注意特殊情况*/
if (request[4] != 9) {
carr = carr.filter((item) => {
return +item[3] == request[4];
})
}
/*要求4:NPsupport(item[4])==request[5],注意特殊情况 */
if (request[5] != 2) {
carr = carr.filter((item) => {
return +item[4] == request[5];
})
}
/*要求5:选择策略,1指CPU优先,2指内存优先。但要注意,这二者都是在合格品数量>request[0]的情况下才需要选,否则直接输出id即可 */
if (carr.length <= request[0]) {
let idvalue = [];
carr.forEach((item) => {
idvalue.push(item[0]);
})
idvalue = idvalue.join(" ");//变成字符串类型才能输出
console.log(carr.length + " " + idvalue);
} else {//供大于求,需要排序
if (request[1] == 1) {
//CPU,排序之后,取前request[0]个
carr.sort( (a, b)=>
(+a[1])-(+b[1])
)//按照CPU升序排序,然后还需要从前往后看,有没有CPU相同的,有相同的话,再比较内存值,内存值小的再往前放,取前request[0]个
let keyCPU = carr[request[0] - 1][1];
let temp = carr.filter((item) => item[1] == keyCPU),start0;
if (temp.length != 1) {//说明最后一个有重复的,必须再按照内存值从小到大的顺序,重置这一部分的顺序
temp.sort((a, b) => (+a[2]) - (+b[2]));
for (let i = 0; i < carr.length; i++) {
if (keyCPU == carr[i][1]) {
start0 = i;
break;
}
}
carr.splice(start0, temp.length, ...temp);//经测试,这是可行的
}
// console.log(carr);
carr=carr.filter((item, index) => index <= request[0]-1);
//所有已经满足要求的数组,但id值未必是升序的
carr.sort((a, b) =>
(+a[0]) - (+b[0])
)//已经满足要求数量的数组,且id值是升序的
let idvalue = [];
carr.forEach((item) => {
idvalue.push(item[0]);
})
idvalue = idvalue.join(" ");//变成字符串类型才能输出
console.log(carr.length + " " + idvalue);
} else {//memSize
carr.sort((a, b) =>
(+a[2]) - (+b[2])
)//按照memSize升序排序,取前request[0]个
//还要看第request[0]个的memSize值与前后有没有重复的,若有,则需要把这些重复的再按CPU核数从小到大重新排序
let keymemSize = carr[request[0] - 1][2];
let temp = carr.filter((item) => item[2] == keymemSize), start1;
if (temp.length != 1) {//说明最后一个有重复的,必须再按照CPU核数值从小到大的顺序,重置这一部分的顺序
temp.sort((a, b) => (+a[1]) - (+b[1]));
for (let i = 0; i < carr.length; i++) {
if (keymemSize == carr[i][2]) {
start1 = i;
break;
}
}
carr.splice(start1, temp.length, ...temp);//经测试,这是可行的
}
carr = carr.filter((item, index) => index <= request[0] - 1);
//所有已经满足要求的数组,但id值未必是升序的
carr.sort((a, b) =>
(+a[0]) - (+b[0])
)//已经满足要求数量的数组,且id值是升序的
// console.log(carr);
let idvalue = [];
carr.forEach((item) => {
idvalue.push(item[0]);
})
idvalue = idvalue.join(" ");//变成字符串类型才能输出
console.log(carr.length + " " + idvalue);
}
}
2022.4.20
题目链接:B站一位UP主的视频
https://www.bilibili.com/video/BV1594y1o7xx?spm_id_from=333.999.0.0
华为4.20笔试第一道题
let score = 94;
function poss(score) {
let two = new Array(10).fill(2),
four = new Array(10).fill(4),
eight = new Array(5).fill(8);
let total = [...two, ...four, ...eight];
let res=0;
function digui(i,score,cnt) {
if (score == 0) { res++;return};
if (cnt == 0) {
if (score == 0) {
res++;
}
return;
}
if (i > total.length) return;
digui(i + 1, score - total[i], cnt);//本题做对了
digui(i + 1, score, cnt - 1);//本题做错了
// return digui(i + 1, score - total[i], cnt) + digui(i + 1, score, cnt - 1);//做对了+做错了
}
digui(0, score, 3);//往下扣分
return res;
}
console.log(poss(score));
2022.4.27
题目链接:csdn博主
let n = 4,
timeN=6,
t = "2 3 10 2 4 20 1 3 15 1 4 25 3 4 8 1 4 16"
t = t.split(" ").map((item)=>item.split(" "));
let sum = 0;
let tstart = t.map((item) => item[0]);
let tend = t.map((item) => item[1]);
tstart = Math.min(...tstart);
tend = Math.max(...tend);
// console.log(tend)
// console.log(tstart);
while (tstart <= n) {
let temp = t.filter((item) => item[0] <= tstart && item[1] >= tstart);
let price = temp.map((item) => item[2]);
sum += Math.min(...price);
tstart++;
}
console.log(sum);