Posts

Showing posts with the label 单调栈

MS 1740. Online Stock Span 很好的一道题

Write a class  StockSpanner  which collects daily price quotes for some stock, and returns the  span  of that stock's price for the current day. The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price. For example, if the price of a stock over the next 7 days were  [100, 80, 60, 70, 60, 75, 85] , then the stock spans would be  [1, 1, 1, 2, 1, 4, 6] . Calls to StockSpanner.next(int price) will have 1 <= price <= 10^5. There will be at most 10000 calls to StockSpanner.next per test case. There will be at most 150000 calls to StockSpanner.next across all test cases. The total time limit for this problem has been reduced by 75% for C++, and 50% for all other languages. Have you met this question in a real interview?    Yes Problem Correction Example Example 1...

1255. Remove K Digits

Code ( Language :C++) ( Judger :ip-172-31-21-252) Edit class Solution { public : /** * @param num: a string * @param k: an integer * @return: return a string */ string removeKdigits ( string &num, int k) { // write your code here //单调栈 stack < char > stk; string res; for ( int i = 0 ; i < num.size(); i++){ while (!stk.empty() && stk.top() > num[i] && k > 0 ){ stk.pop(); k--; } stk.push(num[i]); } while (!stk.empty()){ res = stk.top() + res; stk.pop(); } res = res.substr( 0 , num.size() - k); //corner case 重复的情况 int idx = 0 ; while (res[idx] == '0' && idx < res.size()){ //corner case 0打头 idx++; } res = res.substr(idx); return res.size() ? res : "0...

AM 1734. Sum of Subarray Minimums

Code ( Language :C++) ( Judger :cloudjudge-cluster-7) Edit class Solution { public : /** * @param A: an array * @return: the sum of subarray minimums */ int sumSubarrayMins ( vector < int > &A) { // Write your code here. // 单调栈 找 左右两边 第一个比当前值 小的值。 //右边第一个小的:单调递减栈 //左边第一个小的: 单调递增栈。 //这个题和 max area in histgram 和 in matrix基本思路一样的。 //值得注意 const int size = A.size(); long mod = 1000000007 ; if (size == 0 ){ return 0 ; } stack < int > stk1; // vector<int> right(size, 0); A.push_back( 0 ); int res = 0 ; for ( int i = 0 ; i <= size; i++){ while (!stk1.empty() && A[stk1.top()] >= A[i]){ // inceasing? int tmp = stk1.top(); stk1.pop(); //right[tmp] = i; int left = stk1.empty() ? -1 : stk1.top...

1201. Next Greater Element II

class Solution { public : /** * @param nums: an array * @return: the Next Greater Number for every element */ vector < int > nextGreaterElements( vector < int > &nums) { // Write your code here int n = nums.size(); vector < int > res(n, -1 ); stack < int > stk; for ( int i = 0 ; i < 2 * n; i++){ //循环的经典实用 while (!stk.empty() && nums[stk.top()] < nums[i % n]){ res[stk.top()] = nums[i % n]; stk.pop(); } stk.push(i % n); } return res; } };

1206. Next Greater Element I

You are given two arrays  (without duplicates)   nums1  and  nums2  where  nums1 ’s elements are subset of  nums2 . Find all the next greater numbers for  nums1 's elements in the corresponding places of  nums2 . The Next Greater Number of a number x in  nums1  is the first greater number to its right in  nums2 . If it does not exist, output -1 for this number. 1.All elements in  nums1  and  nums2  are unique. 2.The length of both  nums1  and  nums2  would not exceed 1000. Have you met this question in a real interview?    Yes Problem Correction Example Example 1: Input: nums1 = [4,1,2], nums2 = [1,3,4,2]. Output: [-1,3,-1] Explanation: For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1. For number 1 in the first array, the next greater number for it in the second array is ...

1060. Daily Temperatures

Description 中文 English Given a list of daily  temperatures , produce a list that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead. For example, given the list  temperatures = [73, 74, 75, 71, 69, 72, 76, 73] , your output should be  [1, 1, 4, 2, 1, 1, 0, 0] . 1.The length of  temperatures  will be in the range  [1, 30000] . Each temperature will be an integer in the range  [30, 100] Have you met this question in a real interview?    Yes Problem Correction Example Input: temperatures = [73, 74, 75, 71, 69, 72, 76, 73] Output: [1, 1, 4, 2, 1, 1, 0, 0] class Solution { public : /** * @param temperatures: a list of daily temperatures * @return: a list of how many days you would have to wait until a warmer temperature */ vector < int > dailyTemperature...

1255. Remove K Digits

Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. The length of num is less than 10002 and will be ≥ k. The given num does not contain any leading zero. Have you met this question in a real interview?    Yes Problem Correction Example Example 1: Input: num = "1432219", k = 3 Output: "1219" Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest. Example 2: Input: num = "10200", k = 1 Output: "200" Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes. Example 3: Input: num = "10", k = 2 Output: "0" Explanation: Remove all the digits from the number and it is left with nothing which is 0. class Solution { public : /** * @param num: a string * @param k: an integer * @return: ...