Divide and conquer
The maximum subarray problem
Brute-force: just try every possible pair of buy and sell dates in which buy dates precedes the sell dates
then find the maximum. O(n2)
Divide and conquer
Linear method
/* | |
* Magic code to print china map. | |
* The original idea is from International Obfuscated C Code test, check more here. | |
* http://stackoverflow.com/questions/3533348/how-does-this-code-generate-the-map-of-india | |
*/ | |
#include <stdio.h> | |
int main(){int a,b,c; for (b = c = 10; (a = | |
"-FIGURE?,China *CJ Hello Folks,\ | |
b&M\"D(G*>/#\";,B1:0=12<924;554D\ | |
*76`<W#!?UEZ>YCWEWFTJOP$%CXAX@Z?\ |
#include <stdio.h> | |
//duff's device | |
void duffs_device(const char *src, int length, char *dst) { | |
int n = (length + 7) / 8; | |
switch (length % 8) { | |
case 0: do { *dst++ = *src++; | |
case 7: *dst++ = *src++; | |
case 6: *dst++ = *src++; | |
case 5: *dst++ = *src++; |
/* | |
* check if a string is within a given levenshtein distance to any string in a dictionary | |
* credit: Leo Polovets | |
* see more on quora: https://www.quora.com/Algorithms/Which-is-the-best-programming-algorithm-that-you-have-ever-created | |
*/ | |
#include <iostream> | |
#include <string> | |
#include <algorithm> | |
#include <cstdlib> |
/* the Sieve of Eratosthenes */ | |
#include <cmath> | |
#include <vector> | |
using namespace std; | |
bool[] findPrimes(int n) { | |
vector<bool> isPrimes(n + 1, true); | |
isPrimes[0] = false; | |
isPrimes[1] = false; |
#include <algorithm> | |
using namespace std; | |
struct people { | |
int s; | |
int b; | |
int i; | |
bool operator<(const people &o) const { |
bool isPerfectSquare(int x) { | |
int sqr = Math.sqrt(x); | |
return x == sqr * sqr; | |
} | |
bool isPerfectSquareFaster(int x) { | |
if (x < 0) return false; | |
int h = x & 0xf; | |
if (h == 0 || h == 0x1 || h == 0x4 || h == 0x9) { |
Divide and conquer
The maximum subarray problem
Brute-force: just try every possible pair of buy and sell dates in which buy dates precedes the sell dates
then find the maximum. O(n2)
Divide and conquer
Linear method
Chapter 1:
What's money?
Money is trust. Money means you trust the men or entity who print the papers and
believe later they will receive your papers and
give you the equal value commodity you want.
Chapter 2:
What's bond?
It's still trust. And it also means borrowing money from future.
规模和 diversification of risk
/** | |
* Array is different with pointer, although it is used like a pointer. | |
* Here is an example | |
*/ | |
#include <stdio.h> | |
int main() { | |
int a[5]; | |
printf("%p\n", a); |