Sort the array and check if the start and end of the array match. If not, increment or decrement accordingly.

First sort the array. Then use two pointers. One at the beginning, the other at the end. Check to see if the value at the beginning (the smallest integer) is the same as the value at the end (the highest integer) once converted from negative to positive.

If it's the same, return that value.

If the converted integer is smaller, decrement the right pointer.

If the converted integer is larger, increment the left pointer.

If the left and right pointers meet, there is no matching value so return -1.

`public class Solution { public int FindMaxK(int[] nums) { Array.Sort(nums); int left = 0; int right = nums.Length - 1; while(left < right) { var comparer = nums[left] * -1; if(comparer == nums[right]) { return comparer; } else if(comparer < nums[right]) { right--; } else { left++; } } return -1; }}`

]]>Find position of ch (if any) then use two pointers to swap characters until string is reveresed from beginning to index of ch

Start by checking each character for ch. If we find it, that's where our right pointer will start. Our left pointer will start at the beginning of the string.

While left is less than right swap characters and then decrement right, and increment left. When they meet the substring beginning at index 0 and ending at the index of ch will be reversed.

`public class Solution { public string ReversePrefix(string word, char ch) { var arr = word.ToCharArray(); int left = 0; int right = 0; //find pos of ch if it exists while(right < arr.Length && arr[right] != ch) { right++; } if(right == arr.Length) { //if not return the original word return word; } //two pointers, one at ch, the other at the start of the string while(left < right) { //swap the two characters, increase the left pointer, decrease the right var temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } //when both pointers meet, return return new string(arr); }}`

]]>Use two pointers to iterate through the string checking each adjacent character to see if it is "bad" or "good".

First if the string is 1 or fewer characters we know it is good so we can just return it.

If that's not the case, we change it to an array and then to a list so we can dynamically resize it.

Then while p2 is less than the count of the list, we just iterate through and using the ASCII values, check to see if the two characters are bad. If they are, we remove them from the list and reset out pointers. If they are good, we just increment the pointers.

Finally when we reach the end of the list we return the remaining string.

`public class Solution { public string MakeGood(string s) { if(s.Length == 0 || s.Length == 1) { return s; } //convert string to list var arr = s.ToCharArray(); var list = new List<char>(arr); //iterate through string with 2 pointers var p1 = 0; var p2 = 1; while(p2 < list.Count) { var num1 = list[p1]; var num2 = list[p2]; //check if adjacent characters are bad if(Math.Abs(num1 - num2) == 32) { //if so remove them and reset pointers list.RemoveRange(p1, 2); p1 = 0; p2 = 1; } else { p1 = p2; p2++; } } return new string(list.ToArray()); }}`

]]>Since both arrays are sorted, we can find the minimum common value by iterating from left to right with two pointers.

Initialize both pointers to 0 to start at the beginning of the array. While both pointers are within bounds of the array, check to see if they are pointing to the same value. If so, we have our answer. If not, increment the smaller value.

Because these arrays are sorted and we started at the beginning I.E. with the smallest values, we know the current smallest values don't match and therefore we must check if the next smallest value does match.

Finally if we get through both arrays without finding a match, return -1 since there is no common value.

Time complexity:

O(n)O(n)*O*(*n*) since at worst case we would have to go through all nn*n*values of the arrays.Space complexity:

O(1)O(1)*O*(1) since we are not using any extra space (besides the pointers).

`public class Solution { public int GetCommon(int[] nums1, int[] nums2) { //create two pointers, one for each array int num1 = 0; int num2 = 0; //loop while both pointers are in range while(num1 < nums1.Length && num2 < nums2.Length) { //if the number at both pointers is the same, we found our result if(nums1[num1] == nums2[num2]) { return nums1[num1]; } //if num1 is smaller, increment it by 1 else if(nums1[num1] < nums2[num2]) { num1++; } //otherwise if num2 is smaller, increment it by 1 else { num2++; } } //if end of loop is reached, no result was found return -1; }}`

]]>First check to make sure there's enough elements in the original matrix to meet the new row and column requirements. I did this by creating a new list to store all of the original values (which we will then use later to fill the new matrix) and then get the count of the list and ensure it's the same as the required amount of rows and columns by multiplying r * c and comparing to the count.

If there's not enough, just return the original matrix.

If there is enough, create the new matrix and keep track of what posisiton we're at in the list of original values. Then iterate through the rows and columns and at each new row initialize the column array. At each column add the value and increment the original position.

`public class Solution { public int[][] MatrixReshape(int[][] mat, int r, int c) { //create new list to store original values var nums = new List<int>(); //iterate through original matrix adding values to list for(int row = 0; row < mat.Length; row++) { for(int col = 0; col < mat[row].Length; col++) { nums.Add(mat[row][col]); } } //check that there's enough values to fill up required rows and columns if((r * c) != nums.Count) { //if there's not enough, return original matrix return mat; } //create new matrix var answer = new int[r][]; //keep track of position in the list of original values var originalPos = 0; //iterate through rows and columns adding the value at the current //position in the list of original numbers for(int row = 0; row < r; row++) { //in C# you have to manually create the new columns //so do this when we loop to a new row answer[row] = new int[c]; for(int col = 0; col < c; col++) { answer[row][col] = nums[originalPos]; //iterate position in list of original numbers originalPos++; } } return answer; }}`

]]>Loop through input array and fill answer array in one pass.

In one forloop set the ans[i] and ans[i + nums.Length]

to the current value at nums[i].

`public class Solution { public int[] GetConcatenation(int[] nums) { var numsLength = nums.Length; var ansLength = numsLength * 2; int[] ans = new int[ansLength]; for(int i = 0; i < numsLength; i++) { ans[i] = nums[i]; ans[i + numsLength] = nums[i]; } return ans; }}`

]]>`return the numbers with the highest count. Bucket sort. Iterate through array counting occurences and storing them in a dictionary. Then add the values to a list of lists wherethe index of the list is the count of the occurences of that value. ```public class Solution { public int[] TopKFrequent(int[] nums, int k) { var counts = new Dictionary<int, int>(); var countsArr = new List<List<int>>(); for(int i = 0; i < nums.Length; i++) { //add new list to store numbers for each index countsArr.Add(new List<int>()); //count occurences and add to dictionary if(counts.ContainsKey(nums[i])) { counts[nums[i]]++; } else { counts.Add(nums[i], 1); } } //loop through dictionary and add counts to corresponding //index of countsArr foreach(var val in counts) { //add the value - 1 to match zero based index of array countsArr[val.Value -1].Add(val.Key); } //create array to hold result, make it length k int[] result = new int[k]; //keep track of what index in the result we are currently setting int curr = 0; //loop through countsArr backwards to find biggest results first for(int i = countsArr.Count - 1; i >= 0; i--) { //check if current index has any values if(countsArr[i].Count > 0) { //loop through values at index, adding them to the result for(int j = 0; j < countsArr[i].Count; j++) { //if curr == k we have added enough numbers // to the result array, so return result if(curr == k) { return result; } //otherwise set the current index of the result array // to the current value result[curr] = countsArr[i][j]; //increment current curr++; } } } //return result to satisfy method signature return result; }}`

]]> Count number of occurences of each number, I am also hoping that if someone stumbles upon this it will be of some help because, personally, the more ways something is explained to me, the better chance I have of understanding it.

Use a Dictionary to map each word to its list of anagrams

by sorting each word for easier comparison. Where key is the sorted word and the value is a list of its anagrams contained in the strs array.

Loop through each word in strs and at each word:

Sort the word.

Check if the dictionary contains the sorted word as a key which would indicate an anagram of that word does exist, and we want to group this one with it.

Add this word to the list of anagrams which is attached to these sorted letters. Where the sorted letters are the dictionary key and the current anagram is added to the corresponding list which is the value attached to the key.

`public class Solution { public IList<IList<string>> GroupAnagrams(string[] strs) { var anagrams = new Dictionary<string, List<string>>(); IList<IList<string>> ans = new List<IList<string>>(); for(int i = 0; i < strs.Length; i++) { //get the current word in strs var temp = strs[i].ToCharArray(); //sort that word. i.e in the first iteration temp will //be [a, e, t] Array.Sort(temp); //convert the word to a string so we can add it to the //dictionary var current = new string(temp); //if this sorted word is in the dictionary then it is //an anagram of another word with the same letters if(anagrams.ContainsKey(current)) { //add the current word (eat) to the list attached to //the sorted word 'aet' anagrams[current].Add(strs[i]); } else { //if there is not a key for this sorted word either //there is no matching anagram, or we just haven't //found it yet. So add it to the dictionary anagrams.Add(current, new List<string>{strs[i]}); //add the list of anagrams attached to this sorted //word to the answer we will be returning ans.Add(anagrams[current]); } } //return the list of lists of anagrams return ans; }}`

]]>It was a small change, but a I'll take it!

So the application is a custom file browser to replace the built in one for Windows. The issue was that whenever you used the hotkey to open up a dialog box to add a new folder, file, shortcut, etc., if you selected what you wanted to create but then decided to cancel, the follow up dialog that would prompt you to name your new file would still display. Even the next time you used the hotkey.

After an hour or so of exploring and tracing how things work, I discovered that there is a property behind the dialog box that holds the user's selected action which represented what they were trying to do. I.e. Create a folder or cancel the dialog. Turns out that property was persisting. So all it took was one line.

`viewModel.ResultType.ItemType = AddItemDialogItemType.Cancel;`

And that was about 2 and a half hours of time. The moral is: just dive in and give it your best! You'll undoubtedly be able to find an issue somewhere that is within your skillset and will help out a project! Plus, it's a great feeling!

]]>