[Mock] Facebook 1

shsh·2021년 3월 16일
0

Mock

목록 보기
10/93

303. Range Sum Query - Immutable

Given an integer array nums, find the sum of the elements between indices left and right inclusive, where (left <= right).

Implement the NumArray class:

  • NumArray(int[] nums) initializes the object with the integer array nums.
  • int sumRange(int left, int right) returns the sum of the elements of the nums array in the range [left, right] inclusive (i.e., sum(nums[left], nums[left + 1], ... , nums[right])).

My Answer 1: Accepted (Runtime: 6632 ms - 11.04% / Memory Usage: 17.8 MB - 47.70%)

class NumArray:

    def __init__(self, nums: List[int]):
        self.numArray = nums

    def sumRange(self, left: int, right: int) -> int:
        result = 0
        
        for i in range(left, right+1):
            result += self.numArray[i]
        
        return result


# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(left,right)

나와있는 그대로~


157. Read N Characters Given Read4

Given a file and assume that you can only read the file using a given method read4, implement a method to read n characters.

My Answer 1: Accepted (Runtime: 28 ms - 88.61% / Memory Usage: 14.2 MB - 84.41%)

"""
The read4 API is already defined for you.

    @param buf4, a list of characters
    @return an integer
    def read4(buf4):

# Below is an example of how the read4 API can be called.
file = File("abcdefghijk") # File is "abcdefghijk", initially file pointer (fp) points to 'a'
buf4 = [' '] * 4 # Create buffer with enough space to store characters
read4(buf4) # read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e'
read4(buf4) # read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i'
read4(buf4) # read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file
"""

class Solution:
    def read(self, buf, n):
        """
        :type buf: Destination buffer (List[str])
        :type n: Number of characters to read (int)
        :rtype: The number of actual characters read (int)
        """
        # 이게 뭔 소리야
        
        file = [] # 최종적으로 file 이 됨
        cnt = 0     # 첫번째 for 문을 나가기 위한 조건: n 길이까지 읽도록 (file 길이 > n)
        out = 0     # 첫번째 for 문을 나가기 위한 조건: file 끝까지 읽도록(file 길이 < n)
        
        # 범위 지정
        r = n // 4
        # 4로 나눈 나머지가 있으면 한번 더 read4 하도록
        if n % 4 != 0:
            r += 1
            
        # file 찾기
        for i in range(r):
            read4(buf)
            for j in range(4):
                # file 을 다 읽으면 out = 1
                if buf[j] == ' ':
                    out = 1
                    break
                cnt += 1
                file.append(buf[j])
                buf[j] = ' '    # 본 부분은 ' ' 로 초기화 해주기 => 안해주면 다음 read 때 남아있음
            
            # file 을 다 읽었거나 n 길이만큼 읽었을 경우 break
            if out or cnt >= n:
                break

        # file 을 buf 에 그대로 copy
        for i in range(len(file)):
            buf[i] = file[i]
        
        # file = "abc", n = 4 처럼 (file 길이 < n) 의 경우 return len(file)
        if len(file) < n:
            return len(file)
        
        # (file 길이 > n) 의 경우 return n
        return n

다시 만나고 싶지 않은 문제네요

file 찾아서 buf 에 다시 copy 해주기~

profile
Hello, World!

0개의 댓글

관련 채용 정보

Powered by GraphCDN, the GraphQL CDN