Longest Subarray of 1's After Deleting One Element Solutions in C++
Number 1493
Difficulty Medium
Acceptance 59.6%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element//// Author : liuyubobobo/// Time : 2020-06-27#include <iostream>#include <vector>using namespace std;/// Split/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:int longestSubarray(vector<int>& nums) {vector<pair<int, int>> ones;int start = -1, res = 0;for(int i = 0; i <= nums.size(); i ++)if(i < nums.size() && nums[i])start = start == -1 ? i : start;else if(start != -1){res = max(res, i - start);ones.push_back({start, i});start = -1;}if(ones.size() == 0) return 0;if(ones.size() == 1 && ones[0].second - ones[0].first == nums.size()) return nums.size() - 1;for(int i = 1; i < ones.size(); i ++)if(ones[i - 1].second + 1 == ones[i].first)res = max(res, ones[i - 1].second - ones[i - 1].first + ones[i].second - ones[i].first);return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element//// Author : liuyubobobo/// Time : 2020-06-27#include <iostream>#include <vector>using namespace std;/// Sliding Window/// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {public:int longestSubarray(vector<int>& nums) {int res = 0, l = 0, r = -1, k = 0;while(l < nums.size()){if(k == 2 || r + 1 == nums.size()){k -= !nums[l ++];}else{if(r + 1 < nums.size())k += !nums[++r];res = max(res, r - l + 1 - k);}}return res == nums.size() ? res - 1 : res;}};int main() {return 0;}