FastPrepFastPrep
Problem Brief

Find Minimum Idleness

FULLTIMEOA
See Salesforce online assessment and hiring insights

A game's shaders are rendered using two GPUs: a and b. There is a string s, which represents that for the f shader in which a GPU is used.

  • If shader[i] = "a" then the GPU a is used for the ith shader.
  • If shader[i] = "b" then the GPU b is used in the ith shader.
  • The idleness of this dual GPU system is defined as the maximum number of shaders for which the same GPU is used consecutively. For example, for the string shader = "aalbbbaa", for the first 2 seconds, GPU a is used then for the next 3 seconds, GPU b is used, then for 1 second, GPU a is used. Hence, the idleness of the system is 3.

    In order to reduce the idleness of the system, the following operation can be used at most switchCount times.

    • Select any index i of the string shader. If shader[i] = "a" then change it to shader[i] = "b" and vice versa.

    Find the minimum possible idleness of the system that can be achieved by applying the operations optimally.

    Function Description

    Complete the function findMinimumIdleness in the editor.

    findMinimumIdleness has the following parameters:

    1. String shader: a string representing the GPUs used
    2. int switchCount: the maximum number of switches allowed

    Returns

    int: the minimum possible idleness of the system

    1Example 1

    Input
    shader = "aabbbbaaaa", switchCount = 2
    Output
    2
    Explanation

    It is given that shader = "aabbbbaaaa" and switchCount=2. One optimal solution is:

    • Switch shader[4] (1-based index) to get shader = "aababaaaa".
    • Switch shader[7] (1-based index) to get shader = "aabababaa".

    Now with shader = "aabababaa", the system has an idleness of 2.

    Constraints

    Limits and guarantees your solution can rely on.

    ๐Ÿ‡๐Ÿ‡
    public int findMinimumIdleness(String shader, int switchCount) {
      // write your code here
    }
    
    Input

    shader

    "aabbbbaaaa"

    switchCount

    2

    Output

    2

    Sign in to submit your solution.