关注
奇安信:
1. 商场A 满三件六折,商场B 买二送一(送最便宜的),每件都必须买,求最少花费。如[[1,8,2], [4,3,5]] 输出 6
2. 蚂蚁找食物,从原点出发 找到所有食物的最小花费。80%超时了,类似蜜蜂采蜜
function distance (a,b) {
const [a1, a2] = a.split(',').map(Number), [b1,b2] = b.split(',').map(Number)
return Math.abs(a1-b1) + Math.abs(a2-b2)
}
function getTotalDistance (list, res=0) {
const todoList = ['0,0'].concat(list)
for(let i=1; i<todoList.length; i++) {
res += distance(todoList[i-1], todoList[i])
}
return res
}
function getMinLen ( points, res=Infinity) {
// write code here
const len = points.length
const dfs = (stack) => {
if(stack.length === len) {
res = Math.min(res, getTotalDistance(stack))
return
}
for(let i=0; i<len; i++) {
const cur = points[i].join(',')
if(stack.includes(cur)) continue
stack.push(cur)
dfs(stack)
stack.pop()
}
}
dfs([])
return res
}
查看原帖
点赞 评论
相关推荐
10-02 00:20
厦门大学 Java 点赞 评论 收藏
分享
牛客热帖
正在热议
# 三环求职进展汇总 #
18535次浏览 157人参与
# 华为开奖那些事 #
1574444次浏览 11550人参与
# 正浩创新校招 #
9140次浏览 120人参与
# 国央企求职进展汇总 #
24004次浏览 104人参与
# 你投了多少份简历了? #
39192次浏览 462人参与
# 比亚迪求职进展汇总 #
377437次浏览 2101人参与
# 广发卡校招来了 #
1300次浏览 6人参与
# 机械人,你投提前批了吗? #
11237次浏览 129人参与
# 最后再改一次简历 #
1647831次浏览 26484人参与
# 在国企工作的人,躺平了吗? #
223511次浏览 3148人参与
# 寒假躺平还是提前实习 #
18463次浏览 62人参与
# 我的简历长这样 #
1513082次浏览 24060人参与
# 互联网公司评价 #
235630次浏览 2985人参与
# 比亚迪秋招开啦,你打算投递吗? #
6313次浏览 95人参与
# 你觉得实习只能是打杂吗? #
12067次浏览 111人参与
# 经纬恒润求职进展汇总 #
59882次浏览 712人参与
# 你的简历改到第几版了 #
601837次浏览 8953人参与
# 如果实习可以转正,你会不会放弃秋招 #
129180次浏览 1839人参与
# 国企还是互联网,你怎么选? #
59603次浏览 499人参与
# 选了这个offer,你有没有后悔? #
235649次浏览 2126人参与
# 小米硬件提前批进度交流 #
137848次浏览 1395人参与
# 你的实习什么时候入职 #
42662次浏览 423人参与