2021 vivo school recruits early approval of written examination analysis
The title can’t be written down completely. Vivo will put the title on Niuke after visual inspection. Here is some analysis.
1. The problem of planting flowers
Just found out that it was 605 questions on Leetcode
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。
给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。
示例 1:
输入: flowerbed = [1,0,0,0,1], n = 1
输出: True
示例 2:
输入: flowerbed = [1,0,0,0,1], n = 2
输出: False
注意:
数组内已种好的花不会违反种植规则。
输入的数组长度范围为 [1, 20000]。
n 是非负整数,且不会超过输入数组的大小。
At first I thought it was a dynamic programming problem, which wasted a lot of time, but later found out that it was a problem of finding a pattern.
Problem-solving ideas:
Case 1: Exclude the endpoint
Excluding the endpoints, we assume that the area where flowers can be planted is surrounded by flowers, that is, the continuous 0 in the middle is surrounded by 1.
In the following situation, it is obviously impossible to grow flowers
1 0 0 1;
And this situation:
1 0 0 0 1 can plant 1 flower
1 0 0 0 0 1 can plant 1 flower
1 0 0 0 0 0 1 can plant 2 flowers
1 0 0 0 0 0 0 1 can plant 2 flowers
1 0 0 0 0 0 0 0 1 can plant 3 flowers
1 0 0 0 0 0 0 0 0 1 can plant 3 flowers
It is not difficult to find that every two zeros, there will be many opportunities for a flower.
The formula is easy to calculate. When there are n zeros in the middle, you can plant (n-1)/2 flowers
Case 2: Consider the endpoint
When a certain end point is 0, we can
plant a variety of flowers 1 0 0 can plant 1 flower
1 0 0 0 can plant 1 flower
1 0 0 0 0 can plant 2 flowers
1 0 0 0 0 0 can plant 2 flowers
1 0 0 0 0 0 0 3 flowers can be planted
1 0 0 0 0 0 0 0 3 flowers can be planted
Situation 3: The size of the garden is only 1
When the size of the garden is 1, we cannot get an interval, we only have one point, and we need to deal with this situation specially
In summary, we can get the following code
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int len=in.nextInt();
int plant[]=new int[len];
int curr=0;
while (curr<len){
plant[curr++]=in.nextInt();
}
slove(plant);
}
private static void slove(int nums[]){
if(nums.length==1){
int x=nums[0]==0?1:0;
System.out.println(x);
return;
}
int slow=0;//0
int quick=0;//1
int count=0;
while (slow<nums.length&&quick<nums.length){
while (slow<nums.length&&nums[slow]==1){
slow++;
}
quick=slow;
while (quick<nums.length&&nums[quick]==0){
quick++;
}
if(quick-slow>=3){
count+=counter(quick-slow);
}
//考虑端节点
if((slow==0||quick==nums.length)&&(quick-slow>=2)){
count++;
}
slow=quick;
}
System.out.println(count);
}
//计算两个空白字符之间的
private static int counter(int sub){
return (sub-1)/2;
}
2. Quality test
The main idea is to drop the phone, and drop test to test the quality of the phone.
I won’t say much about this question, a classic Leetcode question: Leetcode-887
3. Merge the pipeline
The main idea of the topic:
Input: n means there are several pipelines.
Output: Arranged in ascending order, the combined pipeline
Very simple, it is a sorting problem. However, a linked list is required in this topic. And there is a static function left in the source code to merge the newly input linked list.
Problem-solving idea: Use a small top heap to merge several input pipelines, then pop them out and merge them into one pipeline. Then in the merge function, re-merge the two linked lists, and insert them as they arrive
public class Main {
private static ListNode dummyNode=new ListNode(-1);
private static ListNode currNode=dummyNode;
public static void main(String[] args) {
currNode=dummyNode;
List<String> lines = new ArrayList<>();
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
int totalLine=Integer.valueOf(scanner.nextLine());
while (totalLine>0) {
lines.add(scanner.nextLine());
totalLine--;
// write your code here
}
PriorityQueue<Integer> queue=new PriorityQueue<>();
for (int i=0;i<lines.size();i++){
String[] nums=lines.get(i).split(" ");
for(String num:nums){
queue.offer(Integer.valueOf(num));
}
}
while (queue.isEmpty()==false){
currNode.next=new ListNode(queue.poll());
currNode=currNode.next;
}
} finally {
if (scanner != null) {
scanner.close();
}
}
// TODO output
ListNode curr=dummyNode.next;
while (curr!=null){
if(curr.next==null){
System.out.print(curr.val);
}else {
System.out.print(curr.val+" ");
}
curr=curr.next;
}
}
static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
private static ListNode mergeNodes(ListNode head ) {
dummyNode=merge(dummyNode.next,head);
return dummyNode.next;
}
private static ListNode merge(ListNode n1,ListNode n2){
if(n1==null||n2==null){
return n1==null?n2:n1;
}
ListNode newDummy=new ListNode(-1);
ListNode curr=newDummy;
while(n1!=null&&n2!=null){
int val=n1.val;
if(n1.val>n2.val){
val=n2.val;
n2=n2.next;
}else {
n1=n1.next;
}
curr.next=new ListNode(val);
curr=curr.next;
}
curr.next=n1==null?n2:n1;
return newDummy.next;
}
}
There are a lot of codes in the topics given by vivo, so I write more here.