Skip to content

Instantly share code, notes, and snippets.

maxSum' :: [Int] -> Int -> Int -> Int
maxSum' [] maxEnd maxSofar = maxSofar
maxSum' (x:xs) maxEnd maxSofar =
maxSum' xs maxEnd' (max maxSofar maxEnd')
where maxEnd' = max (maxEnd + x) 0
maxSum :: [Int] -> Int
maxSum arr = maxSum' arr 0 0
@songpp
songpp / IP.hs
Created January 18, 2011 10:11
parse ip data by haskell -- in-complete
module IP where
import System.IO
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.UTF8 as UTF8
import Data.Binary.Get
import Data.Word
import Data.Char
import Numeric
import Data.Bits
package algrithsm
import annotation.tailrec
/**
* User: flower
* Date: 11-1-18
* Time: 下午6:22
* Description:
*/
@songpp
songpp / ProductPlan.cpp
Created January 20, 2011 10:00
动态规划:生产计划问题
/*
* 动态规划
*
* 生产计划问题 解答
*
* 工厂生产某种产品,每单位(千件)的成本为1(千元),
* 每次开工的固定成本为3(千元),工厂每季度的最大生产能力为6(千件)。
* 经调查,市场对该产品的需求量第一、二、三、四季度分别为 2,3,2,4(千件)。
* 如果工厂在第一、二季度将全年的需求都生产出来,
* 自然可以降低成本(少付固定成本费),
@songpp
songpp / ReverseStackManipulation.scala
Created March 29, 2011 09:23
根据输入和输出,逆向栈的push和pop操作记录。例如,输入:abcd,输出:cdba,结果:[Push(a),Push(b),Push(c),Pop(c),Push(d),Pop(d),Pop(b),Pop(a)]
package algrithsm
import collection.mutable.Stack
import org.slf4j.{LoggerFactory, Logger}
import Console._
/**
* User: flower
* Date: 11-3-29
* Time: 下午3:04
@songpp
songpp / HeapSort.scala
Created April 2, 2011 06:41
scala版堆实现的堆排序和优先级队列
package data
import java.util.Date
object HeapSort {
class MaxHeap[T <% Ordered[T] : ClassManifest](val initialSize: Int) {
private var underlying: Array[T] = new Array[T](initialSize + 1);
private var size = 0
@songpp
songpp / MergeSort.scala
Created April 2, 2011 07:03
归并排序
package data
/**
* MergeSort
* User: flower
* Date: 2010-10-18
* Time: 19:27:14
*/
import java.util.Date
package t1
object Just {
def main(arg : Array[String]) : Unit = {
val a = new Array[Int](10)
val x = Array(6, 23, 123, 5563, 345, 11);
quicksort(x) foreach println
@songpp
songpp / BinarySearchTree.scala
Created April 2, 2011 07:08
scala版 二叉搜索树 BinarySearchTree
package t1
object BinarySearchTree {
def mkTree[K <% Ordered[K], T](key : K, v : T) : Tree[K, T] =
new Node(key, v, Empty, Empty)
case class Person(id : String, name : String)
def main(args : Array[String]) : Unit = {
@songpp
songpp / JavaBinaryTree.scala
Created April 2, 2011 07:11
JAVA BinaryTree
package t1;
public class JavaBinaryTree{
static class Empty extends BinaryTreeMap{
protected Empty(){}
@Override
public boolean isEmpty(){
return true;