[LeetCode] 631. Design Excel Sum Formula

Your task is to design the basic function of Excel and implement the function of sum formula. Specifically, you need to implement the following functions:
Excel(int H, char W): This is the constructor. The inputs represents the height and width of the Excel form. H is a positive integer, range from 1 to 26. It represents the height. W is a character range from 'A' to 'Z'. It represents that the width is the number of characters from 'A' to W. The Excel form content is represented by a height * width 2D integer array C, it should be initialized to zero. You should assume that the first row of C starts from 1, and the first column of C starts from 'A'.

void Set(int row, char column, int val): Change the value at C(row, column) to be val.

int Get(int row, char column): Return the value at C(row, column).

int Sum(int row, char column, List of Strings : numbers): This function calculate and set the value at C(row, column), where the value should be the sum of cells represented by numbers. This function return the sum result at C(row, column). This sum formula should exist until this cell is overlapped by another value or another sum formula.
numbers is a list of strings that each string represent a cell or a range of cells. If the string represent a single cell, then it has the following format : ColRow. For example, "F7" represents the cell at (7, F).
If the string represent a range of cells, then it has the following format : ColRow1:ColRow2. The range will always be a rectangle, and ColRow1 represent the position of the top-left cell, and ColRow2 represents the position of the bottom-right cell.

Example 1:
Excel(3,"C"); 
// construct a 3*3 2D array with all zero.
//   A B C
// 1 0 0 0
// 2 0 0 0
// 3 0 0 0

Set(1, "A", 2);
// set C(1,"A") to be 2.
//   A B C
// 1 2 0 0
// 2 0 0 0
// 3 0 0 0

Sum(3, "C", ["A1", "A1:B2"]);
// set C(3,"C") to be the sum of value at C(1,"A") and the values sum of the rectangle range whose top-left cell is C(1,"A") and bottom-right cell is C(2,"B"). Return 4. 
//   A B C
// 1 2 0 0
// 2 0 0 0
// 3 0 0 4

Set(2, "B", 2);
// set C(2,"B") to be 2. Note C(3, "C") should also be changed.
//   A B C
// 1 2 0 0
// 2 0 2 0
// 3 0 0 6
Note:
  1. You could assume that there won't be any circular sum reference. For example, A1 = sum(B1) and B1 = sum(A1).
  2. The test cases are using double-quotes to represent a character.
  3. Please remember to RESET your class variables declared in class Excel, as static/class variables are persisted across multiple test cases. Please see here for more details.

Thought process:
Topological sort.

Solution:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
class Excel {
    class Cell {
        int value;
        Map<String, Integer> children;
        
        Cell(int value, Map<String, Integer> children) {
            this.value = value;
            this.children = children;
        }
    }

    private Cell[][] form;
    private Stack<int[]> stack = new Stack<>();
    
    public Excel(int H, char W) {
        form = new Cell[H][W - 'A' + 1];
    }
    
    public void set(int r, char c, int v) {
        form[r - 1][c - 'A'] = new Cell(v, new HashMap<>());
        topologicalSort(r - 1, c - 'A');
        sumStack();
    }
    
    public int get(int r, char c) {
        if (form[r - 1][c - 'A'] == null) {
            return 0;
        }
        return form[r - 1][c - 'A'].value;
    }
    
    public int sum(int r, char c, String[] strs) {
        Map<String, Integer> range = convertToMap(strs);
        int sum = getSum(r - 1, c - 'A', range);
        set(r, c, sum);
        form[r - 1][c - 'A'] = new Cell(sum, range);
        
        return sum;
    }
    
    private void topologicalSort(int row, int col) {
        for (int i = 0; i < form.length; i++) {
            for (int j = 0; j < form[0].length; j++) {
                if (form[i][j] != null && form[i][j].children.containsKey("" + (char)('A' + col) + (row + 1))) {
                    topologicalSort(i, j);
                }
            }
        }
        stack.push(new int[]{ row, col });
    }
    
    private void sumStack() {
        while (!stack.isEmpty()) {
            int[] c = stack.pop();
            Cell cell = form[c[0]][c[1]];
            if (cell.children.size() > 0) {
                getSum(c[0], c[1], cell.children);
            }
        }
    }
    
    private int getSum(int row, int col, Map<String, Integer> children) {
        int sum = 0;        
        for (String s : children.keySet()) {
            int i = Integer.parseInt(s.substring(1)) - 1;
            int j = s.charAt(0) - 'A';
            sum += (form[i][j] == null ? 0 : form[i][j].value) * children.get(s);
        }
        form[row][col] = new Cell(sum, children);
        
        return sum;
    }
    
    private Map<String, Integer> convertToMap(String[] strs) {
        Map<String, Integer> map = new HashMap<>();
        for (String s : strs) {
            if (s.indexOf(":") < 0) {
                map.put(s, map.getOrDefault(s, 0) + 1);
            } else {
                String[] range = s.split(":");
                int i1 = Integer.parseInt(range[0].substring(1));
                int i2 = Integer.parseInt(range[1].substring(1));
                char j1 = range[0].charAt(0);
                char j2 = range[1].charAt(0);
                
                for (int i = i1; i <= i2; i++) {
                    for (char j = j1; j <= j2; j++) {
                        String key = "" + j + i;
                        map.put(key, map.getOrDefault(key, 0) + 1);
                    }
                }
            }
        }
        return map;
    }
}

/**
 * Your Excel object will be instantiated and called as such:
 * Excel obj = new Excel(H, W);
 * obj.set(r,c,v);
 * int param_2 = obj.get(r,c);
 * int param_3 = obj.sum(r,c,strs);
 */

Time complexity:

  • Constructor: O(hw).
  • set(): O((hw)^2).
  • get(): O(1).
  • sum(): O((hw)^2 + 2hwl), where l is length of strs.

Comments

Post a Comment

Popular posts from this blog

[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee

[LeetCode] 269. Alien Dictionary