Demo entry 6662571

so

   

Submitted by anonymous on Nov 27, 2017 at 03:33
Language: Java. Code size: 2.5 kB.

/*
class Edge {
	public int u;
	public int v;
}
*/

class Solution1 {
	List<Integer> sort(List<Edge> edges, int n) {
		List<Integer> res = new ArrayList<>();
		if (edges == null) {
			return res;
		}
		int[] indegree = new int[n];
		for (int i = 0; i < edges.size(); i++) {
			indegree[edges.get(i).get(1)]++;
		}

		Queue<Integer> queue = new LinkedList<>();
		Stack<Integer> stack = new Stack<>();
		for (int i = 0; i < indegree.length; i++) {
			if (indegree[i] == 0) {
				queue.offer(i);
			}
		}

		while (!queue.isEmpty()) {
			int pre = queue.poll();
			stack.push(pre);
			for (int i = 0; i < edges.length; i++) {
				if (edges.get(i).get(0) == pre) {
					indegree[edges.get(i).get(1)]--;
					if (indegree[edges.get(i).get(1)] == 0) {
						queue.offer(edges.get(i).get(1))
					}
				}
			}
		}

		while (!stack.isEmpty) {
			res.add(stack.pop());
		}

		return res;
	}
}


class Solution2 {
    public int findNumber(int m) {
        int[] damage = new int[18];
        for (int i = 0; i < 18; i++) {
        	damage[i] = i * i;
        }
        return helper(m, Math.sqrt(m), damage);
    }

    private int helper(int m, int n, int[] damage) {
    	if (m == 0) return 1;
    	if (m < 0 || n <= 0) return 0;
    	return helper(m, n - 1, damage) + helper(m - damage[n], n, damage);
    }
}

/*
class Rect {
	public int top;
	public int bottom;
	public int left;
	public int right;
}
*/


class Solution3 {
	List<Rect> findRects(boolean[][] image) {
		
	}
}

class Solution4 {
	private int maxSubArray(int[] a) {
		int max = Integer.MIN_VALUE, sum = 0;
		for (int n : a) {
			if (sum < 0) {
				sum = num;
			} else {
				sum += num;
			}
			if (sum > max) {
				max = sum;
			}
		}
		return max;
	}

	public int maxSubMatrix(int[][] matrix) {
		int[][] total = matrix;
		for (int i = 1; i < matrix[0].length; i++) {
			for (int j = 0; j < matrix.length; j++) {
				total[i][j] += total[i-1][j];
			}
		}
	
		int maximum = Integer.MIN_VALUE;
		for (int i = 0; i < matrix.length; i++) {
	            for (int j = i; j < matrix.length; j++) {
	            int[] result = new int[matrix[0].length];
				for (int f = 0; f < matrix[0].length; f++) {
					if (i == 0) {
						result[f] = total[j][f];
					} else {
						result[f] = total[j][f] - total[i - 1][f];
					}
				}

				int maximal = maxSubArray(result);
				
				if (maximal > maximum) {
					maximum = maximal;
				}
			}
		}	
		return maximum;
	}

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).