Given a slice a. There are 2 sub slices. One’s length is k, the other’s is p. These 2 sub slices are not overlaid. Find the max sum of the 2 sub slices.
Return -1 if can’t find one.
Example 1:
1 2
Input: [1,3,4,7,5,8,2,9], 3, 2 Output: 35
Example 2:
1 2
Input: [1,3,4,7,5,8], 100, 2 Output: -1
Thought
Move slice K through A, at the left and right side of K place the slice P. To improve the efficiency, store sums of K and P in hash tables.
Given a string s, return a cropped which max length is n. Also, spaces at prefix and postfix need to be removed.
Example 1:
1 2
Input: "Hello World", 8 Output: "Hello"
Example 2:
1 2
Input: " Hello World ", 100 Output: "Hello World"
Example 3:
1 2
Input: "Hello World", 3 Output: ""
Thought
Find the start point l and end point r then return result s[l:r] Left point is easy to find. If s[l] is space, pointer l move right. On the other hand, r starts from l + N, if s[r] is NOT space, pointer r move left.
The tricky point is that if the given string’s length equals to given n, and the string ends with character, the last word would be cropped.
funccrop(s string, n int)string { s = s + " "// append an extra space
// keep moving right until reaching the first character l := 0 for l < len(s)-1 { if s[l] == ' ' { l++ } else { break } }
// start from `l+n` or the last char (extra space) // keep moving left until reaching the first space r := min(l+n, len(s)-1) for r > l { if s[r] == ' ' { break } else { r-- } } return s[l:r] }
funcmin(i, j int)int { if i < j { return i } return j }
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