首页 > 其他分享 >[Algorithm] Stable internships

[Algorithm] Stable internships

时间:2023-01-02 01:44:05浏览次数:40  
标签:const Algorithm interns Stable leadingPickSide intern leadingIndex team internsh

A company has hired N interns to each join one of N different teams. Each intern has ranked their preferences for which teams they wish to join, and each team has ranked their preferences for which interns they prefer.

Given these preferences, assign 1 intern to each team. These assignments should be "stable," meaning that there is no unmatched pair of an intern and a team such that both that intern and that team would prefer they be matched with each other.

In the case there are multiple valid stable matchings, the solution that is most optimal for the interns should be chosen (i.e. every intern should be matched with the best team possible for them).

Your function should take in 2 2-dimensional lists, one for interns and one for teams. Each inner list represents a single intern or team's preferences, ranked from most preferable to least preferable. These lists will always be of length N, with integers as elements. Each of these integers corresponds to the index of the team/intern being ranked. Your function should return a 2-dimensional list of matchings in no particular order. Each matching should be in the format [internIndex, teamIndex].

Sample Input

interns = [
  [0, 1, 2],
  [1, 0, 2],
  [1, 2, 0]
]
teams = [
  [2, 1, 0],
  [1, 2, 0],
  [0, 2, 1]
]

Sample Output

// This is the most optimal solution for interns
[
  [0, 0],
  [1, 1],
  [2, 2]
]
// This is also a stable matching, but it is suboptimal for the interns
// because interns 0 and 2 could have been given better team matchings
[
  [2, 0],
  [1, 1],
  [0, 2]
]

 

Solution

// Intern - leading factor
// Team - side factor
function stableInternships(leadingFactor, sidefactor) {
  const res = {}
  const sideRanks = sidefactor.map(side => {
    return side.reduce((acc, curr, i) => {
      acc[curr] = i
      return acc
    }, {})
  })
  const spots = [...new Array(leadingFactor.length)].map((_, i) => i)
  const rounds = [...new Array(leadingFactor.length)].fill(0)

  while (spots.length > 0) {
    const leadingIndex = spots.pop()
    const leadingPickSide = leadingFactor[leadingIndex][rounds[leadingIndex]]
    rounds[leadingIndex] += 1

    if (!(leadingPickSide in res)) {
      res[leadingPickSide] = leadingIndex
      continue
    }

    const previousLeadingRank = sideRanks[leadingPickSide][res[leadingPickSide]]
    const currentLeadingRank = sideRanks[leadingPickSide][leadingIndex]
    if (previousLeadingRank < currentLeadingRank) {
      spots.push(leadingIndex)
    } else {
      spots.push(res[leadingPickSide])
      res[leadingPickSide] = leadingIndex
    }
  }
  
  return Object.entries(res).map(([side, leading]) => {
    return [leading, parseInt(side)]
  });
}

// Do not edit the line below.
exports.stableInternships = stableInternships;

O(n^2) time | O(n^2) space - where n is the number of interns

标签:const,Algorithm,interns,Stable,leadingPickSide,intern,leadingIndex,team,internsh
From: https://www.cnblogs.com/Answer1215/p/17019347.html

相关文章

  • Algorithm 3 - 数据结构
    数据结构是该好好补补拉。1.线段树2.平衡树3.莫队3.1普通莫队莫队解决的问题一般是区间查询,并且可以离线。利用一种排序区间的方式,保证暴力移动最有指针的复杂度......
  • Algorithm 2 - 一些数论/组合计数知识
    0.一些前置知识莫比乌斯函数:定义\(\mu(x)\)为:当\(x\)含平方因子,则\(\mu(x)=0\);否则设其有\(p\)个质因子,\(\mu(x)=(-1)^p\)。特别的,\(\mu(1)=1\)。莫比乌斯......
  • 简读 || A Belief Propagation Algorithm for Multipath-Based SLAM
    原文链接:ABeliefPropagationAlgorithmforMultipath-BasedSLAM|IEEEJournals&Magazine|IEEEXplore开源代码:https://www2.spsc.tugraz.at/people/eriklei/BP-......
  • [Algorithm] 双关键字查询问题
    问题现在米尔科村的考试时间。每个人都想尽可能不费吹灰之力地通过考试,这并不容易。米尔科意识到,对他来说,最好是找到比他了解更多的人,并向他们学习。每个人都跟着,现在每个......
  • A Neural Algorithm of Artistic Style论文学习
    ANeuralAlgorithmofArtisticStyle文章大致:算法基于深度神经网络,能将任意图片根据任意画家的风格转化,并提供一种方法了解人类如何创造和感知艺术意象featuremap(特征映......
  • STL 算法 <algorithm>,
    STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成。要使用STL中的算法函数必须包含头文件<algorithm>,对于数值算法须包含<numeric>,<functional>......
  • g2o相关问题cs.h,以及no matching function for call to ‘g2o::OptimizationAlgorith
    1.对于cs.h找不到的情况1)编译的时候一定要把csparse在EXTERNAL文件中,编译进去。2)修改CMakeLists.txt文件中的include_directories中的${CPARSE_INCLUDE_DIR},在DIR后面不能加......
  • linux安装stable diffusion2.0完整教程-还不会安装sd2.0?一篇文章教会你AI绘画
    原文地址:https://chenhx.blog.csdn.net/article/details/128383113以下教程出自飞链云AI技术人员,欢迎使用目前国内顶尖的AI绘画工具,微信小程序搜索:【飞链云版图】注意:请......
  • Algorithm - DP
    0.前言关于DP我很早就想做总结性的东西。寒假要提前放假了,那么,加油吧!1.“基础DP”看似简单的算法,却能把你难死1.1区间DP1.2树形DP1.3基环树DP1.4其他......
  • java.security.NoSuchAlgorithmException:Cannot find any provider supporting AES/C
    由于小程序开发的需求,需要在后台对微信接口返回的敏感信息加密数据进行解密,以便开发使用,但是,在解密时出现以下异常:java.security.NoSuchAlgorithmException:Cannotfindan......