其他分享
首页 > 其他分享> > leetcode3

leetcode3

作者:互联网

5271. 访问所有点的最小时间

平面上有 n 个点,点的位置用整数坐标表示 points[i] = [xi, yi]。请你计算访问所有这些点需要的最小时间(以秒为单位)。

你可以按照下面的规则在平面上移动:

示例 1:

输入:points = [[1,1],[3,4],[-1,0]]
输出:7
解释:一条最佳的访问路径是: [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]   
从 [1,1] 到 [3,4] 需要 3 秒 
从 [3,4] 到 [-1,0] 需要 4 秒
一共需要 7 秒

示例 2:

输入:points = [[3,2],[-2,2]]
输出:5

 

提示:

var minTimeToVisitAllPoints = function(points) {
//     let res = 0;
//     for(let i=0; i<points.length-1; i++){
//         let [x, y] = points[i];
//         let [x1, y1] = points[i+1];
//         let xl = (y1-y)/(x1-x);
//         if(xl>0){
//             if(xl===1){
//                 // 斜率1,x的值也可能从大到小,所以y值也可能是递减
//                 res+=Math.abs(x1-x)
//             }else{
//                 res+=(Math.abs(y1-x1)+Math.abs(x1-x))
//             }
//          }else{
//            if(xl===-1){
//                 res+=Math.abs(x-x1)
//             }else{
//                 res+=((-x1-y1)+Math.abs(x1-x))
//             }
//         }
        
//     }
//     return res
// };

/**
 * @param {number[][]} points
 * @return {number}
 */
var minTimeToVisitAllPoints = function(points) {
    let result = 0;
    for (let i = 1; i < points.length; i++) {
        result += Math.max(Math.abs(points[i - 1][0] - points[i][0]), Math.abs(points[i - 1][1] - points[i][1]));
    }
    return result;
};
class Solution(object):
    def minTimeToVisitAllPoints(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """
        ans = 0
        ls = points[0]
        for v in points:
            dx = abs(v[0]-ls[0])
            dy = abs(v[1]-ls[1])
            ans += min(dx, dy) + abs(dx-dy)
            ls = v
        
        return ans

 

  

搜索推荐系统

给你一个产品数组 products 和一个字符串 searchWord ,products  数组中每个产品都是一个字符串。

请你设计一个推荐系统,在依次输入单词 searchWord 的每一个字母后,推荐 products 数组中前缀与 searchWord 相同的最多三个产品。如果前缀相同的可推荐产品超过三个,请按字典序返回最小的三个。

请你以二维列表的形式,返回在输入 searchWord 每个字母后相应的推荐产品的列表。

 

示例 1:

输入:products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
输出:[
["mobile","moneypot","monitor"],
["mobile","moneypot","monitor"],
["mouse","mousepad"],
["mouse","mousepad"],
["mouse","mousepad"]
]
解释:按字典序排序后的产品列表是 ["mobile","moneypot","monitor","mouse","mousepad"]
输入 m 和 mo,由于所有产品的前缀都相同,所以系统返回字典序最小的三个产品 ["mobile","moneypot","monitor"]
输入 mou, mous 和 mouse 后系统都返回 ["mouse","mousepad"]
示例 2:

输入:products = ["havana"], searchWord = "havana"
输出:[["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
示例 3:

输入:products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
输出:[["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
示例 4:

输入:products = ["havana"], searchWord = "tatiana"
输出:[[],[],[],[],[],[],[]]
 

提示:

1 <= products.length <= 1000
1 <= Σ products[i].length <= 2 * 10^4
products[i] 中所有的字符都是小写英文字母。
1 <= searchWord.length <= 1000
searchWord 中所有字符都是小写英文字母。

/**
 * @param {string[]} products
 * @param {string} searchWord
 * @return {string[][]}
 */
var suggestedProducts = function(products, searchWord) {
    const sortP = products.sort();
    const result = [];
    result.push(sortP);
    for (let i = 0; i < searchWord.length; i++) {
        result.push([]);
        for (let j = 0; j < result[i].length; j++) {
            if (result[i][j][i] === searchWord[i]) {
                result[i+1].push(result[i][j]);
            }
        }
    }
    const r = [];
    for (let i = 0; i < searchWord.length; i++) {
        r.push([]);
        for (let j = 0; j < result[i+1].length;j++) {
            r[i].push(result[i+1][j]);
            if (j === 2) break;
        }
    }
    return r;
};

 

标签:havana,leetcode3,abs,products,result,searchWord,points
来源: https://www.cnblogs.com/zhangzs000/p/11924972.html