LeetCode //C - 1493. Longest Subarray of 1‘s After Deleting One Element

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.
 

Example 1:

Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1’s.

Example 2:

Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1’s is [1,1,1,1,1].

Example 3:

Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.

Constraints:
  • 1 < = n u m s . l e n g t h < = 1 0 5 1 <= nums.length <= 10^5 1<=nums.length<=105
  • nums[i] is either 0 or 1.

From: LeetCode
Link: 1493. Longest Subarray of 1’s After Deleting One Element


Solution:

Ideas:
  1. Keep track of the length of the current sequence of 1’s (curr_len).
  2. Keep track of the length of the previous sequence of 1’s (prev_len).
  3. When encountering a 0, update max_len if the sum of curr_len and prev_len plus 1 (for the current 0) is greater than max_len.
  4. After encountering a 0, update prev_len to curr_len (since we can potentially delete this 0 to connect the previous and current sequences of 1’s) and reset curr_len.
  5. After the loop, perform one last check to update max_len because the array might end with a sequence of 1’s.
  6. Return max_len - 1 because we must delete one element. However, if the array contains only 1’s, the function should return numsSize - 1.
Code:
int longestSubarray(int* nums, int numsSize) {
   
    int max_len = 0;
    int curr_len = 0;
    int prev_len = 0;

    for (int i = 0; i < numsSize; i++) {
   
        if (nums[i] == 1) {
   
            curr_len++;
        } else {
   
            max_len = (prev_len + curr_len > max_len) ? prev_len + curr_len : max_len;
            prev_len = curr_len + 1; // Include the current 0 as it could be deleted
            curr_len = 0;
        }
    }

    max_len = (prev_len + curr_len > max_len) ? prev_len + curr_len : max_len;

    // Since we need to delete one element, subtract 1, except when the array is all 1s
    return (numsSize != curr_len) ? max_len - 1 : numsSize - 1;
}

相关推荐

  1. [qt][报错】[Makefile:1293: moc_widget.cpp] Error 1

    2023-12-27 13:06:02       8 阅读
  2. mysql error: #1093

    2023-12-27 13:06:02       37 阅读
  3. LeetCode 1193, 45, 48

    2023-12-27 13:06:02       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-27 13:06:02       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-27 13:06:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-27 13:06:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-27 13:06:02       18 阅读

热门阅读

  1. 数组增删查

    2023-12-27 13:06:02       39 阅读
  2. koa开发基础配置

    2023-12-27 13:06:02       48 阅读
  3. Alibaba Cloud Linux 3.2104 LTS 64位系统可以选择吗?

    2023-12-27 13:06:02       46 阅读
  4. <math.h> 头文件:C语言数学库函数

    2023-12-27 13:06:02       36 阅读
  5. NPM简介与使用指南:打造前端开发的利器

    2023-12-27 13:06:02       42 阅读
  6. chrome去除安全设置

    2023-12-27 13:06:02       41 阅读
  7. 在css中如何实现表单验证效果

    2023-12-27 13:06:02       45 阅读
  8. 如何强制 App 在 iOS 后台不断开与融云的长连接?

    2023-12-27 13:06:02       63 阅读
  9. 活动运营常用的ChatGPT通用提示词模板

    2023-12-27 13:06:02       40 阅读
  10. modbus-tcp-rtu协议图表

    2023-12-27 13:06:02       28 阅读
  11. leetcode | go | 第600题 | 不含连续1的非负整数

    2023-12-27 13:06:02       40 阅读
  12. vue中的动态组件和混入

    2023-12-27 13:06:02       34 阅读