Notice
Recent Posts
Recent Comments
Link
«   2026/09   »
일 월 화 수 목 금 토
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
Tags more
Archives
Today
Total
관리 메뉴

seaking110 님의 블로그

사전캠프 13일차 본문

Today I Learned

사전캠프 13일차

seaking110 2024. 12. 9. 18:02

백트래킹 마지막 문제

 

백준 9663번 N-Queen

public class Main {
	public static StringBuilder sb = new StringBuilder();
	public static int []arr;
	public static int n;
	public static int count = 0;
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));	
		n = Integer.parseInt(bf.readLine());
		arr = new int[n];
		dfs(0);
		System.out.println(count);
	}
	public static void dfs(int dep) {
		if(dep==n) {
			count++;
			return;
		}
		for(int i=0;i<n;i++) {
			arr[dep] = i;
			if(possible(dep)) {
				dfs(dep+1);
			}
		}
	}
	public static boolean possible(int col) {
		for(int i = 0 ; i < col ; i++) {
			//행에 일치하는게 있는지 판별
			if(arr[i]==arr[col]) {
				return false;
			}
			//대각선에 일치하는게 있는지 판별
			else if(Math.abs(col-i) == Math.abs(arr[col]-arr[i])) {
				return false;
			}
				
			}
			
			return true;
	}

}

 

이 문제는 1시간동안이나 못 풀어서 구글의 힘을 빌린 문제다...이차원 배열로 굉장히 먼 길을 떠났는데 일차원 배열로 간단하게 풀리는 걸 보니 참...그나저나 이런 식으로 풀리는걸 보니 신기한 느낌이었다. 

 

엑셀보다 쉽고 빠른 SQL 강의

5주차 완강

 

오늘은 마지막 주차 SQL 강의를 들었다

먼저 값이 없는 경우 MYSQL 에서는 0으로 처리 해주기 때문에 아예 처리를 안하려면 is not null 이라는 값을 넣어 아예 null로 처리하는 방법을 배웠다

 

select a.order_id,
       a.customer_id,
       a.restaurant_name,
       a.price,
       b.name,
       b.age,
       b.gender
from food_orders a left join customers b on a.customer_id=b.customer_id
where b.customer_id is not null

 

혹은 if 로 대체 값을 넣거나 coalesce로 대체값을 넣는 방법을 배웠다

 

또한 SQL 업무를 효율적으로 하기 위해 Pivot Table을 만들어서 사용하는데 

 

Pivot Table : 여러 기준으로 데이터를 집계 할 때, 보기 쉽게 배열하는 테이블

 

 

과거 테스트 문제를 풀 때 많이 애먹었던 Window Function의 종류인 Rank와 Sum 또한 배웠는데

 

select cuisine_type,
       restaurant_name,
       rank() over (partition by cuisine_type order by order_count desc) rn,
       order_count
from
(
select cuisine_type, restaurant_name, count(1) order_count
from food_orders
group by 1, 2
) a

 

rank() over (partition by ~ order by ~)  는 반드시 기억 하자!

 

       sum(cnt_order) over (partition by cuisine_type) sum_cuisine, // 카테고리별 합
       sum(cnt_order) over (partition by cuisine_type order by cnt_order) cum_cuisine 카테고리 별 누적 합

 

sum 역시 rank와 유사한 구조로 쉽게 구현 할 수 있으며 누적합 역시 구현 가능하다.

 

마지막으로 날짜 데이터의 여러 포맷에 대해 공부 했는데

 

년 : Y(4) y(2)

월 : M,m

일 : d,e

요일 : w

 

select date(date) date_type,
       date_format(date(date), '%Y') "년",
       date_format(date(date), '%m') "월",
       date_format(date(date), '%d') "일",
       date_format(date(date), '%w') "요일"
from payments

이런식으로 쉽게 뽑아서 쓸 수 있다!

 

SQL 강의는 이걸 끝으로 다 들었고 생각보다 유익하고 기억을 되살려주는 좋은 강의였던거 같다!!

 

내일부턴 시뮬레이션과 구현 쪽 문제를 풀어보려고한다. 이쪽은 정형화 되있는 알고리즘 보단 많이 풀어보는 수 밖에 없어서 일단 풀어보려고한다!

 

'Today I Learned' 카테고리의 다른 글

사전캠프 15일차  (0) 2024.12.11
사전 캠프 14일차  (1) 2024.12.10
사전캠프 12일차  (0) 2024.12.05
사전 캠프 11일차  (1) 2024.12.04
사전캠프 10일차  (0) 2024.12.03