-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3637-TrionicArrayI.go
More file actions
73 lines (63 loc) · 2.1 KB
/
3637-TrionicArrayI.go
File metadata and controls
73 lines (63 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
// 3637. Trionic Array I
// You are given an integer array nums of length n.
// An array is trionic if there exist indices 0 < p < q < n − 1 such that:
// 1. nums[0...p] is strictly increasing,
// 2. nums[p...q] is strictly decreasing,
// 3. nums[q...n − 1] is strictly increasing.
// Return true if nums is trionic, otherwise return false.
// Example 1:
// Input: nums = [1,3,5,4,2,6]
// Output: true
// Explanation:
// Pick p = 2, q = 4:
// nums[0...2] = [1, 3, 5] is strictly increasing (1 < 3 < 5).
// nums[2...4] = [5, 4, 2] is strictly decreasing (5 > 4 > 2).
// nums[4...5] = [2, 6] is strictly increasing (2 < 6).
// Example 2:
// Input: nums = [2,1,3]
// Output: false
// Explanation:
// There is no way to pick p and q to form the required three segments.
// Constraints:
// 3 <= n <= 100
// -1000 <= nums[i] <= 1000
import "fmt"
func isTrionic(nums []int) bool {
n, i := len(nums), 2
if nums[1] <= nums[0] { return false } // First increasing sequence must exist
// First strictly increasing phase
for i < n && nums[i] > nums[i - 1] {
i++
}
if i == n { return false }
// Strictly decreasing phase
for i < n && nums[i] < nums[i - 1] {
i++
}
if i == n { return false }
// Final strictly increasing phase
for j := i; j < n; j++ {
if (nums[j] <= nums[j - 1]) { return false }
}
return true;
}
func main() {
// Example 1:
// Input: nums = [1,3,5,4,2,6]
// Output: true
// Explanation:
// Pick p = 2, q = 4:
// nums[0...2] = [1, 3, 5] is strictly increasing (1 < 3 < 5).
// nums[2...4] = [5, 4, 2] is strictly decreasing (5 > 4 > 2).
// nums[4...5] = [2, 6] is strictly increasing (2 < 6).
fmt.Println(isTrionic([]int{1,3,5,4,2,6})) // true
// Example 2:
// Input: nums = [2,1,3]
// Output: false
// Explanation:
// There is no way to pick p and q to form the required three segments.
fmt.Println(isTrionic([]int{2,1,3})) // false
fmt.Println(isTrionic([]int{1,2,3,4,5,6,7,8,9})) // false
fmt.Println(isTrionic([]int{9,8,7,6,5,4,3,2,1})) // false
}