Array
-
[string] reorder data in log filesAlgorithm/LeetCode 2025. 3. 9. 10:12
✅ Problemhttps://leetcode.com/problems/reorder-data-in-log-files/description/ ✅ Approach & Solution방식)더보기문제 조건로그의 가장 앞부분은 식별자로서, 순서에 영향을 끼치지 않는다.문자로 구성된 로그가 숫자 로그보다 앞에 오며, 문자 로그는 사전순으로 한다.문자가 동일한 경우에는 식별자순으로 한다.숫자 로그는 입력 순서대로 한다.풀이로그 나누기 (문자 로그, 숫자 로그)로그 정렬 (문자 로그만 정렬 수행)로그 합친 후 반환class Solution { public String[] reorderLogFiles(String[] logs) { List digitLogs = new ArrayList(); ..
-
[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..
-
[array] best time to buy and sell stockAlgorithm/LeetCode 2025. 2. 15. 16:13
✅ Problemhttps://leetcode.com/problems/best-time-to-buy-and-sell-stock/해당 문제는 배열의 값을 그래프로 나열해 시각화 하면 해결 방식 떠올릴 때 도움 됨 ✅ Approach & Solution방식1) 브루트 포스, 시간복잡도 : O(n^2)더보기모든 경우의 수익을 구하게 됨class Solution { public int maxProfit(int[] prices) { int maxProfit = 0; for (int i = 0; i 방식2) 저점과 현재 값과의 차이 계산, 시간복잡도 : O(n)더보기저점과 최대 수익 계산을 for문 안에서 연달아 수행class Solution { public int maxPro..
-
[array] product of array except selfAlgorithm/LeetCode 2025. 2. 8. 17:46
✅ Problemhttps://leetcode.com/problems/product-of-array-except-self/ ✅ Approach & Solution방식1) 시간복잡도 : O(n)더보기자기 자신을 제외하고 자신의 왼쪽 곱셈 결과와 오른쪽 곱셈 결과 곱하기class Solution { public int[] productExceptSelf(int[] nums) { int[] result = new int[nums.length]; // 자기 자신의 왼쪽 곱셈 결과 포함시키기 int p = 1; // 자기 자신은 제외시켜야 하므로 1로 초기화 for (int i = 0; i = 0; i--) { result[i] *= p;..