File tree 2 files changed +41
-1
lines changed
2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,255 LeetCode solutions in JavaScript
1
+ # 1,256 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1036
1036
1359|[ Count All Valid Pickup and Delivery Options] ( ./solutions/1359-count-all-valid-pickup-and-delivery-options.js ) |Hard|
1037
1037
1360|[ Number of Days Between Two Dates] ( ./solutions/1360-number-of-days-between-two-dates.js ) |Easy|
1038
1038
1361|[ Validate Binary Tree Nodes] ( ./solutions/1361-validate-binary-tree-nodes.js ) |Medium|
1039
+ 1362|[ Closest Divisors] ( ./solutions/1362-closest-divisors.js ) |Medium|
1039
1040
1365|[ How Many Numbers Are Smaller Than the Current Number] ( ./solutions/1365-how-many-numbers-are-smaller-than-the-current-number.js ) |Easy|
1040
1041
1366|[ Rank Teams by Votes] ( ./solutions/1366-rank-teams-by-votes.js ) |Medium|
1041
1042
1368|[ Minimum Cost to Make at Least One Valid Path in a Grid] ( ./solutions/1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1362. Closest Divisors
3
+ * https://leetcode.com/problems/closest-divisors/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an integer num, find the closest two integers in absolute difference whose product
7
+ * equals num + 1 or num + 2.
8
+ *
9
+ * Return the two integers in any order.
10
+ */
11
+
12
+ /**
13
+ * @param {number } num
14
+ * @return {number[] }
15
+ */
16
+ function closestDivisors ( num ) {
17
+ const pair1 = findClosestPair ( num + 1 ) ;
18
+ const pair2 = findClosestPair ( num + 2 ) ;
19
+
20
+ return Math . abs ( pair1 [ 1 ] - pair1 [ 0 ] ) <= Math . abs ( pair2 [ 1 ] - pair2 [ 0 ] ) ? pair1 : pair2 ;
21
+
22
+ function findClosestPair ( target ) {
23
+ let minDifference = Infinity ;
24
+ let pair = [ ] ;
25
+
26
+ for ( let divisor = 1 ; divisor <= Math . sqrt ( target ) ; divisor ++ ) {
27
+ if ( target % divisor === 0 ) {
28
+ const complement = target / divisor ;
29
+ const difference = Math . abs ( complement - divisor ) ;
30
+ if ( difference < minDifference ) {
31
+ minDifference = difference ;
32
+ pair = [ divisor , complement ] ;
33
+ }
34
+ }
35
+ }
36
+
37
+ return pair ;
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments