Problem 1: Unique Snowflake Collection
Problem Statement: At n different times, snowflakes of various shapes fall (represented by distinct integers). We want to collect snowflakes from time a to time b such that no duplicate shapes are collected, and the total number of snowflakes collected is maximized.
Solution Approach: Two Pointers Technique (O(n) time complexity)
- Initialize left and right pointers at the start of the sequence
- Expand the right pointer while the current snowflake hasn't been collected
- If a duplicate is found, record the current window size (right-left+1) as a candidate for maximum
- Move the left pointer forward to exclude the leftmost snowflake
- Continue until the right pointer reaches the end of the sequence
Optimizaton Notes:
- Implement custom max function for performance
- Use fast input/output methods
- Apply discretization to reduce value range
Problem 2: Sequence Duplicate Detection
Problem Statement: Given a sequence defined by a0=1 and ai+1=(A×ai+ai mod B) mod C, find the index of the first duplicate element.
Solution Approach: Iterative Generation with Hash Map Tracking
- Initialize a hash map to track seen elements
>li>Generate sequence elemants starting from a0- For each generated element:
- If not in hash map, add it with a seen flag
- If already in hash map, return current index as answer
Problem 3: Word Frequency Counting
Problem Statement: Count the frequency of each word in a given document.
Solution Approach: Aho-Corasick Automaton
- Build an automaton structure where each node represents a character in a word
- Track the count of words ending at each node
- Construct failure links for efficient pattern matching
- Process the document through the automaton
- For each word, its frequency is the sum of counts in its failure tree subtree
Problem 4: Second Minimum Spanning Tree
Problem Statement: Find the second minimum spanning tree of a graph, where its total weight is strictly greater than the minimum spanning tree but minimal among all such possibilities.
Solution Approach: Modified Kruskal's Algorithm with Preprocessing
- First, find the minimum spanning tree using Kruskal's algorithm
- Preprocess the tree to record the maximum and second-maximum edge weights between all pairs of nodes
- For each non-tree edge, calculate the potential new tree weight by replacing the maximum edge on the path between its endpoints
- The minimum of these potential weights gives the second minimum spanning tree
Problem 5: Maximum Cycle Value in Directed Graph
Problem Statement: In a directed graph, for each node i, find the minimum value in a cycle that passes through node x, and then determine the maximum of these minimum values across all nodes.
Solution Approach: Bidirectional Dijkstra
- Build two versions of the graph: original and reversed
- From node x, perform Dijkstra's algorithm on both graphs to find shortest paths to all other nodes
- For each node i, the sum of distances from x to i and from i back to x forms a cycle passing through x
- The maximum of these sums across all nodes is the solution
Problem 6: Constrained Color Spanning Tree
Problem Statement: Given an undirected weighted graph with edges colored either black or white, find the minimum weight spanning tree that contains exactly 'need' white edges.
Solution Approach: Binary Search with Modified Kruskal
- Use binary search on the weight adjustment value for white edges
- For each candidate adjustment value:
- Modify white edge weights by adding the adjustment value
- Run Kruskal's algorithm to find the minimum spanning tree
- Count the number of white edges in the resulting tree
- Select the adjustment value that yields a tree with exactly 'need' white edges
- Final tree weight is the sum minus adjustment value multiplied by 'need'