We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Kotlin code with some details which helped me understand the logic.
funcountSort(arr:Array<Array<String>>):Unit{// Find the min and max values of the associated integers.//// Will be min=0 max=6 from the sample input.varmin=Int.MAX_VALUEvarmax=Int.MIN_VALUEarr.forEach{valnum=Integer.parseInt(it[0])if(num<min)min=numif(num>max)max=num}// Create a count array for counting occurrences of each// associated integer.// Note associated integers might not be 0-indexed so// work out minimum number of spaces required.//// From the sample: max - min + 1 = 6 - 0 + 1 = 7// An array of 7 items will fit each value count (0-6).valcountArray=IntArray(max-min+1)// Go through the input array and count how many occurrences// of each associated integer there are.//// From the sample this is:// 0=6, 1=2, 2=2, 3=1, 4=4, 5=1, 6=4arr.forEach{valnum=Integer.parseInt(it[0])countArray[num-min]++}// Now update the count array to have running totals from left// to right.//// So add the first count to the second, the first 2 counts to// the third, and so on...//// From the sample this is:// 0=6, 1=8, 2=10, 3=11, 4=15, 5=16, 6=20for(indexin1untilcountArray.size){countArray[index]+=countArray[index-1]}// Create the output array. Note it's the same size as the// input.// Fill with dashes by default to save filling them later.valoutput=Array<String>(arr.size){"-"}// This is where everything comes together.//// Loop the input array from the end to the middle. // Start at the end to ensure order is retained (stability).// Finish in the middle as we're told to have dashes for the// first half.for(indexinarr.size-1downToarr.size/2){// Get the associated integer for the item.valnum=Integer.parseInt(arr[index][0])// Calculate where the item goes in the output array.// This is done by looking in the countArray.//// Taking the sample input, the last item is [4, "the"].// So 4 - 0 - 1 = 3// countArray[3] is 11 (see above).valpos=countArray[num-min]-1// Set position in the output array to the value.// Following the example [4, "the"] this will set position// 11 in the output array to "the".output[pos]=arr[index][1]// Decrease the count of the associated integer so next // time the value is place in the correct place as we're// working backwards.countArray[num-min]--}// Print out the output array.output.forEach{print("$it ")}println("")}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Full Counting Sort
You are viewing a single comment's thread. Return to all comments →
Kotlin code with some details which helped me understand the logic.