.leetcode(941. Valid Mountain Array) https://leetcode.com/problems/valid-mountain-array/ 1. 2022/04/23 시도 소요시간: 11분 class Solution { public boolean validMountainArray(int[] arr) { if (arr.length each) { isIncreasing = false; } } else { if (prev < each) { return false;.. 알고리즘 풀이 2022.04.23
.leetcode(1346. Check If N and Its Double Exist) https://leetcode.com/problems/check-if-n-and-its-double-exist/ 1. 2022.04.23 시도 소요시간: 6분 class Solution { public boolean checkIfExist(int[] arr) { if (arr == null || arr.length == 0) { return false; } Set numbers = new HashSet(); Set doubledSet = new HashSet(); for (int each: arr) { if (numbers.contains(each)) { if (each == 0) { return true; } continue; } if (doubledSet.contains(each) || doubled.. 알고리즘 풀이 2022.04.23
.leetcode(80. Remove Duplicates from Sorted Array II) https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 1. 2022.04.16 시도 소요시간: 11분 class Solution { public int removeDuplicates(int[] nums) { int prev = 0; int counter = 1; for (int i=1; i2) { continue; } } else { counter=1; } prev++; nums[prev] = nums[i]; } return prev+1; } } 풀이 접근 과정 1탄하고 거의 비슷한 원리다. 다만 2번이라는 최대치가 있으니 그걸 어떻게 처리할지에 대한 로직만 조금 추가해주면 된다. prev에는 마지막 저장된 인덱스를 저장하고, i로 .. 알고리즘 풀이 2022.04.16
.leetcode(88. Merge Sorted Array) https://leetcode.com/problems/merge-sorted-array/ 1. 2022.04.16 시도 소요시간: 11분 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int last1 = m-1; int last2 = n-1; int index = nums1.length-1; while (last1 >=0 && last2>=0) { int l = nums1[last1]; int r = nums2[last2]; if (l =0) { in.. 알고리즘 풀이 2022.04.16
.leetcode(1089. Duplicate Zeros) https://leetcode.com/problems/duplicate-zeros/ 1. 2022.04.16 시도 소요시간: 4분 class Solution { public void duplicateZeros(int[] arr) { for (int i=arr.length-1; i>=0; i--) { if ( arr[i] == 0) { for (int j=arr.length-2; j>=i; j--) { arr[j+1] = arr[j]; } } } } } 풀이 접근 과정 뒤에서부터 접근하면 더 편리하게 0의 배열을 관리할 수 있다. 0을 발견할 때, 그전껄 하나씩 쉬프트하는 방식으로 구현하였다. 느낀점 내가 푼 풀이보다 훨신 좋은 풀이가 있어서 소개한다. O(n)의 시간복잡도로 풀 수 있다. 원리는 0의 갯.. 알고리즘 풀이 2022.04.16
.leetcode(1295. Find Numbers with Even Number of Digits) https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ 1. 2022.04.16 시도 소요시간: 3분 class Solution { public int findNumbers(int[] nums) { int counter=0; for (int each: nums) { if ((each+"").length() % 2 == 0) { counter++; } } return counter; } } 풀이 접근 과정 문자로 치환하고 length를 구한다. 느낀점 수학적으로 접근하는 코드를 보고 옮겨본다. 이것도 흥미롭다. log10을 취함으로써 10진수 자리수를 알게 되고 그 자리수 갯수를 이용해 푸는 방식이다. class Solution { p.. 알고리즘 풀이 2022.04.16
.leetcode(485. Max Consecutive Ones) https://leetcode.com/problems/max-consecutive-ones/ 1. 2022.04.16 시도 소요시간: 3분 class Solution { public int findMaxConsecutiveOnes(int[] nums) { int max = 0; int sum = 0; for(int num: nums) { if (num == 1) { sum++; continue; } max = Math.max (sum, max); sum = 0; } max = Math.max (sum, max); return max; } } 풀이 접근 과정 1이 나올때마다 1을 더해준다. 0이 나오면 최대값을 갱신해준다. 마지막에 최대값을 확인해준다. 느낀점 ... 알고리즘 정리노트: .leetcode(.. 알고리즘 풀이 2022.04.16
.codility(DisappearingPairs) https://app.codility.com/programmers/trainings/4/disappearing_pairs/ 1. 2022.04.15 시도 소요시간: 21분 import java.util.*; class Solution { public String solution(String S) { Stack prev = new Stack(); for (int i=0; i0 && (S.charAt(prev.peek()) == S.charAt(i))) { prev.pop(); continue; } prev.push(i); } String ret =""; while(!prev.isEmpty()) { ret = S.charAt(prev.pop()) + ret; } return ret; } } 풀이 접근 과정 .. 알고리즘 풀이 2022.04.16
.codility(StrSymmetryPoint) https://app.codility.com/programmers/trainings/4/str_symmetry_point/ 1. 2022.04.15 시도 소요시간: 7분 class Solution { public int solution(String S) { if (S.length()%2 == 0) { return -1; } int left = 0; int right = S.length()-1; while(left < right) { if (S.charAt(left) == S.charAt(right)) { left++; right--; continue; } return -1; } return left; } } 풀이 접근 과정 2의 배수는 제외한다. 왼쪽과 오른쪽 변수를 두고 같은지 비교하면서 가운데 점을 .. 알고리즘 풀이 2022.04.16
.codility(FirstUnique) https://app.codility.com/programmers/trainings/4/first_unique/ 1. 2022.04.15 시도 소요시간: 5분 import java.util.*; class Solution { public int solution(int[] A) { Map map = new HashMap(); for (int i=0; i 알고리즘 풀이 2022.04.16