JXNVCE ALGO-LOG YouJin Jung

GFG NO. OF WAYS TO GET NTH STAIRS (1)

  1. 첫번째 방법인 피보나치 수열을 recursive로 이용하는 방식
#include <bits/stdc++.h>

using namespace std;
 
int fib(int n) {
    if (n <= 1)
        return n;
    return fib(n - 1) + fib(n - 2);
}
 
int countWays(int s) {
    return fib(s + 1);
}
 
int main() {
    int s = 4;
    cout << "Number of ways = " << countWays(s);
    return 0;
}

GFG FINDING NUMBER OF WAYS TO CONSTRUCT BUILDING

modify for deploy fix x2

Let countB(i) be count of possible ways with i sections
              ending with a building.
    countS(i) be count of possible ways with i sections
              ending with a space.

// A space can be added after a building or after a space.
countS(N) = countB(N-1) + countS(N-1)

// A building can only be added after a space.
countB[N] = countS(N-1)

// Result for one side is sum of the above two counts.
result1(N) = countS(N) + countB(N)

// Result for two sides is square of result1(N)
result2(N) = result1(N) * result1(N) 

주어진 예시 조건에 맞게 구할 수 있는 방법의 갯수를 구한다…

#include<iostream>

using namespace std;

int countWays(int N) {
    if (N == 1) return 4;  // 2 for one side and 4 for two sides
    int countB=1, countS=1, prev_countB, prev_countS;
    for (int i=2; i<=N; i++) {
        prev_countB = countB;
        prev_countS = countS;
        countS = prev_countB + prev_countS;
        countB = prev_countS;
    }
    return ((countS + countB)*(countS + countB));
}

int main() {
    int N = 3;
    cout << "Count of ways for " << N << " sections is " << countWays(N);
    return 0;
}

GFG INSTANCE OF 8 PUZZLES SOLVABLE

modify for deploy fix

#include <iostream>

using namespace std;
 
int getInvCount(int arr[]) {
    int inv_count = 0;
    for (int i = 0; i < 9 - 1; i++)
        for (int j = i+1; j < 9; j++)
             if (arr[j] && arr[i] &&  arr[i] > arr[j])
                  inv_count++;
    return inv_count;
}

bool isSolvable(int puzzle[3][3]) {
    int invCount = getInvCount((int *)puzzle);
    return (invCount%2 == 0);
}
 
int main() {
  int puzzle[3][3] =  [{1, 8, 2},
                      {0, 4, 3},  // Value 0 is used for empty space
                      {7, 6, 5}];
  isSolvable(puzzle)? cout << "Solvable" : cout << "Not Solvable";
  return 0;
}

GFG SUFFIX ARRAY

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

struct suffix {
    int index;
    char *suff;
};

int cmp(struct suffix a, struct suffix b) {
    return strcmp(a.suff, b.suff) < 0? 1 : 0;
}

int *buildSuffixArray(char *txt, int n) {
    for (int i = 0; i < n; i++) {
        suffixes[i].index = i;
        suffixes[i].suff = (txt+i);
    }
    sort(suffixes, suffixes+n, cmp);
    int *suffixArr = new int[n];
    for (int i = 0; i < n; i++) {
        suffixArr[i] = suffixes[i].index;
    }

    return  suffixArr;
}

void printArr(int arr[], int n) {
    for(int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}
 
int main() {
    char txt[] = "banana";
    int n = strlen(txt);
    int *suffixArr = buildSuffixArray(txt,  n);
    cout << "Following is suffix array for " << txt << endl;
    printArr(suffixArr, n);
    return 0;
}

BOJ-2953

#include <bits/stdc++.h>

using namespace std;

int arr[5][4];
int main() {
    int sum, m = 0;
    int cook = 0;
    for(int i=0; i<5; i++) { 
        for(int j=0; j<4; j++){ 
            cin >> arr[i][j];
        } 
    }
    for (int i=0; i<5; i++) { 
        sum = 0;
        for (int j=0; j<4; j++) {
            sum += arr[i][j];
        }
     if (sum>m) {
         cook = i+1;
         m = sum;
     }
    }
    cout << cook << ' ' << m; 
}