跳转至

780. 到达终点

题目描述

给定四个整数 sx , sy ,tx 和 ty,如果通过一系列的转换可以从起点 (sx, sy) 到达终点 (tx, ty),则返回 true,否则返回 false

从点 (x, y) 可以转换到 (x, x+y)  或者 (x+y, y)

 

示例 1:

输入: sx = 1, sy = 1, tx = 3, ty = 5
输出: true
解释:
可以通过以下一系列转换从起点转换到终点:
(1, 1) -> (1, 2)
(1, 2) -> (3, 2)
(3, 2) -> (3, 5)

示例 2:

输入: sx = 1, sy = 1, tx = 2, ty = 2 
输出: false

示例 3:

输入: sx = 1, sy = 1, tx = 1, ty = 1 
输出: true

 

提示:

  • 1 <= sx, sy, tx, ty <= 109

解法

方法一:逆向计算

(tx, ty) 开始逆向计算,判断是否可以到达状态 (sx, sy)。由于逆向计算是将 tx, ty 中的较大值减少,因此当 tx > ty 时可以直接将 tx 的值更新为 tx % ty,当 tx < ty 时可以将 ty 的值更新为 ty % tx。逆向计算需要满足 tx > sx && ty > sy && tx != ty

当条件不成立时,根据此时 tx 和 ty 判断是否可以从起点转换到终点。

  • 如果 tx == sx && ty == sy,说明此时已经到达起点状态,返回 true;
  • 如果 tx == sx,若 ty > sy && (ty - sy) % tx == 0,返回 true,否则返回 false;
  • 如果 ty == sy,若 tx > sx && (tx - sx) % ty == 0,返回 true,否则返回 false;
  • 如果 tx ≠ sx && ty ≠ sy,则不可以从起点转换到终点。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution:
    def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:
        while tx > sx and ty > sy and tx != ty:
            if tx > ty:
                tx %= ty
            else:
                ty %= tx
        if tx == sx and ty == sy:
            return True
        if tx == sx:
            return ty > sy and (ty - sy) % tx == 0
        if ty == sy:
            return tx > sx and (tx - sx) % ty == 0
        return False
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
    public boolean reachingPoints(int sx, int sy, int tx, int ty) {
        while (tx > sx && ty > sy && tx != ty) {
            if (tx > ty) {
                tx %= ty;
            } else {
                ty %= tx;
            }
        }
        if (tx == sx && ty == sy) {
            return true;
        }
        if (tx == sx) {
            return ty > sy && (ty - sy) % tx == 0;
        }
        if (ty == sy) {
            return tx > sx && (tx - sx) % ty == 0;
        }
        return false;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
public:
    bool reachingPoints(int sx, int sy, int tx, int ty) {
        while (tx > sx && ty > sy && tx != ty) {
            if (tx > ty)
                tx %= ty;
            else
                ty %= tx;
        }
        if (tx == sx && ty == sy) return true;
        if (tx == sx) return ty > sy && (ty - sy) % tx == 0;
        if (ty == sy) return tx > sx && (tx - sx) % ty == 0;
        return false;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
func reachingPoints(sx int, sy int, tx int, ty int) bool {
    for tx > sx && ty > sy && tx != ty {
        if tx > ty {
            tx %= ty
        } else {
            ty %= tx
        }
    }
    if tx == sx && ty == sy {
        return true
    }
    if tx == sx {
        return ty > sy && (ty-sy)%tx == 0
    }
    if ty == sy {
        return tx > sx && (tx-sx)%ty == 0
    }
    return false
}

评论

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy