Skip to content

Commit c070dfb

Browse files
[LEET-0000] add problem descriptions
1 parent 3a4a6ba commit c070dfb

File tree

6 files changed

+133
-1
lines changed

6 files changed

+133
-1
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)|[Solution] | O(n)|O(n) | Medium| HashMap
6565
|314|[Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeVerticalOrderTraversal.java)| O(n)|O(n) | Medium| HashMap, BFS
6666
|311|[Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SparseMatrixMultiplication.java)| O(m*n*l)|O(m*l)| Medium|
67+
|308|[Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RangeSumQuery2DMutable.java)| ? | ? | Hard| Tree
6768
|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RemoveInvalidParentheses.java)| ? | ? | Hard| BFS
6869
|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BullsandCows.java)| O(n)|O(1) | Easy|
6970
|298|[Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeLongestConsecutiveSequence.java)| O(n)|O(n) | Medium | Tree

leetcode-algorithms/src/main/java/com/stevesun/solutions/PalindromePartitioning.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
public class PalindromePartitioning {
6+
/**Given a string s, partition s such that every substring of the partition is a palindrome.
7+
8+
Return all possible palindrome partitioning of s.
79
10+
For example, given s = "aab",
11+
Return
12+
13+
[
14+
["aa","b"],
15+
["a","a","b"]
16+
]*/
17+
public class PalindromePartitioning {
818

919
public List<List<String>> partition(String s) {
1020
List<List<String>> result = new ArrayList();

leetcode-algorithms/src/main/java/com/stevesun/solutions/PalindromePermutation.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6+
/**Given a string, determine if a permutation of the string could form a palindrome.
7+
8+
For example,
9+
"code" -> False, "aab" -> True, "carerac" -> True.
10+
11+
Hint:
12+
13+
Consider the palindromes of odd vs even length. What difference do you notice?
14+
Count the frequency of each character.
15+
If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?*/
616
public class PalindromePermutation {
717

818
public boolean canPermutePalindrome(String s) {

leetcode-algorithms/src/main/java/com/stevesun/solutions/Permutations.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
import java.util.ArrayList;
33
import java.util.List;
44

5+
/**Given a collection of distinct numbers, return all possible permutations.
6+
7+
For example,
8+
[1,2,3] have the following permutations:
9+
[
10+
[1,2,3],
11+
[1,3,2],
12+
[2,1,3],
13+
[2,3,1],
14+
[3,1,2],
15+
[3,2,1]
16+
]*/
517
public class Permutations {
618
static class Accepted_solution {
719
//this solution has a recursive function that has a return type
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
5+
6+
Range Sum Query 2D
7+
The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.
8+
9+
Example:
10+
Given matrix = [
11+
[3, 0, 1, 4, 2],
12+
[5, 6, 3, 2, 1],
13+
[1, 2, 0, 1, 5],
14+
[4, 1, 0, 1, 7],
15+
[1, 0, 3, 0, 5]
16+
]
17+
18+
sumRegion(2, 1, 4, 3) -> 8
19+
update(3, 2, 2)
20+
sumRegion(2, 1, 4, 3) -> 10
21+
Note:
22+
The matrix is only modifiable by the update function.
23+
You may assume the number of calls to update and sumRegion function is distributed evenly.
24+
You may assume that row1 ≤ row2 and col1 ≤ col2.
25+
*/
26+
public class RangeSumQuery2DMutable {
27+
class Solution {
28+
public class NumMatrix {
29+
int[][] nums;
30+
int[][] tree;
31+
int height;
32+
int width;
33+
34+
public NumMatrix(int[][] matrix) {
35+
if(matrix.length == 0 || matrix[0].length == 0) return;
36+
height = matrix.length;
37+
width = matrix[0].length;
38+
this.nums = new int[height][width];
39+
this.tree = new int[height+1][width+1];
40+
for(int i = 0; i < height; i++){
41+
for(int j = 0; j < width; j++){
42+
update(i, j, matrix[i][j]);
43+
}
44+
}
45+
}
46+
47+
public void update(int rowIndex, int colIndex, int newVal) {
48+
if(height == 0 || width == 0) return;
49+
int delta = newVal - nums[rowIndex][colIndex];
50+
nums[rowIndex][colIndex] = newVal;
51+
for(int i = rowIndex+1; i <= height; i += i&(-i)){
52+
for(int j = colIndex+1; j <= width; j += j&(-j)){
53+
tree[i][j] += delta;//just use its previous value plus delta is good
54+
}
55+
}
56+
}
57+
58+
public int sumRegion(int row1, int col1, int row2, int col2) {
59+
if(height == 0 || width == 0) return 0;
60+
return sum(row2+1, col2+1) + sum(row1, col1) - sum(row1, col2+1) - sum(row2+1, col1);
61+
}
62+
63+
private int sum(int row, int col) {
64+
int sum = 0;
65+
for(int i = row; i > 0; i -= i&(-i)){
66+
for(int j = col; j > 0; j -= j&(-j)){
67+
sum += tree[i][j];
68+
}
69+
}
70+
return sum;
71+
}
72+
73+
}
74+
75+
/**
76+
* Your NumMatrix object will be instantiated and called as such:
77+
* NumMatrix obj = new NumMatrix(matrix);
78+
* obj.update(row,col,val);
79+
* int param_2 = obj.sumRegion(row1,col1,row2,col2);
80+
*/
81+
}
82+
}

leetcode-algorithms/src/main/java/com/stevesun/solutions/TheSkylineProblem.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44
import java.util.Arrays;
55
import java.util.List;
66
import java.util.TreeMap;
7+
/**A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
8+
*
9+
* The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
10+
11+
For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .
12+
13+
The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.
14+
15+
For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].
16+
17+
Notes:
18+
19+
The number of buildings in any input list is guaranteed to be in the range [0, 10000].
20+
The input list is already sorted in ascending order by the left x position Li.
21+
The output list must be sorted by the x position.
22+
There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]*/
23+
724
/**This video is super clear and helpful: https://www.youtube.com/watch?v=GSBLe8cKu0s
825
926
Algorithm:

0 commit comments

Comments
 (0)