1337code Algorithm Solutions

website·Indexed 31 days ago

https://1337code.xyz/

A collection of problem statements and solutions for medium-difficulty algorithmic challenges. Covers a wide range of topics including dynamic programming, backtracking, graph theory, and data structure design, featuring problems such as Course Schedule, Word Break, and Longest Increasing Subsequence.

Tokens
78.6K
Snippets
242
Records
310
Agent score
55%

What's inside 1337code

  1. Calculate the number of set bits for all integers up to n

    Given an integer n, the goal is to return an array ans of length n + 1 where each element ans[i] represents the number of 1s in the binary representation of the integer i.

    Constraints: 0 <= n <= 10^5.

  2. Determine if a binary tree has a root-to-leaf path with a specific sum

    Given the root of a binary tree and an integer targetSum, the objective is to return true if there exists a path from the root node to a leaf node (a node with no children) such that the sum of all node values along that path equals targetSum. If the tree is empty, it contains no root-to-leaf paths and should return false.

    Constraints:

    • Number of nodes: 0 to 5,000
    • Node values: -1,000 to 1,000
    • targetSum: -1,000 to 1,000
  3. Find the length of the longest continuous increasing subsequence

    Given an unsorted array of integers nums, the goal is to find the length of the longest continuous increasing subsequence (subarray). The subsequence must be strictly increasing, meaning for every element at index i, nums[i] < nums[i + 1] must hold true for the duration of the subsequence.

    Constraints:

    • Array length: 1 <= nums.length <= 10^4
    • Element values: -10^9 <= nums[i] <= 10^9
    Example 1:
    Input: nums = [1,3,5,4,7]
    Output: 3
    Explanation: [1,3,5] is the longest continuous increasing subsequence.
    
    Example 2:
    Input: nums = [2,2,2,2,2]
    Output: 1
    Explanation: Must be strictly increasing, so the longest is any single element [2].
  4. Populate next right pointers in a binary tree

    Given a binary tree where each node has a next pointer (initially NULL), the goal is to populate each next pointer to point to its next right node on the same level. If no next right node exists, it remains NULL.

    Constraints:

    • Number of nodes: 0 to 6000
    • Node values: -100 to 100

    Node Structure:

    • val: integer
    • left: Node pointer
    • right: Node pointer
    • next: Node pointer
  5. Find all starting indices of a substring concatenation

    Given a string s and an array of strings words where all words are of the same length, the goal is to find all starting indices of substrings in s that are a concatenation of every word in words exactly once, in any order, and without intervening characters.
  6. Calculate the number of range sums within a given interval

    Given an integer array nums and two integers lower and upper, the goal is to find the number of range sums S(i, j) (the sum of elements from index i to j inclusive, where i <= j) that fall within the inclusive range [lower, upper].

    Constraints:

    • 1 <= nums.length <= 10^5
    • -2^31 <= nums[i] <= 2^31 - 1
    • -10^5 <= lower <= upper <= 10^5
    • The result is guaranteed to fit in a 32-bit integer.
  7. Find the median of two sorted arrays

    Given two sorted arrays nums1 and nums2 of size m and n, the goal is to find the median of the combined sorted arrays. The required time complexity is O(log(m+n)).

    Constraints:

    • 0 <= m, n <= 1000
    • 1 <= m + n <= 2000
    • Elements range from -10^6 to 10^6.
  8. Remove minimum invalid parentheses to make a string valid

    Given a string s containing parentheses and letters, the goal is to remove the minimum number of invalid parentheses so that the resulting string is valid. All possible results must be returned, and the order of the results does not matter.

    Constraints:

    • String length: 1 <= s.length <= 25
    • Characters: Lowercase English letters and parentheses '(' and ')'
    • Maximum of 20 parentheses in the string.
    Example 1:
    Input: s = "()())()"
    Output: ["(())()","()()()"]
    
    Example 2:
    Input: s = "(a)())()"
    Output: ["(a())()","(a)()()"]
    
    Example 3:
    Input: s = ")("
    Output: [""]
  9. Find the Lowest Common Ancestor (LCA) of two binary tree nodes

    The Lowest Common Ancestor (LCA) of two nodes p and q is the lowest node in a binary tree that has both p and q as descendants. A node is considered a descendant of itself.

    Constraints:

    • Number of nodes: [2, 10^5]
    • Node values: -10^9 to 10^9 and are unique.
    • p and q are distinct and guaranteed to exist in the tree.
  10. Understand 1337code.xyz implementation conventions

    When using the solutions on 1337code.xyz, be aware of the following implementation and notation standards:

    • Language: All solutions are written in JavaScript.
    • Slicing Notation: Explanations use Python-style array slicing (e.g., arr[i:j] refers to elements from index i up to, but not including, index j).
    • Dynamic Programming: All DP solutions utilize a bottom-up approach.
    • Data Structures:
      • Min/Max Heaps: Implemented using the datastructures-js/priority-queue library.
      • Queues/Deques: Simulated using standard JavaScript arrays (Note: shift() and unshift() operations are not O(1)).
      • Maps/Sets: Frequently simulated using plain JavaScript objects, though standard Map and Set objects can be used as alternatives.
  11. Find the smallest range covering elements from k sorted lists

    Given k lists of integers sorted in non-decreasing order, find the smallest range [a, b] that contains at least one number from each of the k lists.

    Range [a, b] is considered smaller than [c, d] if:

    1. b - a < d - c
    2. Or if b - a == d - c, then a < c.

    Constraints:

    • 1 <= k <= 3500
    • 1 <= nums[i].length <= 50
    • -10^5 <= nums[i][j] <= 10^5
    • Each list nums[i] is sorted in non-decreasing order.
  12. Count contiguous subarrays with product less than k

    Given an array of integers nums and an integer k, the goal is to find the number of contiguous subarrays where the product of all elements is strictly less than k.

    Constraints:

    • Array length: 1 <= nums.length <= 3 * 10^4
    • Element value: 1 <= nums[i] <= 1000
    • Target product: 0 <= k <= 10^6