File tree Expand file tree Collapse file tree
src/main/java/algorithms/sprint2 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5555public class Deque {
5656
5757 // -------------------- RING BUFFER DEQUE --------------------
58+ private static final int MAX_CAPACITY = 100_000 ;
59+
5860 static final class RingDeque {
5961 private final int [] a ;
6062 private final int cap ;
@@ -63,8 +65,8 @@ static final class RingDeque {
6365 private int size = 0 ;
6466
6567 RingDeque (int cap ) {
66- this .cap = cap ;
67- this .a = new int [cap ];
68+ this .cap = safeCapacity ( cap ) ;
69+ this .a = new int [this . cap ];
6870 }
6971
7072 private int next (int i ) {
@@ -110,6 +112,13 @@ int popBack() {
110112 }
111113 }
112114
115+ private static int safeCapacity (int cap ) {
116+ if (cap < 0 ) {
117+ return 0 ;
118+ }
119+ return Math .min (cap , MAX_CAPACITY );
120+ }
121+
113122 private static void process (FastIn in , FastOut out ) throws Exception {
114123 int n = in .nextInt ();
115124 int m = in .nextInt ();
@@ -233,6 +242,27 @@ private static void test() throws Exception {
233242 )
234243 );
235244
245+ // Некорректная емкость из ввода не должна приводить к аварийному завершению
246+ assertEq (
247+ "error\n error\n " ,
248+ solveIO (
249+ "2\n " +
250+ "-1\n " +
251+ "push_back 1\n " +
252+ "pop_front\n "
253+ )
254+ );
255+
256+ // Слишком большая емкость ограничивается безопасным максимумом до выделения массива
257+ assertEq (
258+ "error\n " ,
259+ solveIO (
260+ "1\n " +
261+ "1000000000\n " +
262+ "pop_front\n "
263+ )
264+ );
265+
236266 // Wrap-around: head/tail должны корректно "перепрыгивать" границу массива
237267 assertEq (
238268 "1\n 4\n 2\n 3\n " ,
You can’t perform that action at this time.
0 commit comments