Skip to content

Instantly share code, notes, and snippets.

View FindTheMin.cpp
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a) FOR(i, 0, a)
#define all(a) a.begin(), a.end()
#define SORT(a) sort(all(a))
View BeautifulStrings.cpp
#include <algorithm>
#include <iostream>
#include <vector>
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a) FOR(i, 0, a)
#define VI vector<int>
#define all(a) (a).begin(), (a).end()
using namespace std;
@krisys
krisys / BalancedSmileys.cpp
Created Jan 30, 2013
Balanced Smileys - At any index, the number of closing brackets should be less than or equal to the number of opening brackets, if it is less, then the difference of closing brackets and opening brackets should be equal to the number of happy smileys. And a similar logic should be applied from the end considering the number of sad smileys.
View BalancedSmileys.cpp
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a) FOR(i, 0, a)
#define all(a) a.begin(), a.end()
@krisys
krisys / BadNeighbors.cpp
Last active May 24, 2020
Bad Neighbors TopCoder
View BadNeighbors.cpp
/* Update - Thanks to some folks who pointed out a flaw in my code.
* I have updated it. Hope it is correct now :-)
* Old version can be found here -
https://gist.github.com/krisys/4089748/262cbc10d9b9f1cb5df771e14a1e143a86d2ecc6/
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
@krisys
krisys / LIS.cpp
Last active Oct 12, 2015
Longest Increasing sequence
View LIS.cpp
#include <iostream>
#include <algorithm>
using namespace std;
int a[] = {4, 3, 1 ,2, 8, 3, 4};
// int a[] = {1, 6, 2 ,3, 4, 5, 7};
// int a[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
// int a[] = {5,4,6,3,7};
int N = sizeof(a)/sizeof(a[0]);
@krisys
krisys / zigzag.cpp
Created Nov 16, 2012
ZigZag Sequence - TopCoder
View zigzag.cpp
#include <iostream>
#include <vector>
#define SIZE 50
#define debug(x) {cout << #x << " : " << x << endl ;}
using namespace std;
int main(){
int a[SIZE] = {396, 549, 22, 819, 611, 972, 730, 638, 978, 342, 566, 514, 752,
871, 911, 172, 488, 542, 482, 974, 210, 474, 66, 387, 1, 872, 799,
@krisys
krisys / dbEngine.py
Created Apr 2, 2012
Script to convert the DB Engine
View dbEngine.py
import MySQLdb
host = ''
user = ''
passwd = ''
db = ''
# setup the connection
conn = MySQLdb.connect (host = host, user = user, passwd = passwd, db=db)
cursor = conn.cursor()
@krisys
krisys / rendered-html.html
Created Jan 28, 2012
template after rendering
View rendered-html.html
<ul>
<li>
<div> A </div>
<div> Statistics </div>
<ul>
<li> A scored 10 runs </li>
<li> A scored 20 runs </li>
<li> A scored 30 runs </li>
</ul>
</li>
@krisys
krisys / django-template.html
Created Jan 28, 2012
How template is rendered.
View django-template.html
<ul>
{% for p in players %}
<li>
<div> {{ p.name }} </div>
<div> Statistics </div>
<ul>
{% for g in p.games %}
<li> {{p}} scored {{g.runs}} runs </li>
{% endfor %}
</ul>
View django-mvc.py
# models.py
class Person(models.Model):
firstname = models.CharField(max_length=30)
lastname = models.CharField(max_length=30)
# views.py
def list_view(request):
person_list = Person.objects.all()