Showing posts with label sort. Show all posts
Showing posts with label sort. Show all posts

Monday, October 26, 2015

Leetcode: H-Index

Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
Show Hint 
    Credits:
    Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
    Hide Tags
     Hash Table Sort
    Hide Similar Problems
     (M) H-Index II
















    -----------------------------------------------------------
    ----------------------------------------------------------
    Sort and check, that's all for a convenient way. 
    To reduced the time, we don't need to sort the whole array, we can sort one then check one. E.g.,when using bubble sort, we can find out the minimal one then check the h value, find the next minimal value then check the h value, ......., until the real h is found. If no is found, then h=0. The testing results highly depend on the testing case. 
    e.g. 
    1    2    2     3     3   citations 
    5   4     3     2     1   potential H index

    check from left to right, when citation[i]>=potential[i], return length-i. 

    OR
    e.g. 
    1    2    2     3     3   citations 
    1   2     3     4     5   potential H index

    check from right to left, when citation[i]>=potential[i], return i. 



    ///////////////////////////////////////////////
    //code
    class Solution {
    public:
           int hIndex(vector<int>& citations) {
                  //check input
                  int len = citations.size();
                  //if (len==0) return 0;
                  //bubble sort and judge
                  for (int i = 0; i<len; i++){
                         for (int j = len - 1; j>i; j--){
                               if (citations[j]<citations[j - 1]) swap(citations[j], citations[j - 1]);
                         }
                         //judge h
                         if (citations[i] >= len - i)return len - i;
                  }
                  return 0;
           }
    };



    Sunday, October 25, 2015

    Leetcode: Reorder List

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,
    reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
    You must do this in-place without altering the nodes' values.
    For example,
    Given {1,2,3,4}, reorder it to {1,4,2,3}.
    Hide Tags
     Linked List








    -----------------------------------------------------------------
    As the pointer of list can't move backward, double pointers method scan from two end to the center doesn't work. So we need to think about way to do it with an forward direction. Following is one of such method:
    1. locate the middle of the list: 
        there are two kinds, odd and even number of nodes, the middle one should be calculated differently. 
    2. reverse the order for all the nodes in right side of middle node. For how to reverse the whole linked list, check there. 
    3. merge the list with forward order. 
    when number of node is even, the procedure is shown below:

    when number of node is odd, the procedure is shown as:



    ////////////////////////////////////////////////
    ////code
    class Solution {
    public:
           void reorderList(ListNode* head) {
                  //check input
                  if (head == NULL || head->next == NULL || head->next->next == NULL) return;
                  //find the middle and final location
                  int len = 1, midLen = 0;
                  ListNode *mid = head, *final = head, *tmp = head, *left = head;
                  while (mid->next != NULL) { len++; mid = mid->next; }
                  final = mid;
                  mid = head;
                  //locate the mid
                  midLen = (len % 2) == 1 ? len / 2 + 1 : len / 2;
                  for (int i = 1; i<midLen; i++){
                         mid = mid->next;
                  }
                  tmp = mid;

                  //reverse the right half list
                  ListNode* cur = tmp->next;
                  ListNode* pre = cur->next;
                  cur->next = NULL;//the final node after reverse
                  while (pre != NULL)
                  {
                         ListNode* temp = pre->next;
                         pre->next = cur;
                         cur = pre;
                         pre = temp;
                  }
                  tmp->next = cur;

                  //reorder the list
                  mid = tmp;
                  for (int i = 1; i <= len - midLen; i++){
                         tmp = left->next;
                         left->next = mid->next;
                         mid->next = mid->next->next;
                         left->next->next = tmp;
                         //update pointers
                         left = left->next->next;
                         if (left == mid)break;
                  }
                  return;
           }
    };






    Thursday, October 22, 2015

    Leetcode: Sort List

    Problem:

    Sort a linked list in O(n log n) time using constant space complexity.
    Hide Tags
     Linked List Sort
    Show Similar Problems




    --------------------------------------------
    1. O(nlogn) sort:  only merge sort, heap sort and quick sort can achieve this goal. But all of them are using recursion, and therefore they are using non-constant space. So we need to do some modifications on them, e.g., we can use iteration to take place the recursion. 
    For quick sort, the iteration doesn't work. But for merge sort, it's possible to use recursion. the general idea can be seen below:
    we can do recursion one by one in each interval loop. 
    In the original merge sort algorithm, when merge two halves into one, we need to create the third vector to temporally store the merged values. This will introduce extra space usage and not constant space. Here we using a method to do merging two halves into one without introducing an additional space: 
    here we use an example when interval=2:



    /////////////////////////////////////////////////////
    //codes
    class Solution {
    public:
           ListNode* sortList(ListNode* head) {
                  //check input
                  if (head == NULL || head->next == NULL)return head;
                  //dummy node
                  ListNode *dum = new ListNode(-1);
                  dum->next = head;
                  ListNode *upHead = dum, *downHead = dum;
                  ListNode *tmp = NULL;
                  //get the number of nodes
                  int len = 0;
                  while (head != NULL){
                         len++;
                         head = head->next;
                  }
                  //outter loop for sort with diff. intervals
                  for (int interval = 1; interval < len; interval = interval * 2){
                         //clear pointers
                         upHead = dum;
                         downHead = dum;

                         //locate the downHead for the first run
                         for (int j = 0; j < interval; j++){
                               downHead = downHead->next;
                         }
                         //check loop stop condition
                         if (downHead->next == NULL)break;
                         //inner loop
                         for (int i = 1;; i++){
                               //check loop stop condition
                               if (downHead->next == NULL)break;
                               int countDownHead = 0;
                               //sort the two half lists
                               while (countDownHead<interval){
                                      //check loop stop condition
                                      if (downHead->next == NULL || upHead == downHead)break;

                                      if (upHead->next->val>downHead->next->val){
                                             tmp = upHead->next;
                                             upHead->next = downHead->next;
                                             downHead->next = downHead->next->next;
                                             upHead->next->next = tmp;

                                             //update upNode
                                             upHead = upHead->next;
                                             countDownHead++;//count downHead was moved for how many times
                                      }
                                      else{
                                             upHead = upHead->next;
                                             //if (upHead == downHead)break;
                                      }
                               }


                               //locate the downHead to the end of previous run
                               for (int j = 0; j < interval - countDownHead; j++){
                                      if (downHead == NULL || downHead->next == NULL)break;
                                      downHead = downHead->next;
                               }
                               upHead = downHead;
                               //locate the downHead to the begin of next run
                               for (int j = 0; j < interval; j++){
                                      if (downHead == NULL || downHead->next == NULL)break;
                                      downHead = downHead->next;
                               }
                         }
                  }
                  head = dum->next;
                  delete dum;
                  return head;
           }
    };