package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/range-sum-query-immutable/
 */
public class Problem303 {
    public static class NumArray {
        private final Map<Integer, Integer> map = new HashMap<>();
        private final int[] nums;

        public NumArray(int[] nums) {
            this.nums = nums;
            if (nums.length > 0) {
                int sum = nums[0];
                for (int i = 1; i < nums.length; i++) {
                    sum += nums[i];
                    map.put(i, sum);
                }
            }
        }

        public int sumRange(int i, int j) {
            if (i == j) {
                return nums[i];
            }
            Integer a = map.get(i);
            if (a == null) {
                a = 0;
            }
            if (i > 0) {
                a -= nums[i];
            }
            Integer b = map.get(j);
            if (b == null) {
                b = 0;
            }
            return b - a;
        }
    }
}

@@ -16,57 +16,8 @@
 e;%0A%0A
-import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A
@@ -145,75 +145,8 @@
 y %7B%0A
-        private final Map%3CInteger, Integer%3E map = new HashMap%3C%3E();%0A
     
@@ -283,43 +283,8 @@
 ) %7B%0A
-                int sum = nums%5B0%5D;%0A
     
@@ -359,13 +359,16 @@
     
-s
+n
 um
+s%5Bi%5D
  
-+
 = nu
@@ -376,45 +376,22 @@
 s%5Bi%5D
-;%0A                    map.put(i, sum)
+ + nums%5Bi - 1%5D
 ;%0A  
@@ -497,12 +497,16 @@
  (i 
-== j
+- 1 %3E= 0
 ) %7B%0A
@@ -537,283 +537,23 @@
 ums%5B
-i%5D;%0A            %7D%0A            Integer a = map.get(i);%0A            if (a == null) %7B%0A                a = 0;%0A            %7D%0A            if (i %3E 0) %7B%0A                a
+j%5D
  -
-=
  nums%5Bi
-%5D;%0A            %7D%0A            Integer b = map.get(j);%0A            if (b == null) %7B%0A                b = 0
+ -1%5D
 ;%0A  
@@ -587,13 +587,15 @@
 urn 
-b - a
+nums%5Bj%5D
 ;%0A  

Optimize solution to problem 303