首页 > 其他分享 >335. Self Crossing (Hard)

335. Self Crossing (Hard)

时间:2023-07-28 17:15:52浏览次数:72  
标签:distance north 335 west Self moving && Crossing true

Description

335. Self Crossing (Hard)

You are given an array of integers distance.

You start at the point (0, 0) on an X-Y plane, and you move distance[0] meters to the north, then distance[1] meters to the west, distance[2] meters to the south, distance[3] meters to the east, and so on. In other words, after each move, your direction changes counter-clockwise.

Return true if your path crosses itself or false if it does not.

 

Example 1:

Input: distance = [2,1,1,2]
Output: true
Explanation: The path crosses itself at the point (0, 1).

Example 2:

Input: distance = [1,2,3,4]
Output: false
Explanation: The path does not cross itself at any point.

Example 3:

Input: distance = [1,1,1,2,1]
Output: true
Explanation: The path crosses itself at the point (0, 0).

 

Constraints:

  • 1 <= distance.length <= 105
  • 1 <= distance[i] <= 105

Solution

We can simulate the movement of the robot and consider the possible collisions with other tracks. For example, when moving north, the robot may intersect with tracks moving west, east, or north. Similarly, when moving west, the robot may collide with tracks moving north, south, or west. We can continue this process for other directions as well.

For instance, when moving west, we can consider the conditions for collisions with tracks moving north, south, or west.

Code

class Solution {
  public:
    bool isSelfCrossing(vector<int> &distance) {
        int n = distance.size();
        if (n <= 3) {
            return 0;
        }
        for (int i = 3; i < n; ++i) {
            cout << i << endl;
            if (i == 3) {
                if (distance[2] <= distance[0] && distance[3] >= distance[1]) {
                    return true;
                }
            } else if (i == 4) {
                if ((distance[i - 1] == distance[i - 3] && distance[i] + distance[i - 4] >= distance[i - 2]) || (distance[i] >= distance[i - 2] && distance[i - 1] <= distance[i - 3])) {
                    return true;
                }
            } else {
                if ((distance[i - 1] <= distance[i - 3] && distance[i] >= distance[i - 2]) || (distance[i] + distance[i - 4] >= distance[i - 2] && distance[i - 1] + distance[i - 5] >= distance[i - 3] && distance[i - 3] > distance[i - 5] && distance[i - 2] > distance[i - 4] && distance[i - 1] <= distance[i - 3]) || (distance[i - 1] == distance[i - 3] && distance[i] + distance[i - 4] >= distance[i - 2])) {
                    return true;
                }
            }
        }
        return false;
    }
};

标签:distance,north,335,west,Self,moving,&&,Crossing,true
From: https://www.cnblogs.com/zwyyy456/p/17588154.html

相关文章

  • 335. 路径交叉 (Hard)
    问题描述335.路径交叉(Hard)给你一个整数数组distance。从X-Y平面上的点(0,0)开始,先向北移动distance[0]米,然后向西移动distance[1]米,向南移动distance[2]米,向东移动distance[3]米,持续移动。也就是说,每次移动后你的方位会发生逆时针变化。判断你所经过的路径......
  • composer:升级自身版本 self-update(2.5.x)
    一,查看升级前的版本:liuhongdi@lhdpc:/data/php/tpapibase$composer--versionComposerversion2.5.12022-12-2215:33:54二,升级,注意先sudo到rootliuhongdi@lhdpc:/data/php/tpapibase$sudo-i[sudo]liuhongdi的密码:root@lhdpc:~#composerself-updateUpgrading......
  • 自动化测试 | python | self说明
    1.从函数的语法上讲,self是形参,就可以是任意的变量名,只不过我们习惯性将这个形参写作self。 2.self是普通的形参,但是在调⽤的时候没有传递实参值,原因是,Python解释器在执⾏代码的时候,⾃动的将调⽤这个⽅法的对象传递给了self,即self的本质是对象。 3.......
  • 模型类中建立外键的常用方法 db_constraint=False,self.user.id
    1.user=models.ForeignKey(to=User,related_name='order_user',on_delete=models.DO_NOTHING,db_constraint=False,verbose_name="下单用户") to=Order:这是ForeignKey的一个参数,用于指定这个外键字段将关联到的目标模型。在这个例子中,外键字段将关联到名为Order的模......
  • P3352 [ZJOI2016] 线段树 思考--zhengjun
    有一个显然的\(O(n^3q)\)的做法:设\(f_{i,l,r,x}\)表示\(i\)次操作过后,区间\([l,r]\)的数\(\lex\),\(a_{l-1},a_{r+1}>x\)的方案数。转移:$$f_{i,l,r,x}=f_{i-1,l,r,x}\timesg_{l,r}+\sum\limits_{j<l}f_{i-1,j,r,x}\times(j-1)+\sum\limits_{j>r}f_{i-1,l......
  • python的传参 self
    Python的传参self在Python中,我们经常会看到类的方法定义中的第一个参数是self。但是,对于初学者来说,这个self是一个比较迷惑的概念。本文将详细介绍Python中的self参数,并通过一些代码示例来加深理解。什么是selfself是一个约定俗成的命名,它表示对象自身。它是类的实例方法的第一......
  • self-attention
    Selfattention考虑了整个sequence的资讯【transfermer中重要的架构是self-attention】 解决sequence2sequence的问题,考虑前后文 Isawasaw第一个saw对应输出动词 第二个输出名词 如何计算相关性【attentionscore】输入的两个向量乘两个矩阵Q=query   k=key......
  • 魔法方法之__iter__(self) && __next__(self)
    __iter____iter__(self)是一个特殊方法,用于返回一个迭代器对象,使得自定义的类可以支持迭代操作。最佳实践:在自定义类中实现 __iter__() 方法时,应该返回一个迭代器对象,通常是自身的实例。迭代器对象应该实现 __next__() 方法,用于返回容器中的下一个元素,并在没有更多元素......
  • [论文速览] A Closer Look at Self-supervised Lightweight Vision Transformers
    Pretitle:ACloserLookatSelf-supervisedLightweightVisionTransformersaccepted:ICML2023paper:https://arxiv.org/abs/2205.14443code:https://github.com/wangsr126/mae-literef:https://mp.weixin.qq.com/s/7FiDLYBZiAX-xkW-dZBU9Q关键词:lightweght,ViT......
  • Paper Reading: Self-paced Ensemble for Highly Imbalanced Massive Data Classifica
    目录研究动机文章贡献分类硬度分布分类硬度的定义分类硬度的优点分类硬度视角下的样本类型本文方法自定步速欠采样硬度协调自定步速因子算法定义实验结果合成数据集实验数据集和实验设置合成数据实验结果类重叠下的鲁棒性真实数据集实验数据集和实验设置真实数据实验结果和重采样......