About Algorithm: Difference between revisions

From My Limbic Wiki
Line 81: Line 81:


==== 3 Sum ====
==== 3 Sum ====
<pre class="code java">
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        Set<List<Integer>> results = new HashSet<>();
        for(int i = 0; i < nums.length - 2; i++){
            // Skip duplicates for fixed element
            if (i > 0 && nums[i] == nums [i - 1]) continue;
            int pointerLeft = i + 1;
            int pointerRight = nums.length -1;
            while(pointerLeft < pointerRight){
                int sum = nums[pointerLeft] + nums[i] + nums[pointerRight];
               
                if(sum == 0){
                    // Got one !
                    results.add(Arrays.asList(nums[pointerLeft], nums[i], nums[pointerRight]));
                    // Skip duplicates for left and right pointers
                    while(pointerLeft < pointerRight && pointerLeft == nums[pointerLeft + 1]){ pointerLeft++; }
                    while(pointerRight > pointerLeft && pointerRight == nums[pointerRight - 1]){ pointerRight--; }
                    pointerLeft++;
                    pointerRight--;
                }else if (sum < 0){
                    // Sum is too small, move left pointer to try to increase the sum
                    pointerLeft++;
                }else{
                    // Sum is too small, move right pointer to try to increase the sum
                    pointerRight--;
                }
            }
        }
        return new ArrayList<>(results);
    }
}
</pre>


==== [[Index.php?title=MergeSort|MergeSort]] ====
==== [[Index.php?title=MergeSort|MergeSort]] ====

Revision as of 03:34, 6 October 2025

Tips

Array Algoritms

Kadane’s Algorithm - adapted on cumulated products

    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;
    }

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;
    }

3 Sum

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        Set<List<Integer>> results = new HashSet<>();

        for(int i = 0; i < nums.length - 2; i++){
            // Skip duplicates for fixed element
            if (i > 0 && nums[i] == nums [i - 1]) continue;

            int pointerLeft = i + 1;
            int pointerRight = nums.length -1;

            while(pointerLeft < pointerRight){
                int sum = nums[pointerLeft] + nums[i] + nums[pointerRight];
                
                if(sum == 0){
                    // Got one !
                    results.add(Arrays.asList(nums[pointerLeft], nums[i], nums[pointerRight]));

                    // Skip duplicates for left and right pointers
                    while(pointerLeft < pointerRight && pointerLeft == nums[pointerLeft + 1]){ pointerLeft++; }
                    while(pointerRight > pointerLeft && pointerRight == nums[pointerRight - 1]){ pointerRight--; }

                    pointerLeft++;
                    pointerRight--;
                }else if (sum < 0){
                    // Sum is too small, move left pointer to try to increase the sum
                    pointerLeft++;
                }else{
                    // Sum is too small, move right pointer to try to increase the sum
                    pointerRight--;
                }
            }
        }
        return new ArrayList<>(results);
    }
}

MergeSort

Graphs

  • Dijkstra
  • Topologic sorting

Cryptography & Compression

  • Shannon-Fano
  • Huffman
  • Diffie-Hellman
  • RSA

Prime Numbers

  • ?

Most beautiful Equation