Continuing my notes on Cracking the Coding Interview.
17.4 Maximum Without Comparison
Write a method to find the larger of two numbers without using comparison or conditional operators such as if-else.
Given two integers a and b, return the larger one. If they are equal, return either.
Use the sign of a-b to choose the result, while accounting for overflow. Overflow can only occur when the values have different signs, so handle each case.
classMax { public: intflag(int x) { return (x >> 31) & 1; } intgetMax(int a, int b) { int fa = flag(a); int fb = flag(b); int fc = flag(a - b);
int k = fa^fb; int ans = 0; if (k == 0)// Same sign { ans = 1 ^ fc; } else// Different signs { ans = 1 ^ fa; } return ans*a + (1 ^ ans)*b; } }wc;
17.5 Master Mind
There are four slots, each containing a red (R), yellow (Y), green (G), or blue (B) ball. For example, RGGB means red in slot 1, green in slots 2 and 3, and blue in slot 4. A player guesses a combination such as YRGB. A correct color in the correct slot is a “hit”; a correct color in the wrong slot is a “pseudo-hit”. A hit must not also count as a pseudo-hit.
Given strings A and guess, representing the color combination and a guess, return an int array: hits first, then pseudo-hits.
A LeetCode problem. First count the hits, then count the unmatched colors. Watch the edge cases.
classResult { public: vector<int> calcResult(string A, string guess) { int A_len = A.length(); int g_len = guess.length();
vector<int>res; if (A_len != g_len)return res; int ans1 = 0, ans2 = 0;
map<char, int>hax; for (int i = 0; i < A_len; i++) { if (A[i] == guess[i])ans1++; else { hax[A[i]]++; } } for (int i = 0; i < g_len; i++) { if (A[i] == guess[i])continue; else { if (hax[guess[i]]) { ans2++; hax[guess[i]]--; } } } res.push_back(ans1); res.push_back(ans2); return res; } };
17.6 Minimal Sort
Given an integer array, find indices m and n such that sorting only the elements between them sorts the entire array. Minimize n-m; in other words, find the shortest such subsequence.
Given an int array A and its size n, return a pair representing the target subsequence’s start and end. Indices start at 0; if the array is already sorted, return [0,0]. All values are positive integers.
First find the middle interval that violates the ordering, record its minimum and maximum, and extend it outward.
string res = ""; int cnt = 0; while (x > 0) { if (x % 1000 != 0) { if(res.length()) res = num_to_string(x % 1000) + " " + bigs[cnt] + "," + res; else res = num_to_string(x % 1000) + " " + bigs[cnt]; } x /= 1000; cnt++; } while (*res.begin() == ' ')res.erase(res.begin()); while (*(res.end() - 1) == ' ')res.erase(res.end() - 1); return res; } string num_to_string(int x) { string res = ""; if (x >= 100) { res = digits[x / 100 - 1] + " Hundred"; x = x % 100; } if (x >= 11 && x <= 19) { if(res.length()) res = res + " " + teens[x % 10 - 1]; else res = teens[x % 10 - 1]; return res; } elseif (x == 10 || x >= 20) { int num = x / 10; x %= 10; if(res.length()) res = res + " " + tens[num - 1]; else res = tens[num - 1]; } if (x) { if(res.length()) res = res + " " + digits[x - 1]; else res = digits[x - 1]; } return res; } };
17.13 Tree to Linked List
TreeNode is a node-like data structure with a val field and pointers to other nodes. It represents a binary search tree (left is the left child and right is the right child). Convert this tree, implemented with TreeNode, into a linked list implemented with ListNode.
Given a pointer root to the root of a binary search tree, return a pointer to the head of the converted linked list.