• leetcode - 780. Reaching Points


    Description

    Given four integers sx, sy, tx, and ty, return true if it is possible to convert the point (sx, sy) to the point (tx, ty) through some operations, or false otherwise.

    The allowed operation on some point (x, y) is to convert it to either (x, x + y) or (x + y, y).

    Example 1:

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Example 2:

    Input: sx = 1, sy = 1, tx = 2, ty = 2
    Output: false
    
    • 1
    • 2

    Example 3:

    Input: sx = 1, sy = 1, tx = 1, ty = 1
    Output: true
    
    • 1
    • 2

    Constraints:

    1 <= sx, sy, tx, ty <= 10^9
    
    • 1

    Solution

    Shrink 1by1

    The possibilities are like a binary tree, use example 1:

    			   1,1
    			/		\
    		1,2			2,1
    		/	\		/ \
    	1,3		3,2	  2,3 	3,1
    	/ \		/ \		/\
      1,4  4,3 3,5 5,2 ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    So instead of searching from the sx, sy, which is the top of the tree, we could start from the leaf, which is the tx, ty

    Note that:
    t x , t y = { s x , s x + s y s x + s y , s y tx, ty = {sx,sx+sysx+sy,sy tx,ty={sx,sx+sysx+sy,sy
    So every time shrink the smaller one from tx, ty, which means find the parent of the node, until we find the source node.

    Time complexity: o ( log ⁡ max ⁡ ( t x , t y ) ) o(\log \max(tx, ty)) o(logmax(tx,ty))
    Space complexity: o ( 1 ) o(1) o(1)

    Shrink by potential maximum

    It’s too slow to shrink one node at a time, we could shrink to the number that is larger than sx or sy

    Code

    Shrink 1by1 (TLE)

    class Solution:
        def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:
            while (tx != sx or ty != sy) and tx >= 1 and ty >= 1:
                if tx > ty:
                    tx, ty = tx % ty, ty
                else:
                    tx, ty = tx, ty % tx
            return tx == sx and ty == sy
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Shrink by potential maximum

    class Solution:
        def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:
            while (tx != sx or ty != sy) and tx >= 1 and ty >= 1:
                if tx > ty:
                    multi_factor = max(1, (tx - sx) // ty)
                    tx, ty = tx - multi_factor * ty, ty
                else:
                    multi_factor = max(1, (ty - sy) // tx)
                    tx, ty = tx, ty - tx * multi_factor
            return tx == sx and ty == sy
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    谷歌宣布:今年将Android 12L系统交付于三星、联想和微软
    25-python面向对象版学员管理系统
    SQLite SQL教程之如何实现contains函数,left join 判断一个字符串包含另外一个(教程含源码)
    阿里云->宝塔配置
    并查集及相关变形
    Linux 内核页表管理
    代码随想录训练营第五十七天|647. 回文子串、516.最长回文子序列
    2120 -- 预警系统题解
    SpringCloud:NetFlix(下)
    使用 Echarts 插件完成中国旅游地图
  • 原文地址:https://blog.csdn.net/sinat_41679123/article/details/133938392