LWC 71: 780. Reaching Points

传送门:780. Reaching Points

Problem:

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.

Example:

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].

思路:
先说说简单的,直接根据题目的意思来,递归求解,如下:

    public boolean reachingPoints(int sx, int sy, int tx, int ty) {return f(sx, sy, tx, ty);}boolean f(int sx, int sy, int tx, int ty) {if (tx < sx || ty < sy) return false;if (tx == sx && ty == sy) return true;if (f(sx, sx + sy, tx, ty) || f(sx + sy, sy, tx, ty)) return true;return false;}

stack over flow,原因很简单,如例子[5, 7, 455955547, 420098884],想想它的递归深度。

我们观察式子(x, y) -> (x + y, y) or (x, x + y),如果两者交替相加,那递归深度大是必然的,但很多情况下可以这么transform:

(x, y) -> (x + y, y) -> (x + 2y, y) -> (x + 3y, y)那么当存在tx很大的情况时,我们可以发现tx直接余上ty就能直接省去若干的叠加。那么递归深度也以指数级减少。比如 (x + 3y) % y = x, 所以只需要一步从(x + 3y, y) -> (x, y)

代码如下:

    public boolean reachingPoints(int sx, int sy, int tx, int ty) {return f(sx, sy, tx, ty);}boolean f(int sx, int sy, int tx, int ty) {if (tx < sx || ty < sy) return false;if(sy == ty && (tx-sx) % sy == 0) return true;if(sx == tx && (ty-sy) % sx == 0) return true;if (ty > tx) {if (f(sx, sy, tx, ty % tx)) return true;}else {if (f(sx, sy, tx % ty, ty)) return true;}return false;}

考虑下终止条件,因为不管 ty % tx, 还是 tx % ty, 都会归简到最初的情况,(x + ky, y) or (x, kx + y),在这种情况下,加个判断即可。

Python版本:

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 tx < sx or ty < sy: return Falseif sx == tx and (ty - sy) % sx == 0: return Trueif sy == ty and (tx - sx) % sy == 0: return Trueif tx < ty: return self.reachingPoints(sx, sy, tx, ty % tx)else: return self.reachingPoints(sx, sy, tx % ty, ty)

LWC 71: 780. Reaching Points相关推荐

  1. leetcode算法练习 JavaScript实现

    leetcode 表格内容由spider.js从leetcode-cn.com爬取. 已做题目答案也从leetcode-cn.com中爬取并生成文件. 解题进度:已解决 140/637 - 简单 94 ...

  2. leetcode 题解 (500-1000题,持续更新,part 2)

    part1(1-500), part3(1000-*) 502. IPO 题意:给定k,w,profits数组和capital数组.k表示最多可完成的任务数.w是初始资本.profits是各个任务的收 ...

  3. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  4. canvas-弹珠游戏

    canvas-弹珠游戏 目录 文章目录 前言 结果展示 代码展示 `pinball.html` `pinball.css` `requestNextAnimationFrame.js` `stopwa ...

  5. c 语言 文本处理范例

    c 语言 文本处理范例 从一个文件列表中读入各个文件名,然后依次打开各个文件进行处理.   1 void load_ann_res_files(char *dir_ann, char *dir_res ...

  6. boost原理与sklearn源码_机器学习sklearn系列之决策树

    一. Sklearn库 Scikit learn 也简称 sklearn, 自2007年发布以来,scikit-learn已经成为Python重要的机器学习库了.支持包括分类.回归.降维和聚类四大机器 ...

  7. Algorithm I assignment Collinear

    这本来应该是第三周的作业,但是由于其他作业逼近deadline,暂时推后了一周完成. 这周的assignment大大提高了我对这门课的看法,不得不说,Algorithms这门课的assignment部 ...

  8. MapInfo MapXtreme 2005 WebGIS 简单鹰眼设计(转)

    form 东人EP的内陆空间! original link: http://www.cnitblog.com/eastperson/archive/2006/10/17/18055.aspx orig ...

  9. 记一次DRBD Unknown故障处理过程

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://koumm.blog.51cto.com/703525/1769112 配置drb ...

  10. 第十六届全国大学智能车竞赛华东赛区成绩汇总

    简 介: 第十六届全国大学生智能车竞赛在上海理工大学举行.各组别成绩和奖项在本文中列出. 关键词: 智能车竞赛,华东赛区 §01 各组成绩 一.基础四轮组 学校名称 队伍名称 成绩 决赛成绩 获奖情况 ...

最新文章

  1. 创建节约内存的JavaBean
  2. classpath路径(转)
  3. EXTJS实现的WEBQQ可以传文件了哈
  4. Python open读写文件实现脚本
  5. WinDBG调试dNet程序总结
  6. 分析chrome中的network面板
  7. 【Python】校选课 第六周作业 py3.0
  8. 使用json-lib进行Java和JSON之间的转换
  9. Nginx 的线程池与性能剖析【转载】
  10. 计算机硬件四则运算实验,实验一四则运算(INTERNET).pdf
  11. 初识vue之axios的封装
  12. Linux操作基础(十七)之Systemd入门教程(二)实战篇
  13. 如何修改bt tracker服务器,bt tracker服务器
  14. 2017百度之星初赛
  15. mysql服务器的字符集
  16. makefile从无到有
  17. 类-描述器-把类对象方法转变为属性方式
  18. gis利器之Gdal(二)shp数据读取
  19. Java中生成随机数的4种方式!
  20. 谷歌浏览器截取长屏幕(全屏截图)

热门文章

  1. Deep Closest Point学习笔记(才开始接触点云)
  2. 回声状态网络(ESN)原理详解(附源码实现)
  3. 回声状态网络(ESN)实现手写数字识别(MNIST)
  4. TestCenter测试管理工具功能详解十六(U)
  5. speedpdf——PDF转PPT免费在线转换还不限制页数哦
  6. Longest Palindrome
  7. 【汇智学堂】基于Socket实现的网络版梅花易数一撮金游戏
  8. 在《2000年通则》中,根据卖方承担义务的不同,将13种贸易术语划分为下列四组:...
  9. 光滑噪声数据常用的方法_几种常见的数据变换方法
  10. java 提交mac地址栏_Mac系统快捷键大全 - 米扑博客