Contact us : Youdao technical team assistant : ydtech01 / Email : ydtech@rd.netease.com
Welcome fresh students
Coming to the 2022 School Recruitment Games
Now facing you
It is the NetEase Youdao team!
(Portal: http://hr.youdao.com/ )
Their canteen is delicious
They never involute
Today, they also brought
10 written programming questions
It is said that all the students are right
I got the offer smoothly!
Classmates, please start your bug
Oh no
Perform it!
1. Warm-up exercise
1.1 Find the duplicate number
Given an n+1 integers, the numbers are all between 1 and n (including 1 and n), we know that there is at least one repeated integer. Nums only assume a unique integer, find the repeated several .
- Difficulty: One star
- Time limit: C/C++ 1 second, other languages 2 seconds
- space limitation: C/C++ 256MB, other languages 512MB
- 64bit IO Format: %lld**
example:
- input : [1,3,4,2,2]
- returns : 2
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 返回重复数字
* @param nums int整型一维数组
* @return int整型
*/
public int duplicate (int[] nums) {
// write code here
int n = nums.length;
int l = 1, r = n - 1, ans = -1;
while (l <= r) {
int mid = (l + r) >> 1;
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (nums[i] <= mid) {
cnt++;
}
}
if (cnt <= mid) {
l = mid + 1;
} else {
r = mid - 1;
ans = mid;
}
}
return ans;
}
}
1.2 Triangle area
Enter the coordinates of the three points, and output the area triangle composed of the three points. (The result is rounded to three decimal places)
- Difficulty: One star
- Time limit: C/C++ 1 second, other languages 2 seconds
- space limitation: C/C++ 256MB, other languages 512MB
- Special Judge, 64bit IO Format: %lld
- Knowledge points: Computational Geometry
example:
- input : 12, -70, 95, 91, -72, 35
- output : 11119.500
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
int main() {
double x1, y1, x2, y2, x3, y3;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
double xa = (x1 - x2);
double ya = (y1 - y2);
double xb = (x3 - x2);
double yb = (y3 - y2);
float area = fabs((xa * yb - ya * xb) / 2.0);
printf("%.3f", area);
return 0;
}
2. Stretching exercise
2.1 Decompose natural numbers
A natural number can be decomposed into several natural numbers to multiply. Now give you a designated natural number n, and ask for the minimum value of decomposition of natural numbers.
- Difficulty:
- Time limit: 5 seconds for C/C++, 10 seconds for other languages
- Space limitation: C/C++ 32MB, other languages 64M
- 64bit IO Format: %lld
example:
- input : 6
- returns : 5
- shows that : 6 is decomposed into 2 * 3, then the smallest sum is 2+3=5
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 得到分解自然数之和的最小值
# @param n int整型 自然数n
# @return int整型
#
class Solution:
def getMinSum(self , n ):
if n <= 1:
return n
temp = int(n / 2)
while temp != 0:
if n % temp == 0:
if temp == 1 and n / temp == n:
print(n)
return n
else:
return self.getMinSum(n / temp) + self.getMinSum(temp)
else:
temp -= 1
2.2 Recovery of abnormal number
There is a one-dimensional integer array fuzzyArray , which stores n numbers from 1 to n, but it is stored out of order; at this time, the number at one position becomes -1. Please use optimal space complexity and time complexity find the position and original value of this abnormal number.
- Difficulty:
- time limit: C/C++ 5 seconds, other languages 10 seconds
- space limitation: C/C++ 256 MB, other languages 512 MB
- 64bit IO Format: %lld
- knowledge points: test development, array
example:
- input : [2, -1, 3]
- returns: [1,1]
- Description of was originally supposed to store the numbers from 1 to 3, but it is out of order, but the actual array is [2, -1, 3], indicating that the position of the array pos=1, the original number 1 becomes -1, so it returns [1, 1]
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 函数:求出异常数的位置和原来的值
# @param fuzzyArray int整型一维数组 含有异常数的数组
# @return int整型一维数组
#
class Solution:
def fuzzyNumber(self , fuzzyArray ):
flag = 1
pos = 0
sumnumber = 0
index = 0
for item in fuzzyArray:
if item == -1:
if flag == 0:
return [-1, -1]
flag = 0
pos = index
else:
sumnumber += item
index += 1
orisum = (index + 1) * index / 2
orinumber = orisum - sumnumber
return [pos, orinumber]
2.3 Average waiting time for orders
There is a tea shop, at the same time can handle only make an order, a customer orders a list of existing orders (two-dimensional array), each order contains two elements: first element represent time orders arrive , orders in the order according to the time of arrival of non-descending order; second element represent time-order needs ; when customer orders arrive, tea shops once idle will start making the order tea. Every customer will always wait for the milk tea shop to complete his order. The milk tea shop will process orders strictly in accordance with the order. Please return the average waiting time for all customers in the order list. And the standard answer error is within the range of 10-5 , are regarded as correct .
- Difficulty:
- Time limit: C/C++ 1 second, other languages 2 seconds
- space limitation: C/C++ 256MB, other languages 512MB
- Special Judge, 64bit IO Format: %lld
- knowledge points: simulation
example:
- input : [[1,2],[1,3],[4,3]]
- returns: 4.00000
Description of arrives at time 1, the milk tea shop immediately starts processing the order, and it is completed at time 3, the first customer needs to wait for 3-1=2;
The second order arrives at time 1, the milk tea shop is processing the first order, the first order is completed at time 3 and starts processing order 2, the second order is completed at time 6, the second customer needs to wait for the time 6-1=5;
The third order arrives at time 4, the milk tea shop is processing the second order, the second order is completed at time 6 and starts processing order 3, the third order is completed at time 9, the second customer needs to wait for the time 9-4=5; so the is (2+5+5)/3=4 .
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param orders int整型二维数组
* @return double浮点型
*/
public double averageWaitingTime (int[][] orders) {
int currentTime=0;
long timeSum=0;//注意越界
for(int[] a:orders){
if(a[0]>currentTime){
timeSum+=a[1];
currentTime=a[0]+a[1];
}else{
timeSum+=a[1]+currentTime-a[0];
currentTime=a[1]+currentTime;
}
}
return (double)timeSum/orders.length;
}
}
Third, whole body exercise
3.1 Numbers and letters
Give you a numbers and capital letters , and find the longest substring so that the substring contains same number of numbers and letters. The substring must be consecutive in the original array. Please return the length of the substring. If there is no such substring, return 0.
- Difficulty: Samsung
- Time limit: C/C++ 1 second, other languages 2 seconds
- space limitation: C/C++ 256 MB, other languages 512 MB
- 64bit IO Format: %lld
- Knowledge points: string processing
Example:
- input: [A,A,A]
- returns: 0
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str char字符型一维数组
* @return int整型
*/
public int findLongestSubarray (char[] str) {
Map<Integer,Integer> map = new HashMap<>();
map.put(0,-1);
int prefixSum = 0;
int longest = 0;
for (int i = 0; i < str.length; i++) {
char c = str[i];
prefixSum += Character.isDigit(c)?-1:1;
if (!map.containsKey(prefixSum)){
map.put(prefixSum,i);
}else{
// i-map.get(prefixSum) == i-left+1
if (i-map.get(prefixSum)>longest){
longest = i-map.get(prefixSum);
}
}
}
return longest;
}
}
3.2 Wooden stick splicing
The woodworker Wang has some wooden sticks of different lengths. He wants to know whether these sticks can be joined together to form a square. Please write a program to solve Xiao Wang's doubts.
explains :
- A single wooden stick can be used as one side of a square, or multiple wooden sticks can be spliced together as one side of a square.
- All sticks must be used, and each stick can only be used once .
- Difficulty: Samsung
- Time limit: C/C++ 1 second, other languages 2 seconds
- space limitation: C/C++ 32MB, other languages 64MB
- 64bit IO Format: %lld
- Knowledge points: dfs, pruning
example:
- input: [4, 1, 1, 1]
- returns: [false]
- Description: cannot be spliced into a square
#include <algorithm>
#include <numeric>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 判断输入不同长度木棍能否拼接成一个正方形
* @param sticks int整型vector 输入木棍长度
* @return bool布尔型
*/
bool canLinkToSquare(vector<int>& sticks) {
if (sticks.size() < 4) {
return false;
}
int len = std::accumulate(sticks.begin(), sticks.end(), 0);
if (len == 0 || len % 4 != 0) {
return false;
}
int max = *std::max_element(sticks.begin(), sticks.end());
if (max > len / 4) {
return false;
}
std::sort(sticks.begin(), sticks.end());
std::vector<bool> marks(sticks.size(), false);
return dfs(sticks, marks, len / 4, 0, 0, 0);
}
/**
*
* 利用dfs判断输入不同长度木棍能否拼接成一个正方形
* @param sticks int整型vector 输入木棍长度
* @param marks bool vector 木棍是否被使用
* @param len int整型 木棍边长
* @param count int整型 已拼成的边的个数
* @param l int整型 当前边的长度
* @param pos size_t整型 当前使用的木棍位置
* @return bool布尔型
*/
bool dfs(const vector<int> &sticks, vector<bool> &marks, const int len,
int count, int l, size_t pos) {
if (count == 3) return true;
for (int i = pos; i < sticks.size(); i++) {
if (marks[i]) continue;
if (l + sticks[i] == len) {
marks[i] = true;
if (dfs(sticks, marks, len, count + 1, 0, 0))
return true;
marks[i] = false;
return false;
} else if (l + sticks[i] < len) {
marks[i] = true;
if (dfs(sticks, marks, len, count, l + sticks[i], i + 1))
return true;
marks[i] = false;
if (l == 0)
return false;
while (i + 1 < sticks.size() && sticks[i] == sticks[i + 1])
i++;
}
}
return false;
}
};
3.3 Delete the shortest sub-array to make the remaining array in order
Enter an integer array array, please delete a sub-array so that the remaining elements in the array are non-incrementing . The sub-array can be a continuous sub-sequence in the original array, or it can be empty. Please return the shortest sub-array of .
- Difficulty: Samsung
- Time limit: C/C++ 1 second, other languages 2 seconds
- Space limitation: C/C++ 256MB, other languages 512MB
- 64bit IO Format: %lld
- knowledge points: array
example:
- input: [5,4,3,7,8,2,1]
- Return value: 2
- Description of is [7,8], and the length is 2. The remaining elements are [5,4,3,2,1], which are non-increasing.
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型一维数组 原数组
* @return int整型
*/
public int findLengthOfShortestSubarray (int[] arr) {
int n = arr.length;
int left = 0;
while (left + 1 < n && arr[left] >= arr[left+1]) {
left++;
}
// [0...left]有序
if (left == n - 1) {
return 0;
}
// [right...n-1]有序
int right = n - 1;
while (right > 0 && arr[right - 1] >= arr[right]) {
right--;
}
// 完全删除一边[left+1, n-1], 或者[0...right - 1]
int result = Math.min(n - left - 1, right);
// 左边和右边各保留一部分
int i = 0, j = right;
while (i <= left && j <= n - 1) {
if (arr[i] >= arr[j]) {
// [0...i] 和 [j...n-1] 有序, 删除 [i+1...j-1]
result = Math.min(result, j - i - 1);
i++;
} else {
// 小的+1
j++;
}
}
return result;
}
}
Fourth, jumping sports
4.1 Task Assignment
In the offline machine translation system, multiple requests for translated sentences are sometimes received at one time. The translation time of these sentences can be estimated as jobs according to the length, and jobs[i] represents the translation time of the i-th requested sentence. The system will start k threads to process these translation tasks at the same time. In order to reduce the response time, we need to allocate these translation requests to different threads for processing. Each request can only be allocated to one thread. The processing time of a thread is the sum of the translation time of all the requested sentences allocated to it. The processing time of the system is the time for all threads to translate the assigned tasks. Your goal is to optimize the so that the system can process all requests as quickly as possible. Please calculate the shortest processing time of the entire system
- Difficulty: Five stars
- Time limit: C/C++ 1 second, other languages 2 seconds
- space limitation: C/C++ 32MB, other languages 64MB
- 64bit IO Format: %lld
- Knowledge points: Greedy, linear dynamic programming
example:
- input: [3,2,3],3
- returns: 3
- description: three requests are allocated to three tasks, and the system processing time is 3
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 调度jobs中的任务,分配给k个worker处理,返回系统最短的处理时间
* @param jobs int整型vector 翻译时长数组
* @param k int整型 开启翻译线程数
* @return int整型
*/
int minimumProcessTime(vector<int>& jobs, int k) {
// write code here
int n = jobs.size();
if (n <= k) {
return *max_element(jobs.begin(), jobs.end());
}
vector<int> tot(1 << n, 0);
for (int i = 1; i < (1 << n); i++) {
for (int j = 0; j < n; j++) {
if ((i & (1 << j)) == 0) continue;
int left = (i - (1 << j));
tot[i] = tot[left] + jobs[j];
break;
}
}
vector<vector<int>> dp(k, vector<int>(1 << n, -1));
for (int i = 0; i < (1 << n); i++) {
dp[0][i] = tot[i];
}
for (int j = 1; j < k; j++) {
for (int i = 0; i < (1 << n); i++) {
int minv = 1e9;
for (int s = i; s; s = (s - 1) & i) { // 枚举 i 的全部子集
int left = i - s;
int val = max(dp[j-1][left], tot[s]);
minv = min(minv, val);
}
dp[j][i] = minv;
}
}
return dp[k-1][(1<<n)-1];
}
};
4.2 Practice makes perfect
There are two oil cans in the oil weng, their capacity is a liter and b liters respectively. The customer wants to buy c liters of oil. Since the two oil have no scale , the oil weng can only adopt the following 3 types operate:
- Fill one of the oil cans with oil
- Pour out all the oil in one of the oil cans
- Pour the oil from one oil can into another oil can. If the oil capacity of the source oiler is greater than the remaining capacity of the target oiler, the source oiler retains the remaining capacity after this operation and the target oiler is full of oil, otherwise the source oiler capacity is empty after this operation, and the target oiler capacity is Previous capacity + source oil tank capacity.
The oil seller wants to know whether the oil volume in one of the oil pots can be equal to the customer's purchase volume c liters after several operations above. Please write a program to solve the problem of oil peddler, if after a few operations can get the output capacity target to be operated minimum number , otherwise the output -1 .
- Difficulty: Five stars
- Time limit: C/C++ 1 second, other languages 2 seconds
- Space limitation: C/C++ 32MB, other languages 64MB
- 64bit IO Format: %lld
- knowledge points: bfs
example:
- input: [5, 3, 6]
- returns: [-1]
- Description of [Can not go through several operations to make the oil volume in one of the oil cans equal to 6]
class Solution {
public:
// 两油壶状态
class State {
public:
State(int _a, int _b) : a(_a), b(_b), step(0), op(-1) {};
public:
int a; // a壶油量
int b; // a壶油量
int step; // 经过多少步到达此状态
int op; // 到达此状态经过的操作编号
};
void update(std::queue<State> &q, std::vector<std::vector<bool>> &visited,
State &next, int step, int op) {
assert(next.a >= 0 && next.b >= 0);
if (!visited[next.a][next.b]) {
next.step = step;
next.op = op;
q.push(next);
visited[next.a][next.b] = true;
}
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 能否经过数次操作得到目标容量,可以的话请输出需要操作的最小次数,不可以的话请输出-1。
* @param a int整型 a壶容量
* @param b int整型 b壶容量
* @param c int整型 目标容量
* @return int整型
*/
int findShortestStep(int a, int b, int c) {
if (c > a && c > b)
return -1;
if (c == a || c == b)
return 1;
if (a == b)
return -1;
else if (b > a)
std::swap(a, b);
if (c > b && c < a && a % b == 0 && c % b == 0) {
int ua = a / b;
int uc = c / b;
return std::min(ua - uc, uc) * 2;
}
if (c == a - b) {
return 2;
}
State init(0, 0);
std::vector<std::vector<bool>> visited(a + 1, std::vector<bool>(b + 1, false));
visited[0][0] = true;
std::queue<State> q;
q.push(init);
while (!q.empty()) {
State s = q.front();
if (s.a == c || s.b == c) {
return s.step;
}
// fill a
State next(0, 0);
if (s.a < a) {
next.a = a;
next.b = s.b;
update(q, visited, next, s.step + 1, 0);
}
// fill b
if (s.b < b) {
next.a = s.a;
next.b = b;
update(q, visited, next, s.step + 1, 1);
}
// drop a
if (s.a) {
next.a = 0;
next.b = s.b;
update(q, visited, next, s.step + 1, 2);
}
// drop b
if (s.b) {
next.a = s.a;
next.b = 0;
update(q, visited, next, s.step + 1, 3);
}
// pour a to b
if (s.a && s.b < b) {
if (s.a <= b - s.b) {
next.a = 0;
next.b = s.b + s.a;
} else {
next.a = s.a - (b - s.b);
next.b = b;
}
update(q, visited, next, s.step + 1, 4);
}
// pour b to a
if (s.b && a > s.a) {
if (s.b <= a - s.a) {
next.a = s.a + s.b;
next.b = 0;
} else {
next.b = s.b - (a - s.a);
next.a = a;
}
update(q, visited, next, s.step + 1, 5);
}
q.pop();
}
return -1;
}
};
Whether you are powerful code god
Still a brave bull
Youdao technical team are looking forward to your joining!
Welcome to post NetEase Youdao !
(Portal: http://hr.youdao.com/ )
eggs: At 19:00 on August 16th (Monday), NetEase Youdao 2022 will face you face-to-face in the technical air-announcement special of Youdao 2022, answering questions and revealing the secrets of Youdao's work!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。