목록2025/06 (3)
Mini

https://leetcode.com/problems/majority-element/description/ * 풀이1절반이상인 원소가 반드시 존재함을이용정렬 -> 중간지점의 원소가 다수원소임class Solution { public int majorityElement(int[] nums) { Arrays.sort(nums); return nums[nums.length/2]; }}O(nlgn) * 풀이2투표 알고리즘 이용 O(n)class Solution { public int majorityElement(int[] nums) { int king=nums[0]; int cnt=1; for(var num : nums){ ..

https://www.acmicpc.net/problem/10653* 시도1완탐) 500 C 250 -> 건너뛸 체크포인트 고르기 -> 불가 * 풀이dp(idx, cnt) : idx, 점프횟수 그때 최소비용이때, OX 퀴즈로 하면 안된다.nxt 후보들에대해 한번에 점프하는 식으로 완탐해야 구현이 쉬워진다.정답코드import java.util.*;class Pair { int y; int x; Pair(int y, int x) { this.y = y; this.x = x; }}public class Main { // 인덱스 i, j개 를 스킵 했을때, 그때 최소거리 static int dp[][] = new int[504][504]; stat..

https://leetcode.com/problems/linked-list-cycle/description/풀이1/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public boolean hasCycle(ListNode head) { HashMap m = new HashMap(); while(head!=null){ if(m.containsKey(hea..