每个词的出现次数 [英] Number of occurences of each term

查看:77
本文介绍了每个词的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个数组 a [n] [2] ,其中 n 可以为<$ c最多$ c> 10 ^ 5 有n个科目和n个学生。全部编号为1,2,...,n。

I'm given an array a[n][2], where n can be 10^5 at max. There are n subjects and n students. All numbered 1, 2, ..., n.

a [i] [0] a [i] [1] 1< = i< = n )表示在第i个主题中,从 a [i] [0] a [i] [1] 的所有学生都通过了,其余的则没有没错我应该找到每个学生通过的科目数量。

a[i][0] and a[i][1] (1 <= i <= n) denote that in i-th subject, all students from a[i][0] to a[i][1] passed, while the rest didn't. I'm supposed to find the number of subjects each student passed in.

例如,

n=5 and a = [ [1,3], [1,3], [1,5], [1,5], [1,5] ] 

应该给出输出

[5, 5, 5, 3, 3]

(2)

n = 10, a = [ [1,10], [1,10], [1,10], [3,5], [1,10], ..., [1,10] ]

预期答案应为

[ 9, 9, 10, 10, 10, 9, 9, 9, 9, 9]


推荐答案

不是很了解您的代码,这是另一种方法。
这类似于间隔问题。让我们举个例子:

Didn't quite understand your code, here is an alternative approach. This is similar to an interval problem. Let's take your example:


  • n = 5

  • a = [[1,3], [1,3],[1,5],[1,5],[1,5]]

我们首先大小为no的数组。的主题数+ 1(为了简化计算)。

We first make an array of size as no. of subjects + 1 (for the sake of simplicity in computation).

1   2   3   4   5   6




  • 现在,让我们一个一个地讲一下。每当遇到间隔时,我们将 +1 添加到起点,并将 -1 添加到终点+ 1 th 位置。

    • Now, let's go through intervals one by one. Whenever we come across an interval, we add +1 to the starting point and -1 to the ending point + 1th location.
    • 数组表示形式:(经过上述整个过程) 。

      Array representation:(after the entire above process).

       1          2          3          4         5         6
      
      +1                               -1                           [1,3]
      +1                               -1                           [1,3]
      +1                                                   -1       [1,5]
      +1                                                   -1       [1,5]
      +1                                                   -1       [1,5]
      




      • 所有待处理就是从左到右开始求和得到每个学生的答案。上面的过程之所以有效,是因为我们在每个时间间隔的最后一个学生之后否定了(如-1一样),因为其他学生未通过该特定主题。因此,在求和时,我们一定会为每个学生得出正确的总数。

      • 总结看起来像:

         1          2          3          4         5         6
        
        +1                               -1                           [1,3]
        +1                               -1                           [1,3]
        +1                                                   -1       [1,5]
        +1                                                   -1       [1,5]
        +1                                                   -1       [1,5]
        
         5          5          5          3         3         0(not considered anyway) 
        

        这篇关于每个词的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆