JXNVCE ALGO-LOG YouJin Jung

GFG-PERMUTATION-COEFFICIENT

시간복잡도 O(n) study material from Geeks for Geeks

#include<bits/stdc++.h>
using namespace std;

int permutationCoeff(int n, int k) {
    int fact[n + 1];
    fact[0] = 1;
 
    for(int i = 1; i <= n; i++)
      fact[i] = i * fact[i - 1];
 
    // P(n,k) = n! / (n - k)!
    return fact[n] / fact[n - k];
}
 
int main() {
    int n = 10, k = 6;
    cout << "Value of P(" << n << ", " << k << ") is: " << permutationCoeff(n, k);
    return 0;
}

GFG-BINOMIAL-COEFFICIENT

Study log of Binomial Coeffiecent using memoization method technique from Geeks for Geeks DP

#include <bits/stdc++.h>
using namespace std;

int binomialCoeffUtil(int n, int k, int** dp) {
    if (dp[n][k] != -1)
        return dp[n][k];
    if (k == 0) {
        dp[n][k] = 1;
        return dp[n][k];
    }
    if (k == n) {
        dp[n][k] = 1;
        return dp[n][k];
    }
    dp[n][k] = binomialCoeffUtil(n - 1, k - 1, dp) + binomialCoeffUtil(n - 1, k, dp);
    return dp[n][k];
}
 
int binomialCoeff(int n, int k) {
    int** dp;
    dp = new int*[n + 1];
 
    for (int i = 0; i < (n + 1); i++) {
        dp[i] = new int[k + 1];
    }

    for (int i = 0; i < (n + 1); i++) {
        for (int j = 0; j < (k + 1); j++) {
            dp[i][j] = -1;
        }
    }
    return binomialCoeffUtil(n, k, dp);
}

int main() {
    int n = 5, k = 2;
    cout << "Value of C(" << n << ", " << k << ") is "
         << binomialCoeff(n, k) << endl;
    return 0;
}

P.S.

10/1 It is my First day of internship!🐻

Below memes show my emotions today

yay

yay

yay

yay

GFG-FIBONACCI-NUMBERS

recursion 이 아닌 DP를 활용한 Fibonacci 수열

#include<bits/stdc++.h>
using namespace std;
 
int fib(int n) {
    int f[n + 2];
    int i;

    f[0] = 0;
    f[1] = 1;
 
    for(i = 2; i <= n; i++) {
       f[i] = f[i - 1] + f[i - 2];
    }
    return f[n];
}

int main() {
    int n;
    cin >> n;
    cout << "fibonacci of n:" << fib(n);
    return 0;
}
 

GFG-UGLY-NUMBERS

Geeks for Geeks DP practice basic 1

#include <iostream>
using namespace std;
 
int maxDivide(int a, int b) {
    while (a % b == 0)
        a = a / b;
    return a;
}
 
int isUgly(int no) {
    no = maxDivide(no, 2);
    no = maxDivide(no, 3);
    no = maxDivide(no, 5);
 
    return (no == 1) ? 1 : 0;
}

int getNthUglyNo(int n) {
    int i = 1;
    int count = 1;

    while (n > count) {
        i++;
        if (isUgly(i))
            count++;
    }
    return i;
}

int main() {
    unsigned no = getNthUglyNo(150);
    cout << "150th ugly no. is " << no;
    unsigned no = getNthUglyNo(100);
    cout << "100th ugly no. is " << no;
    return 0;
}

BOJ-14725

트라이에 대한 접근을 어떤식으로 해야할 줄 몰라서, 구글링으로 공부했다…!!

출처: 어흥님 풀이 - https://imnotabear.tistory.com/312

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>

using namespace std;

struct Node{
  map<string, Node*> child;
  map<string, Node*>::iterator iter;

  void insert(vector<string> v, int idx) {
    if (idx == v.size()) return;
    string ss = v[idx];
    iter = child.find(ss);
    if (iter != child.end()) iter->second->insert(v, idx + 1);
    else {
      Node* n = new Node;
      child.insert({ ss,n });
      n->insert(v, idx + 1);
    }
  }

  void print(int idx) {
    if (child.empty()) return;
    for (auto it = child.begin(); it != child.end(); it++) {
      for (int i = 0; i < idx; i++)
        cout << "--";
      cout << it->first << '\n';
      it->second->print(idx + 1);
    }
  }
};

Node tmp;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  
  int num, val;
  Node* root = new Node;
  string ss;
  cin >> num;
  for (int i = 0; i < num; i++) {
    cin >> val;
    vector<string> v;
    for (int j = 0; j < val; j++) {
      cin >> ss;
      v.push_back(ss);
    }
    root->insert(v, 0);
  }
  root->print(0);
  return 0;
}