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 array a

An array of positive integers.

Constraints:

10 ≤ a.length ≤ 50,

1 ≤ a[i] ≤ 10.

  • [input] integer k

A positive integer, the maximum length of a subarray.

Constraints: 2 ≤ k ≤ 9.

  • [output] an integer

The minimum number of updates.

Puzzles

Stats:

CreatedFeb 15, 2017
PublishedFeb 15, 2017
Warriors Trained1143
Total Skips133
Total Code Submissions2408
Total Times Completed195
JavaScript Completions54
C# Completions38
Python Completions103
Ruby Completions19
Total Stars25
% of votes with a positive feedback rating96% of 55
Total "Very Satisfied" Votes51
Total "Somewhat Satisfied" Votes4
Total "Not Satisfied" Votes0
Total Rank Assessments4
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • JohanWiltink Avatar
  • hobovsky Avatar
  • dfhwze Avatar
  • Just4FunCoder Avatar
  • avermakov Avatar
Ad