2023每日刷题(四十二)
Leetcode—907.子数组的最小值之和 算法思想 参考自y神思想
实现代码
class Solution {
public:int sumSubarrayMins(vector<int>& arr) {long long ans 0;const int mod 1e97;int n arr.size();stack<int>…
文章目录 一、题目二、题解 一、题目
Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no f…
链接:https://nanti.jisuanke.com/t/38228
题意和思路都和https://blog.csdn.net/birdmanqin/article/details/97553976差不多。
#include <bits/stdc.h>
#define ll long long
using namespace std;
const int N 5e510;
const ll inf 1e18;
struct node
…
循环数组的单调栈(单调递减)问题,可以通过两倍数组长度取余操作模拟循环数组。
class Solution:def nextGreaterElements(self, nums: List[int]) -> List[int]:n len(nums)stack []res [0] * nfor i in range(2*n-1, -1, -1):pos i …
文章目录 一、题目二、题解 一、题目
Given an m x n binary matrix mat, return the number of submatrices that have all ones.
Example 1:
Input: mat [[1,0,1],[1,1,0],[1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles…
Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements.
Input
The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 2000) on li…
作者推荐
map|动态规划|单调栈|LeetCode975:奇偶跳
涉及知识点
单调栈
题目
给你一个字符串 s ,一个整数 k ,一个字母 letter 以及另一个整数 repetition 。 返回 s 中长度为 k 且 字典序最小 的子序列,该子序列同时应满足字母 letter 出…
没看答案,暴力解法,时间复杂度O(n^2)。
class Solution:def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:res []for i in nums1:pos nums2.index(i)for num in nums2[pos:]:if num > i:res.append(num)breakel…
A 数组中的最大数对和 数据范围小,直接暴力枚举数对 class Solution {
public:int mx(int x) {//返回10进制表示的数的最大数字int res 0;for (; x; x / 10)res max(res, x % 10);return res;}int maxSum(vector<int> &nums) {int n nums.size();int r…
题目内容 原题链接 给定一个长度为 n n n 的不含前导零的数 x x x ,删除其中 k k k 个数位,不能出现前导零。
问删除 k k k 个位后最小的数
数据范围 1 ≤ n ≤ 5 ⋅ 1 0 5 1\leq n\leq 5\cdot 10^5 1≤n≤5⋅105 0 ≤ k < n 0\leq k<n 0≤k…
题目 同样的LeetCode原题:题目链接
给你一个 m x n 的二进制矩阵 mat ,请你返回有多少个 子矩形 的元素全部都是 1 。 单调栈 解题思路整体和上一篇文章差不多,都是用到了压缩数组的技巧,通过压缩数组来构建一个数组矩阵、以每一…
**接雨水**给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
int trap(int* height, int heightSize){if(heightSize0){return 0;}int all0;//总雨水int stack[heightSize];//数组模拟栈int num0;//栈顶指…
题目链接 Leetcode.321 拼接最大数 hard 题目描述
给定长度分别为 m m m 和 n n n 的两个数组,其元素由 0 ∼ 9 0 \sim 9 0∼9 构成,表示两个自然数各位上的数字。现在从这两个数组中选出 k k k ( k ≤ m n ) (k \leq m n) (k≤mn) 个数字拼接成…
题目
法1:单调栈[原版]
O(N)O(N) 必须掌握算法!!!
class Solution {public int largestRectangleArea(int[] heights) {int n heights.length, res 0;int[] leftMin new int[n], rightMin new int[n];Stack<Integer>…
题目内容 原题链接 给定一个 n n n 行 m m m 列的矩阵,问权值最大的子矩阵的权值是多少。
对于一个矩阵,其权值定义为矩阵中的最小值 m i n v minv minv 乘上矩阵中所有元素的和。
数据范围 1 ≤ n , m ≤ 300 1\leq n,m \leq 300 1≤n,m≤300 1 ≤…
503. 下一个更大元素 II
方法一:将两个nums数组放在一起,使用单调栈求下一个更大元素,最后再把结果集即result数组resize到原数组大小就可以了。
方法二:在遍历的过程中模拟走了两遍nums
class Solution {
public:vector<in…