原创 Poj2663. Tri Tiling

題目鏈接:http://poj.org/problem?id=2663 思路:這道題目要求我們求出有多少種方法用1*2牌去完美覆蓋3*n的棋盤。利用動態規劃,dp[i]表示3*i的棋盤的可完美覆蓋的總數。我們可以將棋盤分爲左右兩

原创 Poj1050.To the Max

題目鏈接:http://poj.org/problem?id=1050; 思路:這道題目要求出最大子矩陣和,可以將二維矩陣計算轉化爲一維最大連續子數組和。依次遍歷所有行,對於當前行i,分別求出row[i,i+1],row[i,i

原创 An activity-selection problem(Greedy Algorithm)

思路:要求出最大解集,可以將所有活動按照結束時間從小到大排序,然後遍歷一遍所有活動,只要當前活動開始時間不小於上一活動結束時間便可加入集合中。這樣求出的最大解集只是可行的最大解集的其中一種。 代碼實現如下: #include

原创 最小生成樹之Prime算法

題目鏈接:http://poj.org/problem?id=1258 思路:題目本質就是要求最小生成樹,根據題目給出的測試數據格式利用prime算法即可,代碼實現如下: #include <iostream> #include

原创 98. Validate Binary Search Tree

題目:Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: T

原创 99. Recover Binary Search Tree

題目:Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structu

原创 55. Jump Game

題目:Given an array of non-negative integers, you are initially positioned at the first index of the array. Each elem

原创 124. Binary Tree Maximum Path Sum

題目:Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes fro

原创 45. Jump Game II

題目:Given an array of non-negative integers, you are initially positioned at the first index of the array. Each elem

原创 134. Gas Station

題目:There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car

原创 longest common subsequence (LCS)

思路:這道題目給出兩個字符串,要求出最長公共子序列。利用動態規劃思想,令dp[i][j]表示x[1…i]和y[1…j]的最長公共子序列,狀態轉移方程爲:dp[i][j] = (x[i-1]==y[j-1]) ? dp[i-1]

原创 120. Triangle

題目:Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cut

原创 129. Sum Root to Leaf Numbers

題目:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example

原创 Poj1160.Post Office

題目鏈接:http://poj.org/problem?id=1160 思路:利用動態規劃思想,令dp[i][j]表示i個郵局在前j個村莊的最優解,dis[i][j]表示在村莊V[i…j]之間只有一個郵局時的最優解,顯然當郵局處

原创 Poj1159.Palindrome

題目鏈接:http://poj.org/problem?id=1159 思路:這道題目要我們求出將給定字符串變爲迴文串所需最小插入字符數。利用動態規劃思想,令dp[i][j]表示將字符串s[i…j]變爲迴文串所需插入的最小字符數