2023 E3 算法题第三题 (Appointment Slots)

2024-04-15 02:44

本文主要是介绍2023 E3 算法题第三题 (Appointment Slots),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目

Task 3
There are N patients (numbered from 0 to N-1) who want to visit the doctor. The doctor has S possible appointment slots, numbered from 1 to S. Each of the patients has two preferences. Patient K would like to visit the doctor during either slot A[K] or slot B[K]. The doctor can treat only one patient during each slot.Is it possible to assign every patient to one of their preferred slots so that there will be at most one patient assigned to each slot?Write a function:class Solution { public boolean solution(int[] A, int[] B, int S); }1that, given two arrays A and B, both of N integers, and an integer S, returns true if it is possible to assign every patient to one of their preferred slots, one patient to one slot, and false otherwise.Examples:Given A = [1, 1, 3], B = [2, 2, 1] and S = 3, the function should return true. We could assign patients in the following way: [1, 2, 3], where the K-th element of the array represents the number of the slot to which patient K was assigned. Another correct assignment would be [2, 1, 3]. On the other hand, [2, 2, 1] would be an incorrect assignment as two patients would be assigned to slot 2.Given A = [3, 2, 3, 1], B = [1, 3, 1, 2] and S = 3, the function should return false. There are only three slots available, but there are four patients who want to visit the doctor. It is therefore not possible to assign the patients to the slots so that only one patient at a time would visit the doctor.Given A = [2, 5, 6, 5], B = [5, 4, 2, 2] and S = 8, the function should return true. For example, we could assign patients in the following way: [5, 4, 6, 2].

解法1

思路

代码


public class AppointmentSlots1 {public class Vistor{public boolean preferenceAVisited;public boolean preferenceBVisited;}public boolean solution1(int[] A, int[] B, int S){List<Vistor> vistors = new ArrayList();int[] slots = new int[S];Arrays.fill(slots, -1);for (int patientIndex =0 ; patientIndex < A.length; patientIndex ++){if (!this.assignPatientToSlot(A, B, slots, patientIndex, vistors)) {return false;}}for (Vistor vistor : vistors){System.out.println(vistor.preferenceAVisited + ":" + vistor.preferenceBVisited);}return true;}public boolean assignPatientToSlot(int[] A, int[] B, int[] slots, int patientIndex, List<Vistor> vistors){if (patientIndex==-1){return false;}if (A[patientIndex]> slots.length){return false;}//        System.out.println(patientIndex + ";" + vistors.size());Vistor vistor = null;if (patientIndex +1< vistors.size()){vistor = vistors.get(patientIndex);}if (vistor == null){vistor = new Vistor();vistor.preferenceAVisited = true;vistors.add(vistor);if (slots[A[patientIndex]-1]==-1){slots[A[patientIndex]-1] = 1;return true;}else if (slots[B[patientIndex]-1]==-1){slots[B[patientIndex]-1] = 1;vistor.preferenceBVisited = true;return true;}else{vistors.remove(patientIndex);return this.assignPatientToSlot(A, B, slots, patientIndex-1, vistors);}}else if (!vistor.preferenceBVisited){vistor.preferenceBVisited = true;if (slots[B[patientIndex]-1]==-1){slots[B[patientIndex]-1] = 1;return true;}else{vistors.remove(patientIndex);return this.assignPatientToSlot(A, B, slots, patientIndex-1, vistors);}}else{return false;}}public static void main(String[] args) {AppointmentSlots1 appointmentSlots = new AppointmentSlots1();// Example usage:int[] A1 = {1, 1, 3};int[] B1 = {2, 2, 1};int S1 = 3;System.out.println("==============Starting case 1========");System.out.println(appointmentSlots.solution1(A1, B1, S1));  // Output: trueint[] A2 = {8, 2, 3, 1};int[] B2 = {1, 3, 1, 2};int S2 = 3;System.out.println("==============Starting Case 2========");System.out.println(appointmentSlots.solution1(A2, B2, S2));  // Output: falseint[] A3 = {2, 5, 6, 5};int[] B3 = {5, 4, 2, 2};int S3 = 8;System.out.println("==============Starting Case 3========");System.out.println(appointmentSlots.solution1(A3, B3, S3));  // Output: true   5 4 6 2int[] A4 = {1, 2, 1, 6, 8, 7, 8};int[] B4 = {2, 3, 4, 7, 7, 8, 7};int S4 = 10;System.out.println("==============Starting Case 4========");System.out.println(appointmentSlots.solution1(A4, B4, S4));  // Output: false}
}

解法2

思路

代码


public class AppointmentSlots2 {public boolean solution1(int[] arrayA, int[] arrayB, int S){int[][] combinedArray = combineArrays(arrayA, arrayB);Set set = new HashSet();// 打印组合后的二维数组for (int i = 0; i < combinedArray.length; i++) {set =  new HashSet();
//            System.out.println(Arrays.toString(combinedArray[i]));for (int j =0 ;j < combinedArray[i].length; j ++){if (combinedArray[i][j] >S){return false;}int previousSize = set.size();set.add(combinedArray[i][j]);int currentSize = set.size();if (previousSize== currentSize){break;}}if (set.size() == combinedArray[i].length){return true;}}return false;}public static int[][] combineArrays(int[] arrayA, int[] arrayB) {int length = arrayA.length;int rows = (int) Math.pow(2, length); // 计算新数组的行数int[][] result = new int[rows][length]; // 创建新的二维数组// 填充新数组for (int i = 0; i < rows; i++) {for (int j = 0; j < length; j++) {// 判断当前位是否为1if ((i >> j & 1) == 1) {result[i][j] = arrayB[j]; // 为1时取数组B的元素} else {result[i][j] = arrayA[j]; // 为0时取数组A的元素}}}return result;}public static void main(String[] args) {AppointmentSlots2 appointmentSlots = new AppointmentSlots2();// Example usage:int[] A1 = {1, 1, 3};int[] B1 = {2, 2, 1};int S1 = 3;System.out.println("==============Starting case 1========");System.out.println(appointmentSlots.solution1(A1, B1, S1));  // Output: trueint[] A2 = {8, 2, 3, 1};int[] B2 = {1, 3, 1, 2};int S2 = 3;System.out.println("==============Starting Case 2========");System.out.println(appointmentSlots.solution1(A2, B2, S2));  // Output: falseint[] A3 = {2, 5, 6, 5};int[] B3 = {5, 4, 2, 2};int S3 = 8;System.out.println("==============Starting Case 3========");System.out.println(appointmentSlots.solution1(A3, B3, S3));  // Output: true   5 4 6 2int[] A4 = {1, 2, 1, 6, 8, 7, 8};int[] B4 = {2, 3, 4, 7, 7, 8, 7};int S4 = 10;System.out.println("==============Starting Case 4========");System.out.println(appointmentSlots.solution1(A4, B4, S4));  // Output: false}}

这篇关于2023 E3 算法题第三题 (Appointment Slots)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/904697

相关文章

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

秋招最新大模型算法面试,熬夜都要肝完它

💥大家在面试大模型LLM这个板块的时候,不知道面试完会不会复盘、总结,做笔记的习惯,这份大模型算法岗面试八股笔记也帮助不少人拿到过offer ✨对于面试大模型算法工程师会有一定的帮助,都附有完整答案,熬夜也要看完,祝大家一臂之力 这份《大模型算法工程师面试题》已经上传CSDN,还有完整版的大模型 AI 学习资料,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

dp算法练习题【8】

不同二叉搜索树 96. 不同的二叉搜索树 给你一个整数 n ,求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种?返回满足题意的二叉搜索树的种数。 示例 1: 输入:n = 3输出:5 示例 2: 输入:n = 1输出:1 class Solution {public int numTrees(int n) {int[] dp = new int

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯: