publicclassLRUCache {privateclassNode{int key;int value;Node prev;Node next;publicNode(int key,int value){this.key= key;this.value= value; } }privateint capacity;privateHashMap<Integer,Node> map;// Head : Most recently usedprivateNode dummyHead;// Tail : Least recently usedprivateNode dummyTail;publicLRUCache(int capacity) {this.capacity= capacity;this.map=newHashMap<Integer,Node>(); dummyHead =newNode(-1,-1); dummyTail =newNode(-1,-1);dummyHead.next= dummyTail;dummyTail.prev= dummyHead; }publicintget(int key) {if(!map.containsKey(key)) return-1;// Get current nodeNode node =map.get(key);// Disconnect it from the linkedlistnode.prev.next=node.next;node.next.prev=node.prev;// Move it to the frontmoveToFront(node);// Return valuereturnnode.value; }publicvoidset(int key,int value) {if(map.containsKey(key)){// Get current nodeNode node =map.get(key);// Disconnect it from the linkedlistnode.prev.next=node.next;node.next.prev=node.prev;// Move it to the frontmoveToFront(node);// set valuenode.value= value; } else {if(map.size() >= capacity){// Remove LRU element and its keyremoveLRU(); }// Create new nodeNode node =newNode(key, value);// Save its key in mapmap.put(key, node);// Move it to the frontmoveToFront(node); } }privatevoidmoveToFront(Node node){Node oldHead =dummyHead.next;// Connect node to dummyHeaddummyHead.next= node;node.prev= dummyHead;// Connect node to oldHeadnode.next= oldHead;oldHead.prev= node; }privatevoidremoveLRU(){Node node =dummyTail.prev;// Disconnect node from linkedlistnode.prev.next=node.next;node.next.prev=node.prev;// Set null of its pointersnode.next=null;node.prev=null;// Remove its key from hashmapmap.remove(node.key); }}
publicclassMedianFinder {// max heap stores all elements smaller / equal to medianPriorityQueue<Integer> maxHeap =newPriorityQueue<Integer>(Collections.reverseOrder());// min heap stores all elements bigger / equal to medianPriorityQueue<Integer> minHeap =newPriorityQueue<Integer>();// Adds a number into the data structure.publicvoidaddNum(int num) {// Check heap topif(maxHeap.size() ==0|| num <=maxHeap.peek()){maxHeap.offer(num); } else {minHeap.offer(num); }// min/max heap's size would not differ more than one// Rebalance if necessarywhile(Math.abs(maxHeap.size() -minHeap.size()) >1){if(maxHeap.size() >minHeap.size()){minHeap.offer(maxHeap.poll()); } else {maxHeap.offer(minHeap.poll()); } } }// Returns the median of current data streampublicdoublefindMedian() {int maxSize =maxHeap.size();int minSize =minHeap.size();if(maxSize == minSize) return (double) (maxHeap.peek() +minHeap.peek()) /2;if(maxSize -1== minSize) return (double) maxHeap.peek();if(maxSize == minSize -1) return (double) minHeap.peek();return0; }};
publicclassSolution {publicint[] maxSlidingWindow(int[] nums,int k) {if(nums ==null||nums.length==0) returnnewint[0];int[] rst =newint[nums.length- k +1];// Deque stores indexes of elements i, in descending order of nums[i]// First : smallet element in current window// Last : biggest element in current windowDeque<Integer> deque =newLinkedList<>();for(int i =0; i <nums.length; i++){while(!deque.isEmpty() && nums[i] > nums[deque.peekFirst()]){deque.pollFirst(); }deque.offerFirst(i);// element to remove from sliding windowif(i - k ==deque.peekLast()) deque.pollLast();if(i - k +1>=0) rst[i - k +1] = nums[deque.peekLast()]; }return rst; }}