Skip to content

Commit 573edf1

Browse files
committed
update
1 parent 5dbf089 commit 573edf1

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Time: 796 ms
2+
// Memory: 2400 KB
3+
// Greedy.
4+
// T:O(sum(ni)), S:O(1)
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1832C_Contrast_Value {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
int t = sc.nextInt();
12+
for (int i = 0; i < t; i++) {
13+
int n = sc.nextInt(), prev = -1, direction = 0, ret = 1;
14+
for (int j = 0; j < n; j++) {
15+
int a = sc.nextInt();
16+
if (prev == -1) {
17+
prev = a;
18+
continue;
19+
}
20+
if (direction == 0) {
21+
if (a > prev) {
22+
direction = 1;
23+
} else if (a < prev) {
24+
direction = -1;
25+
}
26+
} else if (direction == 1 && a < prev) {
27+
ret++;
28+
direction = -1;
29+
} else if (direction == -1 && a > prev) {
30+
ret++;
31+
direction = 1;
32+
}
33+
if (direction != 0 && j == n - 1) {
34+
ret++;
35+
}
36+
prev = a;
37+
}
38+
39+
System.out.println(ret);
40+
}
41+
}
42+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Runtime 1 ms Beats 100.00%
2+
// Memory 44.10 MB Beats 100.00%
3+
// .
4+
// T:O(nlogk), S:O(1)
5+
//
6+
class Solution {
7+
public int smallestIndex(int[] nums) {
8+
int ret = -1;
9+
for (int i = 0; i < nums.length; i++) {
10+
int digitSum = 0;
11+
while (nums[i] > 0) {
12+
digitSum += nums[i] % 10;
13+
nums[i] /= 10;
14+
}
15+
if (digitSum == i) {
16+
ret = i;
17+
break;
18+
}
19+
}
20+
21+
return ret;
22+
}
23+
}

0 commit comments

Comments
 (0)