Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Sunday, October 4, 2020

Sunday, September 27, 2020

Coin Change Problem

 #include <bits/stdc++.h>

using namespace std;

vector<string> split_string(string);

// recursive

int count(vector<int> &coins, int m, int n) {

    if (n == 0) 

        return 1;

    if (n < 0)

        return 0;

     if (m <= 0 && n >= 1)

        return 0;

    return count(coins, m-1, n) + count(coins, m, n-coins[m-1]);

}

// recursive with memoization

unsigned long dp[51][251];// global array initialized to 0 

unsigned long countDP1(vector<int> & coins,  int i, int n)

{

    if(n == 0)

        return 1; //no coins

    if(n < 0) 

        return 0; //no negative money      

    if(i == coins.size()) //no more options of coins

        return 0;

    if(dp[i][n]!=0)

        return dp[i][n];

        //leave it or take it

    return dp[i][n] = countDP1(coins,i+1, n) + countDP1(coins,i, n-coins[i]);

}


//loop with 2D memoization space O(n*m) time O(n*m)

unsigned long countDP2(vector<int> &coins, int m, int n ) 

    int i, j; 

    unsigned long table[n + 1][m],  x, y; 

    for (i = 0; i < m; i++) 

          table[0][i] = 1;  

     for (i = 1; i < n + 1; i++) 

    { 

        for (j = 0; j < m; j++) 

        { 

            // Count of solutions including coins[j] 

            x = (i-coins[j] >= 0) ? table[i - coins[j]][j] : 0; 

            // Count of solutions excluding coins[j] 

            y = (j >= 1) ? table[i][j - 1] : 0; 

            // total count 

            table[i][j] = x + y; 

        } 

    } 

    return table[n][m - 1]; 

}

// loop with 1D memoization space O(n) time O(n*m)

unsigned long countDP( vector<int> &coins, int m, int n ) 

      unsigned long table[n + 1]; 

    memset(table, 0, sizeof(table)); 

      table[0] = 1; 

      for(int i=0; i< m; i++) 

        for(int j=coins[i]; j <= n; j++) 

            table[j] += table[j-coins[i]]; 

      return table[n]; 


// Complete the ways function below.

unsigned long ways(int n, vector<int> coins) {

    return countDP(coins, coins.size(), n);

    //return countDP1(coins, 0, n);

}


int main()

{

    ofstream fout(getenv("OUTPUT_PATH"));

    string nm_temp;

    getline(cin, nm_temp);

    vector<string> nm = split_string(nm_temp);

    int n = stoi(nm[0]);

    int m = stoi(nm[1]);

    string coins_temp_temp;

    getline(cin, coins_temp_temp);

    vector<string> coins_temp = split_string(coins_temp_temp);

    vector<int> coins(m);

    for (int i = 0; i < m; i++) {

        int coins_item = stoi(coins_temp[i]);

        coins[i] = coins_item;

    }

    unsigned long res = ways(n, coins);

    fout << res << "\n";

    fout.close();

    return 0;

}

vector<string> split_string(string input_string) {

    string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {

        return x == y and x == ' ';

    });

    input_string.erase(new_end, input_string.end());

    while (input_string[input_string.length() - 1] == ' ') {

        input_string.pop_back();

    }

    vector<string> splits;

    char delimiter = ' ';

    size_t i = 0;

    size_t pos = input_string.find(delimiter);


    while (pos != string::npos) {

        splits.push_back(input_string.substr(i, pos - i));

        i = pos + 1;

        pos = input_string.find(delimiter, i);

    }

    splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));

    return splits;

}


Saturday, June 6, 2020

Struct with defined number of bits for each member in C

typedef struct _bitfield {
     unsigned one_bit0:1;
     unsigned one_bit1:1;
     unsigned two_bits:2;
     unsigned four_bits:4;
} BITFIELD;


#include
#include

typedef struct _xpto {
int x;
struct {
int p;
unsigned t:1;
}PT;
char o;

} XPTO;

union {
int result;
char byte[2];
struct {
unsigned b0:1;
unsigned b1:1;
unsigned b2:1;
unsigned b3:1;
unsigned b8:28;
} ST;
} adc;

int main(int argc, char *argv[]) {
XPTO myxpto = { .x=10, .PT = { .p=2, .t=1 }, .o='A'};
printf(".x = %d, .PT { .p = %d, .t = %d }, .o = %c\n",myxpto.x,myxpto.PT.p,myxpto.PT.t,myxpto.o);

adc.byte[0] = 1;
adc.byte[1] = 2;
printf("%x\n", adc.result);
adc.ST.b0 = 0;
printf("%x\n", adc.result);
return 0;
}

Overflow problem - Curious case case of Binary Search

https://thebittheories.com/the-curious-case-of-binary-search-the-famous-bug-that-remained-undetected-for-20-years-973e89fc212

When calculating the mid position in the array

This operation can result in a overflow problem when low + high is too much high

mid = (low + high) / 2;

Replacing by this operation we no longer have the same problem
mid = low + (high - low)/2;


.............................................
Other example of overflow problem
int x = 256;
int y = 256;

unsigned long  result;
result = x * 256 * 256 + y*256*256;
the result of the operation will be calculated with size of the highest operand that is integer, and 256 * 256 is already the maximum value of a integer, multiplied by an integer with value 256, the result will not fit in Integer space.

To fix this, we increase the size of the highest operando
result = x * 256 * 256ul + y*256*256ul;
or
result = ((unsigned long)x) * 256 * 256 + ((unsigned long)y)*256*256;



Blog Archive