Skip to content

Instantly share code, notes, and snippets.

View trhgquan's full-sized avatar

Hoang-Quan, Tran trhgquan

View GitHub Profile
/*
* https://github.com/trhgquan
*/
#include <stdio.h>
int PRIME[2000004];
void generatePrimes() {
for (int i = 2; i <= 2000004; ++i)
PRIME[i] = 1;
@trhgquan
trhgquan / P166PROH.c
Created October 15, 2019 06:53
For more algorithms, visit https://github.com/trhgquan/CPP
/*
* https://www.spoj.com/PTIT/problems/P166PROH/
* Code by @trhgquan - https://github.com/trhgquan
*/
#include <stdio.h>
int PRIME[4000];
// Because maximum n is 3000, so prime list limit is bigger than 3000.
void generatePrimes() {
@trhgquan
trhgquan / CDRSANJ.c
Created October 15, 2019 12:53
For more algorithm, visit https://github.com/trhgquan/CPP
/*
* CDRSANJ - https://www.spoj.com/problems/CDRSANJ/
* Code by @trhgquan - https://github.com/trhgquan
*/
#include <stdio.h>
int f(int x) {
if (x == 0 || x == 1 || x == 2)
return x;
/*
* https://www.spoj.com/problems/NUB04/
* Code by @trhgquan - https://github.com/trhgquan
*/
#include <iostream>
#define lli long long int
bool isPrime(lli a) {
@trhgquan
trhgquan / MaximumNonAdj.cpp
Last active October 20, 2019 03:21
For more algorithm, visit https://github.com/trhgquan/CPP
int max(int a, int b) {
return (a > b) ? a : b;
}
int MaximumNonAdj(vector<int> adj) {
int ret = INT_MIN;
adj.insert(adj.begin(), 0);
vector<int> F(adj.size(), 0);
F[1] = max(adj[1], 0);
def maximumNonAdj(arr):
arr.insert(0, 0)
brr = [0] * len(arr)
brr[1] = arr[1]
m = -100000000
for i in range(1, len(arr)):
brr[i] = max(arr[i] + brr[i - 2], brr[i - 1])
if (brr[i] > m): m = brr[i]
return m
@trhgquan
trhgquan / longestArrays.cpp
Last active October 20, 2019 03:42
For more algorithm, visit https://github.com/trhgquan/CPP
/*
* longestArrays.cpp - Dynamic Programming approach.
* Code by @trhgquan - https://github.com/trhgquan
*/
int longestArrays(vector<int> a) {
vector<int> b(a.size(), 1);
int res = 1;
for (unsigned i = 1; i < a.size(); ++i){
if (a[i] >= a[i - 1]) b[i] = b[i - 1] + 1;
res = max(b[i], res);
@trhgquan
trhgquan / root.cpp
Created October 20, 2019 07:24
For more algorithm, visit https://github.com/trhgquan/CPP
int root(int n) {
if (n >= 10) {
int s = 0;
while (n > 0) {
s += n % 10;
n /= 10;
}
return root(s);
}
return n;
@trhgquan
trhgquan / SMPAIR.cpp
Last active October 27, 2019 13:45
For more algorithm, visit https://github.com/trhgquan/CPP
/**
* SMPAIR - https://codechef.com/problems/SMPAIR
* Code by @trhgquan - https://github.com/trhgquan
*/
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
@trhgquan
trhgquan / PAIRS1.cpp
Created October 24, 2019 04:07
For more algorithm, visit https://github.com/trhgquan/CPP
/**
* [Tutorial] PAIRS1 - Count the pairs
* https://www.spoj.com/problems/PAIRS1/
* Code by @trhquan - https://github.com/trhgquan
* Solution: sorting + binary search
*/
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;