본문 바로가기

알고리즘55

trapping rain water https://leetcode.com/problems/trapping-rain-water Trapping Rain Water - LeetCode Can you solve this real interview question? Trapping Rain Water - Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. Example 1: [https://assets.leetcode.com/upl leetcode.com 1. two pointer로 접근하기 왼쪽과 오른쪽에서 각각 이동한다. 왼쪽 방향의 최대치.. 2023. 10. 21.
two sum https://leetcode.com/problems/two-sum 최종 결과에서 for문으로 nums를 순회하면서 현재 인덱스 값을 뺀값을 목표값으로 둔다. 예를 들어, 최종이 8이라면 현재 nums[0] => 2의 목표값은 6이다. 해쉬맵에는 [6, 0] 으로 넣어둔다. for를 순회하면서 nums[1] = 6이고 해쉬맵 키에 6이 있다면 6키의 인덱스와 현재 인덱스인 1를 배열로 리턴하면된다. public int[] solve(int[] nums, int target) { Map idx = new HashMap(); for (int i = 0; i < nums.length; i++) { if (idx.containsKey(nums[i])){ int mapValue = idx.get(nums[i]);.. 2023. 10. 2.
Longest Palindrome Substring https://leetcode.com/problems/longest-palindromic-substring/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 회문은 짝수로 구성될 수도 있고, 홀수로 구성될 수 있다. 그래서 두가지 케이스로 나눠서 최대값을 찾는다. bab지점에 왔다면 좌우 한칸씩 이동하면 cbabc 이고 다시한번 이동하면 dcbabcd가 된다... 2023. 10. 1.
그룹 애너그램 https://leetcode.com/problems/group-anagrams LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com public class GroupAnagram { public List solve(String[] strs) { // 정렬을 한다. Map result = new HashMap(); for (String str : strs) { ch.. 2023. 9. 29.