計數器數組_子數組計數

計數器數組

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.pswp.cn/news/540596.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540596.shtml
英文地址,請注明出處:http://en.pswp.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),增加…