To find all the prime numbers less than or equal to a given integer n by Eratosthenes’ method:
Create a list of consecutive integers from 2 through n: (2, 3, 4, …, n).
Initially, let p equal 2, the smallest prime number.
Enumerate the multiples of p by counting in increments of p from 2p to n, and mark them in the list (these will be 2p, 3p, 4p, …; the p itself should not be marked).
Find the smallest number in the list greater than p that is not marked. If there was no such number, stop. Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3.
When the algorithm terminates, the numbers remaining not marked in the list are all the primes below n.
To figure out a O(1)O(1) space requirement, we would need to get this simple intuition first. For an array of length n:
There can be at most one majority element which is more than⌊n/2⌋ times.
There can be at most two majority elements which are more than⌊n/3⌋ times.
There can be at most three majority elements which are more than⌊n/4⌋ times.
and so on.
Knowing this can help us understand how we can keep track of majority elements which satisfies O(1)O(1) space requirement.
If the current element is equal to one of the potential candidate, the count for that candidate is increased while leaving the count of the other candidate as it is.
If the counter reaches zero, the candidate associated with that counter will be replaced with the next element if the next element is not equal to the other candidate as well.
Both counters are decremented only when the current element is different from both candidates.
k = 0; B[0] = A[0]; k = 1; B[0] = A[len-1]; k = 2; B[0] = A[len-2]; ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicintmaxRotateFunction(int[] nums) { int n=nums.length; int sum=0; int F=0; for (inti=0; i < n; i++) { sum+=nums[i]; F+=i*nums[i]; } int res=F; //F1-F0=sum-nB(0) for (inti= n - 1; i >= 0; i--) { F=F+sum-n*nums[i]; res=Math.max(res,F); } return res; }
publicint[][] multiply(int[][] mat1, int[][] mat2) { int m=mat1.length; int k=mat1[0].length; int n=mat2[0].length; int[][] res=newint[m][n]; for (inti=0; i < m; i++) { for (intj=0; j < n; j++) { for (intp=0; p < k; p++) { res[i][j]+=mat1[i][p]*mat2[p][j]; } } } return res; }
1228. Missing Number In Arithmetic Progression
1 2 3 4 5 6 7 8
publicintmissingNumber(int[] arr) { int n=arr.length; int sum=(arr[0]+arr[n-1])*(n+1)/2; for (int num : arr) { sum-=num; } return sum; }
1010. Pairs of Songs With Total Durations Divisible by 60
1 2 3 4 5 6 7 8 9 10 11
publicintnumPairsDivisibleBy60(int[] time) { int[] arr=newint[60]; for (int i : time) { arr[i%60]++; } long res=(long)arr[0]*(arr[0]-1)/2+arr[30]*(arr[30]-1)/2; for (inti=31; i < 60; i++) { res+=arr[i]*arr[60-i]; } return (int)res; }
1492. The kth Factor of n
dumb
1 2 3 4 5 6 7 8 9
publicintkthFactor(int n, int k) { List<Integer> list=newLinkedList<>(); for (inti=1; i <= n; i++) { if(n%i==0){ list.add(i); } } return k>list.size() ? -1 : list.get(k-1); }
better, but still O(n)
1 2 3 4 5 6 7 8 9 10 11
publicintkthFactor(int n, int k) { for(int i=1; i<=n/2; i++){ if(n%i==0){ k--; } if(k==0){ return i; } } return k==1 ? n : -1; }
Follow up:
Could you solve this problem in less than O(n) complexity?
publicintbulbSwitch(int n) { //only perfect square number has odd number of divisors //the number of perfect square number in [1,n] = sqrt(n) return (int)Math.sqrt(n); }