当前位置:网站首页>Huawei machine test: number of continuous licensing
Huawei machine test: number of continuous licensing
2022-07-19 11:01:00 【Xiao Zhu, Xiao Zhu will never admit defeat】
【 Programming topics |200 branch 】 Number of consecutive cards 【2022 Q1,Q2 Examination questions 】
Title Description
- Give a deck of cards in your hand , Figures from the 0-9, Yes r( Red ),,g( green ),b( Blue ),y( yellow ) Four colors , The rule of playing cards is that each card played must be followed by the same number or color , Otherwise, you cannot select .
- How should players choose to maximize the number of draws , And output the maximum number .
Input description
- first line The number of cards n (1<=n<=9)
- The second line The color of the card (r,g,b,y Four colors indicate )
Output description
- Output the maximum number of cards
Example
Input
1 4 3 4 5
r y b b r
Output
3
explain
If you hit (1, r)-> (5, r), Then you can print two .
If you hit (4,y) -> (4, b) -> (3, b), Then you can make three .
Thought analysis
This problem can also be considered BFS, The same number or the same color are stored in a continuous relationship .
Enter the queue from the first , Count the maximum number of times corresponding to each , Finally, update the maximum value .
You can use two dimensions when saving continuous relationships list
Indexed by serial number , Judge whether the elements are the same or the colors are the same, and add the corresponding list.
And then there's the regular BFS Join the team for processing .
Reference code
import java.util.*;
import java.util.Scanner;
public class lianXuChupaiNum {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] str1 = in.nextLine().split(" ");
String[] str2 = in.nextLine().split(" ");
String[][] arr = new String[str1.length][2];
for (int i = 0; i < str1.length; i++) {
arr[i][0] = str1[i];
arr[i][1] = str2[i];
}
// Store equality
List<List<Integer>> list = new ArrayList<>(arr.length);
for (int i = 0; i < arr.length; i++) {
// initialization
list.add(new ArrayList<>());
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (i != j) {
if (arr[i][0].equals(arr[j][0]) || arr[i][1].equals(arr[j][1])) {
list.get(i).add(j);
}
}
}
}
// BFS
Queue<Integer> queue = new ArrayDeque<>();
Queue<Integer> visited = new ArrayDeque<>(); // Used to judge whether you have visited
int[] res = new int[arr.length];
Arrays.fill(res, 1);
queue.add(0);
visited.add(0);
while (!queue.isEmpty()) {
int poll = queue.poll();
for (int node : list.get(poll)) {
if (!visited.contains(node)) {
visited.add(node);
res[node] = res[poll] + 1;
queue.add(node);
}
}
}
int ans = 0;
for (int i = 0; i < arr.length; i++) {
ans = Math.max(ans, res[i]);
}
System.out.println(ans);
}
}
Welcome to comment and leave your own more concise method .
边栏推荐
- "Baidu side" angrily sprayed the interviewer! Isn't it that the tree time increases by a line number?
- 论文笔记:Mind the Gap An Experimental Evaluation of Imputation ofMissing Values Techniques in TimeSeries
- LeetCode 2325. Decrypt message (map)
- 关于hping打流测试工具
- Environment variable configuration of win10
- String类型函数传递问题
- After summarizing the surface based knowledge of the database
- Common collection properties
- [in vivado middle note ILA IP core]
- leetcode-08
猜你喜欢

Use testeract JS offline recognition picture text record

早期单片机加密的一些方法 【评论区领取资料】

Evaluation method of machine learning model

leetcode-08

Opencv programming: opencv3 X trains its own classifier

Svn learning

Google Earth Engine——Hansen Global Forest Change v1.8 (2000-2020) 森林覆盖度和森林损失量数据集

Category imbalance in classification tasks

How much money can you make by inventing flash memory? This is a Japanese dog blood story

树链剖分思想讲解 + AcWing 2568. 树链剖分(dfs序 + 爬山法 + 线段树)
随机推荐
6G中的卫星通信高效天基计算技术
String类型函数传递问题
Efficient space-based computing technology for satellite communication in 6g
Thinking about the integrated communication of air, space and earth based on the "7.20 Zhengzhou rainstorm"
vulnhub inclusiveness: 1
Win10安装Apache Jena 3.17
input number 纯数字输入 限制长度 限制 最大值
web安全入门-部署Snort开源IDS/IPS系统
Qt 两个重载QListWidget控件对象实现selectitem drag拖拽
Win10 start key click no response
Redis集群、一主二从三哨兵的搭建
Paper notes: mind the gap an empirical evaluation of impaction ofmissing values techniques in timeseries
Detailed explanation of Euler angle, axis angle, quaternion and rotation matrix
PowerCLI 脚本性能优化
Pytoch learning record 2 linear regression (tensor, variable)
Establishment of redis cluster, one master, two slave and three Sentinels
E-commerce sales data analysis and prediction (date data statistics, daily statistics, monthly statistics)
Brush questions with binary tree (2)
LeetCode 745. 前缀和后缀搜索
虚拟化排错概论