Skip to content

Commit 77c22dc

Browse files
committed
some dp/tree
[O]->Optimize Later
1 parent f6b37c9 commit 77c22dc

7 files changed

+401
-29
lines changed

DP/198.house-robber.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @lc app=leetcode id=198 lang=cpp
3+
*
4+
* [198] House Robber
5+
*
6+
* https://leetcode.com/problems/house-robber/description/
7+
*
8+
* algorithms
9+
* Easy (40.79%)
10+
* Total Accepted: 294.2K
11+
* Total Submissions: 721.1K
12+
* Testcase Example: '[1,2,3,1]'
13+
*
14+
* You are a professional robber planning to rob houses along a street. Each
15+
* house has a certain amount of money stashed, the only constraint stopping
16+
* you from robbing each of them is that adjacent houses have security system
17+
* connected and it will automatically contact the police if two adjacent
18+
* houses were broken into on the same night.
19+
*
20+
* Given a list of non-negative integers representing the amount of money of
21+
* each house, determine the maximum amount of money you can rob tonight
22+
* without alerting the police.
23+
*
24+
* Example 1:
25+
*
26+
*
27+
* Input: [1,2,3,1]
28+
* Output: 4
29+
* Explanation: Rob house 1 (money = 1) and then rob house 3 (money =
30+
* 3).
31+
* Total amount you can rob = 1 + 3 = 4.
32+
*
33+
* Example 2:
34+
*
35+
*
36+
* Input: [2,7,9,3,1]
37+
* Output: 12
38+
* Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house
39+
* 5 (money = 1).
40+
* Total amount you can rob = 2 + 9 + 1 = 12.
41+
*
42+
*
43+
*/
44+
class Solution
45+
{
46+
public:
47+
int rob(vector<int> &nums)
48+
{
49+
int even = 0, odd = 0;
50+
for (int i = 0; i < nums.size(); i++)
51+
{
52+
if (i % 2 == 0)
53+
{
54+
even = max(even + nums[i], odd);
55+
}
56+
else
57+
{
58+
odd = max(odd + nums[i], even);
59+
}
60+
}
61+
return max(even, odd);
62+
}
63+
};

DP/70.climbing-stairs.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,24 @@
4040
*
4141
*
4242
*/
43-
class Solution {
44-
public:
45-
int climbStairs(int n) {
46-
if(n==0)return 0;
47-
if(n==1)return 1;
48-
if(n==2)return 2;
49-
int a=1,b=2;
50-
for(int i=2;i<n;i++){
51-
int tmp=a+b;
52-
a=b;
53-
b=tmp;
43+
class Solution
44+
{
45+
public:
46+
int climbStairs(int n)
47+
{
48+
if (n == 0)
49+
return 0;
50+
if (n == 1)
51+
return 1;
52+
if (n == 2)
53+
return 2;
54+
int a = 1, b = 2;
55+
for (int i = 2; i < n; i++)
56+
{
57+
int tmp = a + b;
58+
a = b;
59+
b = tmp;
5460
}
5561
return b;
5662
}
5763
};
58-

Tree/100.same-tree.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* @lc app=leetcode id=100 lang=cpp
3+
*
4+
* [100] Same Tree
5+
*
6+
* https://leetcode.com/problems/same-tree/description/
7+
*
8+
* algorithms
9+
* Easy (49.45%)
10+
* Total Accepted: 353K
11+
* Total Submissions: 713.3K
12+
* Testcase Example: '[1,2,3]\n[1,2,3]'
13+
*
14+
* Given two binary trees, write a function to check if they are the same or
15+
* not.
16+
*
17+
* Two binary trees are considered the same if they are structurally identical
18+
* and the nodes have the same value.
19+
*
20+
* Example 1:
21+
*
22+
*
23+
* Input: 1 1
24+
* ⁠ / \ / \
25+
* ⁠ 2 3 2 3
26+
*
27+
* ⁠ [1,2,3], [1,2,3]
28+
*
29+
* Output: true
30+
*
31+
*
32+
* Example 2:
33+
*
34+
*
35+
* Input: 1 1
36+
* ⁠ / \
37+
* ⁠ 2 2
38+
*
39+
* ⁠ [1,2], [1,null,2]
40+
*
41+
* Output: false
42+
*
43+
*
44+
* Example 3:
45+
*
46+
*
47+
* Input: 1 1
48+
* ⁠ / \ / \
49+
* ⁠ 2 1 1 2
50+
*
51+
* ⁠ [1,2,1], [1,1,2]
52+
*
53+
* Output: false
54+
*
55+
*
56+
*/
57+
/**
58+
* Definition for a binary tree node.
59+
* struct TreeNode {
60+
* int val;
61+
* TreeNode *left;
62+
* TreeNode *right;
63+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
64+
* };
65+
*/
66+
class Solution
67+
{
68+
public:
69+
bool isSameTree(TreeNode *p, TreeNode *q)
70+
{
71+
if (!p || !q)
72+
return p == q;
73+
if (p->val != q->val)
74+
return false;
75+
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
76+
}
77+
};
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* @lc app=leetcode id=102 lang=cpp
3+
*
4+
* [102] Binary Tree Level Order Traversal
5+
*
6+
* https://leetcode.com/problems/binary-tree-level-order-traversal/description/
7+
*
8+
* algorithms
9+
* Medium (47.13%)
10+
* Total Accepted: 341.9K
11+
* Total Submissions: 724.4K
12+
* Testcase Example: '[3,9,20,null,null,15,7]'
13+
*
14+
* Given a binary tree, return the level order traversal of its nodes' values.
15+
* (ie, from left to right, level by level).
16+
*
17+
*
18+
* For example:
19+
* Given binary tree [3,9,20,null,null,15,7],
20+
*
21+
* ⁠ 3
22+
* ⁠ / \
23+
* ⁠ 9 20
24+
* ⁠ / \
25+
* ⁠ 15 7
26+
*
27+
*
28+
*
29+
* return its level order traversal as:
30+
*
31+
* [
32+
* ⁠ [3],
33+
* ⁠ [9,20],
34+
* ⁠ [15,7]
35+
* ]
36+
*
37+
*
38+
*/
39+
/**
40+
* Definition for a binary tree node.
41+
* struct TreeNode {
42+
* int val;
43+
* TreeNode *left;
44+
* TreeNode *right;
45+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
46+
* };
47+
*/
48+
/*12 ms 14.8 MB cpp*/
49+
class Solution
50+
{
51+
public:
52+
vector<vector<int>> levelOrder(TreeNode *root)
53+
{
54+
if (!root)
55+
return {};
56+
vector<vector<int>> an;
57+
vector<TreeNode *> cur, next;
58+
cur.push_back(root);
59+
while (!cur.empty())
60+
{
61+
an.push_back({});
62+
for (TreeNode *i : cur)
63+
{
64+
an.back().push_back(i->val);
65+
if (i->left)
66+
next.push_back(i->left);
67+
if (i->right)
68+
next.push_back(i->right);
69+
}
70+
cur.swap(next);
71+
next.clear();
72+
}
73+
return an;
74+
}
75+
};
76+
/*8 ms 13.8 MB cpp*/
77+
class Solution
78+
{
79+
public:
80+
vector<vector<int>> levelOrder(TreeNode *root)
81+
{
82+
vector<vector<int>> an;
83+
DFS(root, 0, an);
84+
return an;
85+
}
86+
87+
private:
88+
vector<vector<int>> BFS(TreeNode *root)
89+
{
90+
if (!root)
91+
return {};
92+
vector<vector<int>> an;
93+
vector<TreeNode *> cur, next;
94+
cur.push_back(root);
95+
while (!cur.empty())
96+
{
97+
an.push_back({});
98+
for (TreeNode *i : cur)
99+
{
100+
an.back().push_back(i->val);
101+
if (i->left)
102+
next.push_back(i->left);
103+
if (i->right)
104+
next.push_back(i->right);
105+
}
106+
cur.swap(next);
107+
next.clear();
108+
}
109+
return an;
110+
}
111+
void DFS(TreeNode *root, int depth, vector<vector<int>> &an)
112+
{
113+
if (!root)
114+
return;
115+
116+
while (an.size() <= depth)
117+
an.push_back({});
118+
an[depth].push_back(root->val);
119+
DFS(root->left, depth + 1, an);
120+
DFS(root->right, depth + 1, an);
121+
}
122+
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* @lc app=leetcode id=107 lang=cpp
3+
*
4+
* [107] Binary Tree Level Order Traversal II
5+
*
6+
* https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/
7+
*
8+
* algorithms
9+
* Easy (45.72%)
10+
* Total Accepted: 211.4K
11+
* Total Submissions: 461.9K
12+
* Testcase Example: '[3,9,20,null,null,15,7]'
13+
*
14+
* Given a binary tree, return the bottom-up level order traversal of its
15+
* nodes' values. (ie, from left to right, level by level from leaf to root).
16+
*
17+
*
18+
* For example:
19+
* Given binary tree [3,9,20,null,null,15,7],
20+
*
21+
* ⁠ 3
22+
* ⁠ / \
23+
* ⁠ 9 20
24+
* ⁠ / \
25+
* ⁠ 15 7
26+
*
27+
*
28+
*
29+
* return its bottom-up level order traversal as:
30+
*
31+
* [
32+
* ⁠ [15,7],
33+
* ⁠ [9,20],
34+
* ⁠ [3]
35+
* ]
36+
*
37+
*
38+
*/
39+
/**
40+
* Definition for a binary tree node.
41+
* struct TreeNode {
42+
* int val;
43+
* TreeNode *left;
44+
* TreeNode *right;
45+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
46+
* };
47+
*/
48+
49+
/*16 ms 13.7 MB cpp*/
50+
class Solution
51+
{
52+
public:
53+
vector<vector<int>> levelOrderBottom(TreeNode *root)
54+
{
55+
return BFS(root);
56+
// if(!root)return {};
57+
// vector<vector<int>>an;
58+
}
59+
60+
private:
61+
vector<vector<int>> BFS(TreeNode *root)
62+
{
63+
if (!root)
64+
return {};
65+
vector<vector<int>> an;
66+
vector<TreeNode *> cur, next;
67+
cur.push_back(root);
68+
while (!cur.empty())
69+
{
70+
an.push_back({});
71+
for (TreeNode *i : cur)
72+
{
73+
an.back().push_back(i->val);
74+
if (i->left)
75+
next.push_back(i->left);
76+
if (i->right)
77+
next.push_back(i->right);
78+
}
79+
cur.swap(next);
80+
next.clear();
81+
}
82+
reverse(an.begin(), an.end());
83+
return an;
84+
}
85+
void DFS(TreeNode *root, int depth, vector<vector<int>> &an)
86+
{
87+
if (!root)
88+
return;
89+
90+
while (an.size() <= depth)
91+
an.push_back({});
92+
an[depth].push_back(root->val);
93+
DFS(root->left, depth + 1, an);
94+
DFS(root->right, depth + 1, an);
95+
}
96+
};

0 commit comments

Comments
 (0)