在JavaScript中按输入值对数组进行排序

This is a continuation of my new series of posts on algorithms. You can check out my previous one about finding the first non-repeating character in a string here.

这是我关于算法的新系列文章的延续。 您可以在此处查看我的上一个有关在字符串中找到第一个非重复字符的信息

While preparing for a technical interview this past week, I wanted to focus on sorting arrays. I came across this prompt and hated it at the start, but I came to appreciate how much it made me think. On top of that, I’m pretty happy with how I solved it!

在准备上周进行技术面试时,我想专注于对数组进行排序。 一开始我遇到了这个提示并讨厌它,但是我开始欣赏它让我思考了多少。 最重要的是,我对如何解决它感到非常满意!

提示 (The Prompt)

You have some tasks in your account. For each ith of them you know its deadlinesi, which is the last day by which it should be completed. As you can see in your calendar, today's date is day. The program labels each task in accordance with its due date:

您的帐户中有一些任务。 对于每一个ith他们的,你知道它的deadlinesi ,这是最后一天,它应该完成。 正如您在日历中看到的那样,今天的日期是day 。 该程序根据其到期日期为每个任务添加标签:

  • If the task is due today or it’s already overdue, it is labeled as Today;

    如果任务是今天到期或已经过期,则标记为“ 今天”

  • If the task is due within a week but not today — that is, its deadline is somewhere between day + 1 and day + 7 both inclusive - it is labeled as Upcoming;

    如果任务应在一周之内完成,而不是今天进行,也就是说,任务的截止日期在day + 1 day + 7day + 7之间(包括首尾两天),则标记为“ 即将到来”

  • All other tasks are labeled as Later;

    所有其他任务都标记为“ 稍后”

Given an array of deadlines and today's date day, your goal is to find the number of tasks with each label type and return it as an array with the format [Today, Upcoming, Later], where Today, Upcoming and Later are the number of tasks that correspond to that label.

鉴于数组deadlines和今天的日期day ,你的目标是找到任务的数量与每个标签类型并返回它作为带格式的数组[ Today , Upcoming , Later ] ,在今天即将更高版本的数量与该标签相对应的任务。

Example

  • For deadlines = [1, 2, 3, 4, 5] and day = 2, the output should be

    对于deadlines = [1, 2, 3, 4, 5]day = 2 ,输出应为

    For deadlines = [1, 2, 3, 4, 5] and day = 2, the output should betasksTypes(deadlines, day) = [2, 3, 0].

    对于deadlines = [1, 2, 3, 4, 5]day = 2 ,输出应为tasksTypes(deadlines, day) = [2, 3, 0]

  • Today is day 2, so tasks with deadlines at 1 and 2 are labeled as Today. The other three tasks should be completed within a week, so they should be labeled as Upcoming. There are no tasks labeled as Later.

    今天是一天2 ,所以在最后期限任务12被标记为今天 。 其他三项任务应在一周内完成,因此应将其标记为即将来临 。 没有标记为Later的任务。

  • For deadlines = [1, 2, 4, 2, 10, 3, 1, 4, 5, 4, 9, 8] and day = 1, the output should be

    对于deadlines = [1, 2, 4, 2, 10, 3, 1, 4, 5, 4, 9, 8]day = 1 ,输出应为

    For deadlines = [1, 2, 4, 2, 10, 3, 1, 4, 5, 4, 9, 8] and day = 1, the output should betasksTypes(deadlines, day) = [2, 8, 2].

    对于deadlines = [1, 2, 4, 2, 10, 3, 1, 4, 5, 4, 9, 8] tasksTypes(deadlines, day) = [2, 8, 2] deadlines = [1, 2, 4, 2, 10, 3, 1, 4, 5, 4, 9, 8]day = 1 ,输出应为tasksTypes(deadlines, day) = [2, 8, 2]

  • Today is day 1, which means that the two tasks with a deadline of 1 are labeled as Today. Tasks with deadlines 10 and 9 are labeled as Later, since their deadlines are more than 7 days ahead. The other eight tasks are labeled as Upcoming.

    今天是一天1 ,这意味着,随着最后期限的两个任务1被标记为今天 。 截止日期为109任务被标记为“ 较晚” ,因为它们的截止日期提前了7天以上。 其他八个任务标记为“ 即将到来”

Image for post
CodeSignal 谨向CodeSignal借用

我的解决方案 (My Solution)

What a prompt, right? Luckily it’s much less complicated than the prompt makes it seem (in my opinion).

什么提示,对不对? 幸运的是,它并没有提示所显示的复杂(我认为)。

function tasksTypes(deadlines, day) {
let today = 0;
let upcoming = 0;
let later = 0;
const deadline = []; for (let i = 0; i < deadlines.length; i++) {
let dline = deadlines[i]; if (dline <= day) {
today += 1
} else if (dline >= day+1 && dline <= day+7) {
upcoming += 1
} else {
later += 1
}
} deadline.push(today);
deadline.push(upcoming);
deadline.push(later);
return deadline;
}
  • We know that the tasks are going to be sorted into 3 categories: today, upcoming, and later. As a result, the first thing I did was set variables for each of these initialized to 0, because they begin with 0 tasks assigned to them.

    我们知道任务将分为三类: todayupcominglater 。 结果,我要做的第一件事就是将每个初始化为0的变量都设置为变量,因为它们从分配给它们的0个任务开始。

  • I also set the variable deadline to an empty array. This is where we’re going to push the values of today, upcoming, and later after we’ve sorted the input array.

    我还将可变deadline设置为一个空数组。 在这里,我们将在对输入数组进行排序之后推入todayupcoming以及later的值。

  • To iterate through the data, I chose to use a for loop because I know how many iterations we need to make (the length of the input array deadlines).

    为了遍历数据,我选择使用for循环,因为我知道我们需要进行多少次迭代(输入数组deadlines的长度)。

  • To isolate the individual deadline, I set the variable dline equal to the current index place.

    为了隔离各个截止日期,我将变量dline设置为等于当前索引位置。

  • Next, I used a series of if statements to sort the deadlines array. The first statement dictates that today should be added to if the current index value in the deadlines array is less than or equal to the value of day. The next statement dictates that upcoming should be added to if the current index value in the deadlines array is greater than or equal the value of day + 1 and less than or equal to the value of day + 7. The last statement covers the remaining parameters and adds 1 to later.

    接下来,我使用一系列的if语句对deadlines数组进行排序。 第一条语句指示,如果截止日期数组中的当前索引值小于或等于day的值,则应在today加上。 下一条语句指示,如果截止日期数组中的当前索引值大于或等于day + 1的值且小于或等于day + 7的值,则应该添加upcoming出现的值。 最后一条语句覆盖其余参数,并在later 1。

  • Following the if statement, I pushed the values of today, upcoming, and later (in that order due to the constraints of the prompt and, therefore, tests) into the deadline array.

    if语句之后,我将todayupcominglater的值(由于提示和测试的限制,按此顺序)推入了截止日期数组。

  • Finally, I returned the deadline array that’s been populated with the values associated with today, upcoming, and later.

    最后,我返回了截止日期数组,该数组已填充了与todayupcominglater关联的值。

This solution passed all 20 tests within the allotted 4 second run time. If you have another solution, please let me know! Happy coding!

该解决方案在分配的4秒运行时间内通过了所有20个测试。 如果您还有其他解决方案,请告诉我! 祝您编码愉快!

翻译自: https://medium.com/@weberzt/sorting-an-array-by-input-value-in-javascript-60eb96c6474b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值