计数器数组_子数组计数

计数器数组

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,一经查实,立即删除!

相关文章

MaxCompute 2.0—从ODPS到MaxCompute

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

紫外线的形式是什么?

紫外线&#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…

cesium广告牌_公路广告牌

cesium广告牌Description: 描述&#xff1a; This is a standard dynamic programing problem of finding maximum profits with some constraints. This can be featured in any interview coding rounds. 这是在某些约束条件下找到最大利润的标准动态编程问题。 这可以在任何…

【iCore4 双核心板_FPGA】例程十六:基于双口RAM的ARM+FPGA数据存取实验

实验现象&#xff1a; 核心代码&#xff1a; int main(void) {/* USER CODE BEGIN 1 */int i;int address,data;char error_flag 0;char receive_data[50];char buffer[8];char *p;/* USER CODE END 1 *//* MCU Configuration-----------------------------------------------…

laravel 项目迁移_在Laravel迁移

laravel 项目迁移Before moving forward we need to know some facts about it, 在继续前进之前&#xff0c;我们需要了解一些事实&#xff0c; Resources: In these directories, we have already a js, lang, sass and view page. Where, sass and js file holf their uncom…

[AtCoder-ARC073F]Many Moves

题目大意&#xff1a;   有一排n个格子和2枚硬币。   现在有q次任务&#xff0c;每一次要你把其中一枚硬币移到x的位置上&#xff0c;移动1格的代价是1。   两枚硬币不能同时移动&#xff0c;任务必须按次序完成。   现在告诉你两枚硬币初始状态所在的位置a和b&#xf…

OpenStack —— DevStack一键自动化安装

一、DevStack介绍Devstack目前是支持Ubuntu16.04和CentOS 7&#xff0c;而且Devstack官方建议使用Ubuntu16.04&#xff0c;所以我们使用Ubuntu 16.04进行安装。默认无论是Devstack和OpenStack&#xff0c;都是采用Master的代码进行安装&#xff0c;这样经常会出现&#xff0c;今…

Scala铸造

Scala中的类型 (Types in Scala) Type also know as data type tells the compiler about the type of data that is used by the programmer. For example, if we initialize a value or variable as an integer the compiler will free up 4 bytes of memory space and it wi…

OpenCV探索之路(二十五):制作简易的图像标注小工具

搞图像深度学习的童鞋一定碰过图像数据标注的东西&#xff0c;当我们训练网络时需要训练集数据&#xff0c;但在网上又没有找到自己想要的数据集&#xff0c;这时候就考虑自己制作自己的数据集了&#xff0c;这时就需要对图像进行标注。图像标注是件很枯燥又很费人力物力的一件…

图论 弦_混乱的弦

图论 弦Problem statement: 问题陈述&#xff1a; You are provided an input string S and the string "includehelp". You need to figure out all possible subsequences "includehelp" in the string S? Find out the number of ways in which the s…

「原创」从马云、马化腾、李彦宏的对话,看出三人智慧差在哪里?

在今年中国IT领袖峰会上&#xff0c;马云、马化腾、李彦宏第一次单独合影&#xff0c;同框画面可以说很难得了。BAT关心的走势一直是同行们竞相捕捉的热点&#xff0c;所以三位大Boss在这次大会上关于人工智能的见解&#xff0c;也受到广泛关注与多方解读。马云认为机器比人聪明…

字符串矩阵转换成长字符串_字符串矩阵

字符串矩阵转换成长字符串Description: 描述&#xff1a; In this article, we are going to see how backtracking can be used to solve following problems? 在本文中&#xff0c;我们将看到如何使用回溯来解决以下问题&#xff1f; Problem statement: 问题陈述&#xf…

java awt 按钮响应_Java AWT按钮

java awt 按钮响应The Button class is used to implement a GUI push button. It has a label and generates an event, whenever it is clicked. As mentioned in previous sections, it extends the Component class and implements the Accessible interface. Button类用于…

qgis在地图上画导航线_在Laravel中的航线

qgis在地图上画导航线For further process we need to know something about it, 为了进一步处理&#xff0c;我们需要了解一些有关它的信息&#xff0c; The route is a core part in Laravel because it maps the controller for sending a request which is automatically …

Logistic回归和SVM的异同

这个问题在最近面试的时候被问了几次&#xff0c;让谈一下Logistic回归&#xff08;以下简称LR&#xff09;和SVM的异同。由于之前没有对比分析过&#xff0c;而且不知道从哪个角度去分析&#xff0c;一时语塞&#xff0c;只能不知为不知。 现在对这二者做一个对比分析&#xf…

构建安全网络 比格云全系云产品30天内5折购

一年之计在于春&#xff0c;每年的三、四月&#xff0c;都是个人创业最佳的起步阶段&#xff0c;也是企业采购最火热的时期。为了降低用户的上云成本&#xff0c;让大家能无门槛享受到优质高性能的云服务&#xff0c;比格云从3月16日起&#xff0c;将上线“充值30天内&#xff…

数据结构 基础知识

一。逻辑结构: 是指数据对象中数据 素之间的相互关系。 其实这也是我 今后最需要关注的问题 逻辑结构分为以 四种1. 集合结构 2.线性结构 3.数形结构 4&#xff0c;图形结构 二。物理结构&#xff1a; 1&#xff0c;顺序存储结,2 2. 链式存储结构 一&#xff0c;时间复杂…

ruby 变量类中范围_Ruby中的类

ruby 变量类中范围Ruby类 (Ruby Classes) In the actual world, we have many objects which belong to the same category. For instance, I am working on my laptop and this laptop is one of those laptops which exist around the globe. So, this laptop is an object o…