//just a quick generic function that checks the top element of each stack //if the top element of A is smaller than B or B is empty public static bool CanMoveTo(this Stack <int> currentStack, Stack <int> otherStack) { return(otherStack.Count() == 0 || otherStack.Peek() > currentStack.Peek()); }
//static void MaxInContiguousArray() { // int[] arr = { 1, 2, 3, 1, 4, 5, 2, 3, 6 }; // Queue<int> q = new Queue<int>(); // int k = 4; // int max = 0; // int counter = 0; // int //} static long max(long[] hist, long n) { Stack <int> st = new Stack <int>(); int[] left_small = new int[(int)n]; int[] right_small = new int[(int)n]; for (int k = 0; k < n; k++) { if (st.Count() == 0) { left_small[k] = -1; } else if (hist[k] > hist[st.Peek()]) { left_small[k] = st.Peek(); } else { while (st.Count() > 0 && hist[k] <= hist[st.Peek()]) { st.Pop(); } if (st.Count() == 0) { left_small[k] = -1; } else if (hist[k] > hist[st.Peek()]) { left_small[k] = st.Peek(); } } st.Push(k); } st.Clear(); for (int k = (int)n - 1; k >= 0; k--) { if (st.Count() == 0) { right_small[k] = (int)n; } else if (hist[k] > hist[st.Peek()]) { right_small[k] = st.Peek(); } else { while (st.Count() > 0 && hist[k] <= hist[st.Peek()]) { st.Pop(); } if (st.Count() == 0) { right_small[k] = (int)n; } else if (hist[k] > hist[st.Peek()]) { right_small[k] = st.Peek(); } } st.Push(k); } long max = 0; for (int k = 0; k < n; k++) { Console.WriteLine(left_small[k] + " " + right_small[k]); long area = (right_small[k] - left_small[k] - 1) * hist[k]; max = max > area ? max : area; Console.WriteLine(area); } return(max); }
//takes a stack and the Tower[index] of the stack, and returns the position of the tower the stack will move to public int FindMove(Stack <int> currentStack, int i) { Console.WriteLine($"starting FindMove() with int i = {i}"); //just a quick check to make sure we are not checking an empty array if (currentStack.Count() == 0) { return(-1); //if there is no content return empty } //if 0, even, return 1 //if 0, odd, return 2 //if 1, even, return 0 //if 1, odd, return 2 //if 2, even, return 0 //if 2, odd, return 1 int[] remainingStackIndexes = new int[2]; if (i == 0) { remainingStackIndexes = new int[] { 1, 2 } } ; if (i == 1) { remainingStackIndexes = new int[] { 0, 2 } } ; if (i == 2) { remainingStackIndexes = new int[] { 0, 1 } } ; //if whichTower is 0, tower A //if whichTower is 1, tower B, else -1 = no move possible int whichTower = -1; //if height of current Stack is even, move to tower A (first in remainingStackIndexes) //at this point, currentStack can either move to tower B (last in newMoves) or it cannot move at all //we check if it can move to tower B, else return null; int Height = currentStack.Count(); Console.WriteLine(" Height = " + Height); if (Height % 2 == 0) { whichTower = 0; } if (Height % 2 == 1) { whichTower = 1; } Console.WriteLine(" whichTower = " + whichTower); int towerIndex = remainingStackIndexes[whichTower]; Console.WriteLine(" towerIndex = " + towerIndex); //if the tower we are moving to is empty, or bigger than the moving disk // then we have a valid move; return the destination if (Towers[towerIndex].Count == 0 || Towers[towerIndex].Peek() > Towers[i].Peek()) { Console.WriteLine(" We are returning a valid dest of " + towerIndex); return(towerIndex); } //we should only get here if the tower cannot move Console.WriteLine(" We are returning a -1 dest"); return(-1); } } }