|
|
@@ -0,0 +1,51 @@
|
|
|
+// 3637. Trionic Array I
|
|
|
+
|
|
|
+
|
|
|
+function isTrionic(nums: number[]): boolean {
|
|
|
+ if ( nums.length < 4 ) { return false }
|
|
|
+
|
|
|
+ function isIncreasing(ns: number[]): boolean {
|
|
|
+ let len: number = ns.length
|
|
|
+ if ( len == 1 ) { return false }
|
|
|
+ len--
|
|
|
+ let i: number = 0
|
|
|
+ while ( i < len ) { if ( ns[i] >= ns[i+1] ) { return false } i++ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ function isDecreasing(ns: number[]): boolean {
|
|
|
+ let len: number = ns.length
|
|
|
+ if ( len == 1 ) { return false }
|
|
|
+ len--
|
|
|
+ let i: number = 0
|
|
|
+ while ( i < len ) { if ( ns[i] <= ns[i+1] ) { return false } i++ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ let p: number = 1
|
|
|
+ let q: number = 2
|
|
|
+ let nlen: number = nums.length - 1
|
|
|
+
|
|
|
+ while ( true ) {
|
|
|
+ if ( isIncreasing(nums.slice(0, p+1))
|
|
|
+ && isDecreasing(nums.slice(p, q+1))
|
|
|
+ && isIncreasing(nums.slice(q)) ) { return true }
|
|
|
+ q++
|
|
|
+ if ( q >= nlen ) {
|
|
|
+ p++
|
|
|
+ q = p+1
|
|
|
+ if ( q >= nlen ) { return false }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false
|
|
|
+};
|
|
|
+
|
|
|
+function main() {
|
|
|
+ function r(n: number[]) { console.log(`[${n}] = ${isTrionic(n)}`) }
|
|
|
+ r([1,3,5,4,2,6])
|
|
|
+ r([2,1,3])
|
|
|
+ r([1,2,1,2])
|
|
|
+}
|
|
|
+
|
|
|
+main()
|