CC150 20.9

20.9 Numbers are randomly generated and passed to a method. Write a program to find and maintain the median value as new values are generated.

class MedianNum
{

  // O(n)
  void insert(int n)
  {
    if (size == 0)
    {
      head = new Node(n);
      media = head;
    }
    else if (n < head.num)
    {
      Node newHead = new Node(n);
      newHead.next = head;
      head.pre = newHead;
      head = newHead;
      median = median.pre;
    }
    else
    {
      // Find the position
      Node n = head;
      boolean passMedian = false;
      Node node = new Node(i);
      while (n.next != null)
      {
        if (n.next.num > n)
        {
          node.next = n.next;
          n.next.pre = node;
          n.next = node;
          node.pre = n;
          if (passMedian)
          {
            median = median.next;
          }
          else
          {
            median = median.pre;
          }
          break;
        }
        passMedian = n == median;
      }
      
      // At the end
      n.next = node;
      node.pre = n;
      median = median.pre;
    }
    
    size++;
  }
  
  // O(1)
  int medium()
  {
    if (size == 0)
      throw Exception;
      
    if (size % 2 == 1) // odd
    {
      return median.num;
    }
    else // Even
    { 
      return ( median.num + median.next.nu ) / 2;
    }
  }
  
  private class Node
  {
    int num;
    Node next;
    Node.pre;
  }
  
 
  private Node head = null;
  private Node median = null;
  private int size = 0;
}

// What about using heap.

class MedianNum
{
  private size = 0;  
  Heap minHeap; // All nums > median
  Heap maxHeap; // All nums <= median
  
  // O(log n)
  void insert(int n)
  {
    if(size == 0)
    {
      maxHeap.inseart(n);
    }
    else if (size == 1 && n >= maxHeap.top())
    {
      minHeap.inseart(n);
    }
    else if (n <= maxHeap.top())
    {
      minHeap.inseart(maxHeap.removeTop());
      maxHeap.inseart(n);
    }
    else
    {
      maxHeap.inseart(minHeap.removeTop());
      minHeap.inseart(n);
    }
    
    size++;
  }
  
  // O(1)
  in median()
  {
    if (size == 0)
      throw exception
    
    if (size % 2 == 1)
    {
      return maxHeap.top();
    }
    else
    {
      return (maxHeap.top() + minHeap.top()) / 2;
    }
  }
}
时间: 2024-10-12 00:40:27

CC150 20.9的相关文章

CC150 20.11

20.11 Imagine you have a square matrix, where each cell is filled with either black or white. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels. // A brute force solution. // n * n // iterate fr

CC150 20.6

20.6 Describe an algorithm to find the largest 1 million numbers in 1 billion numbers. Assume that the computer memory can hold all one billion numbers. // can hold all numbers. // Cheating! // Do we know the max or min? // Consider using bitmap // I

CC150 20.1

20.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. //  Sorry I don't know. I hate questions like this.

CC150 20.2

20.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle - in other words, each 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect. // Randomly generate a in

CC150 20.3

20.3 Write a method to randomly generate a set of m integers from an array of size n. Each element must have equal probability of being chosen. // Similar to 20.2 // This assume m <= n

CC150 20.4

20.4 Write a method to count the number of 2s between 0 and n. // What this mean? // Given a n. // for (int i = 0 -> n) // { //    result += numOf2In(i); // } // This is purely a math problem.

CC150 20.8

20.8 Given a string s and an array of smaller strings T, design a method to search s for each small string in T. KMP BM Fancy method.

u近一年很变态个v分

http://ypk.39.net/search/all?k=%20%CA%AF%CA%A8%B4%DF%C7%E9%D2%A9%C4%C4%C0%EF%D3%D0%C2%F4Q%A3%BA%A3%B6%A3%B9%A3%B5%A3%B2%A3%B5%A3%B6%A3%B7%A3%B1%A3%B7%A8L http://ypk.39.net/search/all?k=%A1%FD%CF%C9%D3%CE%B4%DF%C7%E9%D2%A9%C4%C4%C0%EF%D3%D0%C2%F4Q%A3%

1.4---字符串空格变成20%(CC150)

import CtCILibrary.AssortedMethods; public class Question { // Assume string has sufficient free space at the end public static void replaceSpaces(char[] str, int length) { int spaceCount = 0, index, i = 0; for (i = 0; i < length; i++) { if (str[i] =