發表文章

String Manipulation - Strings: Making Anagrams [Easy]

  Example Delete   from   and   from   so that the remaining strings are   and   which are anagrams. This takes   character deletions. static   int  makeAnagram(String a, String b) {        if  (a.length() ==  0  ||b.length() ==  0 ) {          return  a.length() ==  0  ? b.length() : a.length();     }      char [] firstChars = a.toCharArray();      char [] secondChars = b.toCharArray();      int [] firstHelper =  new   int [ 26 ];      int [] secondHelper =  new   int [ 26 ];      int  aVal = ( int )  'a' , sum =  0 ;  ...

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;     }

Arrays - Minimum Swaps 2 [M]

 Sample Input 0 4 4 3 1 2 Sample Output 0 3 Explanation 0 Given array  After swapping   we get  After swapping   we get  After swapping   we get  So, we need a minimum of   swaps to sort the array in ascending order. static   int  minimumSwaps( int [] arr) {        int  arrLen = arr.length;          int  count =  0 ;          int  [] sarr = arr.clone();         Arrays.sort(sarr);                   for  ( int  i =  0 ; i < arrLen; i++) {              if  (arr[i] != sarr[i]) {     ...

Arrays - Left Rotation [Easy]

  A   left rotation   operation on an array shifts each of the array's elements     unit to the left. For example, if     left rotations are performed on array   , then the array would become   . Note that the lowest index item moves to the highest index in a rotation. This is called a   circular array . Given an array   of   integers and a number,  , perform   left rotations on the array. Return the updated array to be printed as a single line of space-separated integers. Sample Input 5 4 1 2 3 4 5 Sample Output 5 1 2 3 4   static   int [] rotLeft( int [] a,  int  d) {   int [] arr =  new   int [a.length];          int  N = a.length;          for  ( int  i =  0  ; i < N;  i++){   ...