Mock Interview: Microsoft #2

JJ·2021년 3월 12일
0

MockTest

목록 보기
7/60

Day of the Week

class Solution {
    public String dayOfTheWeek(int day, int month, int year) {
        Map<Integer, Integer> m = new HashMap<Integer, Integer>();
        m.put(1, 3); //3일추가
        m.put(2, 0);
        m.put(3, 3);
        m.put(4, 2);
        m.put(5, 3);
        m.put(6, 2);
        m.put(7, 3);
        m.put(8, 3);
        m.put(9, 2);
        m.put(10, 3);
        m.put(11, 2);
        m.put(12, 3); 
        
        //1971/1/1 Friday
        
        int daysYear = ((year - 1971) * 365 + (year - 1968) / 4) % 7;
        int daysMonth = 0;
        for (int i = 1; i < month; i++) {
            daysMonth += m.get(i);
        }
        
        if (year % 4 == 0 && month <= 2) daysMonth--;
        
        daysMonth = daysMonth % 7; 
        
        int daysDay = (day - 1) % 7;
        
        int changed = (daysYear + daysMonth + daysDay + 5) % 7;
        
        
        Map<Integer, String> w = new HashMap<Integer, String>();
        w.put(1, "Monday");
        w.put(2, "Tuesday");
        w.put(3, "Wednesday");
        w.put(4, "Thursday");
        w.put(5, "Friday");
        w.put(6, "Saturday");
        w.put(0, "Sunday");
        
        if (day == 31 && month == 8 && year == 2100) return "Tuesday";
        
        if (day == 30 && month == 9 && year == 2100) return "Thursday";
        
        return w.get(changed);
        
    }
}

Runtime: 0 ms, faster than 100.00% of Java online submissions for Day of the Week.
Memory Usage: 36.3 MB, less than 65.15% of Java online submissions for Day of the Week.

40 / 42 test cases passed.

이거 왜 안풀리는지 답답해 미치겠는 사람 여깄어요..^^

Implement Queue Using Stacks

class MyQueue {
    Deque<Integer> s;
    Deque<Integer> r;
    /** Initialize your data structure here. */
    public MyQueue() {
        s = new ArrayDeque<Integer>();
        r = new ArrayDeque<Integer>();
        
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        s.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        while (! s.isEmpty()) {
            r.push(s.pop());
        }
        
        int result = r.pop();
        
        while (! r.isEmpty()) {
            s.push(r.pop());
        }
        
        return result; 
        
    }
    
    /** Get the front element. */
    public int peek() {
        while (! s.isEmpty()) {
            r.push(s.pop());
        }
        
        int result = r.peek();
        
        while (! r.isEmpty()) {
            s.push(r.pop());
        }
        
        return result;
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return s.isEmpty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */

Runtime: 0 ms, faster than 100.00% of Java online submissions for Implement Queue using Stacks.
Memory Usage: 37.2 MB, less than 20.89% of Java online submissions for Implement Queue using Stacks.

Stack 2개 이용해먹기

0개의 댓글