leetcode

leetcode solutions for entertainment value
Log | Files | Refs

2144.c (689B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 int cmp(const void* a, const void* b) {
      5 	return *((int*)b)-*((int*)a);
      6 }
      7 
      8 int minimumCost(int* cost, int costSize) {
      9     qsort(cost, costSize, sizeof(int), cmp);
     10     int sum=0;
     11     for (int i=0; i<costSize; i++) {
     12     	if ((i+1)%3>0) {
     13     		int cc = cost[i];
     14     		sum+=cc;
     15     	}
     16     }
     17     return sum;
     18 }
     19 
     20 void test(int* cost, int costSize, int exp) {
     21 	int act = minimumCost(cost, costSize);
     22 	if (act != exp) {
     23 		fprintf(stderr, "test failed! exp %d act %d\n", exp, act);
     24 	}
     25 }
     26 
     27 int main() {
     28 	{
     29 		int cost[] = {1, 2, 3};
     30 		int costSize = 3;
     31 		test(cost, costSize, 5);
     32 	}
     33 	{
     34 		int cost[] = {6,5,7,9,2,2};
     35 		int costSize = 6;
     36 		test(cost, 6, 23);
     37 	}
     38 }