Replies: 3 comments
-
1️⃣ 롤케이크 자르기function solution(topping) {
let result = 0; // 공평하게 자르는 방법의 수
const right = new Map();
for(const t of topping) {
right.set(t, (right.get(t) || 0) + 1);
}
const left = new Map();
// 왼쪽으로 하나씩 옮기면서 비교
for(let i = 0; i < topping.length - 1; i++) {
const t = topping[i];
// 오른쪽에서 하나 빼기
right.set(t, right.get(t) - 1);
if(right.get(t) === 0) right.delete(t); // 다 쓴 토핑 삭제
// 왼쪽에 추가
left.set(t, (left.get(t) || 0) + 1);
if(left.size === right.size) result++;
}
return result;
}2️⃣ 요격 시스템function solution(targets) {
// [1, 4] [4, 5] [3, 7] [4, 8] [5, 12] [11, 13] [10, 14]
// 끝나는 지점 오름차순 정렬
targets.sort((a, b) => a[1] - b[1]);
let count = 0;
let point = -1; // 요격한 포인트 없음
for(const [start, end] of targets) {
if(start >= point) {
count++;
point = end;
}
}
return count;
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
롤 케이크 자르기해결 방법
class Solution {
public int solution(int[] topping) {
int answer = 0;
// 1~10000번 토핑 종류이므로 배열 사용
int[] left = new int[10001]; // 왼쪽 토핑 종류별 개수
int[] right = new int[10001]; // 오른쪽 토핑 종류별 개수
int leftTypeCount = 0; // 왼쪽 토핑 종류 수
int rightTypeCount = 0; // 오른쪽 토핑 종류 수
// 1. 먼저 전체를 오른쪽에 다 넣어두기
for (int t : topping) {
if (right[t] == 0) {
rightTypeCount++; // 처음 등장한 토핑
}
right[t]++;
}
// 2. 하나씩 왼쪽으로 옮겨가며 비교
for (int i = 0; i < topping.length; i++) {
int t = topping[i];
// 왼쪽으로 추가
if (left[t] == 0) {
leftTypeCount++;
}
left[t]++;
// 오른쪽에서 제거
right[t]--;
if (right[t] == 0) {
rightTypeCount--;
}
// 공평하게 나뉜 경우
if (leftTypeCount == rightTypeCount) {
answer++;
}
}
return answer;
}
}요격 시스템해결 방법
import java.util.*;
class Solution {
public int solution(int[][] targets) {
// 1. 끝나는 시점을 기준으로 오름차순 정렬
Arrays.sort(targets, (a, b) -> a[1] - b[1]);
int count = 0; // 요격 미사일 수
int lastShot = 0; // 마지막 요격 위치
for (int[] target : targets) {
int start = target[0];
int end = target[1];
// 현재 미사일이 아직 요격 안된 경우
if (start >= lastShot) {
count++;
lastShot = end; // [start, end) 범위니까 end 에서 쏘면 됨
}
}
return count;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
🧩 [ 롤케이크 자르기 ]⚙️ 문제 풀이
class Solution {
public int solution(int[] toppings) {
int[] cake = new int[10001];
int cake1Topping = 0;
// 토핑이 몇 종류 몇개 인지 파악 하기
for(int i = 0; i < toppings.length; i++){
int topping = toppings[i];
if(cake[topping] == 0){
cake1Topping++;
}
cake[topping]++;
}
int[] cake2 = new int[10001];
int cake2Topping = 0;
int total = 0;
// 케이크 나누기
for(int i = 0; i < toppings.length; i++){
int topping = toppings[i];
cake[topping]--;
if(cake[topping] == 0){
cake1Topping--;
}
if(cake2[topping] == 0){
cake2Topping++;
cake2[topping]++;
}
// 자른 케이크의 토핑 개수가 같다면
if(cake1Topping == cake2Topping){
total++;
}
}
return total;
}
}[ Map 풀이 ]class Solution {
public int solution(int[] toppings) {
Map<Integer, Integer> yourToppingMap = new HashMap<>();
// input 처리
for(int i = 0; i < toppings.length; i++){
int topping = toppings[i];
yourToppingMap.put(topping, yourToppingMap.getOrDefault(topping, 0) + 1);
}
// 내 케이크
Map<Integer, Integer> myToppingMap = new HashMap<>();
int count = 0;
for(int i = toppings.length - 1; i >= 0; i--){
int topping = toppings[i];
myToppingMap.put(topping, myToppingMap.getOrDefault(topping, 0) + 1);
// 상대편 토핑 줄이기
int nextTopping = yourToppingMap.get(topping) - 1;
if(nextTopping == 0){
yourToppingMap.remove(topping);
} else {
yourToppingMap.put(topping, nextTopping);
}
if(yourToppingMap.size() == myToppingMap.size()){
count++;
}
}
return count;
}
}[ 요격 시스템 ]⚙️ 문제 풀이
class Solution {
public int solution(int[][] targets) {
Arrays.sort(targets, (a,b) -> a[0] - b[0]);
int count = 1;
int end = targets[0][1];
for (int row = 1; row < targets.length; row++) {
int start = targets[row][0];
int currentEnd = targets[row][1];
if (end <= start) {
count++;
end = currentEnd;
} else {
end = Math.min(end, currentEnd);
}
}
return count;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
📢 이번 주 알고리즘 스터디 문제
이번 주에는 총 4문제를 풉니다.
(정답률은 프로그래머스 기준)
📌 문제 목록 [ 2단계 ]
🗓️ 발표
🚨 벌금 규칙
🔥 이번 주도 화이팅입니다!
Beta Was this translation helpful? Give feedback.
All reactions