Skip to content

Instantly share code, notes, and snippets.

View cwcai633's full-sized avatar

Chenwei Cai cwcai633

  • Google
  • MTV
View GitHub Profile
@cwcai633
cwcai633 / coinProb.cpp
Created November 30, 2016 04:39
N coins with different probabilities. Get the probability of K heads.
#include <iostream>
#include <vector>
using namespace std;
double coinProbK(vector<double>& c, int k) {
int n = c.size();
vector<vector<double> > dp(n + 1, vector<double>(k + 1, 0.0));
dp[0][0] = 1.0;
for (int i = 1; i <= n; i++) {
dp[i][0] = dp[i-1][0] * (1 - c[i-1]);
@cwcai633
cwcai633 / strStr.cpp
Created November 30, 2016 04:38
strStr with KMP
// https://www.youtube.com/watch?v=GTJr8OvyEVQ
#include <iostream>
#include <vector>
using namespace std;
vector<int> KMPpreprocessing(string needle) {
int len = needle.size();
vector<int> match(len, 0);
int j = 0;
for (int i = 1; i < len; i++) {
@cwcai633
cwcai633 / one.cpp
Created November 30, 2016 04:37
Number of digit 1s before number n
#include <iostream>
#include <math.h>
using namespace std;
class Solution {
public:
int one(int num) {
if (num / 10 == 0) {
return num == 0 ? 0 : 1;
}
@cwcai633
cwcai633 / findLexMin.cpp
Created November 30, 2016 04:36
Find a subsequence with length k and is smallest in lexicographical order
#include <iostream>
#include <string>
using namespace std;
string findMin(string s, int k) {
string res = "";
int len = s.size();
for (int i = 0; i < len; i++) {
while (!res.empty() and res.back() > s[i] and len - i + res.size() > k) {
res.pop_back();
@cwcai633
cwcai633 / minInsertPalindrome.cpp
Created November 30, 2016 04:33
Mininum Insertion for a Palindrome
#include <vector>
#include <iostream>
#include <string>
using namespace std;
int minInsert(string s) {
int i = 0;
int j = s.size() - 1;
int cnt1 = 0;
@cwcai633
cwcai633 / mnist.py
Created January 11, 2016 02:57 — forked from akesling/mnist.py
import os
import struct
import numpy as np
"""
Loosely inspired by http://abel.ee.ucla.edu/cvxopt/_downloads/mnist.py
which is GPL licensed.
"""
def read(dataset = "training", path = "."):