About Algorithm: Difference between revisions
From My Limbic Wiki
| Line 11: | Line 11: | ||
* [[Index.php?title=Binary Search|Binary Search]] | * [[Index.php?title=Binary Search|Binary Search]] | ||
<pre class="code java"> | <pre class="code java"> | ||
private int | private int binarySearch( | ||
int | int[] nums, | ||
int | int leftBoundary, | ||
int | int rightBoundary, | ||
int | int target | ||
) { | |||
int leftIndex = leftBoundary; | |||
int rightIndex = rightBoundary; | |||
while (leftIndex <= rightIndex) { | while (leftIndex <= rightIndex) { | ||
midIndex = (leftIndex + rightIndex) / 2; | int midIndex = (leftIndex + rightIndex) / 2; | ||
midValue = nums[midIndex]; | int midValue = nums[midIndex]; | ||
if (midValue > | if (midValue == target) { | ||
return midIndex; | |||
} else if (midValue > target) { | |||
rightIndex = midIndex - 1; | |||
} else { | |||
leftIndex = midIndex + 1; | leftIndex = midIndex + 1; | ||
} | } | ||
} | } | ||
return | return -1; | ||
} | } | ||
</pre> | </pre> | ||
Revision as of 22:58, 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/
- Two pass are necessary to calculate the left and right products https://leetcode.com/problems/product-of-array-except-self/
- A product invert the sign, the max becomes the min, and the min becomes the max: https://leetcode.com/problems/maximum-product-subarray/
- Binary search exploit "sorted and rotated" structure. At every iteration, it divide the space by 2 using left or right. https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
- Search in Rotated Sorted Array https://leetcode.com/problems/search-in-rotated-sorted-array/
- Find the pivot
- Binary Search to left side, Binary Search to right side
Sorting Algoritms
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