-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
// 1151. 最少交换次数来组合所有的 1 | ||
|
||
// 给出一个二进制数组 data,你需要通过交换位置,将数组中 任何位置 上的 1 组合到一起,并返回所有可能中所需 最少的交换次数。 | ||
|
||
class Solution { | ||
public: | ||
int minSwaps(vector<int>& data) { | ||
int size = data.size(); | ||
int oneCount = 0; | ||
for(int i=0;i<size;++i){ | ||
if(data[i]>0){ | ||
++oneCount; | ||
} | ||
} | ||
if(oneCount<=1){ | ||
return 0; | ||
} | ||
// 窗口内1的数量 | ||
int sum = 0; | ||
// oneCount为窗口大小 | ||
for(int i=0;i<oneCount;++i){ | ||
sum += data[i]; | ||
} | ||
int maxSum = sum; | ||
for(int i=oneCount;i<size;++i){ | ||
sum += data[i]-data[i-oneCount]; | ||
maxSum = sum>maxSum ? sum:maxSum; | ||
} | ||
// 窗口大小-窗口内数量最多的1 = 最少需要移动的0的数量 | ||
return oneCount-maxSum; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
// 325. 和等于 k 的最长子数组长度 | ||
// 给定一个数组 nums 和一个目标值 k,找到和等于 k 的最长连续子数组长度。 | ||
// 如果不存在任意一个符合要求的子数组,则返回 0。 | ||
|
||
class Solution { | ||
public: | ||
// 前缀和+暴力枚举 超时 | ||
int maxSubArrayLen1(vector<int>& nums, int k) { | ||
int maxLen = 0; | ||
int size = nums.size(); | ||
vector<int> prefixSum(size+1,0); | ||
for(int i=1;i<=size;++i){ | ||
prefixSum[i] = prefixSum[i-1] + nums[i-1]; | ||
} | ||
int len = 0; | ||
for(int i=0;i<=size;++i){ | ||
for(int j=i;j<=size;++j){ | ||
if(prefixSum[j]-prefixSum[i]==k){ | ||
len = j-i; | ||
maxLen = len>maxLen ? len : maxLen; | ||
} | ||
} | ||
} | ||
return maxLen; | ||
} | ||
// 前缀和+hash优化 | ||
int maxSubArrayLen(vector<int>& nums, int k) { | ||
int maxLen = 0; | ||
int size = nums.size(); | ||
// 用sum表示包含该数的和 | ||
long sum = 0; | ||
// <第i个数之前的前缀和,i> | ||
unordered_map<long,int> hash(size+1); | ||
hash[0] = 0; | ||
for(int i=0;i<size;++i){ | ||
sum += nums[i]; | ||
if(hash.count(sum-k)>0){ | ||
// 若存在[a,b]的和为k,即sum[b]-prefixSum[a]==k | ||
maxLen = max(i-hash[sum-k]+1,maxLen); | ||
} | ||
// 保存该前缀和 | ||
// 若存在相同的前缀和只保存之前出现的 | ||
hash.insert(make_pair(sum,i+1)); | ||
} | ||
|
||
return maxLen; | ||
} | ||
}; |