Redian新闻
>
请教一道经典的面试真题
avatar
请教一道经典的面试真题# JobHunting - 待字闺中
y*8
1
【 以下文字转载自 SanFrancisco 讨论区 】
发信人: yan2008 (yan), 信区: SanFrancisco
标 题: 没有绿卡,只有学生身份或工作身份的人,可以在美国开小公司吗
发信站: BBS 未名空间站 (Thu Dec 30 21:01:35 2010, 美东)
没有绿卡,只有学生身份或工作身份的人,可以在美国开小公司吗? 如果可以开,需要什
么条件(人数,注册资金等)
谢谢啦!
avatar
x*0
2
给你一个int N,然后给你一个int[],数组里每个元素只能用一次,要求通过加法得到
从1到N的所有数字,返回最少需要添加元素的个数
比如:N = 6,arr = [1, 3]。最少需要添加的元素个数就是1,因为我们只用添加2就
可以了, arr = [1,2,3]。这样所有1到6的元素都能被arr这个数组的subset相加得到。
注意不能使用产生的元素啊。比如说:arr = [1,3] 咱们可以产生4=1+3。 但是不能产
生5(5=4+1),这是非法的。
大家有什么想法吗,最没有效率的方法都想不出来。sad!
avatar
n*n
3
Yes. Check the state's commerce department website for details for different
types of corps.
avatar
l*s
4
try this. The idea is from that for each N, the minimum set of nums are from
1 to x, where x is to make sure Sigma(1,x) == N or just greater than N.
public int minAddition(int N, int[] nums)
{
int i = 1, sum = 0, result = 0;
for(i = 0; sum < N; i++) sum += (result = i + 1);
Array.Sort(nums);
for(int j = 0; j < nums.Length; j++)
if((j == 0 || (j > 0 && nums[j] != nums[j-1])) && nums[j] <= i &&
nums[j] > 0)
result--;
return result;
}
avatar
d*u
5
but no payroll for yourself...
IRS will doubt the purpose behind!
avatar
b*b
6
don't think it's correct, e.g. for N=7, and nums=[1,4], the correct answer
is 1, you just need to add 2 to the array: [1, 2, 4] which can form any
number 1-7.

from

【在 l******s 的大作中提到】
: try this. The idea is from that for each N, the minimum set of nums are from
: 1 to x, where x is to make sure Sigma(1,x) == N or just greater than N.
: public int minAddition(int N, int[] nums)
: {
: int i = 1, sum = 0, result = 0;
: for(i = 0; sum < N; i++) sum += (result = i + 1);
: Array.Sort(nums);
: for(int j = 0; j < nums.Length; j++)
: if((j == 0 || (j > 0 && nums[j] != nums[j-1])) && nums[j] <= i &&
: nums[j] > 0)

avatar
y*8
7
thanks for all!
avatar
b*b
8
I feel this is a searching algorithm, so first you generate all the possible
combinations of with the given array, e.g. for [1,4], the possible numbers
can generate is 1, 4, 5 (4+1). now we go through it in order, let's say
number i is missing, then we add number i to the array, and add all existing
numbers +i to the possible values, e.g. in that example, we add 2, and add
3 (1+2), 6 (4+2), 7 (5+2) to the list, then check what's the next missing
number.
use the bitvector to check what's the next missing number should make it
more space efficient.

【在 b******b 的大作中提到】
: don't think it's correct, e.g. for N=7, and nums=[1,4], the correct answer
: is 1, you just need to add 2 to the array: [1, 2, 4] which can form any
: number 1-7.
:
: from

avatar
l*s
9
Great catch. I guess just need small modification to handle the case of "
Right Sum + 1". "Right Sum" means 6 (1-3), 10(1-4), 55(1-10), etc.

【在 b******b 的大作中提到】
: don't think it's correct, e.g. for N=7, and nums=[1,4], the correct answer
: is 1, you just need to add 2 to the array: [1, 2, 4] which can form any
: number 1-7.
:
: from

avatar
x*0
10
seems that this is a correct solution. Let me think about it.

possible
numbers
existing
add

【在 b******b 的大作中提到】
: I feel this is a searching algorithm, so first you generate all the possible
: combinations of with the given array, e.g. for [1,4], the possible numbers
: can generate is 1, 4, 5 (4+1). now we go through it in order, let's say
: number i is missing, then we add number i to the array, and add all existing
: numbers +i to the possible values, e.g. in that example, we add 2, and add
: 3 (1+2), 6 (4+2), 7 (5+2) to the list, then check what's the next missing
: number.
: use the bitvector to check what's the next missing number should make it
: more space efficient.

avatar
x*0
11
seems that this is a correct solution. Let me think about it.

possible
numbers
existing
add

【在 b******b 的大作中提到】
: I feel this is a searching algorithm, so first you generate all the possible
: combinations of with the given array, e.g. for [1,4], the possible numbers
: can generate is 1, 4, 5 (4+1). now we go through it in order, let's say
: number i is missing, then we add number i to the array, and add all existing
: numbers +i to the possible values, e.g. in that example, we add 2, and add
: 3 (1+2), 6 (4+2), 7 (5+2) to the list, then check what's the next missing
: number.
: use the bitvector to check what's the next missing number should make it
: more space efficient.

avatar
x*0
12
Let me think about this algorithm. Seems pretty ingenious

from

【在 l******s 的大作中提到】
: try this. The idea is from that for each N, the minimum set of nums are from
: 1 to x, where x is to make sure Sigma(1,x) == N or just greater than N.
: public int minAddition(int N, int[] nums)
: {
: int i = 1, sum = 0, result = 0;
: for(i = 0; sum < N; i++) sum += (result = i + 1);
: Array.Sort(nums);
: for(int j = 0; j < nums.Length; j++)
: if((j == 0 || (j > 0 && nums[j] != nums[j-1])) && nums[j] <= i &&
: nums[j] > 0)

avatar
b*e
13
var missing = function(n, a) {
var output = [];
var sum = 0;
var i = 0;
while(true) {
if (i >= a.length || a[i] > sum + 1) {
output.push(sum + 1);
sum += sum + 1;
} else {
sum += a[i];
i++;
}
if (sum >= n) {
break;
}
}
return output;
};

【在 x*****0 的大作中提到】
: 给你一个int N,然后给你一个int[],数组里每个元素只能用一次,要求通过加法得到
: 从1到N的所有数字,返回最少需要添加元素的个数
: 比如:N = 6,arr = [1, 3]。最少需要添加的元素个数就是1,因为我们只用添加2就
: 可以了, arr = [1,2,3]。这样所有1到6的元素都能被arr这个数组的subset相加得到。
: 注意不能使用产生的元素啊。比如说:arr = [1,3] 咱们可以产生4=1+3。 但是不能产
: 生5(5=4+1),这是非法的。
: 大家有什么想法吗,最没有效率的方法都想不出来。sad!

avatar
b*b
14
very good solution, nice!

【在 b***e 的大作中提到】
: var missing = function(n, a) {
: var output = [];
: var sum = 0;
: var i = 0;
: while(true) {
: if (i >= a.length || a[i] > sum + 1) {
: output.push(sum + 1);
: sum += sum + 1;
: } else {
: sum += a[i];

avatar
h*l
15
i think it should match 1 2 4 8 16 ....

【在 x*****0 的大作中提到】
: 给你一个int N,然后给你一个int[],数组里每个元素只能用一次,要求通过加法得到
: 从1到N的所有数字,返回最少需要添加元素的个数
: 比如:N = 6,arr = [1, 3]。最少需要添加的元素个数就是1,因为我们只用添加2就
: 可以了, arr = [1,2,3]。这样所有1到6的元素都能被arr这个数组的subset相加得到。
: 注意不能使用产生的元素啊。比如说:arr = [1,3] 咱们可以产生4=1+3。 但是不能产
: 生5(5=4+1),这是非法的。
: 大家有什么想法吗,最没有效率的方法都想不出来。sad!

avatar
x*0
16
can you give me a little bit description about this algorithm?
Seems pretty smart,but without comment, I did not understand.

【在 b******b 的大作中提到】
: very good solution, nice!
avatar
x*0
17
can you give me a little bit description about this algorithm?
Seems pretty smart,but without comment, I did not understand.

【在 b***e 的大作中提到】
: var missing = function(n, a) {
: var output = [];
: var sum = 0;
: var i = 0;
: while(true) {
: if (i >= a.length || a[i] > sum + 1) {
: output.push(sum + 1);
: sum += sum + 1;
: } else {
: sum += a[i];

avatar
x*0
18
not exactly.

【在 h***l 的大作中提到】
: i think it should match 1 2 4 8 16 ....
avatar
x*0
19
How about this case:
arr = [10, 28], N = 33.
Output: 5 ([1, 2, 4, 8, 26])?

from

【在 l******s 的大作中提到】
: try this. The idea is from that for each N, the minimum set of nums are from
: 1 to x, where x is to make sure Sigma(1,x) == N or just greater than N.
: public int minAddition(int N, int[] nums)
: {
: int i = 1, sum = 0, result = 0;
: for(i = 0; sum < N; i++) sum += (result = i + 1);
: Array.Sort(nums);
: for(int j = 0; j < nums.Length; j++)
: if((j == 0 || (j > 0 && nums[j] != nums[j-1])) && nums[j] <= i &&
: nums[j] > 0)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。