1690D - Black and White Stripe - CodeForces Solution


implementation two pointers *1000

Please click on ads to support us..

Python Code:

def ss(s,n,k):
    cost = [0]
    
    cap = [0]
    for i in s:
        if i=="W":
            cost.append(cost[-1]+1)
        else:
            cost.append(cost[-1])
        cap.append(cap[-1]+1)  
        
    
    
    x = float("inf")
    
    for i in range(n-k+1):
        j = i+k
        x  = min(x,cost[j]-cost[i])
        
    return x


for _ in range(int(input())):
    n,k = map(int,input().split())
    s = input()
    print(ss(s,n,k))

C++ Code:

// Code by Krushikesh Shashikant Pisal

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define all(x) (x).begin(), (x).end()
#define pb push_back;

ll factorial(ll num)
{

    return (num == 1 or num == 0) ? 1 : (num * factorial(num - 1)) % 1000000007;
}

bool isPrime(int n)
{
    if (n <= 1)
    {
        return false;
    }
    if (n <= 3)
    {
        return true;
    }

    if (n % 2 == 0 || n % 3 == 0)
    {
        return false;
    }
    // Using concept of prime number
    // can be represented in form of
    // (6*n + 1) or(6*n - 1) hence
    // we have to go for every multiple of 6 and
    // prime number would always be 1 less or 1 more than
    // the multiple of 6.
    for (int i = 5; i * i <= n; i = i + 6)
    {
        if (n % i == 0 || n % (i + 2) == 0)
        {
            return false;
        }
    }
    return true;
}

ll ceil_fun(ll n, ll num)
{
    ll num2 = n / num;
    if (n % num == 0)
        return num2;
    else
        return num2 + 1;
}

void krushikesh(ll s)
{
    cout<<s<<endl;

}
int main()
{
    ll T = 1;
    string ans;
    ll answer;
    cin >> T;
    while (T--)
    {
        ll n,k;
        cin>>n>>k;

        string s;
        cin>>s;


       vector<ll> v(n);
       ll counter=0;
       for(ll i=0;i<n;i++){
            if(s[i]=='W'){
                counter++;
            }
            v[i]=counter;
       }
        
        ll temp,j;
        ll min_val=INT_MAX;

       for(ll i=k-1;i<n;i++){
            j=i-(k-1);
            temp=v[i]-v[j];
            if(s[j]=='W'){
                temp++;
            }

            min_val=min(min_val,temp);
       }

       answer=min_val;
       krushikesh(answer);
    }
    return 0;
}




/*
    0 1 2 3 4 5 6 7 8 9 10 11 12 13
*/


Comments

Submit
0 Comments
More Questions

660A - Co-prime Array
1692F - 3SUM
1470A - Strange Birthday Party
190D - Non-Secret Cypher
1721B - Deadly Laser
1721C - Min-Max Array Transformation
1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas
1037D - Valid BFS
1144F - Graph Without Long Directed Paths
1228A - Distinct Digits
355B - Vasya and Public Transport
1230A - Dawid and Bags of Candies
1530A - Binary Decimal
1472D - Even-Odd Game
441C - Valera and Tubes
1328E - Tree Queries
265A - Colorful Stones (Simplified Edition)
296A - Yaroslav and Permutations
967B - Watering System
152A - Marks
1398A - Bad Triangle
137A - Postcards and photos
1674D - A-B-C Sort
334A - Candy Bags
855A - Tom Riddle's Diary
1417A - Copy-paste
1038A - Equality
1061A - Coins