奇安信: 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 }
点赞 评论
牛客网
牛客企业服务