제목 | 링크 | 난이도 | 풀이일 | 코드 |
[011] 274. H-Index | LINK | Medium | 2024.04.21 | class Solution { public int hIndex(int[] citations) { int n = citations.length; int[] bucket = new int[n+1]; for (int i = 0; i < n; i++) { int c = citations[i]; if ( c>= n) { bucket[n]++; } else { bucket[c]++; } } int cnt = 0; for (int i = n; i >= 0; i--) { cnt += bucket[i]; if (cnt >= i) { return i; } } return 0; } } |
[022] 380. Insert Delete GetRandom O(1) | LINK | Easy | 2024.04.21 | class RandomizedSet { Set<Integer> randomizedSet; public RandomizedSet() { if (randomizedSet == null) { randomizedSet = new HashSet(); } } public boolean insert(int val) { if (randomizedSet.contains(val)) { return false; } else { randomizedSet.add(val); return true; } } public boolean remove(int val) { if (randomizedSet.contains(val)) { randomizedSet.remove(val); return true; } else { return false; } } public int getRandom() { Random random = new Random(); int n = random.nextInt(randomizedSet.size()); Object[] intArray = randomizedSet.toArray(); return (int)intArray[n]; } } |
[023] 238. Product of Array Except Self | LINK | Medium | 2024.05.11 | class Solution { public int[] productExceptSelf(int[] nums) { int n = nums.length; int[] left = new int[n]; int[] right = new int[n]; int[] answer = new int[n]; left[0] = 1; for (int i = 1; i < n; i++) { left[i] = left[i-1] * nums[i-1]; } right[n-1] = 1; for (int j = n-2; j >= 0; j--) { right[j] = right[j+1] * nums[j+1]; } for (int i = 0; i < n; i++) { answer[i] = left[i] * right[i]; } return answer; } } |
'코딩테스트 > Leetcode' 카테고리의 다른 글
Leetcode 문제풀이 001 - 010 (0) | 2024.04.14 |
---|---|
Leetcode 용어 (0) | 2024.04.14 |