aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-08-23 19:48:07 -0600
committerJack Sangdahl <[email protected]>2025-08-23 19:48:07 -0600
commitbcb15f738e3a11bdf7389b21e57589855463aba8 (patch)
tree6d68f8443b540362e7e24d65d3a5accf5aba4d30
parent199f44e965e130cdd50a59163168f7ba448457dd (diff)
downloadleetcode-bcb15f738e3a11bdf7389b21e57589855463aba8.tar.gz
leetcode-bcb15f738e3a11bdf7389b21e57589855463aba8.zip
1493. longest subarray of 1's after deleting one element
-rw-r--r--longest_subarr_ones.c25
-rw-r--r--longest_subarr_ones.py32
2 files changed, 45 insertions, 12 deletions
diff --git a/longest_subarr_ones.c b/longest_subarr_ones.c
index 049ce33..dc9aa84 100644
--- a/longest_subarr_ones.c
+++ b/longest_subarr_ones.c
@@ -1,6 +1,5 @@
// 1493. Longest Subarray of 1's After Deleting One Element
-#include <math.h>
-#include <stdio.h>
+#include "leetcode.h"
/*
* given a binary array 'nums', you should delete one element from it. return
@@ -8,24 +7,26 @@
* resulting array. return 0 if there is no such subarray.
*/
-int longestSubarray(int *nums, int nums_size) {
- int n = nums_size, zero_cnt = 0, ans = 0, left = 0;
- for (int right = 0; right < n; right++) {
+int longestSubarray(int *nums, int numsSize) {
+ int left = 0, zeros = 0, ans = 0;
+ for (int right = 0; right < numsSize; right++) {
if (!nums[right])
- zero_cnt++;
- while (zero_cnt > 1) {
+ zeros++;
+ while (zeros > 1) {
if (!nums[left])
- zero_cnt--;
+ zeros--;
left++;
}
- ans = fmax(ans, right - left);
+ int length = right - left;
+ if (length > ans)
+ ans = length;
}
return ans;
}
int main() {
int n1[] = {1, 1, 0, 1}, n2[] = {0, 1, 1, 1, 0, 1, 1, 0, 1}, n3[] = {1, 1, 1};
- printf("%d\n", longestSubarray(n1, 4)); // expect: 3
- printf("%d\n", longestSubarray(n2, 9)); // expect: 5
- printf("%d\n", longestSubarray(n3, 3)); // expect: 2
+ printf("%d\n", longestSubarray(n1, ARRAY_SIZE(n1))); // expect: 3
+ printf("%d\n", longestSubarray(n2, ARRAY_SIZE(n2))); // expect: 5
+ printf("%d\n", longestSubarray(n3, ARRAY_SIZE(n3))); // expect: 2
}
diff --git a/longest_subarr_ones.py b/longest_subarr_ones.py
new file mode 100644
index 0000000..a4b3623
--- /dev/null
+++ b/longest_subarr_ones.py
@@ -0,0 +1,32 @@
+# 1493. Longest Subarray of 1's After Deleting One Element
+
+"""
+given a binary array 'nums', you should delete one element from it. return
+the size of the longest non-empty subarray containing only 1's in the
+resulting array. return 0 if there is no such subarray.
+"""
+
+
+class Solution(object):
+ def longestSubarray(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: int
+ """
+ left, zeros, ans = 0, 0, 0
+ for right in range(len(nums)):
+ if nums[right] == 0:
+ zeros += 1
+ while zeros > 1:
+ if nums[left] == 0:
+ zeros -= 1
+ left += 1
+ ans = max(ans, right - left)
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.longestSubarray(nums=[1, 1, 0, 1]))
+ print(obj.longestSubarray(nums=[0, 1, 1, 1, 0, 1, 1, 0, 1]))
+ print(obj.longestSubarray(nums=[1, 1, 1]))