Skip to content

Commit 6777299

Browse files
committed
add 1725
1 parent 6db0f30 commit 6777299

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

javascript/LeetCode/Array/1725.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1725. Number Of Rectangles That Can Form The Largest Square
3+
*
4+
* @param {number[][]} rectangles
5+
* @return {number}
6+
*/
7+
var countGoodRectangles = function(rectangles) {
8+
/**
9+
* 2維陣列,每個陣列元素分別代表該陣列三角形的長l、寬w
10+
* each rectangle are of lengths [5,3,5,5] is min of [l,w]
11+
* 回傳有幾個maxLen可組成三角形
12+
*/
13+
// solution 1
14+
// let rectangleLen = [];
15+
// let countMaxLen = 0;
16+
// for(const eachLen of rectangles){
17+
// rectangleLen.push(parseInt(Math.min(...eachLen)));
18+
// }
19+
// let maxLen = Math.max(...rectangleLen);
20+
// for(let i = 0;i < rectangleLen.length;++i) {
21+
// if(rectangleLen[i] === maxLen){
22+
// countMaxLen++;
23+
// }
24+
// }
25+
// return countMaxLen;
26+
27+
// solution 2.
28+
// time:O(N)
29+
let count = 0, maxLen = 0;
30+
for(const eachLen of rectangles) {
31+
let side = Math.min(...eachLen);
32+
33+
if(side > maxLen){
34+
count = 1;
35+
maxLen = side;
36+
}else if(side === maxLen){
37+
count++;
38+
}
39+
}
40+
return count;
41+
};
42+
let rectangles = [[5,8],[3,9],[5,12],[16,5]]
43+
// Output: 3
44+
// Explanation: The largest squares you can get from each rectangle are of lengths [5,3,5,5].
45+
// The largest possible square is of length 5, and you can get it out of 3 rectangles.
46+
console.log(countGoodRectangles(rectangles));

javascript/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1508,4 +1508,6 @@ Query 0: The element at queries[0] = 0 is nums[0] = 1. The nearest index with th
15081508
Query 1: The element at queries[1] = 3 is nums[3] = 4. No other index contains 4, so the result is -1.
15091509
Query 2: The element at queries[2] = 5 is nums[5] = 3. The nearest index with the same value is 1, and the distance between them is 3 (following the circular path: 5 -> 6 -> 0 -> 1).
15101510
*/
1511-
console.log(solveQueries(nums,queries));
1511+
// console.log(solveQueries(nums,queries));
1512+
1513+

0 commit comments

Comments
 (0)