|
| 1 | +<p>There is a circle of red and blue tiles. You are given an array of integers <code>colors</code> and an integer <code>k</code>. The color of tile <code>i</code> is represented by <code>colors[i]</code>:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li><code>colors[i] == 0</code> means that tile <code>i</code> is <strong>red</strong>.</li> |
| 5 | + <li><code>colors[i] == 1</code> means that tile <code>i</code> is <strong>blue</strong>.</li> |
| 6 | +</ul> |
| 7 | + |
| 8 | +<p>An <strong>alternating</strong> group is every <code>k</code> contiguous tiles in the circle with <strong>alternating</strong> colors (each tile in the group except the first and last one has a different color from its <strong>left</strong> and <strong>right</strong> tiles).</p> |
| 9 | + |
| 10 | +<p>Return the number of <strong>alternating</strong> groups.</p> |
| 11 | + |
| 12 | +<p><strong>Note</strong> that since <code>colors</code> represents a <strong>circle</strong>, the <strong>first</strong> and the <strong>last</strong> tiles are considered to be next to each other.</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<div class="example-block"> |
| 18 | +<p><strong>Input:</strong> <span class="example-io">colors = [0,1,0,1,0], k = 3</span></p> |
| 19 | + |
| 20 | +<p><strong>Output:</strong> <span class="example-io">3</span></p> |
| 21 | + |
| 22 | +<p><strong>Explanation:</strong></p> |
| 23 | + |
| 24 | +<p><strong><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183519.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></strong></p> |
| 25 | + |
| 26 | +<p>Alternating groups:</p> |
| 27 | + |
| 28 | +<p><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-182448.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-182844.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-183057.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></p> |
| 29 | +</div> |
| 30 | + |
| 31 | +<p><strong class="example">Example 2:</strong></p> |
| 32 | + |
| 33 | +<div class="example-block"> |
| 34 | +<p><strong>Input:</strong> <span class="example-io">colors = [0,1,0,0,1,0,1], k = 6</span></p> |
| 35 | + |
| 36 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 37 | + |
| 38 | +<p><strong>Explanation:</strong></p> |
| 39 | + |
| 40 | +<p><strong><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183907.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></strong></p> |
| 41 | + |
| 42 | +<p>Alternating groups:</p> |
| 43 | + |
| 44 | +<p><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184128.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184240.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></p> |
| 45 | +</div> |
| 46 | + |
| 47 | +<p><strong class="example">Example 3:</strong></p> |
| 48 | + |
| 49 | +<div class="example-block"> |
| 50 | +<p><strong>Input:</strong> <span class="example-io">colors = [1,1,0,1], k = 4</span></p> |
| 51 | + |
| 52 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 53 | + |
| 54 | +<p><strong>Explanation:</strong></p> |
| 55 | + |
| 56 | +<p><img alt="" data-darkreader-inline-bgcolor="" data-darkreader-inline-bgimage="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184516.png" style="width: 150px; height: 150px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; --darkreader-inline-bgimage: initial; --darkreader-inline-bgcolor: #181a1b;" /></p> |
| 57 | +</div> |
| 58 | + |
| 59 | +<p> </p> |
| 60 | +<p><strong>Constraints:</strong></p> |
| 61 | + |
| 62 | +<ul> |
| 63 | + <li><code>3 <= colors.length <= 10<sup>5</sup></code></li> |
| 64 | + <li><code>0 <= colors[i] <= 1</code></li> |
| 65 | + <li><code>3 <= k <= colors.length</code></li> |
| 66 | +</ul> |
0 commit comments