Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize sliding window by leetcode

2025-04-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces leetcode how to achieve sliding window, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The basic skill of sliding window is to maintain a sliding window and move the left and right pointer of the window to make the window meet certain conditions. the key is how to deal with the places where the window meets the conditions to make the algorithm more efficient.

The number of maximum consecutive ones

Given an array A consisting of several zeros and ones, we can change the values of K from 0 to 1 at most.

Returns the length of the longest (consecutive) subarray containing only 1.

Example 1:

Input: a = [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], K = 2.

Output: 6

Explanation:

[1,1,1,0,0,1,1,1,1,1,1]

The bold number is flipped from 0 to 1, and the longest subarray is 6.

Example 2:

Input: a = [0re0pence1reparent1jor0re0re0reie1jjjjjjjjjjjjjjjjng.0jpg], K = 3

Output: 10

Explanation:

[0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]

The bold number is flipped from 0 to 1, and the longest subarray is 10.

Ideas for solving the problem:

1. The main point of this question is not the length of the sliding window, but that to maintain the number of zeros in the window K, you need to move the left pointer to subtract the number of zeros by 1.

B,sum

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 260

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report