[Mock] Facebook 3

shsh·2021년 5월 12일
0

Mock

목록 보기
40/93

1528. Shuffle String

Given a string s and an integer array indices of the same length.

The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.

Return the shuffled string.

My Answer 1: Accepted (Runtime: 56 ms - 52.95% / Memory Usage: 14.3 MB - 20.22%)

class Solution:
    def restoreString(self, s: str, indices: List[int]) -> str:
        j = 0
        string = [""]*len(s)
        for i in range(len(indices)):
            string[indices[i]] = s[j]
            j += 1
        
        return ''.join(string)

indices 순서대로 string 리스트에 문자를 저장한 후 str 형태로 return


1266. Minimum Time Visiting All Points

On a 2D plane, there are n points with integer coordinates points[i] = [xi, yi]. Return the minimum time in seconds to visit all the points in the order given by points.

You can move according to these rules:

  • In 1 second, you can either:
    move vertically by one unit,
    move horizontally by one unit, or
    move diagonally sqrt(2) units (in other words, move one unit vertically then one unit horizontally in 1 second).
  • You have to visit the points in the same order as they appear in the array.
  • You are allowed to pass through points that appear later in the order, but these do not count as visits.

My Answer 1: Accepted (Runtime: 60 ms - 53.56% / Memory Usage: 14.3 MB - 65.95%)

class Solution:
    def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:
        a = points[0]
        result = 0
        
        for i in range(1, len(points)):
            result += max(abs(points[i][1] - a[1]), abs(points[i][0] - a[0]))
            
            a = points[i]
        
        return result

이거 정말 별거 아닌데 쫄아서 시간을 낭비함..

그리고 왜인지 모르겠는데 점의 개수랑 헷갈려가지고 첨엔 잘 안됐다는 점..ㅎ
가로, 세로, 대각선으로 움직여야 하는 경우를 모두 분리했었음

그냥 x 끼리의 차이값이랑 y 끼리의 차이값 중에 최댓값을 return 하면 됐다..^^

profile
Hello, World!

0개의 댓글

관련 채용 정보