Mock Interview: Uber

JJ·2021년 7월 13일
0

MockTest

목록 보기
50/60

1281. Subtract the Product and Sum of Digits of an Integer

class Solution {
    public int subtractProductAndSum(int n) {
        int sum = 0;
        int product = 1;
        
        while (n / 10 > 0) {
            sum += n % 10;
            product *= n % 10;
            n = n / 10;
        }
        
        sum += n;
        product *= n; 
        
        return product - sum;
    }
}

Runtime: 0 ms, faster than 100.00% of Java online submissions for Subtract the Product and Sum of Digits of an Integer.
Memory Usage: 36 MB, less than 34.77% of Java online submissions for Subtract the Product and Sum of Digits of an Integer.

착한 아이처럼~ 시키는대로했읍니다^_^

1188. Design Bounded Blocking Queue

class BoundedBlockingQueue {
    int capacity;
    Deque<Integer> values;
    public BoundedBlockingQueue(int capacity) {
        this.capacity = capacity;
        this.values = new ArrayDeque<Integer>();
        
    }
    
    public void enqueue(int element) throws InterruptedException {
        if (this.values.size() < capacity) {
            values.offerFirst(element);
        } else {
            throw new InterruptedException();
        }
    }
    
    public int dequeue() throws InterruptedException {
        if (this.values.size() != 0) {
            return values.pollLast();
        } else {
            throw new InterruptedException();
        }
    }
    
    public int size() {
        return this.values.size();
    }
}

외않되

루션이

class BoundedBlockingQueue {
    Queue<Integer> q = new LinkedList<Integer>();
    int capacity;
	// Synchronization object
    Object lock = new Object();
    
    public BoundedBlockingQueue(int capacity) {
        this.capacity = capacity;
    }
    
    public void enqueue(int element) throws InterruptedException {
        
        synchronized(lock)
        {
		   // in a loop wait for the q to have free slots.
		   // while loop is required here as when we wake up it is possible that some other thread has taken the item. 
		   // notify all wakes up all the threads. it is possible another enqueue thread has taken the free slot. 
            while(q.size() >= capacity)
            {
                lock.wait();
            }
            q.add(element);
			// Wake up the threads. 
            lock.notifyAll();
        }
    }
    
    public int dequeue() throws InterruptedException {
        int val =0;
        synchronized(lock)
        {
			// Again it is possible some other thread has taken the last element. So wait if the size is zero on woken up.
            while(q.size() == 0)
            {
                lock.wait();
            }
            val = q.remove();
            lock.notifyAll();
        }
        
        return val;
    }
    
    public int size() {
        return q.size();
    }
}

Runtime: 18 ms, faster than 18.28% of Java online submissions for Design Bounded Blocking Queue.
Memory Usage: 41.6 MB, less than 5.23% of Java online submissions for Design Bounded Blocking Queue.

lock 살면서 처음본다는점..^^
이거 말고도 Semaphore, Reentrantlock등
진심 걍 듣도보도 못한 것들이 나오는데......^^
당황스럽네요

0개의 댓글