LeetCode Algorithm

박영호·2021년 4월 5일

Today(04/05) Algorithm

1266. Minimum Time Visiting All Points

Example 1:

Input: points = [[1,1],[3,4],[-1,0]]
Output: 7
Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
Time from [1,1] to [3,4] = 3 seconds
Time from [3,4] to [-1,0] = 4 seconds
Total time = 7 seconds

Example 2:

Input: points = [[3,2],[-2,2]]
Output: 5

code

var minTimeToVisitAllPoints = function(points) {
  let result = 0;
    for(let i=0; i<points.length-1; i++) {
        const hor = Math.abs(points[i+1][0]-points[i][0])  
        const ver = Math.abs(points[i+1][1]-points[i][1])

        result += Math.max(hor, ver);
    }
    return result;
};

709. To Lower Case

Example 1:

Input: "Hello"
Output: "hello"

Example 2:

Input: "here"
Output: "here"

Example 3:

Input: "LOVELY"
Output: "lovely"

code

var toLowerCase = function(str) {
    return str.toLowerCase();
};

1732. Find the Highest Altitude

Example 1:

Input: gain = [-5,1,5,0,-7]
Output: 1
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.

Example 2:

Input: gain = [-4,-3,-2,-1,4,3,2]
Output: 0
Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.

code

var largestAltitude = function(gain) {
    let arr = [0];
  
    for(let i =0; i<gain.length; i++){
      arr.push(gain[i] + arr[i])
    }
  
  return Math.max(...arr);
};
profile
무언가에 호기심이 생기면 적극적이고 재밌게 그걸 해결해내고 싶어하는 프론트 엔드 개발자 입니다 .

0개의 댓글