Mock Interview: Google

JJ·2021년 6월 29일
0

MockTest

목록 보기
41/60

1089. Duplicate Zeros

import numpy as np
class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        """
        Do not return anything, modify arr in-place instead.
        """
        numzero = 0;
        for i in range(len(arr)):
            if arr[i] == 0:
                newarr = arr[:i] + arr[i-1:len(arr)]
                arr = newarr
                numzero = numzero + 1
                i = i + 2
            else:
                i = i + 1
        
        
        arr = arr[len(arr) - numzero:]
        
        
class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        """
        Do not return anything, modify arr in-place instead.
        """

        possible_dups = 0
        length_ = len(arr) - 1

        # Find the number of zeros to be duplicated
        for left in range(length_ + 1):

            # Stop when left points beyond the last element in the original list
            # which would be part of the modified list
            if left > length_ - possible_dups:
                break

            # Count the zeros
            if arr[left] == 0:
                # Edge case: This zero can't be duplicated. We have no more space,
                # as left is pointing to the last element which could be included  
                if left == length_ - possible_dups:
                    arr[length_] = 0 # For this zero we just copy it without duplication.
                    length_ -= 1
                    break
                possible_dups += 1

        # Start backwards from the last element which would be part of new list.
        last = length_ - possible_dups

        # Copy zero twice, and non zero once.
        for i in range(last, -1, -1):
            if arr[i] == 0:
                arr[i + possible_dups] = 0
                possible_dups -= 1
                arr[i + possible_dups] = 0
            else:
                arr[i + possible_dups] = arr[i]

Runtime: 64 ms, faster than 94.30% of Python3 online submissions for Duplicate Zeros.
Memory Usage: 14.8 MB, less than 85.46% of Python3 online submissions for Duplicate Zeros.

ㅇㄴ

1007. Minimum Domino Rotations For Equal Row

class Solution {
    public int minDominoRotations(int[] tops, int[] bottoms) {
        //return minimum 
        Map<Integer, Integer> m = new HashMap<Integer, Integer>();
        
        for (int i = 0; i < tops.length; i++) {
            if (tops[i] == bottoms[i]) {
                m.put(tops[i], m.getOrDefault(tops[i], 0) + 1);
            } else {
                m.put(tops[i], m.getOrDefault(tops[i], 0) + 1);
                m.put(bottoms[i], m.getOrDefault(bottoms[i], 0) + 1);
            }
            
        }
        
        int top = -1;
        int bottom = -1;
        int theOne = -1;
        for (int key : m.keySet()) {
            if (m.get(key) >= tops.length) {
                for (int j = 0; j < tops.length; j++) {
                    if (tops[j] == key && bottoms[j] == key) {
                        continue;
                    } else if (bottoms[j] == key) {
                        bottom++;
                    } else if (tops[j] == key) {
                        top++;
                    }
                }
            }
        }
        int rotations = Math.min(top + 1, bottom + 1);
        
        if (rotations == tops.length) {
            return 0; 
        }
        return (top == -1 && bottom == -1) ? -1 : rotations;
    }
}

이건 할때부터 안될거라는 직감이 왔어요^^

class Solution {
  /*
  Return min number of rotations 
  if one could make all elements in A or B equal to x.
  Else return -1.
  */
  public int check(int x, int[] A, int[] B, int n) {
    // how many rotations should be done
    // to have all elements in A equal to x
    // and to have all elements in B equal to x
    int rotations_a = 0, rotations_b = 0;
    for (int i = 0; i < n; i++) {
      // rotations coudn't be done
      if (A[i] != x && B[i] != x) return -1;
      // A[i] != x and B[i] == x
      else if (A[i] != x) rotations_a++;
      // A[i] == x and B[i] != x    
      else if (B[i] != x) rotations_b++;
    }
    // min number of rotations to have all
    // elements equal to x in A or B
    return Math.min(rotations_a, rotations_b);
  }

  public int minDominoRotations(int[] A, int[] B) {
    int n = A.length;
    int rotations = check(A[0], B, A, n);
    // If one could make all elements in A or B equal to A[0]
    if (rotations != -1 || A[0] == B[0]) return rotations;
    // If one could make all elements in A or B equal to B[0]
    else return check(B[0], B, A, n);
  }
}

Runtime: 3 ms, faster than 98.74% of Java online submissions for Minimum Domino Rotations For Equal Row.
Memory Usage: 47.1 MB, less than 37.44% of Java online submissions for Minimum Domino Rotations For Equal Row.

0개의 댓글