In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how LeetCode solves the problem of reshaping matrices. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Topic description:
In array A containing only 0 and 1, a K-bit flipping involves selecting a (contiguous) subarray of length K while changing each 0 in the subarray to 1 and each 1 to 0.
Returns the minimum number of K-bit flips required so that the array has no elements with a value of 0. If not, return-1.
Example 1:
Input: a = [0je 1jue 0], K = 1
Output: 2
Explanation: first flip A [0], then flip A [2].
Example 2:
Input: a = [1pm 1J 0], K = 2
Output:-1
Explanation: no matter how much we flip a subarray of size 2, we can't turn the array into [1].
Example 3:
Input: a = [0pt0pl 0jc0j0pl], K = 3
Output: 3
Explanation:
Flip A [0], A [1], A [2]: a becomes [1magin 1recrimin1pence0jing0jing1jingjing0]
Flip A [4], A [5], A [6]: a becomes [1pjre 1pr 1pr 1pr 0pr 0pl 0]
Flip A [5], A [6], A [7]: a becomes [1magin 1rect 1pm 1jue 1jue 1jue 1]
Analysis of ideas:
Every time you flip a subarray of length K, find the minimum number of flips so that all zeros in the array are changed to 1. If it cannot be implemented, it returns-1.
Conclusion 1: the flip of the latter interval will not affect the previous elements. So you can use the greedy strategy, traverse from left to right, and flip it and the number of Ks that follow each 0. Conclusion 2 the result of even times of flipping is A [I], and the result of odd times of flipping is A [I] ^ 1.
The main reason for the timeout of the above method is that we actually flipped it. According to conclusion 2, the current state of position I is related to the number of times it has been reversed by the previous K-1 elements (parity).
We use the queue to simulate the sliding window, which means that in the previous K-1 elements, where the starting sub-interval is flipped. The sliding window slides from left to right, and if the current position I needs to be flipped, the position is stored in the queue. Traverse to the new location j (j)
< i + K)时,队列中元素的个数代表了 i被前面 K - 1个元素翻转的次数。 当 i 位置被翻转了偶数次,如果 A[i]为 0,那么翻转后仍是 0,当前元素需要翻转; 当 i 位置被翻转了奇数次,如果 A[i]为 1,那么翻转后是 0,当前元素需要翻转。 综合上面两点,我们得到一个结论,如果 len(que) % 2 == A[i] 时,当前元素需要翻转。 当 i + K >When N, it means that the subinterval of size K needs to be flipped, but the remaining elements are less than K, so-1 is returned.
Python implements class Solution (object):
Def minKBitFlips (self, A, K):
"
: type A: List [int]
: type K: int
: rtype: int
"
N = len (A)
Que = collections.deque ()
Res = 0
For i in range (N):
If que and i > = que [0] + K:
Que.popleft ()
If len (que)% 2 = A [I]:
If I + K > N: return-1
Que.append (I)
Res + = 1
Return res
Author: fuxuemingzhu
Link: https://leetcode-cn.com/problems/minimum-number-of-k-consecutive-bit-flips/solution/hua-dong-chuang-kou-shi-ben-ti-zui-rong-z403l/
Source: power buckle (LeetCode)
The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.
Java implements class Solution {
Public int minKBitFlips (int [] A, int K) {
Int len=A.length
Int res=0
Deque deque=new LinkedList ()
For (int ionome0transferi0 & & I > deque.peek () + Kmuri 1) {
Deque.removeFirst ()
}
If (deque.size ()% 2) / I) {
If (iTunk > len) {
Return-1
}
Deque.add (I)
Res=res+1
}
}
Return res
}
}
Thank you for reading! This is the end of the article on "how LeetCode solves the problem of reshaping matrix". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.