Mock Interview: Amazon

JJ·2021년 6월 28일
0

Algorithms

목록 보기
114/114

와 이렇게 좋은 점수 처음 받아봐요
이렇게만 하자 마존아..^^

346. Moving Average from Data Stream

class MovingAverage {

    /** Initialize your data structure here. */
    Queue<Integer> q;
    int nums;
    double total;
    int len;
    public MovingAverage(int size) {
        q = new LinkedList<Integer>();
        nums = 0;
        total = 0.0;
        len = size;
    }
    
    public double next(int val) {
        if (q.size() >= len) {
            int gone = q.poll();
            total -= gone;
            nums--;
        }
        total += val;
        nums++;
        q.add(val);
        
        return total / nums;
    }
}

/**
 * Your MovingAverage object will be instantiated and called as such:
 * MovingAverage obj = new MovingAverage(size);
 * double param_1 = obj.next(val);
 */

Runtime: 88 ms, faster than 48.04% of Python3 online submissions for Moving Average from Data Stream.
Memory Usage: 17.2 MB, less than 83.85% of Python3 online submissions for Moving Average from Data Stream.

Queue를 사용해서 사이즈를 계쏙 보면서 만들어줬읍니다

루션이

class MovingAverage {
  int size, head = 0, windowSum = 0, count = 0;
  int[] queue;
  public MovingAverage(int size) {
    this.size = size;
    queue = new int[size];
  }

  public double next(int val) {
    ++count;
    // calculate the new sum by shifting the window
    int tail = (head + 1) % size;
    windowSum = windowSum - queue[tail] + val;
    // move on to the next head
    head = (head + 1) % size;
    queue[head] = val;
    return windowSum * 1.0 / Math.min(size, count);
  }
}

Runtime: 10 ms, faster than 45.53% of Java online submissions for Number of Equivalent Domino Pairs.
Memory Usage: 48.1 MB, less than 75.89% of Java online submissions for Number of Equivalent Domino Pairs.

호랑이 담배피던 시절에 배운 circular array...
여기서 쓰면 간지날 것 같네요

1128. Number of Equivalent Domino Pairs

class Solution {
    public int numEquivDominoPairs(int[][] dominoes) {
        Map<Integer, Integer> m = new HashMap<Integer, Integer>();
        
        int total = 0;
        for (int[] d : dominoes) {
            if (! m.containsKey(d[0]) && ! m.containsKey(d[1])) {
                m.put(d[0], d[1]);
                m.put(d[1], d[0]);
            } else if (! m.containsKey(d[0])) {
                m.put(d[0], d[1]);
                
                if (m.get(d[1]) == d[0]) {
                    total++;
                }
            } else if (! m.containsKey(d[1])) {
                m.put(d[1], d[0]);
                
                if (m.get(d[0]) == d[1]) {
                    total++;
                }
            } else if (m.containsKey(d[0]) && m.containsKey(d[1])) {
                int val1 = m.get(d[0]);
                int val2 = m.get(d[1]);
                
                if (val1 == d[1] || val2 == d[0]) {
                    total++;
                }
            } else {
                break;
            }
        }
                       
        return total;
    }
}

처음에는 진심 개뻘짓 했읍니다
모든 케이스 나눠주려고 했는데 (문제 이해 못함)

class Solution {
    public int numEquivDominoPairs(int[][] dominoes) {
        Map<Integer, Integer> m = new HashMap<Integer, Integer>();
        
        int total = 0;
        for (int[] d : dominoes) {
            int key = Math.min(d[0], d[1]);
            int val = Math.max(d[0], d[1]);
            
            if (m.containsKey(key)) {
                if (m.get(key) == val) {
                    total++;
                }
            } else {
                m.put(key, val);
            }
        }
        
        return total;
    }
}

정신 차렸다 생각하고 다시 풀었는데 또 문제를 잘못 이해했다는점...^^

class Solution {
    public int numEquivDominoPairs(int[][] dominoes) {
        Map<Integer, Integer> m = new HashMap<Integer, Integer>();
        
        int total = 0;
        for (int[] d : dominoes) {
            int key = Math.min(d[0], d[1]);
            int val = Math.max(d[0], d[1]);
            
            int value = key * 10 + val;
            
            total += m.getOrDefault(value, 0);
            
            m.put(value, m.getOrDefault(value, 0) + 1);

        }
        
        return total;
    }
}

Runtime: 10 ms, faster than 45.53% of Java online submissions for Number of Equivalent Domino Pairs.
Memory Usage: 48.1 MB, less than 75.89% of Java online submissions for Number of Equivalent Domino Pairs.

다시 쉬맵이로 돌아와서 풀기~
이거 문제 워딩이 넘 빡치게 돼있었어요

0개의 댓글