Banner

My Tech Blog (Algorithm)

๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…๐Ÿ’ก 2. ์ ‘๊ทผ๋ฐฉ์‹์ด ๋ฌธ์ œ๋Š” ๋„ˆ๋ฌด ์–ด๋ ค์›Œ์„œ ์Šค์Šค๋กœ ํ’€๊ธฐ ํž˜๋“ค์–ด์„œ ๊ฒ€์ƒ‰์˜ ๋„์›€์„ ๋ฐ›์Œ.์ฃผ์–ด์ง„ ๋ฌธ์ œ๋Š” ๊ทธ๋ž˜ํ”„ ์ด๋ก ์˜ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ(MST, Minimum Spanning Tree) ๋ฌธ์ œ์ด๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ „์ œ์ง€์‹- Union-Find(์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ) ์ž๋ฃŒ๊ตฌ์กฐ- Kruskal's Algorithm (ํฌ๋ฃจ์Šค์นผ) ์•Œ๊ณ ๋ฆฌ์ฆ˜ Kruskal's Algorithm (ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜)ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์œ„์— ๋งํ•œ ์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ(MST)๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ๋ฌธ์ œ์˜ ๋ถ„๋ฅ˜ ๋‹ต๊ฒŒ greedy์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ฒฐ์ •์˜ ์ˆœ๊ฐ„๋งˆ๋‹ค ์ตœ์„ ์˜ ๊ฒฐ์ •์„ ํ•จ์œผ๋กœ์„œ ์ตœ์ข…์ ์ธ ๋‹ต์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ชจ๋“  ์ •์ ์„ ์ตœ์†Œ ๋น„์šฉ์œผ๋กœ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ•ต์‹ฌ์€ ๋ชจ๋“  ๊ฐ„์„ ์„ ๊ฐ€์ค‘์น˜ ๊ธฐ์ค€(์—ฌ๊ธฐ์„œ๋Š” ๋‹ค๋ฆฌ ๊ฐœ์„ค..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…๐Ÿ’ก 2. ์ ‘๊ทผ๋ฐฉ์‹์ž…์ถœ๋ ฅ ์˜ˆ๋กœ ์ฃผ์–ด์ง„ route ๋ฐฐ์—ด์„ ๋ง‰๋Œ€๊ทธ๋ž˜ํ”„๋กœ ๊ทธ๋ ค ๋ดค๋‹ค. ์ตœ์†Œํ•œ์˜ ์นด๋ฉ”๋ผ๋ฅผ ๋ฐฐ์น˜ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๊ตฌ๊ฐ„ ์ข…๋ฃŒ ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ์ฐจ๋Ÿ‰์ด ๊ตฌ๊ฐ„์—์„œ ์นด๋ฉ”๋ผ๋ฅผ ๋‹จ ํ•œ ๋ฒˆ๋งŒ์ด๋ผ๋„ ๋งŒ๋‚˜๋ฉด ๋จ์ข…๋ฃŒ ์ง€์ ์—์„œ ๋‹ค์Œ ๊ตฌ๊ฐ„๊ณผ ๊ฒน์น˜๊ฒŒ ๋˜๋ฏ€๋กœ ์ตœ์†Œํ•œ์˜ ์นด๋ฉ”๋ผ๋ฅผ ๋ฐฐ์น˜๊ฐ€๋Šฅ์นด๋ฉ”๋ผ ๋ฐฐ์น˜๋Š” ๋„๋กœ์˜ ๊ฐ€์žฅ ์™ผ์ชฝ ๋๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉฐ ์‹œ์ž‘๋œ๋‹ค.์ž…์ถœ๋ ฅ ์˜ˆ์‹œ์—์„œ MIN_VALUE์ธ -20 ์ง€์ ๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐ€๋ฉฐ ์ข…๋ฃŒ๊ตฌ๊ฐ„๊ณผ ์‹œ์ž‘๊ตฌ๊ฐ„์ด ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์— ์นด๋ฉ”๋ผ๊ฐ€ ๋ฐฐ์น˜๋œ๋‹ค.๊ตฌ๊ฐ„ ์ค‘ ๊ฐ€์žฅ ์ฒ˜์Œ์œผ๋กœ ๋งŒ๋‚˜๋Š” ์ข…๋ฃŒ ์œ„์น˜๋Š” -15์ด๋‹ค.๋”ฐ๋ผ์„œ ์ด ์œ„์น˜์— ์ฒซ ๋ฒˆ์งธ ์นด๋ฉ”๋ผ๋ฅผ ์„ค์น˜ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  -13์ง€์ ์—์„œ ํ˜„์žฌ ๊ตฌ๊ฐ„๊ณผ ๋‹ค์Œ ๊ตฌ๊ฐ„์ด ๋งŒ๋‚˜์ง€๋งŒ ์ด๋ฏธ ํ•ด๋‹น ๊ตฌ๊ฐ„์—๋Š” ์นด๋ฉ”๋ผ๊ฐ€ ์„ค์น˜ ์™„๋ฃŒ ๋˜์—ˆ์œผ๋ฏ€๋กœ ์Šคํ‚ตํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ ๊ตฌ๊ฐ„์ธ [-..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โŒ 2. ์‹คํŒจํ•œ ์‹œ๋„์ฝ”๋“œ๋Š” ์ž‘๋™ํ•˜์ง€๋งŒ ์ •๋‹ต ์ฒ˜๋ฆฌ X์ด์œ : CAR_ID ์ค‘๋ณต๋จSELECT A.Car_idFROM Car_rental_company_car A JOIN Car_rental_company_rental_history B ON A.Car_id = B.Car_idWHERE A.Car_type = '์„ธ๋‹จ' AND B.Start_date BETWEEN '2022-10-01' AND '2022-10-31'ORDER BY A.Car_id DESC;โญ 3. ์ •๋‹ต์ฝ”๋“œCAR_ID ์ค‘๋ณต์ด ์—†์–ด์•ผ ํ•˜๋ฉฐ -> DISTINCT๋Œ€์—ฌ ๊ธฐ๋ก์ด ์žˆ๋Š” -> ON A.CAR_ID = B.CAR_IDSELECT DISTINCT(A.Car_id)FROM ..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โญ 2. ์ •๋‹ต์ฝ”๋“œ์ถœ๊ณ ์ผ์ด 2022-05-01 ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด '์ถœ๊ณ ์™„๋ฃŒ'๋Šฆ์œผ๋ฉด ์ถœ๊ณ  ๋Œ€๊ธฐ, ์—†์œผ๋ฉด ์ถœ๊ณ  ๋ฏธ์ •์ด๊ณ  ํ•ด๋‹น ์ปฌ๋Ÿผ๋ช…์€ ์ถœ๊ณ ์—ฌ๋ถ€๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์„ค์ •์–ด๋ ค์šด ๋ถ€๋ถ„์€ ์—†๋Š” ์‰ฌ์šด ๋ฌธ์ œ๐ŸฅณSELECT Order_id, Product_id, DATE_FORMAT(Out_date,'%Y-%m-%d') AS Out_date, CASE WHEN Out_date '2022-05-01' THEN '์ถœ๊ณ ๋Œ€๊ธฐ' ELSE '์ถœ๊ณ ๋ฏธ์ •'END AS ์ถœ๊ณ ์—ฌ๋ถ€FROM Food_orderORDER BY Order_id;
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โญ 2. ์ •๋‹ต์ฝ”๋“œSELECT Animal_id, name, CASE WHEN Sex_upon_intake LIKE '%Neutered%' OR Sex_upon_intake LIKE '%Spayed%' THEN 'O' ELSE 'X' END AS ์ค‘์„ฑํ™”FROM ANIMAL_INSORDER BY ANIMAL_ID; ๐Ÿ˜Š 3. TMIANSI SQL ํ‘œ์ค€์—์„œ๋Š” ALIAS ์— ์ž‘์€๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š์ง€๋งŒ ์จ๋„ ์ƒ๊ด€์€ ์—†๋‹ค. (๋‚˜๋Š” ๋”ฐ์˜ดํ‘œ ์ผ์Œ)SELECT Animal_id, name, CASE WHEN Sex_upon_intake LIKE '%Neutered%' ..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โŒ ์‹คํŒจํ•œ ์‹œ๋„์ด๋ฆ„์˜ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋ถ€๋ถ„์ด  ๋ฌธ์ œ์˜ ํ•ต์‹ฌ 1์ฐจ ์‹œ๋„ ์‹คํŒจWHERE ์ ˆ์—์„œ `LOWER(NAME)` ํ•ด ์ฃผ์—ˆ์œผ๋‚˜ ํ‹€๋ฆผ์ฟผ๋ฆฌ๋Š” ๋Œ์•„๊ฐ€๊ณ  ๊ฒฐ๊ณผ๋Š” ๋‚˜์˜ค์ง€๋งŒ ๊ฒฐ๊ตญ WHERE์ ˆ์—์„œ LOWER()๊ณผ LIKE๋ฅผ ํ•จ๊ป˜ ์“ด ๊ฒƒ์€ ๋ถˆํ•„์š”ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. MySQL์—์„œ LIKE ์—ฐ์‚ฐ์ž๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜์ง€ ์•Š๋Š”๋‹ค. SELECT ANIMAL_ID, NAMEFROM ANIMAL_INSWHERE LOWER(ANIMAL_TYPE) = 'dog' AND LOWER(NAME) LIKE '%el%'ORDER BY NAME DESC; 2์ฐจ ์‹œ๋„ ์‹คํŒจ์ด๋ฒˆ์—๋Š” ORDER BY์ ˆ์— LOWER() ํ•จ์ˆ˜๋ฅผ ์จ์„œ ์ •๋ ฌํ–ˆ๋”๋‹ˆ ํ‹€๋ ธ๋‹ค๊ณ  ํ•จ.WHERE ์ ˆ์ด๋ž‘ ์ •๋ ฌ์ด๋ž‘ ๋ณ„๊ฐœ๋‹ˆ๊นŒ ์ •๋ ฌ์—๋Š” LOW..
๐Ÿ“‘ 1. ํŠน์ • ์˜ต์…˜์ด ํฌํ•จ๋œ ์ž๋™์ฐจ ๋ฆฌ์ŠคํŠธ ๊ตฌํ•˜๊ธฐ ๋ฌธ์ œCAR_RENTAL_COMPANY_CAR ํ…Œ์ด๋ธ”์—์„œ '๋„ค๋น„๊ฒŒ์ด์…˜' ์˜ต์…˜์ด ํฌํ•จ๋œ ์ž๋™์ฐจ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ๊ฒฐ๊ณผ๋Š” ์ž๋™์ฐจ ID๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”. โญ ์ •๋‹ต์ฝ”๋“œSELECT * FROM CAR_RENTAL_COMPANY_CARWHERE OPTIONS LIKE '%๋„ค๋น„๊ฒŒ์ด์…˜%'ORDER BY CAR_ID DESC;๐Ÿ“‘ 2. ์ž๋™์ฐจ ๋Œ€์—ฌ ๊ธฐ๋ก์—์„œ ์žฅ๊ธฐ/๋‹จ๊ธฐ ๋Œ€์—ฌ ๊ตฌ๋ถ„ํ•˜๊ธฐ ๐Ÿ“Œ ํ‹€๋ฆฐ์ฝ”๋“œSELECT HISTORY_ID, CAR_ID, DATE_FORMAT(START_DATE, '%Y-%m-%d'), DATE_FORMAT(END_DATE, '%Y-%m-%d'), CASE WHEN DATEDIFF(END_DATE..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…๐Ÿ’ก 2. ์ ‘๊ทผ๋ฐฉ์‹ ๋ฌธ์ œ ์ œํ•œ์กฐ๊ฑด1. ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ ๋‘๋ช…๊นŒ์ง€ ๋ณดํŠธ์— ํƒœ์šธ ์ˆ˜ ์žˆ์Œ2. ๋ชธ๋ฌด๊ฒŒ ํ•ฉ์ด `limit` ์ดํ•˜์—ฌ์•ผ ํ•จ ๋”ฐ๋ผ์„œ ์ตœ์†Œ๋ณดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ „๋žต์„ ์งœ๋ ค๋ฉด ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜์—ฌ ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ์‚ฌ๋žŒ + ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ ์กฐํ•ฉ์„ ์ง์ง€์–ด์•ผ ํ•จ.๊ฐ€์žฅ ํฐ ๋ชธ๋ฌด๊ฒŒ๋ฅผ ๊ฐ€์ง„ ์‚ฌ๋žŒ์„ ์ตœ๋Œ€ํ•œ ๋นจ๋ฆฌ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ๋„ ๋ณดํŠธ ์‚ฌ์šฉ์„ ์ค„์ผ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.๋งŒ์•ฝ ๋‘ ์‚ฌ๋žŒ์˜ ๋ชธ๋ฌด๊ฒŒ ํ•ฉ์ด limit ์ดํ•˜๋ผ๋ฉด, ํ•œ ๋ณดํŠธ์— ํƒœ์šธ ์ˆ˜ ์žˆ๋‹ค. ํ•ฉ์ด limit์„ ์ดˆ๊ณผํ•œ๋‹ค๋ฉด, ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ์„ ๋ฐ˜๋“œ์‹œ ํ•œ ๋ช…๋งŒ ๋ณดํŠธ์— ํƒœ์›Œ์•ผ ํ•œ๋‹ค.์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ๋‚จ์€ ์‚ฌ๋žŒ๋“ค์„ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ตœ์„ ์˜ ์„ ํƒ์ด๋‹ค. โญ 3. ์ •๋‹ต์ฝ”๋“œimport java.util.*;class Solution {    public int solution(i..
์ธ์ ˆ๋ฏธ์˜€๋˜๊ฒƒ
'Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก
์ƒ๋‹จ์œผ๋กœ