package com.freetymekiyan.algorithms.other;

import org.junit.Assert;
import org.junit.Test;

/**
 * Suppose you are a salesman who travel around for business. Given n cities, for city i you can do business Ci times.
 * If you cannot go to the same city the next day. Find the maximum days you can do business.
 * <p>
 * Example 1:
 * Input: [7, 3, 2]
 * Output: 11
 * <p>
 * Example 2:
 * Input: [1000, 1, 1, 1]
 * Output: 7
 */
public class MaxSales {

    public int maxSalesTime(int[] c) {
        if (c == null || c.length == 0) {
            return 0;
        }
        if (c.length == 1) {
            return c[0] > 0 ? 1 : 0;
        }
        int n = c.length;
        int max = 0;
        int index = -1;
        while (true) {
            int curMax = 0;
            int curSecMax = 0;
            int curIdx = -1;
            int curSecIndx = -1;
            for (int j = 0; j < n; j++) {
                if (curIdx == -1) {
                    curMax = c[j];
                    curIdx = j;
                } else if (c[j] > curMax) {
                    curSecMax = curMax;
                    curSecIndx = curIdx;
                    curMax = c[j];
                    curIdx = j;
                } else if (c[j] > curSecMax) {
                    curSecMax = c[j];
                    curSecIndx = j;
                }
            }
            index = index == curIdx ? curSecIndx : curIdx;
            if (index == -1) {
                break;
            }
            c[index]--;
            max++;
        }
        return max;
    }

    @Test
    public void testExamples() {
        MaxSales m = new MaxSales();
        int[] c = {1000, 1, 1, 1};
        Assert.assertEquals(7, m.maxSalesTime(c));
        c = new int[]{7, 3, 2};
        Assert.assertEquals(11, m.maxSalesTime(c));
    }
}

@@ -760,69 +760,25 @@
 x = 
-0;%0A            int curSecMax = 0;%0A            int curIdx = -1
+Integer.MIN_VALUE
 ;%0A  
@@ -790,29 +790,25 @@
      int cur
-SecIn
+I
 dx = -1;%0A   
@@ -870,130 +870,32 @@
 if (
-curIdx =
+index !
 = -1
-) %7B%0A                    curMax = c%5Bj%5D;%0A                    curIdx = j;%0A                %7D else if (c%5Bj%5D %3E curMa
+ && j == inde
 x) %7B
@@ -920,25 +920,15 @@
    c
-urSecMax = curMax
+ontinue
 ;%0A  
@@ -945,122 +945,25 @@
     
-    curSecIndx = curIdx;%0A                    curMax = c%5Bj%5D;%0A                    curIdx = j;%0A                %7D else
+%7D%0A               
  if 
@@ -973,19 +973,16 @@
 j%5D %3E cur
-Sec
 Max) %7B%0A 
@@ -1003,19 +1003,16 @@
      cur
-Sec
 Max = c%5B
@@ -1038,21 +1038,17 @@
      cur
-SecIn
+I
 dx = j;%0A
@@ -1102,39 +1102,8 @@
 ex =
- index == curIdx ? curSecIndx :
  cur
@@ -1127,19 +1127,23 @@
 if (
+c%5B
 index
+%5D--
  == 
--1
+0
 ) %7B%0A
@@ -1183,32 +1183,8 @@
   %7D%0A
-            c%5Bindex%5D--;%0A
     
@@ -1481,20 +1481,107 @@
 esTime(c));%0A
+        c = new int%5B%5D%7B7, 3, 2, 1%7D;%0A        Assert.assertEquals(13, m.maxSalesTime(c));%0A
     %7D%0A%7D%0A

update Max Sales