LeetCode229 多数元素 II 通过消除元素的方法确定候选, 即候选元素可以被消除\(\frac{n}{3}\)次. class Solution: def majorityElement(self, nums: List[int]) -> List[int]: proposal_1, proposal_2, cnt_1, cnt_2, n = 0, 0, 0, 0, len(nums) for i in
Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order. Example 1: Input: nums = [1,1,2] Output: [[1,1,2], [1,2,1], [2,1,1]] Example 2: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],
You are given an integer array prices where prices[i] is the price of a given stock on the \(i\)th day. On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then
题目 题解 我们可以将一个大的二叉搜索树分成越来越小的二叉搜索树 那么问题是如何取左节点 与 右节点的可能性 也就是我们需要的做 便是如何找到左节点范围 右节点范围 二叉搜索树 //左节点比根节点小//右节点比根节点大 //右节点下的子节点也比左节点的子节点大 //已经取过
题目描述 Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same. Since it is impossible to change the length o
思路: 动态开点线段树,这里维护了区间最大值和区间加一个值。 实现: 1 class SegmentTree { 2 public: 3 int N = (int)1e9; 4 class Node { 5 public: 6 // ls 和 rs 分别代表当前区间的左右子节点 7 Node*ls=nullptr, *rs=nullptr; 8
给你一个字符串表达式 s ,请你实现一个基本计算器来计算并返回它的值。 整数除法仅保留整数部分。 你可以假设给定的表达式总是有效的。所有中间结果将在 [-231, 231 - 1] 的范围内。 注意:不允许使用任何将字符串作为数学表达式计算的内置函数,比如 eval() 。 示例 1: 输入:s =
122. 买卖股票的最佳时机 II 贪心,第二天贵,第一天就买入 55. 跳跃游戏 在走过的路上找能到达最远的point,记录这个最远的点。 然后从上次的位置到这次的最远的点的位置的路径上再走一次,记录最远的点。 如果发现最远的点位置和上次的最远的点的位置一致,那么就意味着无法继续前进了,此
题目 点这里看题目。 分析 神奇的题目啊! 以下设被删除的边集为 \(Q\)。 思路一 正常人的思路。 随便拉一棵生成树 \(T\),并定一个根。假如我们只删除了一条树边 \(e\),设 \(S(e)\) 为覆盖 \(e\) 的非树边的集合,则图不连通当且仅当 \(Q\supseteq S(e)\)。 那么删除了多条树边呢?假如我
一、题目 点此看题 二、解法 建立原图的 \(\tt dfs\) 树,分树边和非树边考察连通性。设删边集合是 \(P\),设覆盖树边 \(e\) 的非树边构成集合 \(S_e\),特别地,对于非树边 \(e\) 令 \(S_e=\{e\}\),有结论: 删边后图不连通等价于,\(\exist Q\not=\varnothing,Q\subseteq P,\Delta_{e\in Q}
题意 求区间最大去重后的子段和。 Solution 考虑到 CF997E 的套路。求区间子段计数的问题,可以采用离线的方式。还是一样考虑移动右端点。那么在线段树上 \(i\) 位置存储 \([i,r]\) 的去重后的子段和。 现在考虑端点移动。加入了一个 \(a_r\),只会对上一次出现 \(a_r\) 的位置之后的
题目难度 中等 推荐理由 考验对背包问题的理解 题目知识点 分组背包,0/1背包 题意 农夫约翰有预算 VV,有 NN 台游戏机,每台游戏机价格为 P_iPi。每台游戏机有G_iGi个独立游戏,只有买了这台游戏机才能玩对应的游戏,每个游戏价格为 GP_jGPj,玩了之后奶牛产量增加 PV_jPVj。
代码如下: #include<iostream> #include <cstring> using namespace std; const int MOD = 10000; int n; int c[105][20]; int main() { memset(c, 0, sizeof c); c[1][1] = 1; for(int i = 2; i <= 100; i ++ ) { int rem =
题面 这是AIZU上的一道练习题,搬运VJ的链接:Knapsack Problem with Limitations II You have \(N\) items that you want to put them into a knapsack. Item \(i\) has value \(v_i\), weight \(w_i\) and limitation \(m_i\). You want to find a subset of items to put such th
https://www.luogu.com.cn/problem/P1182涉及知识点:贪心,二分,前缀和黄色题 思路: 答案。在此题中,我们首先要确定答案的范围。显然,答案最小可能是数列中的最大值(记为 l ),最大可能是所有数的和(记为 r)。其次,我们发现,给出一个解,判定这个解是否合法,在此题中显得简单。这启示我们
CF link 你谷 link 与官方题解一样的解法,姑且发一篇题解,个人感觉思路非常自然,不需要什么思维上的火花。 首先考虑从题面入手来分析这道题目的做法,操作 \(n\) 次,每次选取值在 \([i,i+s]\) 区间内的两个点进行交换,最后使得 \(a\) 数组变为 \(b\) 数组,然后 \(b\) 数组中有一些位置待
102. 二叉树的层序遍历 方法1: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullpt
https://leetcode.cn/problems/4sum-ii/solution/si-shu-xiang-jia-ii-by-leetcode-solution/ 时间复杂度和空间复杂度都是O(n2) func fourSumCount(a, b, c, d []int) (ans int) { mp:=make(map[int]int,0) for i:=0;i<len(a);i++{ for j:=0;j<
参考 https://zhuanlan.zhihu.com/p/26587675 因为闭环传递函数比较复杂,特点不鲜明,同时所有的控制都是负反馈,所以只要开环传递函数确定了,闭环传递函数的特性也唯一了。 1、开环传递函数的几个特征 低频增益就是频率接近0时(直流分量),幅频特性的纵坐标值,如下图的蓝色圈所示。 这个
剑指 Offer II 100:三角形中最小路径之和 给定一个三角形 triangle ,找出自顶向下的最小路径和。 每一步只能移动到下一行中相邻的结点上。相邻的结点 在这里指的是 下标 与 上一层结点下标 相同或者等于 上一层结点下标 + 1 的两个结点。也就是说,如果正位于当前行的下标 i ,那么下一
题目 题解 记录一下频率 超过2就删除 代码 1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 class Solution { 5 public: 6 int removeDuplicates(vector<int>& nums) { 7 int frequency = 1; 8 for (int i = 0; i &l
给你一个字符串表达式 s ,请你实现一个基本计算器来计算并返回它的值。 整数除法仅保留整数部分。 你可以假设给定的表达式总是有效的。所有中间结果将在 [-231, 231 - 1] 的范围内。 注意:不允许使用任何将字符串作为数学表达式计算的内置函数,比如 eval() 。 示例: 输入:s = " 3+5
LeetCode213 打家劫舍 II 考虑环形,首位不能同时选择 则分别对 \([0, len - 2]\) 和 \([1, len - 1]\) 做LeetCode198 打家劫舍 class Solution: def rob(self, nums: List[int]) -> int: def solver(start: int, end: int) -> int: first = nums[start]
https://leetcode.cn/problems/path-sum-ii/ /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func pathSum(root *TreeNode, targetSum int) [][]int { stack:=make([]
LeetCode122 买卖股票的最佳时机 II 贪心计算爬峰收益 class Solution: def maxProfit(self, prices: List[int]) -> int: ans, l = 0, len(prices) for i in range(1, l): ans += max(0, prices[i] - prices[i - 1]) return ans