Combo Problems: Selection Sort, BST Reverse InOrder and Subsets
Three quick problems here, again from Daily Coding Problem:
1.
This problem was asked by Google.
Given an array of strictly the characters 'R', 'G', and 'B', segregate the values of the array so that all the Rs come first, the Gs come second, and the Bs come last. You can only swap elements of the array.
Do this in linear time and in-place.
For example, given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G'], it should become ['R', 'R', 'R', 'G', 'G', 'B', 'B'].
2.
Given the root to a binary search tree, find the second largest node in the tree.
3.
This problem was asked by Google.
The power set of a set is the set of all its subsets. Write a function that, given a set, generates its power set.
For example, given the set {1, 2, 3}, it should return {{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}.
The ideas to solve these problems are, respectively:
1.
An easy way to solve that would be by doing a bucket (or counting) sort: count the number of R, B and G using counters, and then reconstruct the string.
But the solution I wrote just swaps the characters using a similar idea as selection sort. It runs in linear time and in place. Code is here: https://github.com/marcelodebarros/dailycodingproblem/blob/master/DailyCodingProblem10192018.cs
2.
If you traverse the tree in order, the elements will be sorted from smallest to largest. In this case do a reverse-in-order (right node, current, left node) and the elements will be sorted from the largest to the smallest. Keep a count, and when you reach your target (in our case 2), return it. Code is here: https://github.com/marcelodebarros/dailycodingproblem/blob/master/DailyCodingProblem10202018.cs
3.
There will be 2^N subsets here. My solution is not recursive, just go from 0 to 2^N-1, do a bit analysis of each index i, and if the bit is on add the corresponding index to the current subset, if it isn't, then don't and continue. Code is here: https://github.com/marcelodebarros/dailycodingproblem/blob/master/DailyCodingProblem10212018.cs
Cheers, Marcelo
1.
This problem was asked by Google.
Given an array of strictly the characters 'R', 'G', and 'B', segregate the values of the array so that all the Rs come first, the Gs come second, and the Bs come last. You can only swap elements of the array.
Do this in linear time and in-place.
For example, given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G'], it should become ['R', 'R', 'R', 'G', 'G', 'B', 'B'].
2.
Given the root to a binary search tree, find the second largest node in the tree.
3.
This problem was asked by Google.
The power set of a set is the set of all its subsets. Write a function that, given a set, generates its power set.
For example, given the set {1, 2, 3}, it should return {{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}.
The ideas to solve these problems are, respectively:
1.
An easy way to solve that would be by doing a bucket (or counting) sort: count the number of R, B and G using counters, and then reconstruct the string.
But the solution I wrote just swaps the characters using a similar idea as selection sort. It runs in linear time and in place. Code is here: https://github.com/marcelodebarros/dailycodingproblem/blob/master/DailyCodingProblem10192018.cs
2.
If you traverse the tree in order, the elements will be sorted from smallest to largest. In this case do a reverse-in-order (right node, current, left node) and the elements will be sorted from the largest to the smallest. Keep a count, and when you reach your target (in our case 2), return it. Code is here: https://github.com/marcelodebarros/dailycodingproblem/blob/master/DailyCodingProblem10202018.cs
3.
There will be 2^N subsets here. My solution is not recursive, just go from 0 to 2^N-1, do a bit analysis of each index i, and if the bit is on add the corresponding index to the current subset, if it isn't, then don't and continue. Code is here: https://github.com/marcelodebarros/dailycodingproblem/blob/master/DailyCodingProblem10212018.cs
Cheers, Marcelo
Very nice, Marcelo! The first problem is actually a famous Dutch national flag problem.
ReplyDelete