About Algorithm: Difference between revisions
From My Limbic Wiki
| Line 2: | Line 2: | ||
* Use Set/Hashet for unique values [https://leetcode.com/problems/contains-duplicate/description/ https://leetcode.com/problems/contains-duplicate/] | * Use Set/Hashet for unique values [https://leetcode.com/problems/contains-duplicate/description/ https://leetcode.com/problems/contains-duplicate/] | ||
* Use Math.max(int,int) to get the max number between a number and a calculation. https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ | * Use Math.max(int,int) to get the max number between a number and a calculation. https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ | ||
* A product invert the sign, the max becomes the min, and the min becomes the max: https://leetcode.com/problems/maximum-product-subarray/ | * A product invert the sign, the max becomes the min, and the min becomes the max: https://leetcode.com/problems/maximum-product-subarray/ | ||
=Sorting Algoritms= | =Sorting Algoritms= | ||
* '''Two Pass''' - to calculate the left and right products | * '''Two Pass''' - ([https://leetcode.com/problems/product-of-array-except-self/ LeetCode])- to calculate the left and right products | ||
<pre class="code java"> | <pre class="code java"> | ||
class Solution { | class Solution { | ||
| Line 32: | Line 27: | ||
} | } | ||
</pre> | </pre> | ||
* '''Binary Search''' - | * '''Binary Search''' - ([https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ LeetCode] - [https://leetcode.com/problems/search-in-rotated-sorted-array/ LeetCode]) - At every iteration, it divide the space by 2 using left or right. | ||
** Find the pivot | |||
** Binary Search to left side, Binary Search to right side | |||
<pre class="code java"> | <pre class="code java"> | ||
private int binarySearch( | private int binarySearch( | ||
Revision as of 23:03, 5 October 2025
Tips
- Use Set/Hashet for unique values https://leetcode.com/problems/contains-duplicate/
- Use Math.max(int,int) to get the max number between a number and a calculation. https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
- A product invert the sign, the max becomes the min, and the min becomes the max: https://leetcode.com/problems/maximum-product-subarray/
Sorting Algoritms
- Two Pass - (LeetCode)- to calculate the left and right products
class Solution {
public int[] productExceptSelf(int[] nums) {
int length = nums.length;
int[] answer = new int[nums.length];
int leftProduct = 1;
for (int i = 0; i < length; i++) {
answer[i] = leftProduct;
leftProduct *= nums[i];
}
int rightProduct = 1;
for (int i = length - 1; i >= 0; i--) {
answer[i] *= rightProduct;
rightProduct *= nums[i];
}
return answer;
}
}
- Binary Search - (LeetCode - LeetCode) - At every iteration, it divide the space by 2 using left or right.
- Find the pivot
- Binary Search to left side, Binary Search to right side
private int binarySearch(
int[] nums,
int leftBoundary,
int rightBoundary,
int target
) {
int leftIndex = leftBoundary;
int rightIndex = rightBoundary;
while (leftIndex <= rightIndex) {
int midIndex = (leftIndex + rightIndex) / 2;
int midValue = nums[midIndex];
if (midValue == target) {
return midIndex;
} else if (midValue > target) {
rightIndex = midIndex - 1;
} else {
leftIndex = midIndex + 1;
}
}
return -1;
}
- Fibonacci
- MergeSort
- QuickSort (Java)
- Bit Manipulation
- Backtracking
- Graph Traversal
- Union-Find
- Dynamic Programming
- Greedy
- Sliding Window / Two Pointers
- BubbleSort (Java)
- Swapping the adjacent numbers two by two by order
Graphs
- Dijkstra
- Topologic sorting
Cryptography & Compression
- Shannon-Fano
- Huffman
- Diffie-Hellman
- RSA
Prime Numbers
- ?
Most beautiful Equation
- P=NP (Les Équations de Yang Mills)
- Millenium problems (Clay Mathematics Institute)
- Few ideas