1) 두 배열 합치기
설명
package com.company.array;
import java.util.ArrayList;
import java.util.Scanner;
public class ArraySum {
public static void main(String[] args) {
ArraySum arraySum = new ArraySum();
Scanner in = new Scanner(System.in);
int array1Count = in.nextInt();
int [] array1 = new int[array1Count];
for (int i=0; i<array1Count; i++) {
array1[i] = in.nextInt();
}
int array2Count = in.nextInt();
int [] array2 = new int[array2Count];
for (int i=0; i<array2Count; i++) {
array2[i] = in.nextInt();
}
arraySum.solution(array1Count, array2Count, array1, array2);
}
void solution(int array1Count, int array2Count, int [] array1, int [] array2) {
ArrayList<Integer> answer = new ArrayList<>();
int p1 = 0, p2 = 0;
while (p1<array1Count && p2<array2Count) {
if (array1[p1] < array2[p2]) {
answer.add(array1[p1]);
p1++;
} else {
answer.add(array2[p2]);
p2++;
}
}
while(p1<array1Count) {
answer.add(array1[p1]);
p1++;
}
while(p2<array2Count) {
answer.add(array2[p2]);
p2++;
}
for (Integer integer : answer) {
System.out.print(integer+" ");
}
}
}
Java
복사
2) 공통원소 구하기
설명
package com.company.twopointer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class FindCommonElements {
public static void main(String [] args) {
FindCommonElements findCommonElements = new FindCommonElements();
Scanner in = new Scanner(System.in);
int array1Count = in.nextInt();
int [] array1 = new int[array1Count];
for (int i=0; i<array1Count; i++) {
array1[i] = in.nextInt();
}
int array2Count = in.nextInt();
int [] array2 = new int[array2Count];
for (int i=0; i<array2Count; i++) {
array2[i] = in.nextInt();
}
findCommonElements.solution(array1Count, array2Count, array1, array2);
}
void solution(int array1Count, int array2Count, int [] array1, int [] array2) {
ArrayList<Integer> answer = new ArrayList<>();
int p1 = 0, p2 = 0;
Arrays.sort(array1);
Arrays.sort(array2);
while(p1 < array1Count && p2 < array2Count) {
if (array1[p1] == array2[p2]) {
answer.add(array1[p1]);
p1++;
p2++;
} else if (array1[p1] < array2[p2]) {
p1++;
} else {
p2++;
}
}
for (Integer integer : answer) {
System.out.print(integer + " ");
}
}
}
Java
복사
3) 최대매출
설명
package com.company.twopointer;
import java.util.Scanner;
public class MaximumSales {
public static void main(String [] args) {
MaximumSales maximumSales = new MaximumSales();
Scanner in = new Scanner(System.in);
int maxDay = in.nextInt();
int cnsctDays = in.nextInt();
int [] sales = new int[maxDay];
for (int i=0; i<maxDay; i++) {
sales[i] = in.nextInt();
}
maximumSales.solution(maxDay, cnsctDays, sales);
}
void solution(int maxDay, int cnsctDays, int [] sales) {
int sum = 0;
int answer = 0;
// 초기값
for (int i=0; i<cnsctDays; i++) {
sum += sales[i];
}
for (int i=cnsctDays; i<maxDay; i++) {
sum = sum - sales[i-cnsctDays] + sales[i];
answer = Math.max(answer, sum);
}
}
}
Java
복사
4. 연속 부분순열
설명
package com.company.twopointer;
import java.util.Scanner;
public class ContinuousSubsequence {
public static void main(String [] args) {
ContinuousSubsequence continuousSubsequence = new ContinuousSubsequence();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int [] numbers = new int[n];
for (int i=0; i<n; i++) {
numbers[i] = in.nextInt();
}
continuousSubsequence.solution(n, m, numbers);
}
void solution(int n, int m, int [] numbers) {
int sum=0, lt=0;
int count = 0;
for (int rt=0; rt<n; rt++) {
sum += numbers[rt];
if (sum == m) {
count++;
}
while (m<=sum) {
sum -= numbers[lt];
lt++;
if (sum == m) {
count++;
}
}
}
System.out.println(count);
}
}
Java
복사
5) 연속된 자연수의 합
설명
package com.company.twopointer;
import java.util.Scanner;
public class SumConsecutiveNumbers {
public static void main(String [] args) {
SumConsecutiveNumbers sumConsecutiveNumbers = new SumConsecutiveNumbers();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int mok = n/2;
int mod = n%2;
int arrCount = mok+mod;
int [] numbers = new int[arrCount];
for (int i=0; i<arrCount; i++) {
numbers[i] = i;
}
sumConsecutiveNumbers.solution(n, numbers, arrCount);
}
void solution(int n, int [] numbers,int count) {
int sum = 0, lt = 0, answer = 0;
for (int rt=0; rt<count; rt++) {
sum += numbers[rt];
if (sum == n) {
answer ++;
}
while (n<=sum) {
sum -= numbers[lt];
lt++;
if (sum == n) {
answer ++;
}
}
}
System.out.println(answer);
}
}
Java
복사
6) 최대 길이 연속부분수열
설명
package com.company.twopointer;
import java.util.Scanner;
public class MaximumSales {
public static void main(String [] args) {
MaximumSales maximumSales = new MaximumSales();
Scanner in = new Scanner(System.in);
int count = in.nextInt();
int k = in.nextInt();
int [] arr = new int[count];
for (int i=0; i<count; i++) {
arr[i] = in.nextInt();
}
maximumSales.solution(count, k, arr);
}
void solution(int count, int k, int [] arr) {
int lt = 0;
int kCount = 0;
int answer = 0;
for (int rt = 0; rt<count; rt++) {
if (arr[rt] == 0) {
kCount ++;
}
while (k < kCount) {
if (arr[lt] == 0) {
kCount --;
}
lt++;
}
answer = Math.max(answer, rt-lt+1);
}
System.out.println(answer);
}
}
Java
복사