Verbose but readable java 2ms solution


  • 0
    D
    public class Solution {
      public boolean isSelfCrossing(int[] x) {
        if (x == null || x.length <= 3) return false;
        int cx = 0, cy = 0;
        Line line1 = new Line(cx, cy, cx, cy + x[0]); cx = cx; cy = cy + x[0];
        Line line2 = new Line(cx, cy, cx - x[1], cy); cx = cx - x[1]; cy = cy;
        Line line3 = new Line(cx, cy, cx, cy - x[2]); cx = cx; cy = cy - x[2];
    
        if (line3.length() <= line1.length()) return !isDecrease(x, 3, line2.length(), line3.length());
        Line line4 = new Line(cx, cy, cx + x[3], cy); cx = cx + x[3]; cy = cy;
        if (line4.length() < line2.length()) return !isDecrease(x, 4, line3.length(), line4.length());
        if (line4.length() == line2.length()) return !isDecrease(x, 4, line3.length() - line1.length(), line4.length());
    
        for (int i = 4; i < x.length; i++) {
          Line line5 = null;
          switch (i % 4) {
            case 0: line5 = new Line(cx, cy, cx, cy + x[i]); cx = cx; cy = cy + x[i]; break;
            case 1: line5 = new Line(cx, cy, cx - x[i], cy); cx = cx - x[i]; cy = cy; break;
            case 2: line5 = new Line(cx, cy, cx, cy - x[i]); cx = cx; cy = cy - x[i]; break;
            case 3: line5 = new Line(cx, cy, cx + x[i], cy); cx = cx + x[i]; cy = cy; break;
            default: throw new RuntimeException();
          }
          if (line5.length() < line3.length() - line1.length()) return !isDecrease(x, i + 1, line4.length(), line5.length());
          if (line5.length() <= line3.length()) return !isDecrease(x, i + 1, line4.length() - line2.length(), line5.length());
    
          line1 = line2;
          line2 = line3;
          line3 = line4;
          line4 = line5;
        }
    
        return false;
      }
    
      private boolean isDecrease(int[] x, int s_ind, int len1, int len2) {
        for (; s_ind < x.length; s_ind++) {
          if (x[s_ind] >= len1) return false;
          len1 = len2;
          len2 = x[s_ind];
        }
        return true;
      }
    
      class Line {
        int x1, y1, x2, y2;
        Line(int x1, int y1, int x2, int y2) {
          this.x1 = x1;
          this.y1 = y1;
          this.x2 = x2;
          this.y2 = y2;
        }
        boolean isVertical() {
          return x1 == x2;
        }
        boolean isHorizontal() {
          return y1 == y2;
        }
        int length() {
          return isVertical() ? Math.abs(y1 - y2) : Math.abs(x1 - x2);
        }
        public String toString() {
          StringBuffer sb = new StringBuffer();
          sb.append("(" + x1 + "," + y1 + ") ");
          sb.append("(" + x2 + "," + y2 + ") ");
          sb.append("length = " + length());
          return sb.toString();
        }
      }
    
      public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.isSelfCrossing(new int[]{1,1,2,2,3,3,4,4,10,4,4,3,3,2,2,1,1}));
      }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.