计数器数组_子数组计数

计数器数组

Problem statement:

问题陈述:

Given an array of N positive integers a1, a2,  ..., an. The value of each contiguous subarray of a given array is the maximum element present in that subarray. The task is to return the number of subarrays having value strictly greater than K.

给定N个正整数数组a 1 ,a 2 ,...,a n 。 给定数组的每个连续子数组的值是该子数组中存在的最大元素。 任务是返回值严格大于K的子数组数。

Input:

输入:

The first line contains two space-separated positive integers N and K denoting the size of the array and the value of K. The second line contains N space-separated positive integers denoting the elements of the array.

第一行包含两个空间隔开的正整数N,K表示的阵列的尺寸和K值。 第二行包含N个以空格分隔的正整数,它们表示数组的元素。

Output:

输出:

Output the number of subarrays having value strictly greater than K.

输出值严格大于K的子数组数。

Constraints:

限制条件:

1 <= T <= 50
1 <= N <= 100
1 <= a[i] <= 105

Example:

例:

Test case: 1
Input: 
5 3
3 4 5 2 7
Output:
13
Test case: 2
4 1
1 2 3 4
Output:
9

Explanation:

说明:

Test case 1: 
All possible subarrays are listed below 
with their respective value (maximum in the subarray)
3 -> 3
4 -> 4
5 -> 5
2 -> 2
7 -> 7
3, 4 -> 4
4, 5 -> 5
5, 2 -> 5
2, 7 -> 7
3, 4, 5 -> 5
4, 5, 2 -> 5
5, 2, 7 -> 7
3, 4, 5, 2 -> 5
4, 5, 2, 7 -> 7
3, 4, 5, 2, 7 -> 7
So, number of valid subarrays is 13

Solution Approach:

解决方法:

  1. Create dp[n][n] to store value (maximum) of subarray;

    创建dp [n] [n]来存储子数组的值(最大值);

  2. Initialize count = 0 which will be our final result;

    初始化计数= 0 ,这将是我们的最终结果;

  3. Base case computation (single length subarrays),

    基本案例计算(单长度子数组),

    for i=0 to n
    // since only one element in single length subarray, 
    // just check for that element
    if(a[i]>k) 
    dp[i][i]=a[i];
    count++;
    else
    dp[i][i]=-1; // or arr[i] itslef
    end for
    
    
  4. Computing all length subarray cases,

    计算所有长度的子阵列案例,

    for subarray length,len=2 to n
    for start=0 to n-len
    end=start+len-1; // last element of subarray
    if(a[end]>k || dp[start][end-1]>k)
    dp[start][end]=std::max(a[end],dp[start][end-1]);
    count++;
    else
    dp[start][end]=-1;  
    end for
    end for
    
    

Okay, so the strategy is to compute for subarray a[start..end] with help of already computed one a[start..end-1].

好的,因此该策略是在已经计算的a [start..end-1]的帮助下为子数组a [start..end]计算。

Subarray a[start..end] will only make a count if a[start..end-1] already makes a count ( yes because, it's part of the subarray so, anything max in it would be max in the parent one too) Or if a[end]>k.

数组a [start..end]仅在a [start..end-1]已经计数的情况下才会计数(是的,因为它是子数组的一部分,所以其中的任何最大值在父数组中也是最大值)或者a [end]> k

In both the cases maximum of the subarray, a[start..end] is greater than K

在这两种情况下,子数组的最大值a [start..end]都大于K

That's what we have done.

那就是我们所做的。

Below is illustration for our test case 1,

下面是我们的测试用例1的说明,

Input array: [3, 4, 5, 2, 7] and K=3.

输入数组: [3,4,5,2,7]K = 3

So, let's compute the basic test case first

因此,让我们先计算基本测试用例

We need a 5X5 DP array for this

为此,我们需要一个5X5 DP阵列

Count of subarrays (1)

Starting to compute for other values.

开始计算其他值。

Len=2

Len = 2

Start=0, end=1

开始= 0,结束= 1

Count of subarrays (2)

Start=1, end=2

开始= 1,结束= 2

Count of subarrays (3)

Start=2, end=3

开始= 2,结束= 3

Count of subarrays (4)

Start=3, end=4

开始= 3,结束= 4

Count of subarrays (5)

Len=3

Len = 3

Start=0, end=2

开始= 0,结束= 2

Count of subarrays (6)

Start=1, end=3

开始= 1,结束= 3

Count of subarrays (7)

Start=2, end=4

开始= 2,结束= 4

Count of subarrays (8)

Len=4

Len = 4

Start=0, end=3

开始= 0,结束= 3

Count of subarrays (9)

Start=1, end=4

开始= 1,结束= 4

Count of subarrays (10)

Len=5

Len = 5

Start=0, end=3

开始= 0,结束= 3

Count of subarrays (11)

Done!

做完了!

Count is 13 (count of positive values in the array).

Count是13(数组中正值的数量)。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int maximum(int a, int b)
{
return (a > b) ? a : b;
}
int subArray(vector<int> a, int n, int k)
{
int dp[n][n];
int count = 0;
for (int i = 0; i < n; i++) {
if (a[i] > k) {
dp[i][i] = a[i];
count++;
}
else
dp[i][i] = -1; //or arr[i] itslef
}
for (int len = 2; len <= n; len++) {
for (int start = 0; start <= n - len; start++) {
int end = start + len - 1;
if (a[end] > k || dp[start][end - 1] > k) {
dp[start][end] = std::max(a[end], dp[start][end - 1]);
count++;
}
else
dp[start][end] = -1;
}
}
return count;
}
int main()
{
int n, item, k;
cout << "Input size of array\n";
cin >> n;
cout << "Input k\n";
cin >> k;
cout << "Add the array elements\n";
vector<int> a;
for (int j = 0; j < n; j++) {
scanf("%d", &item);
a.push_back(item);
}
cout << "Total count of valid subarray is " << subArray(a, n, k) << endl;
return 0;
}

Output:

输出:

Input size of array
5
Input k
3
Add the array elements
3 4 5 2 7
Total count of valid subarray is 13

翻译自: https://www.includehelp.com/icp/count-of-subarrays.aspx

计数器数组

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540596.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[转载] 列表、元组及通用序列操作

参考链接&#xff1a; Python | 重点数据类型 (字符串&#xff0c;列表&#xff0c;元组&#xff0c;迭代)(String, List, Tuple, Iteration) 序列是Python中最基本的数据结构&#xff08;一些基本特性类似于C中的数组模板类&#xff09;&#xff0c;序列中的每一个元素都有相…

onActivityResult()后onresume()

当你调用完一个存在的activity之后&#xff0c;onActivityResult将会返回以下数据&#xff1a;你调用时发出的requestCode、被调用activity的结果标志resultCode&#xff08;如RESULT_OK&#xff09;和其他的额外数据。我们期望的都是得到RESULT_OK&#xff0c;表示调用成功&am…

java反射用法示例_Java包| 类型,用法,示例

java反射用法示例配套 (Packages) Packages in Java is simply a mechanism to encapsulate (i.e. to put in a short and concise form) a group of classes,interfaces,enumerations, sub packages, etc. In real world, application is developed in such a manner so that …

[转载] python 元组tuple - python基础入门(14)

参考链接&#xff1a; Python元组Tuple 目录 一.元组tuple定义 二.元组tuple查询 三.元组tuple不支持删除/修改数据 四.元组tuple与列表list的相互转换 五.重点总结 在上一篇文章中我们讲解了关于python列表List的相关内容&#xff0c;今天给大家解释一下列表List的…

MaxCompute 2.0—从ODPS到MaxCompute

从ODPS到MaxCompute-阿里大数据的进化之路是一个商用大数据系统发展史&#xff0c;一个商业大数据系统要解决的问题有可靠性&#xff0c;高性能&#xff0c;安全性等等六个方面。内部产品名ODPS的MaxCompute&#xff0c;是阿里巴巴内部发展的一个高效能、低成本&#xff0c;完全…

python数值类型_Python数值类型

python数值类型In programming, Data Types are an essential concept. Data of various types can be stored in variables as per the task we want the variables to perform. 在编程中&#xff0c;数据类型是必不可少的概念。 根据我们希望变量执行的任务&#xff0c;各种类…

[转载] Python高级变量(列表、元组、字典、字符串、公共方法)

参考链接&#xff1a; Python | 重点数据类型 (字符串&#xff0c;列表&#xff0c;元组&#xff0c;迭代)(String, List, Tuple, Iteration) 文章目录 高级变量类型目标知识点回顾 01. 列表1.1 列表的定义1.2 列表常用操作del 关键字&#xff08;科普&#xff09;关键字、函数…

python 操作mongodb数据库参考文档

参考文档链接&#xff1a;https://pypi.python.org/pypi/pymongo pymongo的参考文档http://api.mongodb.com/python/current/tutorial.html mongoengine的参考文档&#xff1a;https://pypi.python.org/pypi/mongoengine#downloads Flask-MongoEngine的参考文档&#xff1a;htt…

php eot eod_EOD的完整形式是什么?

php eot eodEOD&#xff1a;一天结束 (EOD: End Of Day) EOD is an abbreviation of "End Of Day". EOD是“ End Of Day”的缩写 。 It is an expression, which is commonly used in the Gmail platform. In a particular mail, if the sender wants to give the d…

[转载] python元组 tuple

参考链接&#xff1a; Python元组Tuple 类型特点&#xff1a;可以存放多个、 可以重复的&#xff0c;有顺序的数据&#xff0c;数据不可变。 如果项目中需要定义多个数据到一个变量中存放 存放的数据&#xff0c;在项目运行过程中&#xff0c;会发生数据的增加、修改、删除…

aio nio aio_AIO的完整形式是什么?

aio nio aioAIO&#xff1a;多合一 (AIO: All-in-one) AIO is an abbreviation of "all-in-one", which is also known as an MFP (multi-function product/printer/peripheral), multi-functional or multi-function device (MFD). It is a workplace machine that …

[转载] python基础入门二

参考链接&#xff1a; Python集合Set 写代码,有如下变量,请按照要求实现每个功能 &#xff08;共6分&#xff0c;每小题各0.5分&#xff09; name ” aleX” 1)移除 name 变量对应的值两边的空格,并输出处理结果 2) 判断 name 变量对应的值是否以 “al” 开头,并输出结果
…

组合数据类型练习,英文词频统计实例上

1、字典实例&#xff1a;建立学生学号成绩字典&#xff0c;做增删改查遍历操作。 建立&#xff1a; d{0001:99,0003:89,0004:98,0005:100,0006:78} 增&#xff1a;d[0002]79 删&#xff1a;d.pop(0001) 改&#xff1a;d[0004]100 查&#xff1a;print(d[0002]) 遍历操作&#x…

茱莉亚分形_茱莉亚的NaN Constant

茱莉亚分形Julia| NaN / Nan64常数 (Julia | NaN/Nan64 Constant) Nan / Nan64 is a constant of the Float64 type in Julia programming language, it represents "not-a-number" value. Nan / Nan64是Julia编程语言中Float64类型的常量&#xff0c;它表示“非数字…

[转载] Python3 数组

参考链接&#xff1a; Python中的Array | 数组1(简介和功能) 一、list和array的区别 Python的数组通过Numpy包的array实现。 Python里二者最大的区别是&#xff0c;list可以存储不同类型的数据&#xff0c;而array只能存储相同类型的数据。 import numpy #直接定义 a […

201671010128 2017-09-24《Java程序设计》之继承

1.继承的概念及理解&#xff1a; 继承是java面向对象编程技术的一块基石&#xff0c;因为它允许创建分等级层次的类。继承就是子类继承父类的特征和行为&#xff0c;使得子类对象&#xff08;实例&#xff09;具有父类的实例域和方法&#xff0c;或子类从父类继承方法&#xff…

紫外线的形式是什么?

紫外线&#xff1a;紫外线 (UV: Ultraviolet) UV is an abbreviation of Ultraviolet. In RO water purifiers, the bacteria or germs which are present in the water cannot get killed by reverse osmosis process but this process can banish the dissolved solids and i…

[js高手之路] html5 canvas系列教程 - 掌握画直线图形的常用API

我们接着上文[js高手之路] html5 canvas系列教程 - 认识canvas以及基本使用方法继续. 一、直线的绘制 cxt.moveTo( x1, y1 )&#xff1a; 将画笔移动到x1, y1这个点 cxt.lineTo( x2, y2 )&#xff1a;将画笔从起点开始画直线&#xff0c;一直画到终点坐标( x2, y2 ) cxt.stroke…

金矿问题

Description: 描述&#xff1a; This is a standard interview problem featured in interview coding rounds of Amazon, Flipkart. 这是亚马逊Flipkart的采访编码回合中的标准采访问题。 Problem statement: 问题陈述&#xff1a; Given a gold mine of n*m dimensions, e…

[转载] python中的数组类型及特点

参考链接&#xff1a; Python中的Array | 数组2(简介和功能) 名称 表示方法示例 是否有序 函数方法&#xff08;增删等&#xff09; 特点 List 类型表示&#xff1a;L L [Adam, 95.5, Lisa, 85] 有序 增加&#xff1a;&#xff08;1&#xff09;L.append(Paul),增加…