作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/delete-and-earn/description/

题目描述

Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.

Example:

Given a / b = 2.0, b / c = 3.0.
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? .
return [6.0, 0.5, -1.0, 1.0, -1.0 ].

The input is: vector<pair<string, string>> equations, vector& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector.

According to the example above:

equations = [ ["a", "b"], ["b", "c"] ],
values = [2.0, 3.0],
queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ].

The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.

题目大意

已经给出了某些变量的比值,求新的变量的比值。如果这个变量没有出现过,或者不可到达,那么返回-1.

解题方法

这个题其实是一个带权有向图。

题目中给了顶点和顶点之间的关系,其实就是绘制了这个图。然后要求的新的比值其实就是从一个顶点到达另外一个顶点的路径,并且把这条路径上所有的权重相乘。

注意,如果a/b=3,那么从a到b是3,那么从b到a是1/3.

既然是从一个顶点出发到达另外一个顶点,所以应该是dfs解决的问题。

为了防止在DFS中走已经走过了的路,所以需要使用visited保存每次已经访问过的节点。

Python代码如下:

class Solution:
def calcEquation(self, equations, values, queries):
"""
:type equations: List[List[str]]
:type values: List[float]
:type queries: List[List[str]]
:rtype: List[float]
"""
table = collections.defaultdict(dict)
for (x, y), value in zip(equations, values):
table[x][y] = value
table[y][x] = 1.0 / value
ans = [self.dfs(x, y, table, set()) if x in table and y in table else -1.0 for (x, y) in queries]
return ans def dfs(self, x, y, table, visited):
if x == y:
return 1.0
visited.add(x)
for n in table[x]:
if n in visited: continue
visited.add(n)
d = self.dfs(n, y, table, visited)
if d > 0:
return d * table[x][n]
return -1.0

方法二:

并查集。留给二刷。

参考资料:

https://www.youtube.com/watch?v=UwpvInpgFmo
https://zxi.mytechroad.com/blog/graph/leetcode-399-evaluate-division/

日期

2018 年 9 月 10 日 —— 教师节快乐~
2019 年 3 月 16 日 —— 周末加油~

最新文章

  1. Android Butterknife 8.4.0 使用方法总结
  2. Python学习笔记——字典
  3. tcpdump 获取http请求url
  4. 怎么解决ZBrush保存历史记录太多问题
  5. 一些iOS心得
  6. [Under the hood]---Matt Pietrek October 1996 MSJ
  7. WordPress 常用数据库SQL查询语句大全
  8. 36.在字符串中删除特定的字符[Delete source from dest]
  9. 解决tomcat部署多个虚拟机时报IllegalStateException: Web app root system property already set to 的问题
  10. 让一个小Div(子)在大Div(父)中垂直水平都居中
  11. C#中String和string有什么区别
  12. 《FPGA零基础入门到精通视频教程》-第001b讲软件的破解
  13. Hadoop 4、Hadoop MapReduce的工作原理
  14. el表达式跟ognl表达式的区别
  15. php+ajax发起流程和审核流程(以请假为例)
  16. 在Intellij Idea中使用JSTL标签库
  17. JAVA005-基本数据类型变量的存储
  18. Python实现工厂模式
  19. 加速Android Studio编译速度
  20. 评分卡模型剖析之一(woe、IV、ROC、信息熵)

热门文章

  1. 62-Binary Tree Level Order Traversal
  2. a.out的由来
  3. 7个连环问揭开java多线程背后的弯弯绕
  4. 学习java 7.21
  5. [C++] vptr, where are you?
  6. Mybatis相关知识点(一)
  7. 顺序栈(C++)
  8. android:textAppearance解析
  9. Java Web 实现Mysql 数据库备份与还原
  10. 【Spring Framework】Spring入门教程(五)AOP思想和动态代理