目录
84. 柱状图中最大的矩形 Largest-rectangle-in-histogram 🌟🌟🌟
85. 最大矩形 Maximal Rectangle 🌟🌟🌟
87. 扰乱字符串 Scramble String 🌟🌟🌟
🌟 每日一练刷题专栏 🌟
Rust每日一练 专栏
Golang每日一练 专栏
Python每日一练 专栏
C/C++每日一练 专栏
Java每日一练 专栏
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。
示例 1:
输入:heights = [2,1,5,6,2,3] 输出:10 解释:最大的矩形为图中红色区域,面积为 10
示例 2:
输入: heights = [2,4] 输出: 4
提示:
代码1: 暴力枚举
fn largest_rectangle_area(heights: Vec) -> i32 { let n = heights.len(); let mut max_area = 0; for i in 0..n { let mut left = i; let mut right = i; while left > 0 && heights[left - 1] >= heights[i] { left -= 1; } while right < n - 1 && heights[right + 1] >= heights[i] { right += 1; } let area = heights[i] * (right - left + 1) as i32; if area > max_area { max_area = area; } } max_area } fn main() { let heights = vec![2, 1, 5, 6, 2, 3]; println!("{}", largest_rectangle_area(heights)); // 输出:10 let heights = vec![2, 4]; println!("{}", largest_rectangle_area(heights)); // 输出:4 }
代码2: 单调栈
fn largest_rectangle_area(heights: Vec) -> i32 { let n = heights.len(); let mut stack = Vec::new(); let mut max_area = 0; for i in 0..=n { let cur_height = if i == n { 0 } else { heights[i] }; while !stack.is_empty() && cur_height < heights[*stack.last().unwrap()] { let h = heights[stack.pop().unwrap()]; let w = if stack.is_empty() { i } else { i - stack.last().unwrap() - 1 }; let area = h * w as i32; if area > max_area { max_area = area; } } stack.push(i); } max_area } fn main() { let heights = vec![2, 1, 5, 6, 2, 3]; println!("{}", largest_rectangle_area(heights)); // 输出:10 let heights = vec![2, 4]; println!("{}", largest_rectangle_area(heights)); // 输出:4 }
输出:
10
4
给定一个仅包含 0 和 1 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。
示例 1:
输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] 输出:6 解释:最大矩形如上图所示。
示例 2:
输入:matrix = [] 输出:0
示例 3:
输入:matrix = [["0"]] 输出:0
示例 4:
输入:matrix = [["1"]] 输出:1
示例 5:
输入:matrix = [["0","0"]] 输出:0
提示:
代码:
fn maximal_rectangle(matrix: Vec>) -> i32 { let rows = matrix.len(); if rows == 0 { return 0; } let cols = matrix[0].len(); let mut heights = vec![0; cols]; let mut max_area = 0; for i in 0..rows { for j in 0..cols { if matrix[i][j] == '1' { heights[j] += 1; } else { heights[j] = 0; } } let area = largest_rectangle_area(&heights); if area > max_area { max_area = area; } } max_area } fn largest_rectangle_area(heights: &Vec ) -> i32 { let mut stack = Vec::new(); let mut max_area = 0; let mut i = 0; while i <= heights.len() { let h = if i == heights.len() { 0 } else { heights[i] }; while !stack.is_empty() && h < heights[*stack.last().unwrap()] { let height = heights[stack.pop().unwrap()]; let width = if stack.is_empty() { i } else { i - stack.last().unwrap() - 1 }; let area = height * width as i32; if area > max_area { max_area = area; } } stack.push(i); i += 1; } max_area } fn main() { let matrix = vec![ vec!['1', '0', '1', '0', '0'], vec!['1', '0', '1', '1', '1'], vec!['1', '1', '1', '1', '1'], vec!['1', '0', '0', '1', '0'] ]; println!("{}", maximal_rectangle(matrix)); }
输出:
6
使用下面描述的算法可以扰乱字符串 s 得到字符串 t :
给你两个 长度相等 的字符串 s1 和 s2,判断 s2 是否是 s1 的扰乱字符串。如果是,返回 true ;否则,返回 false 。
示例 1:
输入:s1 = "great", s2 = "rgeat" 输出:true 解释:s1 上可能发生的一种情形是: "great" --> "gr/eat" // 在一个随机下标处分割得到两个子字符串 "gr/eat" --> "gr/eat" // 随机决定:「保持这两个子字符串的顺序不变」 "gr/eat" --> "g/r / e/at" // 在子字符串上递归执行此算法。两个子字符串分别在随机下标处进行一轮分割 "g/r / e/at" --> "r/g / e/at" // 随机决定:第一组「交换两个子字符串」,第二组「保持这两个子字符串的顺序不变」 "r/g / e/at" --> "r/g / e/ a/t" // 继续递归执行此算法,将 "at" 分割得到 "a/t" "r/g / e/ a/t" --> "r/g / e/ a/t" // 随机决定:「保持这两个子字符串的顺序不变」 算法终止,结果字符串和 s2 相同,都是 "rgeat" 这是一种能够扰乱 s1 得到 s2 的情形,可以认为 s2 是 s1 的扰乱字符串,返回 true
示例 2:
输入:s1 = "abcde", s2 = "caebd" 输出:false
示例 3:
输入:s1 = "a", s2 = "a" 输出:true
提示:
代码:
fn is_scramble(s1: String, s2: String) -> bool { let s1 = s1.as_bytes(); let s2 = s2.as_bytes(); let n = s1.len(); if s1 == s2 { return true; } if n != s2.len() { return false; } let mut dp = vec![vec![vec![false; n + 1]; n]; n]; for i in 0..n { for j in 0..n { dp[i][j][1] = s1[i] == s2[j]; } } for l in 2..=n { for i in 0..=n-l { for j in 0..=n-l { for k in 1..l { if (dp[i][j][k] && dp[i+k][j+k][l-k]) || (dp[i][j+l-k][k] && dp[i+k][j][l-k]) { dp[i][j][l] = true; break; } } } } } dp[0][0][n] } fn main() { let s1 = String::from("abcde"); let s2 = String::from("caebd"); println!("{}", is_scramble(s1, s2)); let s1 = String::from("a"); let s2 = String::from("a"); println!("{}", is_scramble(s1, s2)); }
输出:
false
true
✨ 持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/
Rust每日一练 专栏(2023.5.16~)更新中... | |
Golang每日一练 专栏(2023.3.11~)更新中... | |
Python每日一练 专栏(2023.2.18~2023.5.18)暂停更 | |
C/C++每日一练 专栏(2023.2.18~2023.5.18)暂停更 | |
Java每日一练 专栏(2023.3.11~2023.5.18)暂停更 |
6.13 生日快乐