Skip to content

Instantly share code, notes, and snippets.

View shahril96's full-sized avatar
🐢

Mohd Shahril shahril96

🐢
View GitHub Profile
@shahril96
shahril96 / lis.cpp
Created June 11, 2015 04:20
POC of Longest Increasing Sub-Sequence with complexity O(mn)
/* References :
http://www.geeksforgeeks.org/dynamic-programming-set-3-longest-increasing-subsequence/
https://www.youtube.com/watch?v=4fQJGoeW5VE */
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
@shahril96
shahril96 / struct_qsort.c
Last active January 23, 2023 01:36
Example of sorting struct using qsort inside standard C
/* Performance of qsort, worst case O(n log n)
Example input :
3
4 9
-3 48
-100 48
Example output :
@shahril96
shahril96 / print_subsequences.c
Last active August 29, 2015 14:23
Print all string sub-sequences (or power-set in set theory) using binary mapping technique
/* example input-output
input :
abc
expected output :
1 =>
2 => a
3 => b
4 => ab
@shahril96
shahril96 / knapsack_problem.c
Created June 27, 2015 20:54
0-1 Knapsack problem solved using bottom-up fashion (tabulated dynamic programming)
#include <stdio.h>
#include <string.h>
#define max(X,Y) ((X) > (Y)) ? (X) : (Y)
typedef struct item
{
int value;
int weight;
} Item;
@shahril96
shahril96 / pi.c
Created July 26, 2015 05:34
Implementation of Chudnovsky's algorithm to calculate approximation of Pi using C
#include <stdio.h>
#include <math.h>
#define SIZE 100
unsigned __int128 fact(int N) // calculate factorial with O(n)
{
static unsigned __int128 memo[SIZE] = {-1};
if(memo[N] > -1) return memo[N];
else return memo[N] = (N <= 1) ? 1 : fact(N-1) + fact(N-2);
@shahril96
shahril96 / fact128.c
Last active October 18, 2022 18:41
Factorial using __int128 integer
#include <stdio.h>
typedef unsigned __int128 uint128;
/* printing 128-bit value isn't official support yet
so here's implementation of converting 128-bit value into string */
char *str_uint128(uint128 n)
{
/* got confused about arithmetic operation with '0'? read more about ascii :) */
static char buf[1000] = {0}, *buft = buf;
@shahril96
shahril96 / lcs.py
Last active October 2, 2015 16:48
My first attempt toward Python language by implementing Longest Common Sub-sequence algorithm
# recursive method
def lcs(a, b):
if(len(a) <= 0 or len(b) <= 0):
return 0
elif(a[-1] == b[-1]):
return lcs(a[:-1], b[:-1]) + 1
@shahril96
shahril96 / secret.c
Last active October 4, 2015 07:43
Secret Code generator programming task (Universiti Malaysia Pahang)
#include <stdio.h>
int main()
{
char D[2], M[2], Y[4], name[100]; // char string
printf("Please enter your birthdate (DDMMYYYY): ");
scanf("%2s%2s%4s", D, M, Y);
printf("Please enter your name : ");
@shahril96
shahril96 / sumOfPower.java
Last active October 17, 2015 18:46
Sum of Power (ACM ICPC Al-Khawarizmi 2011) O(1) solution
import java.math.BigInteger;
import java.util.Scanner;
public class i
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int T, N, K;
@shahril96
shahril96 / heun.cpp
Created October 23, 2015 15:50
Heun's method for computing ordinary differential equation
#include <iostream>
using namespace std;
// cara nk buat function biasa
// return = output calculation tu
double f(double x, double y)
{
return 2 * x * y;
}