5 kyu
Simple Fun #125: Array Equalization
54 of 195myjinxin2015
Description:
Task
You are given an array a
of positive integers and an intger k
. You may choose some integer X
and update a
several times, where to update means to perform the following operations:
pick a contiguous subarray of length not greater than the given k;
replace all elements in the picked subarray with the chosen X.
What is the minimum number of updates required to make all the elements of the array the same?
Example
For a = [1, 2, 2, 1, 2, 1, 2, 2, 2, 1, 1, 1] and k = 2
, the output should be 4
.
Here's how a will look like after each update:
[1, 2, 2, 1, 2, 1, 2, 2, 2, 1, 1, 1] ->
[1, 1, 1, 1, 2, 1, 2, 2, 2, 1, 1, 1] ->
[1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1] ->
[1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1] ->
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Input/Output
[input]
integer arraya
An array of positive integers.
Constraints:
10 ≤ a.length ≤ 50,
1 ≤ a[i] ≤ 10.
[input]
integerk
A positive integer, the maximum length of a subarray.
Constraints: 2 ≤ k ≤ 9.
[output]
an integer
The minimum number of updates.
Puzzles
Similar Kata:
Stats:
Created | Feb 15, 2017 |
Published | Feb 15, 2017 |
Warriors Trained | 1143 |
Total Skips | 133 |
Total Code Submissions | 2408 |
Total Times Completed | 195 |
JavaScript Completions | 54 |
C# Completions | 38 |
Python Completions | 103 |
Ruby Completions | 19 |
Total Stars | 25 |
% of votes with a positive feedback rating | 96% of 55 |
Total "Very Satisfied" Votes | 51 |
Total "Somewhat Satisfied" Votes | 4 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 4 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 6 kyu |