Skip to content

Commit 0d4b3b6

Browse files
committed
Solve #310
1 parent 0e10183 commit 0d4b3b6

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,5 @@ mod n0304_range_sum_query_2d_immutable;
230230
mod n1009_pancake_sorting;
231231
mod n0306_additive_number;
232232
mod n0307_range_sum_query_mutable;
233+
mod n0309_best_time_to_buy_and_sell_stock_with_cooldown;
234+
mod n0310_minimum_height_trees;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* [309] Best Time to Buy and Sell Stock with Cooldown
3+
*
4+
* Say you have an array for which the i^th element is the price of a given stock on day i.
5+
*
6+
* Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:
7+
*
8+
*
9+
* You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
10+
* After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
11+
*
12+
*
13+
* Example:
14+
*
15+
*
16+
* Input: [1,2,3,0,2]
17+
* Output: 3
18+
* Explanation: transactions = [buy, sell, cooldown, buy, sell]
19+
*
20+
*/
21+
pub struct Solution {}
22+
23+
// submission codes start here
24+
25+
/*
26+
dp[i]: max profit with selling at day i
27+
dp2[i]: max profit till day i
28+
29+
dp[i] = max(dp[i-1] + p[i] - p[i-1], dp2[i-2], dp2[i-3] + p[i] - p[i-1])
30+
*/
31+
impl Solution {
32+
pub fn max_profit(prices: Vec<i32>) -> i32 {
33+
if prices.len() < 2 { return 0 }
34+
if prices.len() == 2 {
35+
return i32::max(0, prices[1] - prices[0]);
36+
}
37+
let mut dp = vec![0; prices.len()];
38+
let mut dp2 = vec![0; prices.len()];
39+
let mut max = 0;
40+
dp[0] = 0;
41+
dp2[0] = 0;
42+
dp[1] = prices[1] - prices[0];
43+
dp2[1] = i32::max(dp2[0], dp[1]);
44+
dp[2] = i32::max(prices[2] - prices[1], prices[2]-prices[0]);
45+
dp2[2] = i32::max(dp2[1], dp[2]);
46+
for i in 3..prices.len() {
47+
dp[i] = i32::max(dp[i-1]+prices[i]-prices[i-1],
48+
i32::max(
49+
dp2[i-2],
50+
dp2[i-3]+prices[i]-prices[i-1]
51+
)
52+
);
53+
dp2[i] = i32::max(dp2[i-1], dp[i]);
54+
}
55+
let mut temp = 0;
56+
for &m in dp.iter() {
57+
if m > temp {
58+
temp = m;
59+
}
60+
}
61+
temp
62+
}
63+
}
64+
65+
// submission codes end
66+
67+
#[cfg(test)]
68+
mod tests {
69+
use super::*;
70+
71+
#[test]
72+
fn test_309() {
73+
assert_eq!(Solution::max_profit(vec![1,2,3,0,2]), 3);
74+
assert_eq!(Solution::max_profit(vec![4,2,7,1,11]), 10);
75+
}
76+
}

src/n0310_minimum_height_trees.rs

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* [310] Minimum Height Trees
3+
*
4+
* For an undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write a function to find all the MHTs and return a list of their root labels.
5+
*
6+
* Format<br />
7+
* The graph contains n nodes which are labeled from 0 to n - 1. You will be given the number n and a list of undirected edges (each edge is a pair of labels).
8+
*
9+
* You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.
10+
*
11+
* Example 1 :
12+
*
13+
*
14+
* Input: n = 4, edges = [[1, 0], [1, 2], [1, 3]]
15+
*
16+
* 0
17+
* |
18+
* 1
19+
* / \
20+
* 2 3
21+
*
22+
* Output: [1]
23+
*
24+
*
25+
* Example 2 :
26+
*
27+
*
28+
* Input: n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]
29+
*
30+
* 0 1 2
31+
* \ | /
32+
* 3
33+
* |
34+
* 4
35+
* |
36+
* 5
37+
*
38+
* Output: [3, 4]
39+
*
40+
* Note:
41+
*
42+
*
43+
* According to the <a href="https://en.wikipedia.org/wiki/Tree_(graph_theory)" target="_blank">definition of tree on Wikipedia</a>: &ldquo;a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.&rdquo;
44+
* The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
45+
*
46+
*
47+
*/
48+
pub struct Solution {}
49+
50+
// submission codes start here
51+
52+
use std::mem;
53+
impl Solution {
54+
pub fn find_min_height_trees(n: i32, edges: Vec<Vec<i32>>) -> Vec<i32> {
55+
let n = n as usize;
56+
let mut matrix: Vec<Vec<usize>> = vec![vec![]; n];
57+
for edge in edges.iter() {
58+
matrix[edge[0] as usize].push(edge[1] as usize);
59+
matrix[edge[1] as usize].push(edge[0] as usize);
60+
}
61+
let mut count = n;
62+
let mut la: Vec<usize> = vec![];
63+
let mut lb: Vec<usize> = vec![];
64+
for i in 0..n {
65+
if matrix[i].len() <= 1 {
66+
la.push(i);
67+
}
68+
}
69+
while count > 2 {
70+
count -= la.len();
71+
for &i in la.iter() {
72+
let j = matrix[i][0];
73+
let idx = matrix[j].iter().position(|&r| r == i).unwrap();
74+
matrix[j].remove(idx);
75+
if matrix[j].len() == 1 {
76+
lb.push(j);
77+
}
78+
}
79+
la.clear();
80+
mem::swap(&mut la, &mut lb);
81+
}
82+
la.into_iter().map(|i| i as i32).collect()
83+
}
84+
}
85+
86+
// submission codes end
87+
88+
#[cfg(test)]
89+
mod tests {
90+
use super::*;
91+
92+
#[test]
93+
fn test_310() {
94+
assert_eq!(Solution::find_min_height_trees(4, vec![vec![1, 0], vec![1, 2], vec![1, 3]]), vec![1]);
95+
assert_eq!(Solution::find_min_height_trees(6, vec![vec![0, 3], vec![1, 3], vec![2, 3], vec![4, 3], vec![5, 4]]), vec![3, 4]);
96+
assert_eq!(Solution::find_min_height_trees(1, vec![]), vec![0]);
97+
}
98+
}

0 commit comments

Comments
 (0)