package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/count-number-of-teams/
 */
public class Problem1395 {
    public int numTeamsBackward(int[] rating) {
        int answer = 0;
        for (int i = 0; i < rating.length; i++) {
            answer += numTeams(rating, i, i, 1);
        }
        int[] reversed = new int[rating.length];
        for (int i = rating.length - 1, j = 0; i >= 0; i--, j++) {
            reversed[j] = rating[i];
        }
        for (int i = 0; i < reversed.length; i++) {
            answer += numTeams(reversed, i, i, 1);
        }
        return answer;
    }

    private static int numTeams(int[] rating, int i, int j, int count) {
        if (count == 3) {
            return 1;
        }
        if (j == rating.length) {
            return 0;
        }
        if (rating[i] < rating[j]) {
            int a = numTeams(rating, i, j + 1, count);
            int b = numTeams(rating, j, j + 1, count + 1);
            return a + b;
        }
        return numTeams(rating, i, j + 1, count);
    }

    public static void main(String[] args) {
        Problem1395 prob = new Problem1395();
        System.out.println(prob.numTeamsBackward(new int[]{2,5,3,4,1})); // 3
        System.out.println(prob.numTeamsBackward(new int[]{2,1,3})); // 0
        System.out.println(prob.numTeamsBackward(new int[]{1,2,3,4})); // 4
    }
}

@@ -16,34 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -126,24 +126,16 @@
 numTeams
-Backward
 (int%5B%5D r
@@ -158,129 +158,44 @@
  int
- answer = 0;%0A        for (int i = 0; i %3C rating.length; i++) %7B%0A            answer += numTeams(rating, i, i, 1);%0A        %7D
+%5B%5D greater = new int%5Brating.length%5D;
 %0A   
@@ -205,24 +205,23 @@
   int%5B%5D 
-reversed
+smaller
  = new i
@@ -259,16 +259,23 @@
 (int i =
+ 0; i %3C
  rating.
@@ -284,124 +284,59 @@
 ngth
- - 1, j = 0; i %3E= 0; i--, j++) %7B%0A            reversed%5Bj%5D = rating%5Bi%5D;%0A        %7D%0A        for (int i = 0; i
+; i++) %7B%0A            for (int j = i + 1; j
  %3C r
-eversed
+ating
 .len
@@ -332,33 +332,33 @@
  rating.length; 
-i
+j
 ++) %7B%0A          
@@ -363,46 +363,40 @@
     
-answer += numTeams(reversed, i, i, 1);
+    if (rating%5Bi%5D %3C rating%5Bj%5D) %7B
 %0A   
@@ -396,34 +396,35 @@
 ) %7B%0A        
-%7D%0A
+   
         
 return answe
@@ -415,194 +415,140 @@
     
-return answer
+ greater%5Bj%5D++
 ;%0A    
-%7D%0A%0A
     
-private static int numTeams(int%5B%5D rating, int i, int j, int count) %7B%0A        if (count == 3) %7B%0A            return 1;%0A        %7D%0A        if (j == rating.length) %7B
+        %7D else if (rating%5Bi%5D %3E rating%5Bj%5D) %7B%0A                    smaller%5Bi%5D++;%0A                %7D%0A            %7D
 %0A   
@@ -556,65 +556,80 @@
     
+%7D%0A
     
-return 0;%0A        %7D%0A        if (rating%5Bi%5D %3C rating%5Bj%5D
+    int answer = 0;%0A        for (int i = 0; i %3C rating.length; i++
 ) %7B%0A
@@ -644,134 +644,135 @@
     
+for (
 int 
-a = numTeams(rating, i, j + 1, count);%0A            int b = numTeams(rating, j, j + 1, count + 1);%0A            return a + b
+j = i; j %3C rating.length; j++) %7B%0A                if (rating%5Bi%5D %3C rating%5Bj%5D) %7B%0A                    answer += greater%5Bi%5D
 ;%0A  
@@ -777,26 +777,24 @@
         
-%7D%0A
         
 return n
@@ -789,375 +789,150 @@
     
-return numTeams(rating, i, j + 1, count);%0A
+%7D else if (rating%5Bi%5D %3E rating%5Bj%5D) %7B%0A      
     
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1395 prob = new Problem1395();%0A        System.out.println(prob.numTeamsBackward(new int%5B%5D%7B2,5,3,4,1%7D)); // 3%0A        System.out.println(prob.numTeamsBackward(new int%5B%5D%7B2,1,3%7D)); // 0%0A        System.out.println(prob.numTeamsBackward(new int%5B%5D%7B1,2,3,4%7D)); // 4
+      answer += smaller%5Bj%5D;%0A                %7D%0A            %7D%0A        %7D%0A        return answer;
 %0A   

Solve problem 1395