Download presentation
Presentation is loading. Please wait.
Published byLorraine Adams Modified over 9 years ago
1
Problem Analysis September 18, 2015 CSE 232, Shane Carr
2
Down By The Bay: Language Stats
3
Five Strategies Based on those in CtCI and CP3 Brute Force and Fix Base Case and Build Simplify and Generalize Reduce and Relate Do It Yourself
4
Brute Force and Fix 1. Bottlenecks: Focus on improving the slowest step. Improving a faster step won’t improve your overall time complexity. 2. Unnecessary Work: Reduce your search space. Are there cases you’re testing that are redundant? 3. Duplicated Work: Eliminate repeated sub-computations. Think Fibonacci. Print all positive integer solutions to the equation a 3 + b 3 = c 3 + d 3 subject to the constraints 0 < a < b < 1000 0 < c < d < 1000 a < c CtCI chapter VII
5
Base Case and Build Model the problem as a recurrence. Suppose you were handed the solution to a problem of size N-1. How could you use that information to solve the problem of size N? Can you split the problem in half, solve each half, and combine the two halves back together? (D&C) Given an array of distinct integer values, count the number of pairs of integers that have difference k. For example, given the array {1, 7, 5, 9, 2, 12, 3} and the difference k=2, you would find the pairs (1,3), (3,5), (5,7), and (7,9). CtCI chapter VII
6
Simplify and Generalize Are there constraints you can add or remove from the problem? Try solving a trivial case, and incrementally make it more difficult until you get the original problem. There is an ant standing at each vertex of a polygon with N sides. If each ant chooses a direction at random and starts walking at the same speed along the edges of the polygon, what is the probability that two ants will run into each other? CtCI problem 6.4
7
Reduce and Relate Setting aside the problem’s theme for a moment, does it sound like something we studied before in this class? Telltale Signs Graph problems Binary trees Strings Points / Geometry The Prussian Parakeet is known for its unusual mating pattern. At sunrise on April 1 every year, each parakeet flies toward its nearest neighbor, meeting it at the midpoint between their original locations. Given a list of coordinates of parakeets, determine the first point at which mating will occur. Assume all parakeets fly at the same speed.
8
Do It Yourself Guess what: you’re a human! Humans are smart! Try solving the example input yourself. How did you do it? Does it give a hint to an efficient algorithm that a computer could use? You are given two strings of length N. Can you determine is one is an anagram of the other? For example, given the strings “programming” and “imronmrgpag”, you would return True. CtCI problem 1.2
9
One possible problem- solving process Solve the example input by hand. Think about the problem as a recurrence. Simplify the problem down to a few easy cases. Look for bottlenecks, duplicated work, and unnecessary work. Consider how a new data structure like a Hash Table could help.
10
Examples How would you solve each of these problems?
11
Example 1: Palindromes You are given a string of length N. Can you determine if it is a permutation (anagram) of a palindrome? For example, given “Tact Coa”, you would return True, since that string is an anagram of “Taco Cat”. CtCI problem 1.4
12
Example 2: One Away You are given two strings of lengths M and N, M≤N. You are allowed to make three kinds of edits: insert, remove, and replace. Can you determine if the two strings are one edit (or zero edits) away from each other? For example: PALE, PLE -> truePALES, PALE -> true PALE, BALE -> truePALE, BAE -> false CtCI problem 1.5
13
Example 3: Intersection You are given two singly linked lists. Determine if the two lists intersect. 3 -> 1 -> 5 -> 9 -> 7 -> 2 -> 1 4 -> 6 --^ 3 -> 1 -> 5 -> 9 -> 7 -> 2 -> 1 4 -> 6 -> 7 -> 2 -> 1 CtCI problem 2.7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.