-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay78
177 lines (127 loc) · 4.57 KB
/
Day78
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
//A. Love Story
// https://codeforces.com/problemset/problem/1829/A
import java.util.Scanner;
public class LoveStory {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read the number of test cases
int t = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
// Define the target string
String target = "codeforces";
// Process each test case
for (int i = 0; i < t; i++) {
String s = scanner.nextLine(); // Read the input string
// Count differences
int differenceCount = countDifferences(s, target);
System.out.println(differenceCount); // Output the result for this test case
}
scanner.close();
}
private static int countDifferences(String s, String target) {
int count = 0;
// Compare each character in the strings
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != target.charAt(i)) {
count++;
}
}
return count;
}
}
//A. Spell Check
//https://codeforces.com/problemset/problem/1722/A
import java.util.Scanner;
public class SpellCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
String s = scanner.next(); // Read the string
if (isValidSpelling(s, n)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
scanner.close();
}
private static boolean isValidSpelling(String s, int n) {
if (n != 5) {
return false;
}
int countT = 0, countI = 0, countM = 0, countU = 0, countR = 0;
for (char c : s.toCharArray()) {
switch (c) {
case 'T': countT++; break;
case 'i': countI++; break;
case 'm': countM++; break;
case 'u': countU++; break;
case 'r': countR++; break;
default: return false;
}
}
return countT == 1 && countI == 1 && countM == 1 && countU == 1 && countR == 1;
}
}
//B. Fair Division
//https://codeforces.com/problemset/problem/1472/B
import java.util.Scanner;
public class FairDivision {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int[] candies = new int[n];
int count1 = 0, count2 = 0;
for (int j = 0; j < n; j++) {
candies[j] = scanner.nextInt();
if (candies[j] == 1) {
count1++;
} else {
count2++;
}
}
int totalWeight = count1 + 2 * count2;
if (totalWeight % 2 != 0) {
System.out.println("NO");
continue;
}
int targetWeight = totalWeight / 2;
boolean canDivide = false;
for (int used2 = 0; used2 <= Math.min(count2, targetWeight / 2); used2++) {
int remainingWeight = targetWeight - used2 * 2;
if (remainingWeight <= count1) {
canDivide = true;
break;
}
}
System.out.println(canDivide ? "YES" : "NO");
}
scanner.close();
}
}
//45. Jump Game II
//https://leetcode.com/problems/jump-game-ii/description/?envType=study-plan-v2&envId=top-interview-150
class Solution {
public int jump(int[] nums) {
int n = nums.length;
if (n == 1) {
return 0;
}
int jumps = 0;
int currentEnd = 0;
int currentFarthest = 0;
for (int i = 0; i < n - 1; i++) {
currentFarthest = Math.max(currentFarthest, i + nums[i]);
if (i == currentEnd) {
jumps++;
currentEnd = currentFarthest;
}
}
return jumps;
}
}