第30期:IT培训-领先公司的当前问题和挑战

你好!我们到了!今天,我们有30周年纪念文章!

我们再次从领先的IT公司的访谈中为您准备了一系列有趣的问题和任务!上一期中有关问题的答案已经发布



问题将每周出现-敬请期待!该专栏由招聘机构Spice IT支持

本周,我们从美国Visa公司的采访中收集了任务。

问题


1. 100名戴着红色/黑色帽子的囚犯
100名入狱的囚犯排成一列,朝一个方向。每个囚犯都戴着黑色或红色的帽子。囚犯可以在队列中看到所有在囚犯面前的帽子,但是看不到他站在囚犯身后的帽子。
囚犯将从队列中的最后一个囚犯开始询问每个囚犯的帽子颜色。如果囚犯说出正确的颜色,则将其保存,否则执行。如果允许他们在囚犯开始询问其帽子的颜色之前讨论一项策略,最多可以节省多少囚犯。

传递
100 . . , , .
, . , , , . , , .
.

2. 在人们只想要男孩的国家中男孩和女孩的比例
在一个国家,所有家庭都想要一个男孩。他们一直生孩子,直到男孩出生。该国男孩和女孩的预期比例是多少?

传递
. . ?

任务


1. 懒惰的餐饮者问题
给定整数N,它表示可以在一块薄煎饼上切的块数,找到通过N块可以形成的最大块数

输入:输入
的第一行包含一个整数T,表示测试用例的数量。然后是T测试用例。每个测试用例包含一行。每个测试用例的第一行包含一个整数N。

输出:
与每个测试用例相对应的新行中,打印出可以进行N个切割而形成的最大块数。

约束:示例: 输入输出

1 ≤ T ≤ 100
1 ≤ N ≤ 106




2
5
3


16
7

传递
N, , . , , N .

:
T, . T. . N.

:
, , N .

:

1 ≤ T ≤ 100
1 ≤ N ≤ 106


:

2
5
3


16
7

2. 峰值元素
Given an array A of N integers. The task is to find a peak element in it.
An array element is peak if it is not smaller than its neighbours. For corner elements, we need to consider only one neighbour.

Note: There may be multiple peak element possible, in that case you may return any valid index.

Input Format:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer N. Then in the next line are N space separated values of the array.

Output Format:
For each test case output will be 1 if the index returned by the function is an index with peak element.

User Task:
您无需输入任何信息。只需完成提供的函数peakElement()并返回有效索引即可。

约束:示例: 输入:输出:说明: 测试案例1:在给定的数组中,3是峰值元素。测试案例2: 4是峰值元素。
1 <= T <= 100
1 <= N <= 100
1 <= A[] <= 1000




2
3
1 2 3
2
3 4


3
4





传递
A N . , .
, . .

. , .

:
T, . T. N. N .

:
1 <= T <= 100
1 <= N <= 100
1 <= A [] <= 1000


:
:

2
3
1 2 3
2
3 4

:
3
4


:
1:
3 .
2: 4 .

3. 最大间隔重叠
Consider a big party where a log register for guest’s entry and exit times is maintained. Find the time at which there are maximum guests in the party. Note that entries in register are not in any order.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the entry and exit array. Then the next two line contains the entry and exit array respectively.

Output:
Print the maximum no of guests and the time at which there are maximum guests in the party.

Constraints:
1<=T<=10^5
1<=N<=10^5
1<=entry[i],exit[i]<=10^5


Example:
Input:

2
5
1 2 10 5 5
4 5 12 9 12
7
13 28 29 14 40 17 3
107 95 111 105 70 127 74


Output:
3 5
7 40

, . , . , .

:
T, . T. n, . .

:
, .

:
1 <= <= 10 ^ 5
1 <= N <= 10 ^ 5
1 <= [I], [I] <= 10 ^ 5


:
:

2
5
1 2 10 5 5
4 5 12 9 12
7
13 28 29 14 40 17 3
107 95 111 105 70 127 74


:
3 5
7 40



1
99 , 100- 50-50 .
, .

100- , . , , 99- .

99- 100- . 100-. 99- .

100- «» ( )
) 99- , .
) 99- , — .

100- «» ( )
) 99- , — .
) 99- , — .

98- 99- .

97 1 .

2
: . , , , .

.
NG .

, (1-p) , .

NG .

NG = 0*(1-p) + 1*p*(1-p) + 2*p*p*(1-p) + 3*p*p*p*(1-p) + 4*p*p*p*p*(1-p) +.....

p = 1/2 (1-p) = 1/2 .

NG = 0*(1/2) + 1*(1/2)2 + 2*(1/2)3 + 3*(1/2)4 + 4*(1/2)5 + ...
1/2*NG = 0*(1/2)2 + 1*(1/2)3 + 2*(1/2)4 + 3*(1/2)5 + 4*(1/2)6 + ...


NG - NG/2 = 1*(1/2)2 + 1*(1/2)3 + 1*(1/2)4 + 1*(1/2)5 + 1*(1/2)6 + ...

1
NG/2 = (1/4)/(1-1/2) = 1/2

NG = 1

1
, : (n + c*c + 2) / 2
int main() {
	int t,n,final;
	cin>>t;
	while(t--){
	   cin>>n;
	   final=(n+(n*n)+2)/2;
	   cout<<final<<endl;
	}
	return 0;
}

2
// arr: input array
// n: size of array
int peakElement(int a[], int n)
{
    int i;
    for(i=1;i<n-1;i++){
        if(a[i]>a[i-1] && a[i]>a[i+1]){
            return i;
        }
    }
    if(n>1 && a[0]>a[1])
        return a[0];
    if(n-2>=0 && a[n-1]>a[n-2])
    return a[n-1];
}

3
/*package whatever //do not write package name here */

import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		
		while (T > 0) {
		    int N = sc.nextInt();
		    int[] entry = new int[N];
		    int[] exit = new int[N];
		    for (int i = 0; i < N; i++) {
		        entry[i] = sc.nextInt();
		    }
		    
		    for (int i = 0; i < N; i++) {
		        exit[i] = sc.nextInt();
		    }
		    
		    Arrays.sort(entry);
		    Arrays.sort(exit);
		    //System.out.println(Arrays.toString(entry));
		    //System.out.println(Arrays.toString(exit));
		    
		    int counter = 0;
		    int maxCounter = -1;
		    int maxTime = -1;
		    
		    int entryIdx = 0;
		    int exitIdx = 0;
		    
		    while (entryIdx < N && exitIdx < N) {
		        if (entry[entryIdx] <= exit[exitIdx]) {
		            counter++;
		            if (counter > maxCounter) {
		                maxCounter = counter;
		                maxTime = entry[entryIdx];
		            }
		            entryIdx++;
		        } else {
		            counter--;
		            exitIdx++;
		        }
		    }
		    
		    System.out.println(maxCounter + " " + maxTime);   
		    T--;
		}
	}
}

Source: https://habr.com/ru/post/undefined/


All Articles