1242. Web Crawler Multithreaded

Given a URL startUrl and an interface HtmlParser, implement a Multi-threaded web crawler to crawl all links that are under the same hostname as startUrl. Return all URLs obtained by your web crawler in any order. Your crawler should: Start from the page: startUrl Call HtmlParser.getUrls(url) to get all URLs from a webpage of a given URL. Do not crawl the same link twice. Explore only the links that are under the same hostname as startUrl. As shown in the example URL above, the hostname is example.org. For simplicity’s sake, you may assume all URLs use HTTP protocol without any port specified. For example, the URLs http://leetcode.com/problems and http://leetcode.com/contest are under the same hostname, while URLs http://example.org/test and http://example.com/abc are not under the same hostname. ...

November 4, 2021 · 3 min · volyx

1287. Element Appearing More Than 25% In Sorted Array

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer. Example 1: Input: arr = [1,2,2,6,6,6,6,7,10] Output: 6 Example 2: Input: arr = [1,1] Output: 1 Constraints: 1 <= arr.length <= 10^4 0 <= arr[i] <= 10^5 Solution class Solution { public int findSpecialInteger2(int[] arr) { int n = arr.length; int val1 = arr[n / 4]; int val2 = arr[n / 4 * 2] ; int val3 = arr[n / 4 * 3]; int val4 = arr[n - 1]; int count1 = countFreq(arr, val1); int count2 = countFreq(arr, val2); int count3 = countFreq(arr, val3); int count4 = countFreq(arr, val4); if (count1 >= count2 && count1 >= count3 && count1 >= count4) { return val1; } if (count2 >= count1 && count2 >= count3 && count2 >= count4) { return val2; } if (count3 >= count1 && count3 >= count2 && count3 >= count4) { return val3; } return val4; } int countFreq(int[] arr, int val) { int mid = Arrays.binarySearch(arr, val); int right = mid; while (right < arr.length - 1 && arr[right] == arr[right + 1]) { right++; } int left = mid; while (left > 0 && arr[left] == arr[left - 1]) { left--; } return right - left; } } Solution 2 class Solution { public int findSpecialInteger(int[] arr) { int n = arr.length; int t = n / 4; for (int i = 0; i < n - t; i++) { if (arr[i] == arr[i + t]) { return arr[i]; } } return -1; } }

October 28, 2021 · 2 min · volyx

121. Best Time to Buy and Sell Stock

You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. ...

October 20, 2021 · 2 min · volyx

415. Add Strings

Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string. You must solve the problem without using any built-in library for handling large integers (such as BigInteger). You must also not convert the inputs to integers directly. Example 1: Input: num1 = "11", num2 = "123" Output: "134" Example 2: Input: num1 = "456", num2 = "77" Output: "533" Example 3: Input: num1 = "0", num2 = "0" Output: "0" Constraints: ...

October 9, 2021 · 2 min · volyx

1662. Check If Two String Arrays are Equivalent

Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise. A string is represented by an array if the array elements concatenated in order forms the string. Example 1: Input: word1 = ["ab", "c"], word2 = ["a", "bc"] Output: true Explanation: word1 represents string "ab" + "c" -> "abc" word2 represents string "a" + "bc" -> "abc" The strings are the same, so return true. Example 2: Input: word1 = ["a", "cb"], word2 = ["ab", "c"] Output: false Example 3: Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"] Output: true Constraints: ...

September 16, 2021 · 2 min · volyx

1360. Number of Days Between Two Dates

1360. Number of Days Between Two Dates Write a program to count the number of days between two dates. The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples. Example 1: Input: date1 = "2019-06-29", date2 = "2019-06-30" Output: 1 Example 2: Input: date1 = "2020-01-15", date2 = "2019-12-31" Output: 15 Constraints: The given dates are valid dates between the years 1971 and 2100. Solution import java.time.*; class Solution { public int daysBetweenDates(String date1, String date2) { return Math.abs(countDays(date1) - countDays(date2)); } int [] MONTHS = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int countDays(String date) { String[] parts = date.split("-"); Integer years = Integer.parseInt(parts[0]); Integer months = Integer.parseInt(parts[1]); Integer days = Integer.parseInt(parts[2]); for (int year = 1971; year < years; year++) { if (isLeapYear(year)) { days += 366; } else { days += 365; } } for (int month = 1; month < months; month++) { if (isLeapYear(years) && month == 2) { days += 29; } else { days += MONTHS[month]; } } return days; } boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } public int daysBetweenDates2(String date1, String date2) { LocalDateTime time1 = LocalDate.parse(date1).atTime(LocalTime.NOON); LocalDateTime time2 = LocalDate.parse(date2).atTime(LocalTime.NOON); Duration duration = Duration.between(time1, time2); return (int) Math.abs(duration.toDays()); } }

August 15, 2021 · 2 min · volyx

1528. Shuffle String

1528. Shuffle String A city’s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Given the locations and heights of all the buildings, return the skyline formed by these buildings collectively. The geometric information of each building is given in the array buildings where buildings[i] = [lefti, righti, heighti]: lefti is the x coordinate of the left edge of the ith building. righti is the x coordinate of the right edge of the ith building. heighti is the height of the ith building. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0. ...

July 31, 2021 · 3 min · volyx

1266. Minimum Time Visiting All Points

1266. Minimum Time Visiting All Points On a 2D plane, there are n points with integer coordinates points[i] = [xi, yi]. Return the minimum time in seconds to visit all the points in the order given by points. You can move according to these rules: In 1 second, you can either: move vertically by one unit, move horizontally by one unit, or move diagonally sqrt(2) units (in other words, move one unit vertically then one unit horizontally in 1 second). You have to visit the points in the same order as they appear in the array. You are allowed to pass through points that appear later in the order, but these do not count as visits. Example 1: Input: points = [[1,1],[3,4],[-1,0]] Output: 7 Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0] Time from [1,1] to [3,4] = 3 seconds Time from [3,4] to [-1,0] = 4 seconds Total time = 7 seconds Example 2: Input: points = [[3,2],[-2,2]] Output: 5 Constraints: ...

July 30, 2021 · 2 min · volyx

270. Closest Binary Search Tree Value

270. Closest Binary Search Tree Value Given the root of a binary search tree and a target value, return the value in the BST that is closest to the target. Example 1: Input: root = [4,2,5,1,3], target = 3.714286 Output: 4 Example 2: Input: root = [1], target = 4.428571 Output: 1 Constraints: The number of nodes in the tree is in the range [1, 104]. 0 <= Node.val <= 10^9 -10^9 <= target <= 10^9 Solution /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { //////////////// O(logN) //////////////////////// public int closestValue(TreeNode root, double target) { int closest = root.val; while (root != null) { if (Math.abs(root.val - target) < Math.abs(closest - target)) { closest = root.val; } root = root.val > target ? root.left: root.right; } return closest; } //////////////// O(N) //////////////////////// int closest; double closestDelta; public int closestValue2(TreeNode root, double target) { closest = root.val; closestDelta = Math.abs((double) root.val - target); find(root, target); return closest; } void find(TreeNode node, double target) { if (node == null) return; double currentDelta = Math.abs((double) node.val - target); if (currentDelta < closestDelta) { closest = node.val; closestDelta = currentDelta; } find(node.left, target); find(node.right, target); } ///////////// In Order Traversal O(N) public int closestValue1(TreeNode root, double target) { Deque<TreeNode> deq = new ArrayDeque<>(); long prev = Long.MIN_VALUE; while (deq.size() > 0 || root != null) { while (root != null) { deq.offerLast(root); root = root.left; } root = deq.removeLast(); if (prev <= target && target < root.val) { if (Math.abs(target - prev) < Math.abs(target - root.val)) { return (int) prev; } else { return root.val; } } prev = root.val; root = root.right; } return (int) prev; } } Solution 2022-01-30 class Solution { public int closestValue(TreeNode root, double target) { int closest = root.val; while (root != null) { if (Math.abs(target - root.val) < Math.abs(closest - target)) { closest = root.val; } root = (root.val > target) ? root.left: root.right; } return closest; } }

July 26, 2021 · 2 min · volyx

252. Meeting Rooms

252. Meeting Rooms Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings. Example 1: Input: intervals = [[0,30],[5,10],[15,20]] Output: false Example 2: Input: intervals = [[7,10],[2,4]] Output: true Constraints: 0 <= intervals.length <= 10^4 intervals[i].length == 2 0 <= starti < endi <= 10^6 Solution class Solution { public boolean canAttendMeetings(int[][] intervals) { if (intervals.length < 2) { return true; } Arrays.sort(intervals, (a, b) -> a[0] - b[0]); for (int i = 1; i < intervals.length; i++) { int[] curr = intervals[i]; int[] prev = intervals[i - 1]; if (overlap(prev, curr)) { return false; } } return true; } /* ---------------| |-------------------------- -----------| |-------------- */ boolean overlap(int[] a, int[] b) { return Math.max(a[0], b[0]) < Math.min(a[1], b[1]); } }

July 22, 2021 · 1 min · volyx