There is no output window in Mac’s Maya so I can’t print out text by std::out. The only way I know is to use Global::displayInfo. However, Global::displayInfo only accepts MString. I haven’t digged into what MString is yet but just provide some examples here. Hope you can get the basic idea:)
1 2 3 4 5 6 7 8
Global::displayInfo("hello"); // Works
string str = "hello"; Global::displayInfo(str); // This will fail
char str[10]; sprintf(str, "hello"); // Writes content to `str` Global::displayInfo(str); // Works
Write a custom Maya command that print “Hello world!” in Maya console. There are Python API 1.0 and 2.0. The syntax would be different. We will integrate with API 2.0 in this example.
console.log(a); // maps to property "job" console.log("---") console.log(b); // property "none" doesn't exist so b is undefined console.log("---") console.log(rest_props); // row but without "job" property
Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. If needle is empty, return 0.
Thoughts
strategies:
two pointers to compare each characters of needle and haystack (2 while loops)
iterate through haystack to get slices to compare with needle
iterate through haystack to get slices. Hash each slice and compare the hashed with the hashed needle
Given a inorder-traversal result and a preorder-traversal result of a tree, Figure out the tree structure.
For example:
inorder: 1,2,3,4,5,6,7
preorder: 5,2,1,4,3,6,7
What is the structure of this tree? Can we build a tree based on these two clues?
Thoughts
The first element of preorder-traversal must be the Root of a tree. On the other hand, we can determine left and right from inorder-traversal after we get the root. As the result, we can again get inorder and preorder result of left-sub-tree and right-sub-tree, then both left and right redo above process until they get no or only one element.
Overview the tree for now
So we can expect we can get the whole tree if we repeat the process.
Let’s draw some pictures to understand this quesiton. If there are 4 piles of bananas like below, the given array would be [3, 2, 4, 1].
Then if Koko can eat 3 bananas per hour, it will takes her 5 hours to eat all the bananas like blow.
Now, if the limit hours h is 5 then 3 bananas per hour satisfies. However, if h, for example, is 3, then Koko need to SPEEDS UP to finish all the bananas. On the other hand, if h is 10 which means we still have time right? So Koko should SLOW DOWN to certain speed to make herself chill but still can finish all the bananas in time.
So what is the best eating speed is the question!
Thoughts
The maximum of speed should equals to the number of the bananas of the tallest pile
The minimun of speed should equals to 1
Which means we are trying to find the best from 1 ~ piles.max(). Searching a value from a sorted array… maybe binary search is a soslution.
// find the matched speed or continue go down to sub tree match time.cmp(&h) { // spend too much time to eat, speed up Ordering::Greater => Solution::min_eating_speed_inner(piles, h, mid + 1, max), // match, try to find a better (slower) speed _ => { letnew_speed = Solution::min_eating_speed_inner(piles, h, min, mid - 1);
if new_speed > 0 { return new_speed; } mid } } }
fncount_eating_time(piles: &Vec<i32>, speed: i32) ->i32 { // count the time to eat each pile and return sum letsum = piles .into_iter() .map(|pile| { if pile % speed > 0 { return pile / speed + 1 } return pile / speed }) .sum();
The core concept is to find the target from a sorted array which is quite suitable to use binary serach I think.
Here are my instructions:
Get the middle element, mid
Compare the mid with the target. there will be 3 branches: a. target < mid: continue to find the target from the LEFT side of the mid b. target > mid: continue to find the target from the RIGHT side of the mid c. target = mid: In this case, [index_of_mid, index_of_mid] will be our base answer so keep it. Then based on this answer, we need to go further to the both left and right to seek if there is other elements match to the search target.
For example, if the given array is [1, 2, 3, 4, 4, 4, 4, 6, 9], and the search target is 4.
Then we are trying to get this green range.
When we go through the instructions, we will get the middle element, and hit the c case.
Then base on this position, we seek towar left and right to get the final range.