-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList_Answers.java
More file actions
186 lines (164 loc) · 6.32 KB
/
ArrayList_Answers.java
File metadata and controls
186 lines (164 loc) · 6.32 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
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
178
179
180
181
182
183
184
185
186
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class ArrayList_Answers {
public static void main(String[] args) {
// === 문제 1 ===
System.out.println("문제1: " + solution1(new ArrayList<>(Arrays.asList(1, 1, 3, 3, 0, 1, 1))));
// [1, 3, 0, 1]
// === 문제 2 ===
System.out.println("문제2: " + solution2(new ArrayList<>(Arrays.asList(5, 9, 7, 10)), 5));
// [5, 10]
System.out.println("문제2: " + solution2(new ArrayList<>(Arrays.asList(3, 2, 6)), 10));
// [-1]
// === 문제 3 ===
System.out.println("문제3: " + solution3(new ArrayList<>(Arrays.asList(2, 1, 3, 4, 1))));
// [2, 3, 4, 5, 6, 7]
// === 문제 4 ===
System.out.println("문제4: " + solution4(new ArrayList<>(Arrays.asList(1, 5, 2, 6, 3, 7, 4)), new ArrayList<>(Arrays.asList(2, 5, 3))));
// 5
// === 문제 5 ===
System.out.println("문제5: " + solution5(new ArrayList<>(Arrays.asList(1, 2, 3, 4, 6, 7, 8, 0))));
// 14
// === 문제 6 ===
System.out.println("문제6: " + solution6(new ArrayList<>(Arrays.asList(4, 7, 12)), new ArrayList<>(Arrays.asList(true, false, true))));
// 9
// === 문제 7 ===
System.out.println("문제7: " + solution7(new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5))));
// [1]
System.out.println("문제7: " + solution7(new ArrayList<>(Arrays.asList(1, 3, 2, 4, 2))));
// [1, 2, 3]
// === 문�� 8 ===
System.out.println("문���8: " + solution8(new ArrayList<>(Arrays.asList(1, 8, 3))));
// [8, 3]
System.out.println("문제8: " + solution8(new ArrayList<>(Arrays.asList(10))));
// [-1]
// === 문제 9 ===
System.out.println("문제9: " + solution9(new ArrayList<>(Arrays.asList(3, 1, 2, 3))));
// 2
System.out.println("문제9: " + solution9(new ArrayList<>(Arrays.asList(3, 3, 3, 2, 2, 4))));
// 3
// === 문제 10 ===
System.out.println("��제10: " + solution10(new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6))));
// [6, 5, 4, 3, 2, 1]
}
// 문제 1. 같은 숫자는 싫어
public static ArrayList<Integer> solution1(ArrayList<Integer> arr) {
ArrayList<Integer> result = new ArrayList<>();
for (int i = 0; i < arr.size(); i++) {
if (i == 0 || !arr.get(i).equals(arr.get(i - 1))) {
result.add(arr.get(i));
}
}
return result;
}
// 문�� 2. 나누어 떨어지는 숫자 배열
public static ArrayList<Integer> solution2(ArrayList<Integer> arr, int divisor) {
ArrayList<Integer> result = new ArrayList<>();
for (int num : arr) {
if (num % divisor == 0) {
result.add(num);
}
}
if (result.isEmpty()) {
result.add(-1);
return result;
}
Collections.sort(result);
return result;
}
// 문제 3. 두 개 뽑아서 더하기
public static ArrayList<Integer> solution3(ArrayList<Integer> numbers) {
ArrayList<Integer> result = new ArrayList<>();
for (int i = 0; i < numbers.size(); i++) {
for (int j = i + 1; j < numbers.size(); j++) {
int sum = numbers.get(i) + numbers.get(j);
if (!result.contains(sum)) {
result.add(sum);
}
}
}
Collections.sort(result);
return result;
}
// 문제 4. K��째수
public static int solution4(ArrayList<Integer> arr, ArrayList<Integer> command) {
int i = command.get(0);
int j = command.get(1);
int k = command.get(2);
ArrayList<Integer> sliced = new ArrayList<>(arr.subList(i - 1, j));
Collections.sort(sliced);
return sliced.get(k - 1);
}
// 문제 5. 없는 숫자 더하기
public static int solution5(ArrayList<Integer> numbers) {
int total = 45;
for (int num : numbers) {
total -= num;
}
return total;
}
// 문제 6. 음양 더하기
public static int solution6(ArrayList<Integer> absolutes, ArrayList<Boolean> signs) {
int sum = 0;
for (int i = 0; i < absolutes.size(); i++) {
if (signs.get(i)) {
sum += absolutes.get(i);
} else {
sum -= absolutes.get(i);
}
}
return sum;
}
// 문제 7. 모의고사
public static ArrayList<Integer> solution7(ArrayList<Integer> answers) {
int[] p1 = {1, 2, 3, 4, 5};
int[] p2 = {2, 1, 2, 3, 2, 4, 2, 5};
int[] p3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
int s1 = 0, s2 = 0, s3 = 0;
for (int i = 0; i < answers.size(); i++) {
if (answers.get(i) == p1[i % p1.length]) s1++;
if (answers.get(i) == p2[i % p2.length]) s2++;
if (answers.get(i) == p3[i % p3.length]) s3++;
}
int max = Math.max(s1, Math.max(s2, s3));
ArrayList<Integer> result = new ArrayList<>();
if (s1 == max) result.add(1);
if (s2 == max) result.add(2);
if (s3 == max) result.add(3);
return result;
}
// 문제 8. 제일 작은 수 제거하기
public static ArrayList<Integer> solution8(ArrayList<Integer> arr) {
if (arr.size() == 1) {
return new ArrayList<>(Arrays.asList(-1));
}
int min = Collections.min(arr);
ArrayList<Integer> result = new ArrayList<>();
for (int num : arr) {
if (num != min) {
result.add(num);
}
}
return result;
}
// 문��� 9. 폰켓몬
public static int solution9(ArrayList<Integer> nums) {
int pick = nums.size() / 2;
ArrayList<Integer> types = new ArrayList<>();
for (int num : nums) {
if (!types.contains(num)) {
types.add(num);
}
}
return Math.min(types.size(), pick);
}
// 문제 10. 리스트 뒤집기
public static ArrayList<Integer> solution10(ArrayList<Integer> arr) {
ArrayList<Integer> result = new ArrayList<>();
for (int i = arr.size() - 1; i >= 0; i--) {
result.add(arr.get(i));
}
return result;
}
}