Repainting Street (Codeforces 1457B)
Tags: Greedy, brute force

There is a street with $$$n$$$ houses in a line, numbered from $$$1$$$ to $$$n$$$. The house $$$i$$$ is initially painted in color $$$c_i$$$. The street is considered beautiful if all houses are painted in the same color. Tom, the painter, is in charge of making the street beautiful. Tom's painting capacity is defined by an integer, let's call it $$$k$$$.On one day, Tom can do the following repainting process that consists of two steps:   He chooses two integers $$$l$$$ and $$$r$$$ such that $$$ 1 \le l \le r \le n $$$ and $$$ r - l + 1 = k $$$.  For each house $$$i$$$ such that $$$l \le i \le r$$$, he can either repaint it with any color he wants, or ignore it and let it keep its current color. Note that in the same day Tom can use different colors to repaint different houses.Tom wants to know the minimum number of days needed to repaint the street so that it becomes beautiful.

Input: The first line of input contains a single integer $$$t$$$ ($$$ 1 \le t \le 10^4$$$), the number of test cases. Description of test cases follows.In the first line of a test case description there are two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 10^5$$$).The second line contains $$$n$$$ space-separated integers. The $$$i$$$-th of these integers represents $$$c_i$$$ ($$$1 \le c_i \le 100$$$), the color which house $$$i$$$ is initially painted in.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: Print $$$t$$$ lines, each with one integer: the minimum number of days Tom needs to make the street beautiful for each test case. 

Note: In the first test case Tom should paint houses 1 and 2 in the first day in color 2, houses 5 and 6 in the second day in color 2, and the last house in color 2 on the third day.In the second test case Tom can, for example, spend 6 days to paint houses 1, 2, 4, 5, 6, 7 in color 3.In the third test case Tom can paint the first house in the first day and houses 6, 7, and 8 in the second day in color 3.

Sample Inputs:
3
10 2
1 1 2 2 1 1 2 2 2 1
7 1
1 2 3 4 5 6 7
10 3
1 3 3 3 3 1 2 1 3 3

Sample Outputs:
3
6
2

Solution (Python):
from sys import stdin, stdout
from collections import Counter
from itertools import permutations
import bisect
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()

def solve():
    (n, k) = I()
    c = list(I())
    s = list(set(c))
    ans = 1000005
    for x in s:
        cnt = 0
        i = 0
        while i < n:
            if c[i] != x:
                i += k
                cnt += 1
            else:
                i += 1
        ans = min(ans, cnt)
    print(ans)
T = int(I1())
for _ in range(T):
    solve()

Solution (Python):
def fun(A, n, k):
    c = set(A)
    res = 10 ** 8
    for m in c:
        ans = 0
        i = 0
        while i < n:
            if A[i] != m:
                ans += 1
                i += k
            else:
                i += 1
        res = min(res, ans)
    return res
for _ in range(int(input())):
    (n, k) = map(int, input().split())
    A = list(map(int, input().split()))
    print(fun(A, n, k))

Solution (Python):
def solve_for_color(color, n, k, arr):
    (answer, head) = (0, 0)
    while head < n:
        while arr[head] == color:
            head += 1
            if head >= n:
                return answer
        (answer, head) = (answer + 1, head + k)
    return answer

def solve():
    (n, k) = map(int, input().split())
    house_paintings = list(map(int, input().split()))
    used_colors = set(house_paintings)
    return min(map(lambda c: solve_for_color(c, n, k, house_paintings), used_colors))
if __name__ == '__main__':
    for _ in range(int(input())):
        print(solve())