Pocket Gems 一轮电面

原帖地址:一亩三分地

把电面面经里的题做了一圈,但是还是一个没考。。。1. Given unsorted array of int, each element is the length of a rod, return the minimum total cost of combining all rods.
Cost of combining two rods = length of the combined rod.
Example: [3, 4, 6]
Combine 3 4, get rod 7, cost = 7
Combine 7 6, get rod 13, cost = 13
total cost = 7 + 13 = 20
我用MinHeap做的,先把所有的length发到minheap, 然后poll两个出来加起来再放到heap里,同时update total cost。 time complexity O(nlogn)

2. given string of if else condition (no white space), construct a condition tree according to the string (return root Node)
input: a?b:c
output: a
/\
b c
input: a?b?c:d:e
output: a
/\
b e
/\
c d
我是用recursion做的,code写的有问题,大家在网上找答案吧
感觉第二题写的很一般,但是居然半小时后给了我2面,不会是耍我吧。。。
I’ll keep you updated, 求大米!!!!!