File tree 3 files changed +219
-0
lines changed
3 files changed +219
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 221. Maximal Square
2
+
3
+ - Difficulty: Medium.
4
+ - Related Topics: Dynamic Programming.
5
+ - Similar Questions: Maximal Rectangle, Largest Plus Sign.
6
+
7
+ ## Problem
8
+
9
+ Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
10
+
11
+ ** Example:**
12
+
13
+ ```
14
+ Input:
15
+
16
+ 1 0 1 0 0
17
+ 1 0 1 1 1
18
+ 1 1 1 1 1
19
+ 1 0 0 1 0
20
+
21
+ Output: 4
22
+ ```
23
+
24
+ ## Solution
25
+
26
+ ``` javascript
27
+ /**
28
+ * @param {character[][]} matrix
29
+ * @return {number}
30
+ */
31
+ var maximalSquare = function (matrix ) {
32
+ var m = matrix .length ;
33
+ var n = (matrix[0 ] || []).length ;
34
+ var dp = Array (m).fill (0 ).map (_ => Array (n));
35
+ var max = 0 ;
36
+
37
+ for (var k = 0 ; k < m; k++ ) {
38
+ dp[k][0 ] = matrix[k][0 ] === ' 1' ? 1 : 0 ;
39
+ max = Math .max (max, dp[k][0 ]);
40
+ }
41
+
42
+ for (var p = 0 ; p < n; p++ ) {
43
+ dp[0 ][p] = matrix[0 ][p] === ' 1' ? 1 : 0 ;
44
+ max = Math .max (max, dp[0 ][p]);
45
+ }
46
+
47
+ for (var i = 1 ; i < m; i++ ) {
48
+ for (var j = 1 ; j < n; j++ ) {
49
+ if (matrix[i][j] === ' 1' ) {
50
+ dp[i][j] = Math .min (dp[i - 1 ][j - 1 ], dp[i - 1 ][j], dp[i][j - 1 ]) + 1 ;
51
+ max = Math .max (max, dp[i][j]);
52
+ } else {
53
+ dp[i][j] = 0 ;
54
+ }
55
+ }
56
+ }
57
+
58
+ return max * max;
59
+ };
60
+ ```
61
+
62
+ ** Explain:**
63
+
64
+ nope.
65
+
66
+ ** Complexity:**
67
+
68
+ * Time complexity : O(m* n).
69
+ * Space complexity : O(m* n).
Original file line number Diff line number Diff line change
1
+ # 226. Invert Binary Tree
2
+
3
+ - Difficulty: Easy.
4
+ - Related Topics: Tree.
5
+ - Similar Questions: .
6
+
7
+ ## Problem
8
+
9
+ Invert a binary tree.
10
+
11
+ ** Example:**
12
+
13
+ Input:
14
+
15
+ ```
16
+ 4
17
+ / \
18
+ 2 7
19
+ / \ / \
20
+ 1 3 6 9
21
+ ```
22
+
23
+ Output:
24
+
25
+ ```
26
+ 4
27
+ / \
28
+ 7 2
29
+ / \ / \
30
+ 9 6 3 1
31
+ ```
32
+
33
+ ** Trivia:**
34
+ This problem was inspired by this original tweet by Max Howell:
35
+
36
+ Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.
37
+
38
+ ## Solution
39
+
40
+ ``` javascript
41
+ /**
42
+ * Definition for a binary tree node.
43
+ * function TreeNode(val) {
44
+ * this.val = val;
45
+ * this.left = this.right = null;
46
+ * }
47
+ */
48
+ /**
49
+ * @param {TreeNode} root
50
+ * @return {TreeNode}
51
+ */
52
+ var invertTree = function (root ) {
53
+ if (root) {
54
+ swap (root);
55
+ invertTree (root .left );
56
+ invertTree (root .right );
57
+ }
58
+ return root;
59
+ };
60
+
61
+ var swap = function (node ) {
62
+ var left = node .left ;
63
+ node .left = node .right ;
64
+ node .right = left;
65
+ };
66
+ ```
67
+
68
+ ** Explain:**
69
+
70
+ nope.
71
+
72
+ ** Complexity:**
73
+
74
+ * Time complexity : O(n).
75
+ * Space complexity : O(n).
Original file line number Diff line number Diff line change
1
+ # 234. Palindrome Linked List
2
+
3
+ - Difficulty: Easy.
4
+ - Related Topics: Linked List, Two Pointers.
5
+ - Similar Questions: Palindrome Number, Valid Palindrome, Reverse Linked List.
6
+
7
+ ## Problem
8
+
9
+ Given a singly linked list, determine if it is a palindrome.
10
+
11
+ ** Example 1:**
12
+
13
+ ```
14
+ Input: 1->2
15
+ Output: false
16
+ ```
17
+
18
+ ** Example 2:**
19
+
20
+ ```
21
+ Input: 1->2->2->1
22
+ Output: true
23
+ ```
24
+
25
+ ** Follow up:**
26
+ Could you do it in O(n) time and O(1) space?
27
+
28
+ ## Solution
29
+
30
+ ``` javascript
31
+ /**
32
+ * Definition for singly-linked list.
33
+ * function ListNode(val) {
34
+ * this.val = val;
35
+ * this.next = null;
36
+ * }
37
+ */
38
+ /**
39
+ * @param {ListNode} head
40
+ * @return {boolean}
41
+ */
42
+ var isPalindrome = function (head ) {
43
+ var left = null ;
44
+ var right = null ;
45
+ var slow = head;
46
+ var fast = head;
47
+ var tmp = null ;
48
+
49
+ while (fast && fast .next ) {
50
+ fast = fast .next .next ;
51
+ tmp = slow .next ;
52
+ slow .next = left;
53
+ left = slow;
54
+ slow = tmp;
55
+ }
56
+ right = fast ? slow .next : slow;
57
+
58
+ while (left && right) {
59
+ if (left .val !== right .val ) return false ;
60
+ left = left .next ;
61
+ right = right .next ;
62
+ }
63
+
64
+ return true ;
65
+ };
66
+ ```
67
+
68
+ ** Explain:**
69
+
70
+ nope.
71
+
72
+ ** Complexity:**
73
+
74
+ * Time complexity : O(n).
75
+ * Space complexity : O(1).
You can’t perform that action at this time.
0 commit comments