-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay68
143 lines (102 loc) · 3.31 KB
/
Day68
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
//A. The New Year: Meeting Friends
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x1 = scanner.nextInt();
int x2 = scanner.nextInt();
int x3 = scanner.nextInt();
int[] coordinates = {x1, x2, x3};
Arrays.sort(coordinates);
int median = coordinates[1];
int totalDistance = (coordinates[2] - median) + (median - coordinates[0]);
System.out.println(totalDistance);
}
}
//A. Police Recruits
import java.util.Scanner;
public class PoliceRecruits {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int availableOfficers = 0;
int untreatedCrimes = 0;
// int untreatedof =0;
for (int i = 0; i < n; i++) {
int event = scanner.nextInt();
if (event == -1) {
if (availableOfficers > 0) {
availableOfficers--;
} else {
untreatedCrimes++;
}
} else {
availableOfficers += event;
}
}
System.out.println(untreatedCrimes);
}
}
//167. Two Sum II - Input Array Is Sorted
public class Solution {
public int[] twoSum(int[] numbers, int target) {
int left = 0;
int right = numbers.length - 1;
while (left < right) {
int sum = numbers[left] + numbers[right];
if (sum == target) {
return new int[]{left + 1, right + 1};
} else if (sum < target) {
left++;
} else {
right--;
}
}
return new int[0]; // This line is technically unreachable due to problem constraints
}
}
//367. Valid Perfect Square
https://leetcode.com/problems/valid-perfect-square/
public class Solution {
public boolean isPerfectSquare(int num) {
if (num < 2) {
return true; // 0 and 1 are perfect squares
}
int left = 2;
int right = num / 2;
while (left <= right) {
int mid = left + (right - left) / 2;
long midSquared = (long) mid * mid;
if (midSquared == num) {
return true;
} else if (midSquared < num) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
}
//Problem 5 :441. Arranging Coins
//Problem :https://leetcode.com/problems/arranging-coins/description/
class Solution {
public int arrangeCoins(int n) {
long left = 0;
long right = n;
while (left <= right) {
long mid = left + (right - left) / 2;
long totalCoins = mid * (mid + 1) / 2;
if (totalCoins == n) {
return (int) mid;
} else if (totalCoins < n) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return (int) right;
}
}
//