1480、给你一个数组 nums 。数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]…nums[i]) 。
请返回 nums 的动态和。
示例 1:
输入:nums = [1,2,3,4]
输出:[1,3,6,10]
解释:动态和计算过程为 [1, 1+2, 1+2+3, 1+2+3+4] 。
class Solution {
public:
vector<int> runningSum(vector<int>& nums) {
for(int i=1;i<nums.size();i++){
nums[i]+=nums[i-1];
}
return nums;
}
};
结果:
执行用时:12 ms, 在所有 C++ 提交中击败了6.55% 的用户
内存消耗:8.8 MB, 在所有 C++ 提交中击败了100.00% 的用户
1481、
给你一个整数数组 arr 和一个整数 k 。现需要从数组中恰好移除 k 个元素,请找出移除后数组中不同整数的最少数目。
示例 2:
输入:arr = [4,3,1,1,3,3,2], k = 3
输出:2
解释:先移除 4、2 ,然后再移除两个 1 中的任意 1 个或者三个 3 中的任意 1 个,最后剩下 1 和 3 两种整数。
map 数据结构应用
class Solution {
public:
int findLeastNumOfUniqueInts(vector<int>& arr, int k) {
map<int,int> mp;
vector<int> vec;
for(int i=0;i<arr.size();i++){
if(mp.count(arr[i])>0) mp[arr[i]]++;
else mp.insert({arr[i],1});
}
for(auto it:mp) vec.push_back(it.second);
sort(vec.begin(),vec.end());
int i=0,cnt=0;
while(i<vec.size()){
cnt+=vec[i];
if(cnt<=k) i++;
else break;
}
return vec.size()-i;
}
};
结果:
执行用时:788 ms, 在所有 C++ 提交中击败了8.64% 的用户
内存消耗:64.4 MB, 在所有 C++ 提交中击败了100.00% 的用户
将map 改为unordered_map,提速
class Solution {
public:
int findLeastNumOfUniqueInts(vector<int>& arr, int k) {
unordered_map<int,int> mp;
vector<int> vec;
for(int i=0;i<arr.size();i++) mp[arr[i]]++;
for(auto it:mp) vec.push_back(it.second);
sort(vec.begin(),vec.end());
int i=0,cnt=0;
while(i<vec.size()){
cnt+=vec[i];
if(cnt<=k) i++;
else break;
}
return vec.size()-i;
}
};
结果:
执行用时:420 ms, 在所有 C++ 提交中击败了82.28% 的用户
内存消耗:62.6 MB, 在所有 C++ 提交中击败了100.00% 的用户
1482、给你一个整数数组 bloomDay,以及两个整数 m 和 k 。
现需要制作 m 束花。制作花束时,需要使用花园中 相邻的 k 朵花 。
花园中有 n 朵花,第 i 朵花会在 bloomDay[i] 时盛开,恰好 可以用于 一束 花中。
请你返回从花园中摘 m 束花需要等待的最少的天数。如果不能摘到 m 束花则返回 -1 。
示例 1:
输入:bloomDay = [1,10,3,10,2], m = 3, k = 1
输出:3
解释:让我们一起观察这三天的花开过程,x 表示花开,而 _ 表示花还未开。
现在需要制作 3 束花,每束只需要 1 朵。
1 天后:[x, _, _, _, _] // 只能制作 1 束花
2 天后:[x, _, _, _, x] // 只能制作 2 束花
3 天后:[x, _, x, _, x] // 可以制作 3 束花,答案为 3
大佬思路,二分。寻找数组中定长序列。
//对天数进行二分
//当天数足够长(bloomday中最大值)时。一定可以得到m束(当总数m*k合法)
//二分找最少且符合条件的天数
class Solution {
public:
bool check(vector<int>& day,int m,int k,int t){
int ans=0;//最多可以得到几束
for(int i=0,cnt=0;i<day.size();i++){
if(day[i]<=t){
cnt++;
if(cnt==k) {ans++;cnt=0;}
}else cnt=0;
}
return ans>=m;
}
int minDays(vector<int>& bloomDay, int m, int k) {
if(m*k>bloomDay.size()) return -1;
int l=1,r=0,mid=0;
for(int num:bloomDay) r=max(num,r);
//开始二分
while(l<r){
mid=(l+r)/2;
if(check(bloomDay,m,k,mid)){//判断该天数是否合法,合法则寻找更小的天数,不合法则寻找大天数
r=mid;
}else l=mid+1;
}
return r;
}
};
结果:
执行用时:368 ms, 在所有 C++ 提交中击败了82.07% 的用户
内存消耗:63.5 MB, 在所有 C++ 提交中击败了100.00% 的用户
LCA最近公共祖先问题
1483、给你一棵树,树上有 n 个节点,按从 0 到 n-1 编号。树以父节点数组的形式给出,其中 parent[i] 是节点 i 的父节点。树的根节点是编号为 0 的节点。
请你设计并实现 getKthAncestor(int node, int k) 函数,函数返回节点 node 的第 k 个祖先节点。如果不存在这样的祖先节点,返回 -1 。
树节点的第 k 个祖先节点是从该节点到根节点路径上的第 k 个节点。
输入:
["TreeAncestor","getKthAncestor","getKthAncestor","getKthAncestor"]
[[7,[-1,0,0,1,1,2,2]],[3,1],[5,2],[6,3]]输出:
[null,1,0,-1]解释:
TreeAncestor treeAncestor = new TreeAncestor(7, [-1, 0, 0, 1, 1, 2, 2]);treeAncestor.getKthAncestor(3, 1); // 返回 1 ,它是 3 的父节点
treeAncestor.getKthAncestor(5, 2); // 返回 0 ,它是 5 的祖父节点
treeAncestor.getKthAncestor(6, 3); // 返回 -1 因为不存在满足要求的祖先节点
简单线性查找,引起超时!!!
class TreeAncestor {
public:
vector<int> vec;
TreeAncestor(int n, vector<int>& parent) {
vec=parent;
}
int getKthAncestor(int node, int k) {
if(node>=vec.size()) return -1;
int cnt=0;
for(;cnt<k;cnt++){
if(vec[node]==-1) return -1;
node=vec[node];
}
return node;
}
};
/**
* Your TreeAncestor object will be instantiated and called as such:
* TreeAncestor* obj = new TreeAncestor(n, parent);
* int param_1 = obj->getKthAncestor(node,k);
*/
该问题为经典的LCA问题(Last-Common-Ancestor),本次使用倍增法,讲解见:LCA问题(倍增法)
最重要的递推式:DP[i][j] = DP[ DP[i][j-1] ] [j-1]。DP[i][j],表示的是以下标为i的节点,跳2^j层得到父节点下标。
DP[i][j-1]是结点i往上跳2^(j-1)层的祖先,那我们就在跳到这个结点的基础上,再向上跳2^(j-1)层,这样就相当于从结点i,先跳2^(j-1)层,再跳2^(j-1)层,最后还是到达了2^j层。
// 使用每次跳2^j,因为任意数可以表示为二进制。
class TreeAncestor {
public:
int dp[50005][20];
TreeAncestor(int n, vector<int>& parent) {
for(int i=0;i<n;i++)
dp[i][0]=parent[i];
for(int j=1;(1<<j)<n;j++){ //1<<j,j表示成二进制,每次向左移动一位。1-2-4-8。
for(int i=0;i<n;i++){ // i,j顺序
if(dp[i][j-1]==-1) dp[i][j]=-1;
else dp[i][j]=dp[dp[i][j-1]][j-1];
}
}
}
int getKthAncestor(int node, int k) {
for(int i=0;i!=20;++i){
if(k&(1<<i))node=dp[node][i]; //枚举二进制位
if(node==-1)return -1;
}
return node;
}
};
/**
* Your TreeAncestor object will be instantiated and called as such:
* TreeAncestor* obj = new TreeAncestor(n, parent);
* int param_1 = obj->getKthAncestor(node,k);
*/
结果:
执行用时:644 ms, 在所有 C++ 提交中击败了87.91% 的用户
内存消耗:114.1 MB, 在所有 C++ 提交中击败了100.00% 的用户