leetcode
스도쿠 문제 풀이 정리 (LeetCode 36 & 37)
스도쿠 문제 풀이 정리 (LeetCode 36 & 37)
2025.08.311. Valid Sudoku (문제 36)문제9×9 스도쿠 보드가 주어짐.빈 칸은 '.'로 표시.규칙:각 행(row) 은 1~9 중복 없음각 열(col) 은 1~9 중복 없음각 3×3 박스도 중복 없음아직 완성되지 않아도, 현재 보드가 유효한지 검사.풀이 아이디어행, 열, 박스별로 이미 등장한 숫자를 기록.중복이 나타나면 false, 끝까지 없으면 true.Set을 활용하면 중복 검사 로직이 매우 간단해짐.Java 코드 (Set 버전)import java.util.*;class Solution { public boolean isValidSudoku(char[][] board) { List> rows = new ArrayList(); List> cols = new ArrayL..
[코딩 테스트] LeetCode - Count Hills and Valleys in an Array (Java)
[코딩 테스트] LeetCode - Count Hills and Valleys in an Array (Java)
2025.07.27문제 링크: https://leetcode.com/problems/count-hills-and-valleys-in-an-array난이도: Easy ~ Medium태그: 배열, 시뮬레이션, 패턴 인식문제 설명정수 배열 nums가 주어질 때, "Hill(봉우리)"과 "Valley(골짜기)"의 개수를 구하라는 문제다.Hill: 현재 값이 양 옆 값보다 클 때Valley: 현재 값이 양 옆 값보다 작을 때조건: 연속된 동일 값들은 하나의 구간으로 간주해야 한다.예시Input: [2,4,1,6,5]Output: 3 초안 풀이 (좌우 확장 비교 방식)처음엔 다음과 같은 방식으로 풀었다:중복 값은 무시현재 인덱스를 기준으로 왼쪽과 오른쪽으로 같은 값을 건너뛰며 비교현재 값이 양쪽보다 크거나 작으면 Hill 또는 Va..
35. Search Insert Position
35. Search Insert Position
2024.06.07문제 정의 주어진 정렬된 배열에서 목표 값을 찾고, 찾지 못하면 목표 값이 삽입될 인덱스를 반환하는 문제입니다. 이 문제는 O(log n) 시간 복잡도로 해결해야 합니다. https://leetcode.com/problems/search-insert-position/description/Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity...
979. Distribute Coins in Binary Tree
979. Distribute Coins in Binary Tree
2024.05.19You are given the root of a binary tree with n nodes where each node in the tree has node.val coins. There are n coins in total throughout the whole tree.In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent.Return the minimum number of moves required to make every node have exactly one coin.Example 1..
밀렸던 릿코드 풀기 (232, 150, 739, 2966, 1291)
밀렸던 릿코드 풀기 (232, 150, 739, 2966, 1291)
2024.02.03릿코드 문제가 영어라 접근하기 쉽지 않지만 그래도 풀어보면 자료구조에 대해 이해할 수 있어서 좋은점이 많다. 첫문제는 비교적 쉬운 문제이다. 232. Implement Queue using Stacks 처음에 접근할 때는 HashSet 을 사용해보려고 했는데 순서가 보장이 안되더라.. 왜냐하면 내부적으로 해시테이블을 구현하는데 해시함수에 의해서 순서가 달라진다. LinkedHashSet을 이용하면 풀 수 있을거 같다.(+ 정렬되어있다면 TreeSet을 이용할 수 있다. 추가, 삭제, 조회 작업시 O(logn) 으로 수행된다. ) 근데 접근하기 편한 ArrayList 를 써서 풀었다. 😎 Time Complexity push - O(1) pop - O(n) //비효율적 peek - O(1) empty - ..
LeetCode : Partition Label
LeetCode : Partition Label
2021.07.07https://leetcode.com/problems/partition-labels/submissions/ 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 class Solution { public List partitionLabels(String s) { //last Index 넣어라 HashMap hm = new HashMap(); String[] splitMsg = s.split(""); for(int i=0; i