Skip to content

Commit c2b2f3c

Browse files
committed
Add solution #1035
1 parent 1fc69b1 commit c2b2f3c

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,106 LeetCode solutions in JavaScript
1+
# 1,107 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -842,6 +842,7 @@
842842
1032|[Stream of Characters](./solutions/1032-stream-of-characters.js)|Hard|
843843
1033|[Moving Stones Until Consecutive](./solutions/1033-moving-stones-until-consecutive.js)|Medium|
844844
1034|[Coloring A Border](./solutions/1034-coloring-a-border.js)|Medium|
845+
1035|[Uncrossed Lines](./solutions/1035-uncrossed-lines.js)|Medium|
845846
1037|[Valid Boomerang](./solutions/1037-valid-boomerang.js)|Easy|
846847
1038|[Binary Search Tree to Greater Sum Tree](./solutions/1038-binary-search-tree-to-greater-sum-tree.js)|Medium|
847848
1041|[Robot Bounded In Circle](./solutions/1041-robot-bounded-in-circle.js)|Medium|

solutions/1035-uncrossed-lines.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 1035. Uncrossed Lines
3+
* https://leetcode.com/problems/uncrossed-lines/
4+
* Difficulty: Medium
5+
*
6+
* You are given two integer arrays nums1 and nums2. We write the integers of nums1 and nums2
7+
* (in the order they are given) on two separate horizontal lines.
8+
*
9+
* We may draw connecting lines: a straight line connecting two numbers nums1[i] and nums2[j]
10+
* such that:
11+
* - nums1[i] == nums2[j], and
12+
* - the line we draw does not intersect any other connecting (non-horizontal) line.
13+
*
14+
* Note that a connecting line cannot intersect even at the endpoints (i.e., each number can only
15+
* belong to one connecting line).
16+
*
17+
* Return the maximum number of connecting lines we can draw in this way.
18+
*/
19+
20+
/**
21+
* @param {number[]} nums1
22+
* @param {number[]} nums2
23+
* @return {number}
24+
*/
25+
var maxUncrossedLines = function(nums1, nums2) {
26+
const m = nums1.length;
27+
const n = nums2.length;
28+
29+
const dp = new Array(m + 1).fill().map(() => new Array(n + 1).fill(0));
30+
31+
for (let i = 1; i <= m; i++) {
32+
for (let j = 1; j <= n; j++) {
33+
if (nums1[i - 1] === nums2[j - 1]) {
34+
dp[i][j] = dp[i - 1][j - 1] + 1;
35+
} else {
36+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
37+
}
38+
}
39+
}
40+
41+
return dp[m][n];
42+
};

0 commit comments

Comments
 (0)