public int solution2(int[] A, int[] B) { // 37% // Correctness 50% // Performance 25% var count = 0; System.Collections.Stack list = new System.Collections.Stack(); for (int i = 0; i < A.Length; i++) { var current = new fishObj() { Size = A[i], Direction = B[i] }; if (list.Count == 0 && current.Direction == 0) { count++; continue; } if (current.Direction == 1) { list.Push(current); } else { for (int x = 0; x < list.Count; x++) { var compare = (fishObj)list.Pop(); if (compare.Size > current.Size) { list.Push(compare); break; } else if (x == list.Count) { count++; // bug aqui } else { current = compare; } } } } //Console.WriteLine($"{solution2(new int[]{2,6,4,3,1,5}, new int[]{0,1,0,1,0,0})} - 2"); //0 - esquerda, 1 - direita) count = count + list.Count; // ou aqui return(count); }
// A size // B direction (0 - esquerda, 1 - direita) public int solution(int[] A, int[] B) { // 37% // Correctness 50% // Performance 25% System.Collections.Stack list = new System.Collections.Stack(); for (int i = 0; i < A.Length; i++) { var current = new fishObj() { Size = A[i], Direction = B[i] }; if (list.Count == 0) { list.Push(current); continue; } var last = (fishObj)list.Pop(); if (last.Direction == 0 && current.Direction == 1 || last.Direction == current.Direction) { list.Push(last); list.Push(current); } else { if (last.Size > current.Size) { list.Push(last); } else { list.Push(current); } } } return(list.Count); }