查看: 1133| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 这题就好像一组火车时刻表,问能不能从某站到某站

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
I am given an array of Transactions in a ledger. A Transaction object has three things.

  • Sender (which means who started this transaction)
  • Receiver (means who is the destination of transaction)
  • Timestamp (at what time this transaction was executed)


Now I need to write a method findIfTransactionIsValid() which will have the array of all transactions, one sender, one receiver. A transaction is valid is following cases:
  • if sender and receiver are same
  • The timestamp should be increasing (what I mean here is if A -> B happens at Time 2 and B-> C happens at Time 1, then A->C is not a valid transaction, however if B -> C happens at Time 3 then A --> C is a valid transaction)


Example:
Transaction
{
  Sender -> Receiver (Timestamp)
}

Example: [T is timestamp here]
  1. A -> B (T=0)
  2. B -> C (T=1)
  3. C -> F (T=0)
复制代码


findIfTransactionIsValid(A, C) -> this should return true
findIfTransactionIsValid(B, F) -> false (time is backwards)




上一篇:求刷题建议,coding的基本功提高
下一篇:想问一下leetcode上题目的答案在哪可以看到...
🔗
 楼主| 14417335 2019-2-20 22:02:30 | 只看该作者
全局:
步惊云 发表于 2019-2-20 10:33
没什么太多想法, bfs吧. node上存到下一个node的不同时间.

差不多算single source (shortest) path那种 ...

这题随着时间的变化,edges会发生变化。觉得bfs和dfs都可以。针对楼顶的问题,
- 需要对每个node的edges用时间进行排序。方便搜索哪些边可以走
- 如果有环visited过了就不必再计算了。

问题的followup会有很多种。见过的面经里有的是:

从点A到点Z最早的到达时间,应该是dijkstra。

能到达点Z的最晚从A点出发时间。我的想法是每个node的edges用时间倒序。从A的最晚的时间开始DFS。每个node需要记录哪些边已经走过以避免重复计算。这样如果A最晚的不行,倒数第二晚的再计算就不必计算已经测试过的边了。

不知还有无其它followups。

可能还有:从点A开始必须经过点C有没有到达点Z的可能
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表