laydate 跨度_股票跨度问题

本文深入探讨了股票跨度问题,这是一个金融领域的经典问题,涉及到一系列每日股票价格的计算。文章提供了两种解决方案,包括蛮力法和基于栈的方法,详细解释了算法原理及其实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

laydate 跨度

Problem statement:

问题陈述:

The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate the span of stock's price for all n days.

该股跨度的问题是,我们有一系列n个每日报价的股票的财务问题,我们需要计算股票的价格的跨度为所有n天。

The span Si of the stock's price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.

给定日i的股票价格跨度Si被定义为给定日之前连续的最大天数,为此,当日股票价格小于或等于给定日的价格。 。

For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}.

例如,如果将7天价格的数组指定为{100,80,60,70,60,75,85},则对应的7天的跨度值为{1、1、1、2、1、4 ,6}。

Input:

输入:

The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N, N is the size of the array. The second line of each test case contains N input arr[i].

输入的第一行包含一个整数T,表示测试用例的数量。 每个测试用例的第一行是NN是数组的大小。 每个测试用例的第二行包含N个输入arr [i]

Output:

输出:

For each testcase, print the span values for all days.

对于每个测试用例,请打印整天的跨度值。

Examples:

例子:

    Input:	
    T = 1
    N = 6
    [10, 4, 5, 90, 120, 80]
    
    Output: 
    1 1 2 4 5 1

    Input:
    T = 1
    N = 7
    [2,4,5,6,7,8,9]
    
    Output: 
    1 2 3 4 5 6 7

Solution Approach

解决方法

1) Brute Force Approach

1)蛮力法

In this approach, we will use the count variable for each element and check all the elements before it, if the elements are smaller than or equal to the current element then increase the count variable.

在这种方法中,我们将对每个元素使用count变量,并检查它之前的所有元素,如果元素小于或等于当前元素,则增加count变量。

We will store each element's count in a vector and then print the elements.

我们将每个元素的计数存储在向量中,然后打印这些元素。

This approach will traverse the elements in the entire array hence time complexity will be O(n*n).

这种方法将遍历整个数组中的元素,因此时间复杂度将为O(n * n)

Pseudo Code:

伪代码:

function(arr[],n):
	//vector to store the elements which are <= to current element.
	vector<int>v1

	//1st element is always 1 as there is no other element 
	v1.push_back(1)
	for(int i=1;i<n;i++)
		cnt=1;
	// Traverse left while the next element   
	// on left is smaller than price[i]  
	for(int j = i - 1; (j >= 0) &&  (arr[i] >=arr[j]); j--)
		cnt++;
		v1.push_back(cnt);

	for(int i=0;i<n;i++) //print the results.
		cout<<v1[i]<

Time Complexity: O(n*n)
Space Complexity: (n)

C++ Implementation:

Output

Enter number of test cases: 3
Enter size of stock's price array: 5
Enter stock prices: 1 2 5 9 2  
Span Values: 1 2 3 4 1 
Enter size of stock's price array: 9
Enter stock prices: 1 2 3 4 5 6 7 8 9
Span Values: 1 2 3 4 5 6 7 8 9 
Enter size of stock's price array: 7       
Enter stock prices: 100 99 98 97 96 95 94
Span Values: 1 1 1 1 1 1 1 

2) Stack based solution

In this approach we will use stack with pair, we will store array element and its index, each time we iterate through array element. We check the previous next greater element, it the previous greater element is found at some location i, then we will take the difference between the current index element and the previous greater element.

We will use a vector for each index to push the number of elements that are smaller than or equal to the current element.

We will use a boolean variable flag, if the element is pushed according to condition then make variable flag true otherwise push 1 as only that element is valid and all element before is greater than it.

Pseudo Code:

Time Complexity for above approach: O(n)
space Complexity for above approach: O(n)

C++ Implementation:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{
    ll t;
    cout << "Enter number of test cases: ";
    cin >> t;

    while (t--) {
        ll n; //size of stock array variable.
        cout << "Enter size of stock's price array: ";
        cin >> n;

        ll arr[n];

        cout << "Enter stock prices: ";
        for (ll i = 0; i < n; i++)
            cin >> arr[i];
        stack<pair<ll, ll> > st; //stack declaration.
        st.push({ INT_MAX, -1 });
        vector<ll> v1;
        for (ll i = 0; i < n; i++) {
            bool flag = false;
            while (!st.empty()) {
                // is stack top is greater than current element then
                // put the difference value in the vector.
                if (st.top().first > arr[i]) 
                {
                    v1.push_back(i - st.top().second);
                    flag = true;
                    break;
                }
                else
                    st.pop(); //if top value is not greater than pop.
            }
            if (flag == false)
                v1.push_back(1);
            st.push({ arr[i], i });
        }
        cout << "Span Values: "; //print then span values.
        for (ll i = 0; i < n; i++)
            cout << v1[i] << " ";
        cout << "\n";
    }
    return 0;
}

Output

Problem reference: https://leetcode.com/problems/online-stock-span/





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.


function(arr[],n):
	//vector to store the elements which are <= to current element.
	vector<int>v1

	//1st element is always 1 as there is no other element 
	v1.push_back(1)
	for(int i=1;i<n;i++)
		cnt=1;
	// Traverse left while the next element   
	// on left is smaller than price[i]  
	for(int j = i - 1; (j >= 0) &&  (arr[i] >=arr[j]); j--)
		cnt++;
		v1.push_back(cnt);

	for(int i=0;i<n;i++) //print the results.
		cout<<v1[i]<

Time Complexity: O(n*n)
Space Complexity: (n)

C++ Implementation:

Output

Enter number of test cases: 3
Enter size of stock's price array: 5
Enter stock prices: 1 2 5 9 2  
Span Values: 1 2 3 4 1 
Enter size of stock's price array: 9
Enter stock prices: 1 2 3 4 5 6 7 8 9
Span Values: 1 2 3 4 5 6 7 8 9 
Enter size of stock's price array: 7       
Enter stock prices: 100 99 98 97 96 95 94
Span Values: 1 1 1 1 1 1 1 

2) Stack based solution

In this approach we will use stack with pair, we will store array element and its index, each time we iterate through array element. We check the previous next greater element, it the previous greater element is found at some location i , then we will take the difference between the current index element and the previous greater element.

We will use a vector for each index to push the number of elements that are smaller than or equal to the current element.

We will use a boolean variable flag , if the element is pushed according to condition then make variable flag true otherwise push 1 as only that element is valid and all element before is greater than it.

Pseudo Code:

Time Complexity for above approach: O(n)
space Complexity for above approach: O(n)

C++ Implementation:

 # include  < bits/stdc++.h >
using namespace std ;

typedef long long ll ;

int main ( )
{
    ll t ;
    cout < < " Enter number of test cases:  " ;
    cin > > t ;

    while ( t - - ) {
        ll n ; //size of stock array variable.
        cout < < " Enter size of stock's price array:  " ;
        cin > > n ;

        ll arr [ n ] ;

        cout < < " Enter stock prices:  " ;
        for ( ll i = 0 ; i < n ; i + + )
            cin > > arr [ i ] ;
        stack < pair < ll , ll > > st ; //stack declaration.
        st . push ({ INT_MAX , - 1 }) ;
        vector < ll > v1 ;
        for ( ll i = 0 ; i < n ; i + + ) {
            bool flag = false ;
            while ( ! st . empty ( ) ) {
                // is stack top is greater than current element then
                // put the difference value in the vector.
                if ( st . top ( ) . first > arr [ i ] ) 
                {
                    v1 . push_back ( i - st . top ( ) . second ) ;
                    flag = true ;
                    break ;
                }
                else
                    st . pop ( ) ; //if top value is not greater than pop.
            }
            if ( flag = = false )
                v1 . push_back ( 1 ) ;
            st . push ({ arr [ i ] , i }) ;
        }
        cout < < " Span Values:  " ; //print then span values.
        for ( ll i = 0 ; i < n ; i + + )
            cout < < v1 [ i ] < < "   " ;
        cout < < " \n " ;
    }
    return 0 ;
}

Output

Problem reference: https://leetcode.com/problems/online-stock-span/





评论和讨论

广告:您是博主吗? 加入我们的Blogging论坛


翻译自: https://www.includehelp.com/icp/stock-span-problem.aspx

laydate 跨度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值