image_2022-12-22_01-42-08.png
8.7 KB
#N2481. Minimum Cuts to Divide a Circle
problem link
#solution
problem link
#solution
class Solution {
public int numberOfCuts(int n) {
return (n==1?0:n/(2-n%2));
}
}👍2
#note
If we want to look through all subsets of array:
If we want to look through all subsets of array:
int n=arr.length;
for(int i=0; i<(1<<n); i++){
for(int j=0; j<n; j++){
if((i&(1<<j))>0){
System.out.println(arr[j]+" ");
}
}
}
🔥2👌1
image_2023-01-02_03-24-42.png
61 KB
Runtime1 msBeats95.21%
Memory40.3 MBBeats73.73%
#N290. Word Pattern
problem link
#solution
class Solution {
public boolean wordPattern(String pattern, String s) {
Map<Character, String> map = new HashMap<>();
int i=0;
for(String part: s.split(" ")){
if(i==pattern.length()||map.containsKey(pattern.charAt(i)) && !map.get(pattern.charAt(i)).equals(part)
||map.containsValue(part) && get(map, part)!=pattern.charAt(i))
return false;
map.put(pattern.charAt(i), part);
i++;
}
return i==pattern.length();
}
public char get(Map<Character, String> map, String part){
for (Map.Entry<Character, String> entry : map.entrySet()) {
if (entry.getValue().equals(part)) {
return entry.getKey();
}
}
return '0';
}
}👍2
image_2023-01-04_06-06-40.png
39.5 KB
Runtime11 msBeats98.9%
Memory51.3 MBBeats99.48
#N2244. Minimum Rounds to Complete All Tasks
problem link
#solution
class Solution {
public int minimumRounds(int[] tasks) {
Arrays.sort(tasks);
int l=tasks.length;
if(l>1 && tasks[l-2]!=tasks[l-1]) return -1;
int r=0, type=tasks[0], count=0;
for(int i=0; i<l; i++){
if(type==tasks[i]) count++;
if(type!=tasks[i] || i==l-1){
type=tasks[i];
if(count==1) return -1;
else r+=count/3 + (count%3+count%3%2)/2;
count=1;
}
}
return r;
}
}👍3⚡2
image_2023-01-06_04-11-25.png
42.7 KB
Runtime70 msBeats76.84%
Memory80 MBBeats68.31%
#N452. Minimum Number of Arrows to Burst Balloons
problem link
#solution
class Solution {
public int findMinArrowShots(int[][] points) {
int n=points.length;
//Arrays.sort(points, Comparator.comparingInt(o -> o[0]));
Arrays.sort(points, (a, b) -> Integer.compare(a[0], b[0]));
int l=points[0][0], r=points[0][1], count=1;
for(int i=0; i<n; i++){
if(points[i][0]>r || points[i][1]<l){ // not cross
l=points[i][0];
r=points[i][1];
count++;
}else{
l=Math.max(l, points[i][0]);
r=Math.min(r, points[i][1]);
}
}
return count;
}
}👍2
image_2023-01-10_19-12-08.png
24.3 KB
Runtime0 msBeats100%#N100. Same Tree
Memory40 MBBeats62.93%
problem link
#solution
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null) return true;
if(p==null||q==null) return false;
if(p.val!=q.val) return false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}👍4
image_2023-01-15_02-26-20.png
65.8 KB
Runtime2 msBeats88.7%
Memory40.4 MBBeats99.43%
#N1061. Lexicographically Smallest Equivalent String
problem link
#solution
class Solution {
public String smallestEquivalentString(String s1, String s2, String base) {
int[] graph = new int[26];
for(int i=0; i<26; i++) graph[i]=i;
for(int i=0; i<s1.length(); i++){
int a1=s1.charAt(i)-'a';
int a2=s2.charAt(i)-'a';
int p1=find(graph, a1);
int p2=find(graph, a2);
if(p1<p2) graph[p2]=p1;
else graph[p1]=p2;
}
StringBuilder sb = new StringBuilder();
for(Character ch: base.toCharArray())
sb.append((char)(find(graph, ch-'a')+'a'));
return sb.toString();
}
public int find(int[] graph, int x){
while(x!=graph[x])
x=graph[x];
return x;
}
}👍5❤1
Leetcode in Java && Oracle
image_2023-01-15_02-26-20.png
#note
union-find algorithm is to find cycle in undirected graph
union-find algorithm is to find cycle in undirected graph
image_2023-01-16_01-04-58.png
49.8 KB
Runtime9 msBeats96.66%
Memory117.2 MBBeats89.51%
#N1971. Find if Path Exists in Graph
problem link
#solution
class Solution {
public boolean validPath(int n, int[][] edges, int source, int destination) {
int[] parents=new int[n];
for(int i=0; i<n; i++) parents[i]=i;
for(int[] edge: edges){
int p1=find(parents, edge[0]);
int p2=find(parents, edge[1]);
if(p1<p2) parents[p2]=p1;
else parents[p1]=p2;
}
return find(parents, source) == find(parents, destination);
}
public int find(int[] parents, int x){
while(x!=parents[x]){
x=parents[x];
}
return x;
}
}👍1
image_2023-01-16_01-43-53.png
41.2 KB
Runtime9 msBeats90.97%
Memory42.1 MBBeats44.5%
#N1995. Count Special Quadruplets
problem link
#solution
class Solution {
public int countQuadruplets(int[] nums) {
int res=0, l=nums.length;
Map<Integer, Integer> count = new HashMap<>();
count.put(nums[l-1]-nums[l-2], 1);
for(int b=l-3; b>=1; b--){
for(int a=b-1; a>=0; a--){
res+=count.getOrDefault(nums[b]+nums[a], 0);
}
for(int d=l-1; d>b; d--){
count.put(nums[d]-nums[b], count.getOrDefault(nums[d]-nums[b], 0) +1);
}
}
return res;
}
}image_2023-05-26_12-11-11.png
34.9 KB
Runtime1 msBeats99.47%
Memory44.2 MBBeats45.81%
#N605. Can Place Flowers
problem link
#solution
Memory44.2 MBBeats45.81%
#N605. Can Place Flowers
problem link
#solution
class Solution {
public boolean canPlaceFlowers(int[] f, int n) {
int l = f.length;
if(n==0) return true;
if(l==1) return f[0]==0 && n == 1;
for(int i=0; i<l; i++){
if(i==0 && f[0] + f[1] == 0) {
n--;
i++;
}
else if(i==l-1 && f[l-1] + f[l-2]==0) {
n--;
i++;
}
else if(i>0 && i<l-1 && f[i]+f[i-1]+f[i+1]==0){
n--;
i++;
}
}
return n<=0;
}
}👍5
image_2023-06-05_10-24-26.png
45.6 KB
Runtime9 msBeats39.59%
Memory45.1 MBBeats10.38%
#N345. Reverse Vowels of a String
problem link
#solution
Memory45.1 MBBeats10.38%
#N345. Reverse Vowels of a String
problem link
#solution
class Solution {
public String reverseVowels(String s) {
String vowels = "aeiouAEIOU";
char[] arr = s.toCharArray();
for(int i=0, j=arr.length-1; i<j; ){
if(vowels.contains(""+arr[i]) && vowels.contains(""+arr[j])){
char temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
else if(vowels.contains(""+arr[i])) j--;
else if(vowels.contains(""+arr[j])) i++;
else{
i++;
j--;
}
}
return new String(arr);
}
}👍2
image_2023-06-05_11-03-29.png
44.9 KB
Runtime8 msBeats60.13%
Memory42.3 MBBeats87.73%
#N151. Reverse Words in a String
problem link
#solution
Memory42.3 MBBeats87.73%
#N151. Reverse Words in a String
problem link
#solution
class Solution {
public String reverseWords(String s) {
String[] arr = s.split("\\s+");
for(int i = 0, j = arr.length-1; i<j; ){
if(arr[i].equals("")){
i++; continue;
}
if(arr[j].equals("")){
j--; continue;
}
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++; j--;
}
StringBuilder sb = new StringBuilder();
for(String ss: arr) {
if(!ss.equals("")) sb.append(ss).append(" ");
}
return sb.toString().trim();
}
}👍2🔥1
image_2023-06-05_11-48-24.png
30.2 KB
Runtime2 msBeats50.58%
Memory51.1 MBBeats53.48%
#N238. Product of Array Except Self
problem link
#solution
Memory51.1 MBBeats53.48%
#N238. Product of Array Except Self
problem link
#solution
class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int arr[] = new int[n];
int left[] = new int[n];
arr[0] = left[n-1] = 1;
for(int i=1; i<n; i++){
arr[i] = arr[i-1]*nums[i-1];
}
for(int i=n-2; i>-1; i--){
left[i] = left[i+1]*nums[i+1];
}
for(int i=0; i<n; i++){
arr[i]*=left[i];
}
return arr;
}
}🔥2❤1
image_2023-06-05_12-50-01.png
23.6 KB
Runtime3 msBeats49.59%
Memory131.8 MBBeats25.49%
#N334. Increasing Triplet Subsequence
problem link
#solution
Memory131.8 MBBeats25.49%
#N334. Increasing Triplet Subsequence
problem link
#solution
class Solution {
public boolean increasingTriplet(int[] nums) {
int small = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
for(int n: nums){
if(n<=small) small = n;
else if(n<=mid) mid = n;
else return true;
}
return false;
}
}⚡2👍2
image_2023-06-08_13-07-40.png
46.5 KB
Runtime1 msBeats96.95%
Memory43 MBBeats49.21%
#N443. String Compression
problem link
#solution
Memory43 MBBeats49.21%
#N443. String Compression
problem link
#solution
class Solution {
public int compress(char[] chars) {
String s;
StringBuilder sb = new StringBuilder();
int count=1;
for(int i=1; i<chars.length; i++){
if(chars[i]==chars[i-1]){
count++;
}else{
sb.append(chars[i-1]);
if(count!=1) sb.append(count);
count=1;
}
}
sb.append(chars[chars.length-1]);
if(count!=1) sb.append(count);
s=sb.toString();
for(int i=0; i<s.length(); i++){
chars[i]=s.charAt(i);
}
return s.length();
}
}👍4
image_2023-06-12_10-40-06.png
39.5 KB
Runtime5 msBeats83.94%
Memory41.1 MBBeats40.44%
#N228. Summary Ranges
problem link
#solution
Memory41.1 MBBeats40.44%
#N228. Summary Ranges
problem link
#solution
class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> list = new ArrayList<>();
if(nums.length==0) return list;
int a=nums[0], b=nums[0];
for(int i=1; i<nums.length; i++){
if(nums[i]-nums[i-1]==1){
b=nums[i];
}else{
if(a!=b) list.add(a+"->"+b);
else list.add(""+a);
a=nums[i];
b=nums[i];
}
}
if(a!=b) list.add(a+"->"+b);
else list.add(""+a);
return list;
}
}👍8⚡1
#note
Use Sieve of Eratosthenes algorithm to find all prime numbers till n.
Use Sieve of Eratosthenes algorithm to find all prime numbers till n.
algorithm Sieve of Eratosthenes is
input: an integer n > 1.
output: all prime numbers from 2 through n.
let A be an array of Boolean values, indexed by integers 2 to n,
initially all set to true.
for i = 2, 3, 4, ..., not exceeding √n do
if A[i] is true
for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n do
set A[j] := false
return all i such that A[i] is true.
Time - O(nloglogn)
Example problem: https://leetcode.com/problems/closest-prime-numbers-in-range/description