You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
2
+
3
+
// Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.
4
+
5
+
// For example,
6
+
// Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
7
+
8
+
// Given word1 = “coding”, word2 = “practice”, return 3.
9
+
// Given word1 = "makes", word2 = "coding", return 1.
10
+
11
+
// Note:
12
+
// You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
// Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them.
2
+
3
+
// Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get information of whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
4
+
5
+
// You are given a helper function bool knows(a, b) which tells you whether A knows B. Implement a function int findCelebrity(n), your function should minimize the number of calls to knows.
6
+
7
+
// Note: There will be exactly one celebrity if he/she is in the party. Return the celebrity's label if there is a celebrity in the party. If there is no celebrity, return -1.
8
+
9
+
// Hide Company Tags LinkedIn Facebook
10
+
// Show Tags
11
+
12
+
13
+
14
+
/**
15
+
* Definition for knows()
16
+
*
17
+
* @param {integer} person a
18
+
* @param {integer} person b
19
+
* @return {boolean} whether a knows b
20
+
* knows = function(a, b) {
21
+
* ...
22
+
* };
23
+
*/
24
+
25
+
/**
26
+
* @param {function} knows()
27
+
* @return {function}
28
+
*/
29
+
varsolution=function(knows){
30
+
/**
31
+
* @param {integer} n Total people
32
+
* @return {integer} The celebrity
33
+
*/
34
+
returnfunction(n){
35
+
varcandidate=0;
36
+
37
+
// iterate through the list,
38
+
// if candidate is known by i -> continue
39
+
// if i doesnt know candidate, i becomes the candidate
40
+
for(vari=1;i<n;i++){
41
+
if(!knows(i,candidate)){
42
+
candidate=i;
43
+
}
44
+
}
45
+
46
+
for(i=0;i<n;i++){
47
+
if(i===candidate){
48
+
continue;
49
+
}
50
+
51
+
// if candidate is not known by i or candidate know i
// Given a nested list of integers, return the sum of all integers in the list weighted by their depth.
2
+
3
+
// Each element is either an integer, or a list -- whose elements may also be integers or other lists.
4
+
5
+
// Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.
6
+
7
+
// Example 1:
8
+
// Given the list [[1,1],2,[1,1]], return 8. (four 1's at depth 1, one 2 at depth 2)
9
+
10
+
// Example 2:
11
+
// Given the list [1,[4,[6]]], return 17. (one 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 1*3 + 4*2 + 6*1 = 17)
12
+
13
+
// Hide Company Tags LinkedIn
14
+
// Show Tags
15
+
// Show Similar Problems
16
+
17
+
/**
18
+
* // This is the interface that allows for creating nested lists.
19
+
* // You should not implement it, or speculate about its implementation
20
+
* function NestedInteger() {
21
+
*
22
+
* Return true if this NestedInteger holds a single integer, rather than a nested list.
23
+
* @return {boolean}
24
+
* this.isInteger = function() {
25
+
* ...
26
+
* };
27
+
*
28
+
* Return the single integer that this NestedInteger holds, if it holds a single integer
29
+
* Return null if this NestedInteger holds a nested list
30
+
* @return {integer}
31
+
* this.getInteger = function() {
32
+
* ...
33
+
* };
34
+
*
35
+
* Return the nested list that this NestedInteger holds, if it holds a nested list
36
+
* Return null if this NestedInteger holds a single integer
37
+
* @return {NestedInteger[]}
38
+
* this.getList = function() {
39
+
* ...
40
+
* };
41
+
* };
42
+
*/
43
+
/**
44
+
* @param {NestedInteger[]} nestedList
45
+
* @return {number}
46
+
*/
47
+
vardepthSumInverse=function(nestedList){
48
+
49
+
functiongetDepth(arr,lvl){
50
+
varmaxDepth=lvl;
51
+
52
+
for(vari=0;i<arr.length;i++){
53
+
if(!arr[i].isInteger()){
54
+
// maxDepth represents the max depth at that level,
55
+
// e.g. [[[[55]]],[[31]],[99],[],75]
56
+
// at lvl 1, we want to know which [[[55]]], [[31]], [99], [], 75
0 commit comments