1041. Robot Bounded In Circle

1041. Robot Bounded In Circle On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions: “G”: go straight 1 unit; “L”: turn 90 degrees to the left; “R”: turn 90 degrees to the right. The robot performs the instructions given in order, and repeats them forever. Return true if and only if there exists a circle in the plane such that the robot never leaves the circle. ...

June 3, 2021 · 3 min · volyx

17. Letter Combinations of a Phone Number

17. Letter Combinations of a Phone Number Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. Example 1: Input: digits = "23" Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] Example 2: Input: digits = "" Output: [] Example 3: Input: digits = "2" Output: ["a","b","c"] ...

June 3, 2021 · 1 min · volyx

189. Rotate Array

189. Rotate Array Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2: Input: nums = [-1,-100,3,99], k = 2 Output: [3,99,-1,-100] Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] Constraints: ...

June 3, 2021 · 2 min · volyx

22. Generate Parentheses

22. Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example 1: Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] Example 2: Input: n = 1 Output: ["()"] Constraints: 1 <= n <= 8 Solution class Solution { public List<String> generateParenthesis(int n) { List<String> res = new ArrayList<>(); backtracking("(", 1, 0, n, res); return res; } void backtracking(String s, int open, int closed, int n, List<String> res) { if (open + closed == 2 * n) { res.add(s); return; } if (open < n) backtracking(s + "(", open + 1, closed, n, res); if (open > closed) backtracking(s + ")", open, closed + 1, n, res); } }

June 3, 2021 · 1 min · volyx

90. Subsets II

90. Subsets II Given an integer array nums that may contain duplicates, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. Example 1: Input: nums = [1,2,2] Output: [[],[1],[1,2],[1,2,2],[2],[2,2]] Example 2: Input: nums = [0] Output: [[],[0]] Constraints: 1 <= nums.length <= 10 -10 <= nums[i] <= 10 Solution class Solution { public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> res = new ArrayList<>(); List<Integer> current = new ArrayList<>(); Arrays.sort(nums); backtrack(nums, 0, current, res); return res; } void backtrack(int[] nums, int index, List<Integer> current, List<List<Integer>> res) { res.add(new ArrayList<>(current)); for (int i = index; i < nums.length; i++) { if (i > index && nums[i - 1] == nums[i]) { continue; } current.add(nums[i]); backtrack(nums, i + 1, current, res); current.remove(current.size() - 1); } } }

June 3, 2021 · 1 min · volyx

343. Integer Break

343. Integer Break Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers. Return the maximum product you can get. Example 1: Input: n = 2 Output: 1 Explanation: 2 = 1 + 1, 1 × 1 = 1. Example 2: Input: n = 10 Output: 36 Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36. Constraints: ...

May 30, 2021 · 2 min · volyx

46. Permutations

46. Permutations Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Example 1: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example 2: Input: nums = [0,1] Output: [[0,1],[1,0]] Example 3: Input: nums = [1] Output: [[1]] Constraints: 1 <= nums.length <= 6 -10 <= nums[i] <= 10 All the integers of nums are unique. Solution class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res = new ArrayList<>(); backtrack(res, nums, 0); return res; } void backtrack(List<List<Integer>> res, int[] nums, int index) { if (index == nums.length - 1) { res.add(toList(nums)); return; } for (int i = index; i < nums.length; i++) { swap(nums, i, index); backtrack(res, nums, index + 1); swap(nums, i, index); } } List<Integer> toList(int[] nums) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < nums.length; i++) { list.add(nums[i]); } return list; } void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } Solution 2021-11-22 class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res = new ArrayList<>(); permuteAt(0, nums, res); return res; } void permuteAt(int i, int[] nums, List<List<Integer>> res) { if (i == nums.length - 1) { res.add(toList(nums)); return; } for (int j = i; j < nums.length; j++) { swap(i, j, nums); permuteAt(i + 1, nums, res); swap(i, j, nums); } } void swap(int i, int j, int[] arr) { int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } List<Integer> toList(int[] values) { List<Integer> res = new ArrayList<>(values.length); for (int val : values) { res.add(val); } return res; } }

May 30, 2021 · 2 min · volyx

78. Subsets

78. Subsets Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. Example 1: Input: nums = [1,2,3] Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] Example 2: Input: nums = [0] Output: [[],[0]] Constraints: 1 <= nums.length <= 10 -10 <= nums[i] <= 10 All the numbers of nums are unique. Solution class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> subsets = new ArrayList<>(); int n = nums.length; for (int i = (int) Math.pow(2, n); i < (int) Math.pow(2, n + 1); i++) { // generate bitmasks from 00.000 to 111..111 String bitmask = Integer.toBinaryString(i).substring(1); List<Integer> curr = new ArrayList<>(); for (int j = 0; j < n; j++) { if (bitmask.charAt(j) == '1') curr.add(nums[j]); } subsets.add(curr); } return subsets; } public List<List<Integer>> subsets3(int[] nums) { List<List<Integer>> subsets = new ArrayList<>(); List<Integer> current = new ArrayList<>(); dfs(0, nums, current, subsets); return subsets; } void dfs(int index, int[] nums, List<Integer> current, List<List<Integer>> subsets) { if (index == nums.length) { subsets.add(new ArrayList<>(current)); return; } current.add(nums[index]); dfs(index + 1, nums, current, subsets); current.remove(current.size() - 1); dfs(index + 1, nums, current, subsets); } public List<List<Integer>> subsets2(int[] nums) { List<List<Integer>> subsets = new ArrayList<>(); List<Integer> current = new ArrayList<>(); generateSubsets(0, nums, current, subsets); return subsets; } void generateSubsets(int index, int[] nums, List<Integer> current, List<List<Integer>> subsets) { subsets.add(new ArrayList<>(current)); for (int i = index; i < nums.length; i++) { current.add(nums[i]); generateSubsets(i + 1, nums, current, subsets); current.remove(current.size() - 1); } } } Solution 2021-11-22 class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> res = new ArrayList<>(); List<Integer> curr = new ArrayList<>(); dfs(0, nums, res, curr); return res; } void dfs(int index, int[] nums, List<List<Integer>> res, List<Integer> curr) { if (index == nums.length) { res.add(List.copyOf(curr)); return; } curr.add(nums[index]); dfs(index + 1, nums, res, curr); curr.remove(curr.size() - 1); dfs(index + 1, nums, res, curr); } }

May 30, 2021 · 2 min · volyx

1876. Substrings of Size Three with Distinct Characters

1876. Substrings of Size Three with Distinct Characters A string is good if there are no repeated characters. Given a string s​​​​​, return the number of good substrings of length three in s​​​​​​. Note that if there are multiple occurrences of the same substring, every occurrence should be counted. A substring is a contiguous sequence of characters in a string. Example 1: Input: s = "xyzzaz" Output: 1 Explanation: There are 4 substrings of size 3: "xyz", "yzz", "zza", and "zaz". The only good substring of length 3 is "xyz". Example 2: Input: s = "aababcabc" Output: 4 Explanation: There are 7 substrings of size 3: "aab", "aba", "bab", "abc", "bca", "cab", and "abc". The good substrings are "abc", "bca", "cab", and "abc". Constraints: ...

May 29, 2021 · 1 min · volyx

1877. Minimize Maximum Pair Sum in Array

1877. Minimize Maximum Pair Sum in Array The pair sum of a pair (a,b) is equal to a + b. The maximum pair sum is the largest pair sum in a list of pairs. For example, if we have pairs (1,5), (2,3), and (4,4), the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8. Given an array nums of even length n, pair up the elements of nums into n / 2 pairs such that: ...

May 29, 2021 · 2 min · volyx