Algorithm
-
[array] merge sorted arrayAlgorithm/LeetCode 2025. 2. 22. 15:39
✅ Problemhttps://leetcode.com/problems/merge-sorted-array/description/ ✅ Approach & Solution공통 : 투 포인터 사용 방식1) 작은 원소부터 넣기 (추가 공간 사용)더보기정답값을 담을 추가 배열(result)을 선언해 작은 원소부터 넣음문제 조건에서 nums1 배열에 값을 담으라고 했으므로 추가 배열(result)에 저장된 값들을 nums1 배열로 옮김nums1 배열은 m + n 개의 원소를 담을 수 있음class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int[] result = new int[m + n]; int p..
-
[linked list] merge two sorted listsAlgorithm/LeetCode 2025. 2. 22. 14:44
✅ Problemhttps://leetcode.com/problems/merge-two-sorted-lists/description/ ✅ Approach & Solution방식1) while문 사용더보기정답 연결리스트에 대한 head, tail 역할 포인터 생성첫 노드 연결에 대한 null 처리를 하지 않도록 하기 위해 dummyNode 생성함/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next)..
-
[linked list] palindrome linked listAlgorithm/LeetCode 2025. 2. 16. 18:50
✅ Problemhttps://leetcode.com/problems/palindrome-linked-list/description/ ✅ Approach & Solution공통리스트의 앞과 뒤의 값을 비교하여 값이 다른 경우 팰린드롬이 아님 방식1) 추가 자료구조 사용더보기1-1) Stack 사용/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.nex..
-
[array] maximum subarrayAlgorithm/LeetCode 2025. 2. 15. 17:10
✅ Problemhttps://leetcode.com/problems/maximum-subarray/description/ ✅ Approach & Solution방식) 시간복잡도 : O(n)더보기부분집합의 합이 양수일 때 : 해당 부분집합 유지부분집합의 합이 음수일 때 : 새로운 부분집합으로 시작class Solution { public int maxSubArray(int[] nums) { int currentSum = 0; int maxSum = Integer.MIN_VALUE; for (int num : nums) { currentSum += num; maxSum = Math.max(maxSum, currentSum);..