英文:
What is the way forward without using just array loops to solve the colored rectangle problem in Javascript?
问题
我有一个编程作业要完成,但卡在了这个问题上。我在互联网上搜索了潜在的解决方案,但要么是收费的,要么解决得很差。
我正在寻找JavaScript的解决方案。
问题:
给定N个彩色矩形,它们位于笛卡尔坐标系的中心,其边与坐标轴平行。每个矩形都以其宽度(沿x轴)和高度(沿y轴)唯一标识。
任务
确定纸张上彩色部分的面积。换句话说,确定至少属于一个矩形的单位方块的数量。示例
假设
N=3 矩形1: [8,2] 即宽度=8,高度=2 矩形2: [4,4] 即宽度=4,高度=4 矩形3: [2,6] 即宽度=2,高度=6
答案是28
函数描述
在编辑器中完成提供的函数solve。该函数接受以下2个参数,并返回彩色纸张的面积。
N表示矩形的数量
arr表示包含N个矩形尺寸的Nx2矩阵输入格式
注意:这是您必须使用的自定义输入格式。
第一行包含T,表示测试用例的数量。T还指定了您必须在不同输入集上运行solve函数的次数。
对于每个测试用例
第一行包含整数N
接下来的每个N行包含两个整数X和Y,分别表示相应矩形的尺寸(宽度和高度)。
我尝试使用数组方法,但那只是解决方案的一小部分?对此完全卡住了,任何帮助都将不胜感激。
英文:
I have a coding assignment to do and I am stuck on this one question. I scoured the internet for potential solutions but they're either paywalled or poorly solved.
I'm looking for a solution in JavaScript.
The question:
>You are given N colorful rectangles, which are centered in the center of the Cartesian coordinate system and their sides are parallel to the coordinate axes. Each rectangle is uniquely identified with its width (along the x-axis) and height (along the y-axis)
>
>Task
>Determine the area of the colored part of the paper. In other words, determine the number of unit squares that belong to at least one rectangle
>
>Example
>
>Assumptions
>
>N=3
>Rectangle 1: [8,2] i.e width=8, height=2
>Rectangle 2: [4,4] i.e width=4, height=4
>Rectangle 3: [2,6] i.e width=2, height=6
>
>
>The answer is 28
>
>Function Description
>
>Complete the function solve provided in the editor. This function takes the following 2 parameters and returns the area of the colored part of the paper
>
>N represents the number of rectangles
>arr represents Nx2 matrix containing the dimensions of N rectangles
>
>Input Format
>
>Note: This is the input format that you must use to provide custom input
>
>The first line contains T denoting the number of test cases. T also specifies the number of times you have to run the solve function on a different set of inputs.
>
>For each test case
>
>The first line contains the integer N
>
>Each of the following N lines contains two integers X and Y, dimensions (width and height, respectively) of the corresponding rectangles.
I tried using array methods but that's just a small part of the solution? Completely stuck with this and any help is appreciated
答案1
得分: 1
对于任务,我假设所有的宽度和高度都从坐标系的原点开始。
对于只有整数宽度和高度的离散情况(似乎是您的情况),可以通过维护一个所有着色点的集合来相对容易地解决问题。
对于输入中的每个矩形,您可以尝试生成该矩形覆盖的不同点的集合。如果将所有这些点添加到集合中,所有重叠的点将只被添加一次。
然后,该集合将包含至少被输入矩形之一着色的所有1x1网格方块,因此对它们进行计数最终将为您提供面积。
您可能会发现这篇关于集合的文档有用。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
英文:
Looking at the task, I assume that all of the widths and heights start off at the origin of the coordinate system.
For the discrete case where input rectangles have only integer widths and heights (which appears to be your case), the problem could quite easily be solved by maintaining a set of all coloured points.
For every rectangle in your input, you can try and generate a collection of distinct points that that rectangle covers. If you add all of these points to a set, all of the overlapping points will only be added once.
The set would then contain all of the 1x1 grid squares which have been coloured by at least one of the input rectangles, so counting them up would ultimately give you the area.
You might find this piece of documentation about sets to be useful. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
答案2
得分: 1
以下是代码的翻译部分:
function solve(rects) {
// 按照宽度降序、高度降序排序
rects.sort((a, b) => b[0] - a[0] || b[1] - a[1]);
// 向总面积添加水平切片
let y = 0;
let area = 0;
for (const [width, height] of rects) {
if (height <= y) continue; // 此矩形适合在之前的矩形内:无影响
area += (height - y) * width; // 添加垂直突出部分的面积
y = height;
}
return area;
}
// 两个测试
console.log(solve([[8, 2], [4, 4], [2, 6]])); // 28
console.log(solve([[2, 10], [4, 4], [2, 2], [8, 8], [6, 6]])); // 68
不包括代码的部分不需要翻译。
英文:
You could sort the input giving priority to wider rectangles, and then visit each rectangle in that order to add the slice that exceeds the previous rectangle's height.
That's it:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function solve(rects) {
// Sort by descending width, descending height
rects.sort((a, b) => b[0] - a[0] || b[1] - a[1]);
// Add horizontal slices to the total area
let y = 0;
let area = 0;
for (const [width, height] of rects) {
if (height <= y) continue; // This rectangle fits in a previous one: no effect
area += (height - y) * width; // Add the part that sticks out vertically
y = height;
}
return area;
}
// Two tests
console.log(solve([[8, 2], [4, 4], [2, 6]])); // 28
console.log(solve([[2, 10], [4, 4], [2, 2], [8, 8], [6, 6]])); // 68
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论