Skip to content

Commit 484b6ea

Browse files
committed
some problem analysis
1 parent fd64978 commit 484b6ea

11 files changed

+147
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
.c9revisions/
1717
.settings
1818
.idea/
19+
.DS_Store

add_new_problem.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
title="$1"
4+
lowercasename="$2"
5+
6+
if [ $lowercasename"x" == "x" ] || [ "$title""x" == "x" ] ; then
7+
echo "[ERROR] problem name empty."
8+
exit 1
9+
fi
10+
11+
if [ -f analysis/$lowercasename.md ]; then
12+
echo "[ERROR] File exists: "analysis/$lowercasename.md
13+
exit 1
14+
fi
15+
16+
if [ -f src/$lowercasename.cpp ]; then
17+
echo "[ERROR] File exists: "src/$lowercasename.cpp
18+
exit 1
19+
fi
20+
21+
cp template.md analysis/$lowercasename.md
22+
cp template.cpp src/$lowercasename.cpp
23+
24+
sed -i.bak "s:lowercasename:${lowercasename}:g" analysis/$lowercasename.md
25+
sed -i.bak "s:title:${title}:g" analysis/$lowercasename.md
26+
rm analysis/$lowercasename.md.bak
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Word Ladder [ [sourcecode](../src/WordLadder.cpp) | [problem](https://oj.leetcode.com/problems/word-ladder/) ]
2+
3+
4+
dp解法:
5+
设dp[k][i]表示到price[i]位置,k次transaction能带来的最大的profit
6+
如果price[i]处没有交易:
7+
dp[k][i] = dp[k][i-1]
8+
如果在price[i]卖出:
9+
dp[k][i] = max(price[i]-price[j]+dp[k-1][j-1]) (j为第k次交易买入,j=[0,i-1])
10+
如果对于每个i都需要遍历一遍j,复杂度是O(n^2)
11+
优化:max(price[i]-price[j]+dp[k-1][j-1])中,-price[j]+dp[k-1][j-1]每次都重复计算了,
12+
当i=p时,j从1到p-1循环,i=p+1时,j从1到p循环,1到p-1和之前的是一样的,所以没有必要重复计算,
13+
只需要计算j=p即,-price[p]+dp[k-1][p-1]的值
14+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Binary Tree Maximum Path Sum [ [sourcecode](../src/BinaryTree MaximumPathSum.cpp) | [problem](https://leetcode.com/problems/binary-tree-maximum-path-sum/) ]
2+
3+
4+
##分析
5+
求二叉树中任意两个节点路径的和的最大值。
6+
用递归法求解。有三种情况:
7+
1. 最长路径在左子树中,answer[root] = answer[root->left]
8+
2. 最长路径在右子树中,answer[root] = answer[root->right]
9+
3. 最长路径包含root节点,用leftMax[root]表示以左子树某个节点开始到root结尾的路径的最大值,rightMax[root]表示以右子树某个节点开始到root结尾的路径的最大值
10+
``
11+
answer[root] = max(leftMax[root->left], 0) + root + max(rightMax[root->right],0)
12+
leftMax[root] = max(
13+
max(leftMax[root->left], 0),
14+
max(rightMax[root->right],0)
15+
) + root
16+
``
17+
18+
19+
##解法
20+

analysis/maximum-gap.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Maximum Gap [ [sourcecode](../src/maximum-gap.cpp) | [problem](https://leetcode.com/problems/maximum-gap/) ]
2+
3+
##解法分析
4+
乱序排列的数组找出相邻两个数最大的差值。如果排序再找,就是O(nlogn)的时间。
5+
O(n)的解法:桶排序。
6+
先找出min和max,然后将max-min分成n-1等份,因为除去max和min,只有n-2个值,分成n-1等份,根据鸽巢原理,必然有一个是空的。那么gap>=(max-min)/n-1。而落在同一个桶内的所有元素的gap肯定小于桶宽度(max-min)/n-1。所以同一个桶内,只需要记录最小值和最大值,用来和相邻的桶比较。扫描一遍记录每个桶内元素的最小值最大值。然后再扫描一遍桶,相邻的有数的桶之间计算gap,取最大的gap就是结果。
7+
8+
##步骤或伪代码

analysis/two-sum.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
@file two-sum.md
2-
@author Brian
3-
@version 1.0
4-
@date 2014-09-11
5-
6-
7-
##Two Sum [ [sourcecode](../src/TwoSum.cpp) | [problem](https://oj.leetcode.com/problems/two-sum/) ]
1+
##Two Sum [ [sourcecode](../src/TwoSum.cpp) | [problem](https://leetcode.com/problems/two-sum/) ]
82

93
##分析
104
这个题目看上去很简单,就是在一堆数里面找两个数,使得他们的和为给定的值。但是,其实这个题目要求比较高,而且坑很多。首先如果想要AC,普通的暴力枚举是不行的,因为时间复杂度是O(n^2)。

analysis/word-ladder-ii.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Word Ladder II [ [sourcecode](../src/WordLadderII.cpp) | [problem](https://leetcode.com/problems/word-ladder-ii/) ]
2+
3+
4+
##分析
5+
单词变换每次只能变一个字母,词典里是中间可以用来过度的词。题目可以转换为无向图最短路劲问题。
6+
一个词是地图中的一个点,两个词如果只相差一个字母,就连一条边。给定起点和终点,求起点到终点的最短路径。用Dijkstra算法或者Bellman-Ford算法都行。
7+
8+
##解法
9+

analysis/zigzag-conversion.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
##ZigZag Conversion [ [sourcecode](../src/ZigZagConversion.cpp) | [problem](https://oj.leetcode.com/problems/zigzagconversion/) ]
2+
##ZigZag Conversion [ [sourcecode](../src/ZigZagConversion.cpp) | [problem](https://oj.leetcode.com/problems/zigzag-conversion/) ]
33

44
##分析
55
将字符串按照zigzag形式输出。纯模拟。找规律。

src/maximum-gap.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @author Brian
3+
* @version 1.0
4+
* @date
5+
*/
6+
7+
#include <iostream>
8+
#include <cstdio>
9+
#include <cstdlib>
10+
#include <cstring>
11+
#include <memory.h>
12+
#include <algorithm>
13+
#include <math.h>
14+
#include <queue>
15+
#include <vector>
16+
using namespace std;
17+
18+
#define fillzero(d) memset(d,0,sizeof(d))
19+
#define print(arr,n) for(int i=0;i<n;i++) printf("%d ",arr[i]);printf("\n");
20+
#define MAXINT 0x7fffffff
21+
#define read(c) scanf("%d",&(c))
22+
#define FOR(a,b) for(int i=(a);i<(b);i++)
23+
24+
class Solution {
25+
public:
26+
27+
};
28+
int main(){
29+
Solution s;
30+
return 0;
31+
}

template.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @author Brian
3+
* @version 1.0
4+
* @date
5+
*/
6+
7+
#include <iostream>
8+
#include <cstdio>
9+
#include <cstdlib>
10+
#include <cstring>
11+
#include <memory.h>
12+
#include <algorithm>
13+
#include <math.h>
14+
#include <queue>
15+
#include <vector>
16+
using namespace std;
17+
18+
#define fillzero(d) memset(d,0,sizeof(d))
19+
#define print(arr,n) for(int i=0;i<n;i++) printf("%d ",arr[i]);printf("\n");
20+
#define MAXINT 0x7fffffff
21+
#define read(c) scanf("%d",&(c))
22+
#define FOR(a,b) for(int i=(a);i<(b);i++)
23+
24+
class Solution {
25+
public:
26+
27+
};
28+
int main(){
29+
Solution s;
30+
return 0;
31+
}

template.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## title [ [sourcecode](../src/lowercasename.cpp) | [problem](https://leetcode.com/problems/lowercasename/) ]
2+
3+
##解法分析
4+
5+
##步骤或伪代码

0 commit comments

Comments
 (0)