[Algorithm] Stable internships
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2021-01-02 [React] Redux Toolkit notes
2019-01-02 [Algorithm] Heap data structure and heap sort algorithm
2019-01-02 [Debug] Diagnose a Slow Page Using Developer Tools
2019-01-02 [Tools] Deploy a Monorepo to Now V2
2019-01-02 [PWA] Add Push Notifications to a PWA with React in Chrome and on Android
2018-01-02 [Poi] Use Poi to Build an Index.js with Modern JavaScript Features
2016-01-02 [ES6] Rest Parameter