ue 抗锯齿 渲染序列失灵_锯齿序列Java解决方案

ue 抗锯齿 渲染序列失灵

A sawtooth sequence is a sequence of number that alternate between increasing and decreasing. In other words, each element is either strictly greater than it’s neighboring element or strictly less than it’s neighboring elements.

锯齿序列是在增加和减少之间交替的数字序列。 换句话说,每个元素要么严格大于其相邻元素,要么严格小于其相邻元素。

Problem Statement:

问题陈述:

Given an array of integers arr, your task is to count the number of contiguous subarrays that represent a sawtooth sequence of at least two elements.

给定一个整数数组arr ,您的任务是计算代表至少两个元素的锯齿序列的连续子数组的数量。

  • For arr = [9, 8, 7, 6, 5], the output should be countSawSubarrays(arr) = 4.

    对于arr = [ 9,8,7,6,5 ] ,输出应为countSawSubarrays(arr)= 4。

Since all the elements are arranged in decreasing order, it won’t be possible to form any sawtooth subarray of length 3 or more. There are 4 possible subarrays containing two elements, so the answer is 4.

由于所有元素都是按降序排列的,因此无法形成任何长度为3或更大的锯齿子阵列。 有4个可能的子数组包含两个元素,因此答案是4

  • For arr = [10, 10, 10], the output should be countSawSubarrays(arr) = 0.

    对于arr = [ 10,10,10 ] ,输出应为countSawSubarrays(arr)= 0。

Since all of the elements are equal, none of subarrays can be sawtooth, so the answer is 0.

由于所有元素都是相等的,因此子数组都不能是锯齿状的,因此答案是0

  • For arr = [1, 2, 1, 2, 1], the output should be countSawSubarrays(arr) = 10.

    对于arr = [ 1,2,1,2,1 ] ,输出应为countSawSubarrays(arr)= 10。

All contiguous subarrays containing at least two elements satisfy the condition of the problem. There are 10 possible contiguous subarrays containing at least two elements, so the answer is 10.

所有包含至少两个元素的连续子数组都满足问题的条件。 有10个可能的连续子数组包含至少两个元素,因此答案是10

Input:

输入:

An array of integers

整数数组

Output:

输出:

  • return the number of sawtooth subarrays.

    返回锯齿形子数组的数量。

Solution:

解:

import java.util.*;
import java.lang.*;
import java.io.*;public class Solution {
// Function to count the number of maximal contiguous
// increasing and decreasing subsequences
static long countSawSubarrays(int[] arr) {
int n = arr.length;
long ans = 0;
long[] saw = new long[n];

// check edge cases for n = 0,1,2
if (n == 0 || n == 1) {
return 0;
}
if (n == 2) {
if(arr[0] > arr[1] || arr[1] > arr[0]) return 1;
else return 0;
} // saw[i] represent the size of sawtooth subarray including index i.
// first element cannot form a sawtooth sequence
saw[0] = 0;

// initialize the saw[] array for n=2
if(arr[0] > arr[1] || arr[1] > arr[0]) {
saw[1] = 2;
}

for (int i=2; i<n; i++) {
// check if including ith element forms a sawtooth sequence
if((arr[i-2] < arr[i-1] && arr[i-1] > arr[i])
|| (arr[i-2] > arr[i-1] && arr[i-1] < arr[i]))
{
if(i==2) {
saw[i] = saw[i-2] + 3;
}
else {
saw[i] = saw[i-2] + 2;
}
}
// if ith element forms a sawtooth sequence just with previous element
else if(arr[i-1] > arr[i] || arr[i] > arr[i-1]) {
saw[i] = 2;
}
}

// compute ans, which is sum of all (non-zero values - 1)
for(int i=0; i<n; i++) {
if(saw[i] <= 0) continue;
ans += saw[i] - 1;
}
return ans;
}

// Driver code
public static void main (String[] args)
{
int[] arr = {1,2,1,2,1};
int[] arr2 = {9,8,7,6,5};
int[] arr3 = {-1000000000,1000000000};
int[] arr4 = {10, 10, 10, 10}; System.out.println(countSawSubarrays(arr));
System.out.println(countSawSubarrays(arr2));
System.out.println(countSawSubarrays(arr3));
System.out.println(countSawSubarrays(arr4));
}
}

Output:

输出

10
4
1
0

翻译自: https://medium.com/swlh/sawtooth-sequence-java-solution-460bd92c064

ue 抗锯齿 渲染序列失灵

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值