Advertisement
sweet1cris

Untitled

Jan 9th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.58 KB | None | 0 0
  1. // TreeMap Version
  2. import java.util.*;
  3.  
  4.  
  5. public class Solution {
  6.     /**
  7.      * @param nums
  8.      *            : A list of integers.
  9.      * @return: The median of the element inside the window at each moving.
  10.      */
  11.     public  ArrayList<Integer> medianSlidingWindow(int[] nums, int k) {
  12.         int n = nums.length;
  13.         TreeSet<Node> minheap = new TreeSet<Node>();
  14.         TreeSet<Node> maxheap = new TreeSet<Node>();
  15.         ArrayList<Integer> result = new ArrayList<Integer> ();
  16.        
  17.         if (k == 0)
  18.             return result;
  19.        
  20.         int half = (k+1)/2;
  21.         for(int i=0; i<k-1; i++) {
  22.             add(minheap, maxheap, half, new Node(i, nums[i]));
  23.         }
  24.         for(int i=k-1; i<n; i++) {
  25.             add(minheap, maxheap, half, new Node(i, nums[i]));
  26.             result.add(minheap.last().val);
  27.             remove(minheap,maxheap, new Node(i-k+1, nums[i-k+1]));
  28.         }
  29.         return result;
  30.     }
  31.    
  32.     void add(TreeSet<Node>minheap, TreeSet<Node> maxheap, int size, Node node) {
  33.         if (minheap.size()<size) {
  34.             minheap.add(node);
  35.         }
  36.         else {
  37.             maxheap.add(node);
  38.         }
  39.         if (minheap.size()==size) {
  40.             if (maxheap.size()>0 && minheap.last().val>maxheap.first().val) {
  41.                 Node s = minheap.last();
  42.                 Node b = maxheap.first();
  43.                 minheap.remove(s);
  44.                 maxheap.remove(b);
  45.                 minheap.add(b);
  46.                 maxheap.add(s);
  47.             }
  48.         }
  49.     }
  50.    
  51.     void remove(TreeSet<Node>minheap, TreeSet<Node> maxheap, Node node) {
  52.         if (minheap.contains(node)) {
  53.             minheap.remove(node);
  54.         }
  55.         else {
  56.             maxheap.remove(node);
  57.         }
  58.     }
  59. }
  60.  
  61. class Node implements Comparable<Node>{
  62.     int id;
  63.     int val;
  64.     Node(int id, int val) {
  65.         this.id = id;
  66.         this.val = val;
  67.     }
  68.     public int compareTo(Node other) {
  69.         Node a =(Node)other;
  70.         if (this.val == a.val) {
  71.             return this.id - a.id;
  72.         }
  73.         return this.val - a.val;
  74.     }
  75. }
  76.  
  77.  
  78. // Normal heap Version
  79. public class Solution {
  80.     /**
  81.      * @param nums: A list of integers.
  82.      * @return: The median of the element inside the window at each moving.
  83.      */
  84.     public ArrayList<Integer> medianSlidingWindow(int[] nums, int k) {
  85.         // write your code here
  86.         ArrayList<Integer> result = new ArrayList<Integer>();
  87.         int size = nums.length;
  88.         if (size == 0 || size < k) {
  89.             return result;
  90.         }
  91.  
  92.         PriorityQueue<Integer> minPQ = new PriorityQueue<Integer>();
  93.         PriorityQueue<Integer> maxPQ = new PriorityQueue<Integer>(11, Collections.reverseOrder());
  94.  
  95.         int median = nums[0];
  96.         int j = 0;
  97.         if (k == 1) {
  98.             result.add(median);
  99.         }
  100.  
  101.         for (int i = 1; i < size; i++) {
  102.             if (nums[i] > median) {
  103.                 minPQ.offer(nums[i]);
  104.             } else {
  105.                 maxPQ.offer(nums[i]);
  106.             }
  107.  
  108.             if (i > k - 1) {
  109.                 if (nums[j] > median) {
  110.                     minPQ.remove(nums[j]);
  111.                 } else if (nums[j] < median) {
  112.                     maxPQ.remove(nums[j]);
  113.                 } else {
  114.                     median = Integer.MIN_VALUE;
  115.                 }
  116.                 j++;
  117.             }
  118.  
  119.             if (median == Integer.MIN_VALUE) {
  120.                 median = minPQ.size() > maxPQ.size() ? minPQ.poll() : maxPQ.poll();
  121.             } else {
  122.                 while (minPQ.size() >= maxPQ.size() + 2) {
  123.                     maxPQ.offer(median);
  124.                     median = minPQ.poll();
  125.                 }
  126.                 while (maxPQ.size() >= minPQ.size() + 1) {
  127.                     minPQ.offer(median);
  128.                     median = maxPQ.poll();
  129.                 }
  130.             }
  131.             if (i >= k - 1) {
  132.                 result.add(median);
  133.             }
  134.         }
  135.  
  136.         return result;
  137.     }
  138. }
  139.  
  140. // Hash Heap Version
  141. import java.util.*;
  142.  
  143. class HashHeap {
  144.     ArrayList<Integer> heap;
  145.     String mode;
  146.     int size_t;
  147.     HashMap<Integer, Node> hash;
  148.  
  149.     class Node {
  150.         public Integer id;
  151.         public Integer num;
  152.  
  153.         Node(Node now) {
  154.             id = now.id;
  155.             num = now.num;
  156.         }
  157.  
  158.         Node(Integer first, Integer second) {
  159.             this.id = first;
  160.             this.num = second;
  161.         }
  162.     }
  163.  
  164.     public HashHeap(String mod) {
  165.         // TODO Auto-generated constructor stub
  166.         heap = new ArrayList<Integer>();
  167.         mode = mod;
  168.         hash = new HashMap<Integer, Node>();
  169.         size_t = 0;
  170.     }
  171.  
  172.     int peak() {
  173.         return heap.get(0);
  174.     }
  175.  
  176.     int size() {
  177.         return size_t;
  178.     }
  179.  
  180.     Boolean empty() {
  181.         return (heap.size() == 0);
  182.     }
  183.  
  184.     int parent(int id) {
  185.         if (id == 0) {
  186.             return -1;
  187.         }
  188.         return (id - 1) / 2;
  189.     }
  190.  
  191.     int lson(int id) {
  192.         return id * 2 + 1;
  193.     }
  194.  
  195.     int rson(int id) {
  196.         return id * 2 + 2;
  197.     }
  198.  
  199.     boolean comparesmall(int a, int b) {
  200.         if (a <= b) {
  201.             if (mode == "min")
  202.                 return true;
  203.             else
  204.                 return false;
  205.         } else {
  206.             if (mode == "min")
  207.                 return false;
  208.             else
  209.                 return true;
  210.         }
  211.  
  212.     }
  213.  
  214.     void swap(int idA, int idB) {
  215.         int valA = heap.get(idA);
  216.         int valB = heap.get(idB);
  217.  
  218.         int numA = hash.get(valA).num;
  219.         int numB = hash.get(valB).num;
  220.         hash.put(valB, new Node(idA, numB));
  221.         hash.put(valA, new Node(idB, numA));
  222.         heap.set(idA, valB);
  223.         heap.set(idB, valA);
  224.     }
  225.  
  226.     Integer poll() {
  227.         size_t--;
  228.         Integer now = heap.get(0);
  229.         Node hashnow = hash.get(now);
  230.         if (hashnow.num == 1) {
  231.             swap(0, heap.size() - 1);
  232.             hash.remove(now);
  233.             heap.remove(heap.size() - 1);
  234.             if (heap.size() > 0) {
  235.                 siftdown(0);
  236.             }
  237.         } else {
  238.             hash.put(now, new Node(0, hashnow.num - 1));
  239.         }
  240.         return now;
  241.     }
  242.  
  243.     void add(int now) {
  244.         size_t++;
  245.         if (hash.containsKey(now)) {
  246.             Node hashnow = hash.get(now);
  247.             hash.put(now, new Node(hashnow.id, hashnow.num + 1));
  248.  
  249.         } else {
  250.             heap.add(now);
  251.             hash.put(now, new Node(heap.size() - 1, 1));
  252.         }
  253.  
  254.         siftup(heap.size() - 1);
  255.     }
  256.  
  257.     void delete(int now) {
  258.         size_t--;
  259.         ;
  260.         Node hashnow = hash.get(now);
  261.         int id = hashnow.id;
  262.         int num = hashnow.num;
  263.         if (hashnow.num == 1) {
  264.  
  265.             swap(id, heap.size() - 1);
  266.             hash.remove(now);
  267.             heap.remove(heap.size() - 1);
  268.             if (heap.size() > id) {
  269.                 siftup(id);
  270.                 siftdown(id);
  271.             }
  272.         } else {
  273.             hash.put(now, new Node(id, num - 1));
  274.         }
  275.     }
  276.  
  277.     void siftup(int id) {
  278.         while (parent(id) > -1) {
  279.             int parentId = parent(id);
  280.             if (comparesmall(heap.get(parentId), heap.get(id)) == true) {
  281.                 break;
  282.             } else {
  283.                 swap(id, parentId);
  284.             }
  285.             id = parentId;
  286.         }
  287.     }
  288.  
  289.     void siftdown(int id) {
  290.         while (lson(id) < heap.size()) {
  291.             int leftId = lson(id);
  292.             int rightId = rson(id);
  293.             int son;
  294.             if (rightId >= heap.size() || (comparesmall(heap.get(leftId), heap.get(rightId)) == true)) {
  295.                 son = leftId;
  296.             } else {
  297.                 son = rightId;
  298.             }
  299.             if (comparesmall(heap.get(id), heap.get(son)) == true) {
  300.                 break;
  301.             } else {
  302.                 swap(id, son);
  303.             }
  304.             id = son;
  305.         }
  306.     }
  307. }
  308.  
  309. public class Solution {
  310.     /**
  311.      * @param nums
  312.      *            : A list of integers.
  313.      * @return: The median of the element inside the window at each moving.
  314.      */
  315.     public ArrayList<Integer> medianSlidingWindow(int[] nums, int k) {
  316.         // write your code here
  317.  
  318.         ArrayList<Integer> ans = new ArrayList<Integer>();
  319.         if (nums.length == 0)
  320.             return ans;
  321.         int median = nums[0];
  322.         HashHeap minheap = new HashHeap("min");
  323.         HashHeap maxheap = new HashHeap("max");
  324.         for (int i = 0; i < nums.length; i++) {
  325.             if (i != 0) {
  326.                 if (nums[i] > median) {
  327.                     minheap.add(nums[i]);
  328.                 } else {
  329.                     maxheap.add(nums[i]);
  330.                 }
  331.             }
  332.  
  333.             if (i >= k) {
  334.                 if (median == nums[i - k]) {
  335.                     if (maxheap.size() > 0) {
  336.                         median = maxheap.poll();
  337.                     } else if (minheap.size() > 0) {
  338.                         median = minheap.poll();
  339.                     }
  340.  
  341.                 } else if (median < nums[i - k]) {
  342.                     minheap.delete(nums[i - k]);
  343.                 } else {
  344.                     maxheap.delete(nums[i - k]);
  345.                 }
  346.             }
  347.  
  348.             while (maxheap.size() > minheap.size()) {
  349.                 minheap.add(median);
  350.                 median = maxheap.poll();
  351.             }
  352.             while (minheap.size() > maxheap.size() + 1) {
  353.                 maxheap.add(median);
  354.                 median = minheap.poll();
  355.             }
  356.  
  357.             if (i + 1 >= k) {
  358.                 ans.add(median);
  359.             }
  360.         }
  361.         return ans;
  362.     }
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement