253. Meeting Rooms II

253. Meeting Rooms II Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required. Example 1: Input: intervals = [[0,30],[5,10],[15,20]] Output: 2 Example 2: Input: intervals = [[7,10],[2,4]] Output: 1 Constraints: 1 <= intervals.length <= 10^4 0 <= starti < endi <= 10^6 Solution class Solution { /* [[0,30],[5,10],[15,20]] [ 0,30] [ 5,10] ---- [15,20] (1, 10), (2, 7), (3, 19), (8, 12), (10, 20), (11, 30) */ public int minMeetingRooms(int[][] intervals) { Arrays.sort(intervals, (a,b) -> a[0] - b[0]); PriorityQueue<int[]> pq = new PriorityQueue<int[]>((a,b) -> { return a[1] - b[1]; }); for (int[] interval: intervals) { if (!pq.isEmpty() && pq.peek()[1] <= interval[0]) { pq.poll(); } pq.add(interval); } return pq.size(); } } Solution 2 class Solution { public int minMeetingRooms(int[][] intervals) { Integer min = Integer.MAX_VALUE; Integer max = Integer.MIN_VALUE; for (int[] i: intervals) { min = Math.min(min, i[0]); max = Math.max(max, i[1]); } int[] times = new int[max - min + 1]; for (int[] i: intervals) { times[i[0] - min]++; times[i[1] - min]--; } int count = 0; int result = 0; for (int time: times) { count += time; result = Math.max(result, count); } return result; } }

July 22, 2021 · 1 min · volyx

157. Read N Characters Given Read4

157. Read N Characters Given Read4 Given a file and assume that you can only read the file using a given method read4, implement a method to read n characters. Method read4: The API read4 reads four consecutive characters from file, then writes those characters into the buffer array buf4. The return value is the number of actual characters read. Note that read4() has its own file pointer, much like FILE *fp in C. ...

July 20, 2021 · 4 min · volyx

303. Range Sum Query - Immutable

303. Range Sum Query - Immutable Given an integer array nums, handle multiple queries of the following type: Calculate the sum of the elements of nums between indices left and right inclusive where left <= right. Implement the NumArray class: NumArray(int[] nums) Initializes the object with the integer array nums. int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + … + nums[right]). Example 1: Input ["NumArray", "sumRange", "sumRange", "sumRange"] [[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]] Output [null, 1, -1, -3] Explanation NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]); numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1 numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1 numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3 Constraints: ...

July 20, 2021 · 2 min · volyx

345. Reverse Vowels of a String

1482. Minimum Number of Days to Make m Bouquets Given a string s, reverse only all the vowels in the string and return it. The vowels are ‘a’, ’e’, ‘i’, ‘o’, and ‘u’, and they can appear in both cases. Example 1: Input: s = "hello" Output: "holle" Example 2: Input: s = "leetcode" Output: "leotcede" Constraints: 1 <= s.length <= 3 * 105 s consist of printable ASCII characters. Solution class Solution { public String reverseVowels(String s) { Set<Character> vowels = new HashSet<>(); vowels.addAll(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')); int lo = 0; int hi = s.length() - 1; char[] word = s.toCharArray(); while (lo < hi) { while (lo < hi && !vowels.contains(word[lo])) { lo++; } while (lo < hi && !vowels.contains(word[hi])) { hi--; } char c = word[lo]; word[lo] = word[hi]; word[hi] = c; lo++; hi--; } return new String(word); } }

July 19, 2021 · 1 min · volyx

977. Squares of a Sorted Array

977. Squares of a Sorted Array Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2: Input: nums = [-7,-3,2,3,11] Output: [4,9,9,49,121] Constraints: 1 <= nums.length <= 104 -104 <= nums[i] <= 104 nums is sorted in non-decreasing order. Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach? ...

July 19, 2021 · 2 min · volyx

21. Merge Two Sorted Lists

21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. Example 1: Input: l1 = [1,2,4], l2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: Input: l1 = [], l2 = [] Output: [] Example 3: Input: l1 = [], l2 = [0] Output: [0] ...

July 16, 2021 · 3 min · volyx

1213. Intersection of Three Sorted Arrays

1213. Intersection of Three Sorted Arrays Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays. Example 1: Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8] Output: [1,5] Explanation: Only 1 and 5 appeared in the three arrays. Example 2: Input: arr1 = [197,418,523,876,1356], arr2 = [501,880,1593,1710,1870], arr3 = [521,682,1337,1395,1764] Output: [] Constraints: ...

July 15, 2021 · 2 min · volyx

496. Next Greater Element I

496. Next Greater Element I The next greater element of some element x in an array is the first greater element that is to the right of x in the same array. You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2. For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1. ...

July 15, 2021 · 3 min · volyx

13. Roman to Integer

13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, 2 is written as II in Roman numeral, just two one’s added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II. ...

July 1, 2021 · 3 min · volyx

1175. Prime Arrangements

1175. Prime Arrangements Return the number of permutations of 1 to n so that prime numbers are at prime indices (1-indexed.) (Recall that an integer is prime if and only if it is greater than 1, and cannot be written as a product of two positive integers both smaller than it.) Since the answer may be large, return the answer modulo 10^9 + 7. Example 1: Input: n = 5 Output: 12 Explanation: For example [1,2,5,4,3] is a valid permutation, but [5,2,3,4,1] is not because the prime number 5 is at index 1. Example 2: Input: n = 100 Output: 682289015 Constraints: ...

June 30, 2021 · 2 min · volyx