發表文章

目前顯示的是有「Sorting」標籤的文章

Sorting - Fraudulent Activity Notifications [M]

Key: 中位數, 所有數字由低到高排序後,取中間的數  For example,  and . On the first three days, they just collect spending data. At day , we have trailing expenditures of . The median is  and the day's expenditure is . Because , there will be a notice. The next day, our trailing expenditures are  and the expenditures are . This is less than  so no notice will be sent. Over the period, there was one notice sent. Sample Input 0 9 5 2 3 4 2 3 6 8 4 5 Sample Output 0 2 static   int  activityNotifications( int [] expenditure,  int  d) {        int  notificationCount =  0 ;          int [] data =  new   int [ 201 ];          for  ( int  i =  0 ; i < d; i++) {             data[expenditure[i]]++;   ...

Sorting - Comparator [M]

 Sample Input 5 amy 100 david 100 heraldo 50 aakansha 75 aleksa 150 Sample Output aleksa 150 amy 100 david 100 aakansha 75 heraldo 50 Explanation The players are first sorted descending by score, then ascending by name. class  Checker  implements  Comparator<Player> {      // complete this method      public   int  compare(Player a, Player b) {          if (a.score>b.score)  return  - 1 ;          else   if (a.score<b.score)  return   1 ;          else   return  a.name.compareTo(b.name);          } } public   class  Solution {      public   static   void  main(String[] args) {       ...

Sorting - Mark and Toys [Easy]

用有限的錢, 買最多玩具  Sample Input 7 50 1 12 5 111 200 1000 10 Sample Output 4 Explanation He can buy only   toys at most. These toys have the following prices:  . static   int  maximumToys( int [] prices,  int  k) {        Arrays.sort(prices);           for ( int  i =  0 ; i < prices.length; i++){           k-=prices[i];             if (k <  0 )  return  i;           }           return  prices.length;     }

Sorting - Bubble Sort [Easy]

 https://ithelp.ithome.com.tw/articles/10226353 Sample Input 0 STDIN Function ----- -------- 3 a[] size n = 3 1 2 3 a = [1, 2, 3] Sample Output 0 Array is sorted in 0 swaps. First Element: 1 Last Element: 3 Explanation 0 The array is already sorted, so   swaps take place. Sample Input 1 3 3 2 1 Sample Output 1 Array is sorted in 3 swaps. First Element: 1 Last Element: 3 Explanation 1 The array is  not sorted , and its initial values are:  . The following   swaps take place: At this point the array is sorted and the three lines of output are printed to stdout. static   void  countSwaps( int [] a) {        int  temp;          int  totalswap= 0 ;          for ( int  i= 0 ;i<a.length;i++)         {             ...