【leetcode】Reaching Points
题目如下:
A move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y). Given a starting point (sx, sy) and a target point (tx, ty), return True if and only if a sequence of moves exists to transform the point (sx, sy) to (tx, ty). Otherwise, return False. Examples:
Input: sx = 1, sy = 1, tx = 3, ty = 5
Output: True
Explanation:
One series of moves that transforms the starting point to the target is:
(1, 1) -> (1, 2)
(1, 2) -> (3, 2)
(3, 2) -> (3, 5) Input: sx = 1, sy = 1, tx = 2, ty = 2
Output: False Input: sx = 1, sy = 1, tx = 1, ty = 1
Output: True Note:
sx, sy, tx, ty will all be integers in the range [1, 10^9].
解题思路:看到sx,sy,tx,ty的最大值可以是10^9,那就基本上放弃穷举法了。本题比较适合倒推法,例如题目中给的例子[1,1] -> [3,5]:要想得到[3,5],那么前面的一组数组必定是[3,2] (即[3,5-3])计算得来,而[3,2]又是从[1,2] (即[3-2,2])来的,最后推出[1,1]。 原理就是用[tx,ty]中较大值减去较小值依次计算得来。所以,很快我就写出来如下代码:
while tx > 0 and ty > 0:
if ty == tx:
ret = False
break
if ty > tx:
ty -= tx
else:
tx -=ty
if sx == tx and sy == ty:
ret = True
break
return ret
可以这段代码却被[1,1,10^9,1]的输入打败。一时间我竟然想不出更好的办法,直到第二天,脑子里才出现了“用除法代替减法”的方案。方案也很简单,如果tx > ty,那么tx可以利用除法得到一个不小于max(ty,sx)的最小数。完整代码如下:
class Solution(object):
def reachingPoints(self, sx, sy, tx, ty):
"""
:type sx: int
:type sy: int
:type tx: int
:type ty: int
:rtype: bool
"""
if sx == tx and sy == ty:
return True
ret = True
while tx > 0 and ty > 0:
if ty == tx:
ret = False
break
if ty > tx:
max_y = max(tx,sy)
div = (ty - max_y) / tx
div = max (div,1)
ty -= div * tx
else:
max_x = max(ty,sx)
div = (tx - max_x) / ty
div = max (div,1)
tx -= div * ty
if sx == tx and sy == ty:
ret = True
break
return ret
【leetcode】Reaching Points的更多相关文章
- 【leetcode】Max Points on a Line
Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...
- 【leetcode】Max Points on a Line(hard)☆
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- 移动端自动化==>AppiumApi接口详解
Appium 初始化配置信息(Desired Capabilities) Desired Capabilities实际上就是一个字典,它主要用于向Appium Server提供初始化配置参数,如:想要 ...
- SAP屏幕事件的控制
1. INITALIZATION事件 该事件在屏幕未显示之前执行,对程序设置值及屏幕元素进行初始化赋值. REPORT Y001. PARAMETERS QUAL_DAY TYPE D DEFAUL ...
- 2018.03.27 pandas duplicated 和 replace 使用
#.duplicated / .replace import numpy as np import pandas as pd s = pd.Series([1,1,1,1,1,2,3,3,3,4,4, ...
- 【算法与数据结构】图的最小生成树 MST - Prim 算法
Prim 算法属于贪心算法. #include <stdio.h> #define VERTEXNUM 7 #define INF 10000 typedef struct Graph { ...
- Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib 的一种情形
没有引用任何.net 4.5的东西,也没有引用 Newtonsoft.dll,原因是引用了微软的tlb类型库,引用方法如 https://www.cnblogs.com/nanfei/p/108798 ...
- Servlet 响应 响应相关与重定向 请求 获取表单数据2种方法
一.HttpServletResponse (响应) 包括下面三个: 1.响应消息行 HTTP/1.1 200 OK 200是HTTP状态码, 代表请求已成功. (查httpservletres ...
- ftp搭建mysql服务器
一.将mysql放入FTP服务器中1.安装FTP yum install -y vsftpd2.准备ftp主目录 mkdir /var/ftp/mysql57/3.官网下载yum仓库的包. ...
- linux ftp使用相关
ftp 7.7.6.201 21121 name:aaa password:123456
- dotnet sdk 的镜像tag 相关
https://hub.docker.com/_/microsoft-dotnet-core-sdk/ 微软的dotnet sdk 的 tag 微软貌似改默认镜像 dockerhub 里面的tag与 ...
- [转帖]Oracle 查询各表空间使用情况--完善篇
Oracle 查询各表空间使用情况--完善篇 链接:http://blog.itpub.net/28602568/viewspace-1770577/ 标题: Oracle 查询各表空间使用情况--完 ...