A long time ago, when the most basic model of an HP3000 computer used to cost $95,000 (over half a million in today's dollars), a very wise man called Gordon Moore made predictions about how computers would become cheaper and more powerful each year. According to Moore, the number of components per integrated circuit would double every two years. Thanks to the creative, determined engineers working in circuit printing technology, we do indeed have smaller, cheaper and more powerful computers today.
Circuit printing, as we call photolithography, is an extremely complex yet rewarding field, and ASML needs the best software engineers in the world to make this magic happen. We work closely with our clients to help them print their circuits in the most effective way. One of our clients requests us to write a method to optimize their circuit efficiency. The circuit is represented as a string consisting of the letters "M" and "L", where M represents Memory units and L represents Logic units. The efficiency of the circuit is measured as the length of the longest interval of letters "M". For example, given input string "LMMMLMMMMLLLM", the longest interval is 4.
Our customer wants to change the circuit in such a way that the longest M-interval will be equal to K. We can change any unit at any position in the circuit, i.e. either we can change any "M" to "L" or any "L" to "M". The objective of this challenge is to calculate the minimum number of changes we have to make in order to achieve the desired longest M-interval length K.
Write a function:
class Solution { public int solution(String S, int K); }
where the first argument, S, represents the circuit as a string of length N that consists of only characters "M" and/or "L" and the second argument, K, is the desired longest M-interval in the string. The return value shall be the minimum number of changes to achieve K as the longest M-interval in the input string.
For example, given S = "MLMMLLM" and K = 3, your function should return 1. We can change the letter at position 4 (counting from 0) to obtain "MLMMMLM", in which the longest interval of letters "M" is exactly three characters long.
For another example, given S = "MLMMMLMMMM" and K = 2, your function should return 2. We can, for example, modify the letters at positions 2 and 7 to get the string "MLLMMLMLMM", which satisfies the desired property.
Write an efficient algorithm for the following assumptions:
- string S is made only of the characters 'M' and/or 'L';
- N is an integer within the range [1..100,000];
- K is an integer within the range [0..N].
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen m
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen -> MMMMM
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen -> MMMMMMM
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen -> MMMMMMMM
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen -> MMMMMMMM
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Ich muss nur die langen ver
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Ich muss die langen verkürzen und sonst nichts machen, wenn alle Ms
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Ich muss die langen verkürzen und sonst nichts machen, wenn nicht alle Ms < K sind
//-> Case1: Alle Ms < K
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Ich muss die langen verkürzen und sonst nichts machen, wenn nicht alle Ms < K sind
//-> Case1: Alle Ms < K
//-> Case2: Es gibt
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Ich muss die langen verkürzen und sonst nichts machen, wenn nicht alle Ms < K sind
//-> Case1: Alle Ms < K
//-> Case2: Es gibt Ms >= K
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Ich muss die langen verkürzen und sonst nichts machen, wenn nicht alle Ms < K sind
//-> Case1: Alle Ms < K
//-> Case2: Es gibt Ms >= K
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//-> Case1: Alle Ms < K
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Ich muss die langen verkürzen und sonst nichts machen, wenn nicht alle Ms < K sind
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//-> Case1: Alle Ms < K
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//-> Case1: Alle Ms < K
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//
//-> Case1: Alle Ms < K
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
/
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist.
//2.Ggfls
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist.
//2.Ggfls nochmal durchiterieren und Case 1 fixen
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
for(int i=0;i<S.length)
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
for(int i=0;i<S.length();i++){
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
int lastMLength=0;
for(int i=0;i<S.length();i++){
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int lastMLength=0;
for(int i=0;i<S.length();i++){
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if()
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength)
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K)
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K)
isCase2 = true;
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K)
isCase2 = true;
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K)
isCase2 = true;
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
totalNeededOperationsCase2+=lastMLength
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
totalNeededOperationsCase2+=lastMLength
}
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for()
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
}
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
}
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int totalNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
}
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int totalNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Bei allen Ms anfangen und die nötigen changes zählen, das minimum nehmen
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k)
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
}
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
if(c!='M'){
lCounter++;
}
}
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
if(k==N)
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
if(k==N) return minNeededOperationsCase1;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then
if(k==N) return minNeededOperationsCase1;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
if()
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar =
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operationsneeded
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operationsneeded
//
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
//compare to current minimum
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
int currentOpsNeeded=minNeededOperationsCase1;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
//compare to current minimum
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
int currentOpsNeeded=minNeededOperationsCase1 - 1;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
//compare to current minimum
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
//compare to current minimum
if(c=='M'){
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
}
//compare to current minimum
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
//compare to current minimum
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar)
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i)
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && )
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if()
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 =
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<k;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
Solution.java:47: error: cannot find symbol for(int i=0;i<k;i++){ ^ symbol: variable k location: class Solution Solution.java:55: error: cannot find symbol if(k==N) return minNeededOperationsCase1; ^ symbol: variable k location: class Solution Solution.java:55: error: cannot find symbol if(k==N) return minNeededOperationsCase1; ^ symbol: variable N location: class Solution Solution.java:60: error: cannot find symbol if(S.charAt(k)=='M') minNeededOperationsCase1++; ^ symbol: variable k location: class Solution Solution.java:62: error: cannot find symbol for(int i=k;i<S.length();i++){ ^ symbol: variable k location: class Solution Solution.java:64: error: cannot find symbol char firstChar = S.charAt(i-k); ^ symbol: variable k location: class Solution 6 errors
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(k==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(k)=='M') minNeededOperationsCase1++;
for(int i=k;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-k);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==N) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
Solution.java:55: error: cannot find symbol if(K==N) return minNeededOperationsCase1; ^ symbol: variable N location: class Solution 1 error
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=0;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
['MMLMMLMMLMMLMM', 3]
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.M;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.floor(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
function result: 2
function result: 3
function result: 9
['MMLMMLMMLMMLMM', 3]
['MMMLMMMMMLMM', 2]
['MMMLMMMMMLMM', 1]
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.rio(lastMLength/K+0.5)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.round(lastMLength/K)-1;
}
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
function result: 2
function result: 3
function result: 9
['MMLMMLMMLMMLMM', 3]
['MMMLMMMMMLMM', 2]
['MMMLMMMMMLMM', 1]
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.round(lastMLength/K)-1;
}
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.round(lastMLength/K)-1;
}
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
function result: 2
function result: 1
function result: 6
['MMLMMLMMLMMLMM', 3]
['MMMLMMMMMLMM', 2]
['MMMLMMMMMLMM', 1]
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.round(lastMLength/K)-1;
}
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.round(lastMLength/K)-1;
}
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
}
else{ //Can only be L
if(lastMLength >= K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
if(isCase2){
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
totalNeededOperationsCase2+=Math.round(lastMLength/K)-1;
}
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength >= K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength >=
K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Um die langen Ms zu verkürzen muss ich ihre länge durch K teilen, runden und 1 abziehen
//-> MMMMMMMM 8/3=2.6 2.6~= 3 , 3-1=2 -> 2 changes
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
function result: 2
function result: 2
function result: 4
['MMLMMLMMLMMLMM', 3]
['MMMLMMMMMLMM', 2]
['MMMLMMMMMLMM', 1]
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
}
function result: 2
function result: 2
function result: 4
function result: 4
function result: 3
function result: 8
['MMLMMLMMLMMLMM', 3]
['MMMLMMMMMLMM', 2]
['MMMLMMMMMLMM', 1]
['LLLLLLLLLLLLLLLL', 4]
['MMMMMMMMMMMMMMMM', 4]
['MLMLLLMMMLMMM', 0]
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
//
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
//Tests 2,2,4,4,3,8
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
//Tests 2,2,4,4,3,8
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
/* OLD SOLUTION
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
*/
}
//Tests 2,2,4,4,3,8
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
/* OLD SOLUTION
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
*/
}
//Tests 2,2,4,4,3,8
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
/* OLD SOLUTION
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
*/
}
//Tests 2,2,4,4,3,8
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
/* OLD SOLUTION
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
*/
}
//Tests 2,2,4,4,3,8
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
//Tests 2,2,4,4,3,8
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
//Tests 2,2,4,4,3,8
}
function result: 2
function result: 2
function result: 4
function result: 4
function result: 3
function result: 8
function result: 2
['MMLMMLMMLMMLMM', 3]
['MMMLMMMMMLMM', 2]
['MMMLMMMMMLMM', 1]
['LLLLLLLLLLLLLLLL', 4]
['MMMMMMMMMMMMMMMM', 4]
['MLMLLLMMMLMMM', 0]
['MLMMMMLMMLLLL', 8]
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
//Tests 2,2,4,4,3,8
}
function result: 2
function result: 2
function result: 4
function result: 4
function result: 3
function result: 8
function result: 2
['MMLMMLMMLMMLMM', 3]
['MMMLMMMMMLMM', 2]
['MMMLMMMMMLMM', 1]
['LLLLLLLLLLLLLLLL', 4]
['MMMMMMMMMMMMMMMM', 4]
['MLMLLLMMMLMMM', 0]
['MLMMMMLMMLLLL', 8]
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S, int K) {
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=3
//MLMMMLLLMLMLMMLLLMMMMMLMLMLLMM K=7
//1,-1,3,-3,1,-1,1,-1,2,-3,5,-1,1,-1,1,-2,2
//-> Case1: Alle Ms < K
//Alle Substrings der länge K ansehen und die Ls zählen (+1 pro M an der Grenze)
//-> Case2: Es gibt Ms >= K
//Algorithmus vorschlag:
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
//2.Ggfls nochmal durchiterieren und Case 1 fixen
//Optimierung kommt später
//TODO put all in one loop
//1.Einmal durchiterieren und schauen ob es Case 2 ist. (Und gleich solven)
boolean isCase2 = false;
int totalNeededOperationsCase2=0;
int minNeededOperationsCase1=Integer.MAX_VALUE;
int lastMLength=0;
for(int i=0;i<S.length();i++){
char c = S.charAt(i);
if(c=='M'){
lastMLength++;
if(lastMLength > K){
isCase2 = true;
totalNeededOperationsCase2++;
lastMLength=0;
}
}
else{ //Can only be L
lastMLength=0;
}
}
if(!isCase2){
int lCounter=0;
for(int i=0;i<K;i++){
char c = S.charAt(i);
if(c!='M'){
lCounter++;
}
}
minNeededOperationsCase1 = lCounter;
//because then this is the only case
if(K==S.length()) return minNeededOperationsCase1;
//This needs to happen before in case the value is incremented (we dont know)
int currentOpsNeeded=minNeededOperationsCase1;
//In this case the M train has become too big
if(S.charAt(K)=='M') minNeededOperationsCase1++;
for(int i=K;i<S.length();i++){
char c = S.charAt(i);
char firstChar = S.charAt(i-K);
//adapt current operations needed value
if(c=='L'){
currentOpsNeeded++;
}
if(firstChar=='L'){
currentOpsNeeded--;
}
//compare to current minimum
int additionForComparison = 0;
if(firstChar == 'M'){
additionForComparison++;
}
if(i<S.length()-1 && S.charAt(i+1)=='M'){
additionForComparison++;
}
if(currentOpsNeeded+additionForComparison < minNeededOperationsCase1){
minNeededOperationsCase1 = currentOpsNeeded+additionForComparison;
}
}
return minNeededOperationsCase1;
}
else{
return totalNeededOperationsCase2;
}
}
//Tests 2,2,4,4,3,8
}
The solution obtained perfect score.