Leetcode: Self Crossing

You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south, x[3] metres to the east and so on. In other words, after each move your direction changes counter-clockwise.

Write a one-pass algorithm with O(1) extra space to determine, if your path crosses itself, or not.

Example 1:
Given x =
[2, 1, 1, 2]
,
┌───┐
│   │
└───┼──>
    │

Return true (self crossing)
Example 2:
Given x =
[1, 2, 3, 4]
,
┌──────┐
│      │
│
│
└────────────>

Return false (not self crossing)
Example 3:
Given x =
[1, 1, 1, 1]
,
┌───┐
│   │
└───┼>

Return true (self crossing)

4th line may cross with 1st line, and so on: 5th with 2nd, ...etc

5th line may cross with 1st line, and so on: 6th with 2nd, ...etc

6th line also may cross with 1st line, and so on: 7th with 2nd, ...etc

However, if 7th line also cross with 1st line, the following definitely happens at the same time:

  a. 7th line cross with 2nd line

  b. 6th line cross with 1st line

  we have covered these cases.

 1 public class Solution {
 2     public boolean isSelfCrossing(int[] x) {
 3         if (x.length <= 3) return false;
 4         for (int i=3; i<x.length; i++) {
 5             //check if 4th line cross with the first line and so on
 6             if (x[i]>=x[i-2] && x[i-1]<=x[i-3]) return true;
 7
 8             //check if 5th line cross with the first line and so on
 9             if (i >= 4) {
10                 if (x[i-1]==x[i-3] && x[i]+x[i-4]>=x[i-2]) return true;
11             }
12
13             //check if 6th line cross with the first line and so on
14             if (i >= 5) {
15                 if (x[i-2]>=x[i-4] && x[i]>=x[i-2]-x[i-4] && x[i-1]<=x[i-3] && x[i-1]>=x[i-3]-x[i-5]) return true;
16             }
17         }
18         return false;
19     }
20 }
时间: 2024-10-11 18:41:39

Leetcode: Self Crossing的相关文章

[LeetCode][JavaScript]Self Crossing

Self Crossing You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south,x[3] metres to the east and so on. In other words, after each move your

【LeetCode】Self Crossing(335)

1. Description You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south, x[3] metres to the east and so on. In other words, after each move you

LeetCode 335. Self Crossing

挺有趣的一道题,看题解才勉强做出... 题意就是给n个长度,然后以上左下右的顺序走.比如 [2,1,1,2]就是先上走2 左走1 下走1 右走2 如果还有就继续向上,向左…… 求路径中是否存在交叉. 如果在纸上画一画,就会发现,在保证不出现交叉的前提下,可画的类型无非几种 (实不相瞒,我用微信截图随便画的) 就是说如果出现交叉,并不需要考虑和相离较远的某条线段的交叉,因为肯定会和附近的某条线进行交叉.所以算法是 O(N) 而不是 O(N^2) 总结一下可能出现的交叉 不画了 copy from 

[LeetCode] Frog Jump 青蛙过河

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord

LeetCode Frog Jump

原题链接在这里:https://leetcode.com/problems/frog-jump/description/ 题目: A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Giv

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个