首页 > 其他分享 >区间计数相关题目 907,828

区间计数相关题目 907,828

时间:2022-08-24 00:12:22浏览次数:52  
标签:arr 907 int 计数 length 828 new stack2 stack1

907. Sum of Subarray Minimums Medium

Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7.

Example 1:

Input: arr = [3,1,2,4]
Output: 17
Explanation: 
Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]. 
Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.
Sum is 17.

Example 2:

Input: arr = [11,81,94,43,3]
Output: 444

Constraints:

  • 1 <= arr.length <= 3 * 104
  • 1 <= arr[i] <= 3 * 104

分析:

如果brute force的话,时间复杂度在O(N3)  或者 做优化后O(N2)

  [3] [3,1] [3,1,2] [3,1,2,4]

  [1] [1,2] [1,2,4]

  [2] [2,4]

  [4]

 

如果我们从每个数字角度考虑的话,会发现:

3可以贡献的只有一次[3]

1可以贡献的有 [3,1] [3,1,2] [3,1,2,4] [1] [1,2] [1,2,4]

2可以贡献的有  [2] [2,4]

4可以贡献的只有 [4]

 

因此,针对1 ,可以贡献的次数为:

[3,1,2,4]   left=-1(左侧小于1的位置), right=4(右侧小于1的位置)  ,pos=1(1自己所在位置), 1可以贡献的次数即为:(1-(-1))*(4-1) = 6

针对 2可以贡献的次数为:

[3,1,2,4]  left=1, right = 4, 可以贡献的次数即为: (2-1)*(4-2) = 2

 

class Solution {
    public int sumSubarrayMins(int[] arr) {
     //定义stack1计算每个数字的左侧/右侧小于自己的数字位置 Stack<Integer> stack1 = new Stack(); Stack<Integer> stack2 = new Stack(); int[] preSmaller = new int[arr.length]; int[] postSmaller = new int[arr.length]; for(int i=0;i<arr.length;i++){ while(!stack1.isEmpty() && arr[stack1.peek()] >=arr[i]) stack1.pop(); preSmaller[i] = stack1.isEmpty() ? -1 : stack1.peek(); stack1.push(i); } for(int i=arr.length-1;i>=0;i--){ while(!stack2.isEmpty() && arr[stack2.peek()] >arr[i]) stack2.pop(); postSmaller[i] = stack2.isEmpty() ? arr.length : stack2.peek(); stack2.push(i); }
     //计算结束后 long sum = 0; long mod = (long)1e9 + 7; for(int i=0;i<arr.length;i++){ sum += ((long)(i-preSmaller[i]) * (postSmaller[i]-i) * arr[i])%mod; sum %= mod; } return (int)sum; } }

 

828. Count Unique Characters of All Substrings of a Given String Hard

Let's define a function countUniqueChars(s) that returns the number of unique characters on s.

  • For example, calling countUniqueChars(s) if s = "LEETCODE" then "L""T""C""O""D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.

Given a string s, return the sum of countUniqueChars(t) where t is a substring of s. The test cases are generated such that the answer fits in a 32-bit integer.

Notice that some substrings can be repeated so in this case you have to count the repeated ones too.

 Example 1:

Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Every substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10

Example 2:

Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.

Example 3:

Input: s = "LEETCODE"
Output: 92

Constraints:

  • 1 <= s.length <= 105
  • s consists of uppercase English letters only.

同样,bruteforce ,时间复杂度为O(N3) 或者 优化后O(N2)

但如果我们从每个字母的贡献度来考虑的话,比如:

ABCA

第一个A可以贡献的次数为: A,AB,ABC,ABCA

B可以贡献的次数为:AB,B,BC,BCA

class Solution {
    public int uniqueLetterString(String s) {
        int[] left = new int[s.length()];
        int[] right = new int[s.length()];
        int[] lastPos = new int[26];
        Arrays.fill(lastPos, -1);
        for(int i=0;i<s.length();i++){
            char c = s.charAt(i);
            left[i] = lastPos[c-'A'];
            lastPos[c-'A'] = i;
        }
        Arrays.fill(lastPos, s.length());
        for(int i=s.length()-1;i>=0;i--){
            char c = s.charAt(i);
            right[i] = lastPos[c-'A'];
            lastPos[c-'A'] = i;
        }
        int sum = 0;
        for(int i=0;i<s.length();i++){
            sum += (i-left[i])*(right[i]-i);
        }
        return sum;
    }
}

 

标签:arr,907,int,计数,length,828,new,stack2,stack1
From: https://www.cnblogs.com/cynrjy/p/16618334.html

相关文章

  • PAT 计数
    https://www.acwing.com/problem/content/1585/状态机的解法#include<iostream>#include<cstring>usingnamespacestd;constintN=100010,MOD=1e9+7;......
  • IPQ6018 wallys OpenWrt 2.4/5G dual bands 802.11ax Indoor Aluminium alloy mater
    QCN9074WiFi6ECardOpenWRT,IPQ6010,802.11ax,2x22.4G&5GQCN9074WiFiCardIPQ6010,802.11ax,2x22.4G&5G,SupportOpenWRT  MT7915/MT7975/IPQ6000/IP......
  • 计数DP 1
    到你了,我的Boss其实所有的计数\(DP,\)都会有一句话叫做维护贡献就是在\(i\)阶段的一些互斥的状态,推广到\(i+1\)阶段的同时进行递推产生的方案数。计数DP你要清楚你在......
  • 338. 比特位计数
     难度简单1064收藏分享切换为英文接收动态反馈给你一个整数 n ,对于 0<=i<=n 中的每个 i ,计算其二进制表示中 1 的个数 ,返回一个长度为 n+1 的数......
  • 计数类组合数DP(玩个球)A3
    n种颜色的球,每种m个,给出一个这些球的排列,就会从左往右把第一个遇到的某种颜色涂白。问有多少不一样的颜色序列(n,m<=2000)发现白色的球都一样,当成一种算,我们只关注当前:(1)白......
  • 题解 [ZJOI2010]排列计数
    好题。%你赛考到了不会摆烂,后来发现原来有向下取整,题面没有。。。(就算有我也做不出来啦qAq首先我们会发现这个长得就是小根堆,答案就变成了小根堆的计数。首先最小的......
  • 《GB28286-2012》PDF下载
    《GB28286-2012工业zhayao通用技术条件》PDF下载《GB28286-2012》简介本标准规定了工业zhayao通用的技术要求、试验方法、检验规则、标志、包装等;本标准适用于工程、......
  • P5131 荷取融合——计数dp,组合计数
    本题是一个计数类的问题,其中需要有一些优化。简单思路从题面和数据范围,可以猜测算法时间复杂度大概是\(O(nk)\),所以不难想到用动态规划:设\(f(i,j)\)为前\(i\)个数中选\(......
  • CF512D Fox And Travelling(DP 计数)
    CF512DFoxAndTravelling给定一张\(n\)个点\(m\)条边的无向图,每次选择一个叶子结点并将它和连接它的边删除。对于每个\(k\in[0,n]\),问有序选择\(k\)个点的方案......