algorithm

Insertion Sort with binary search

和自甴很熟 提交于 2021-02-17 19:03:17
问题 When implementing Insertion Sort, a binary search could be used to locate the position within the first i - 1 elements of the array into which element i should be inserted. How would this affect the number of comparisons required? How would using such a binary search affect the asymptotic running time for Insertion Sort? I'm pretty sure this would decrease the number of comparisons, but I'm not exactly sure why. 回答1: Straight from Wikipedia: If the cost of comparisons exceeds the cost of

Find the length of the longest valid parenthesis sequence in a string, in O(n) time

不问归期 提交于 2021-02-17 10:30:42
问题 My friend ran into a question in an interview and he was told that there is an O(n) solution. However, neither of us can think it up. Here is the question: There is a string which contains just ( and ) , find the length of the longest valid parentheses substring, which should be well formed. For example ")()())" , the longest valid parentheses is ()() and the length is 4. I figured it out with dynamic programming, but it is not O(n). Any ideas? public int getLongestLen(String s) { if (s ==

Counting the number of characters inside double quotation marks

蓝咒 提交于 2021-02-17 06:30:07
问题 I want to find the number of characters inside a double quotation mark. For example : Case 1 "Hello World" , "Some output : Error // missing quotation marks after Some Case 2 "Hello Word" , "Some" output : 14 // All quotation marks are complete I have written the program for counting the total characters, the index of the first quotation mark and the total number of quotations marks using recursion. What approach shall I use to solve this problem? 回答1: Here is a working version using

Two egg dropping puzzle variation: unknown/infinite floors

白昼怎懂夜的黑 提交于 2021-02-17 05:46:12
问题 Preface This problem was inspired by a similar question last week on SO, that got deleted before it was clear what the real question was. I think this variation makes a nice problem that I wanted to share. Two Egg Problem A detailed definition and solution can be found here, but I will add a quick summary: Definition You are given two eggs, and access to a k -storey building. Both eggs are identical. The aim is to find out the highest floor f* from which an egg will not break when dropped out

Print all possible paths in binary tree

送分小仙女□ 提交于 2021-02-17 03:29:09
问题 I'm trying to print all possible paths in a binary tree. I am able to print all root to leaf paths, but cannot figure out how to add leaf-to-leaf paths.(i'm using preorder traversal for root to leaf). So, basically: If my tree is 6 / \ 4 0 / \ \ 1 3 1 If want to the code to print all the paths: 6,4,1 6,4,3 6,0,1 1,4,6,0,1 3,4,6,0,1 1,4,3 4,6,0 4,6,0,1 etc. Can anyone please help me with this binary tree? I would really appreciate your help, as I'm new to this society and to Python/Java.

Finding the maximum sum that can be formed from a set, by partitioning it into two subset

一世执手 提交于 2021-02-17 02:55:08
问题 Decription Given a set of numbers S. Find maximum sum such that Sum(A 1 ) = Sum(A 2 ) Where, A 1 ⊂S and A 2 ⊂S and A 1 ⋂A 2 =∅ And Sum(X), is the sum of all elements within the set X. Approach Brute Force The easiest approach is: print maximumSum(0,0,0) def maximumSum(index,sum1,sum2): ans=0 if sum1 == sum2: ans=sum1 if index >= len(S): return ans m1=maximumSum(index+1,sum1+S[index],sum2) m2=maximumSum(index+1,sum1,sum2+S[index]) m3=maximumSum(index+1,sum1,sum2) return max(m,m1,m2,m3) Time

Finding the maximum sum that can be formed from a set, by partitioning it into two subset

那年仲夏 提交于 2021-02-17 02:54:07
问题 Decription Given a set of numbers S. Find maximum sum such that Sum(A 1 ) = Sum(A 2 ) Where, A 1 ⊂S and A 2 ⊂S and A 1 ⋂A 2 =∅ And Sum(X), is the sum of all elements within the set X. Approach Brute Force The easiest approach is: print maximumSum(0,0,0) def maximumSum(index,sum1,sum2): ans=0 if sum1 == sum2: ans=sum1 if index >= len(S): return ans m1=maximumSum(index+1,sum1+S[index],sum2) m2=maximumSum(index+1,sum1,sum2+S[index]) m3=maximumSum(index+1,sum1,sum2) return max(m,m1,m2,m3) Time

Rank and unrank permutations with just one cycle

三世轮回 提交于 2021-02-17 02:04:55
问题 I want to rank and unrank permutations with one cycle in lexicographical order with a given len. A permutation with one cycles is where you can visit in this cycle each element. p:= (2,3,1) is a permutation with one cycle. Has rank 1. p:= (3,1,2) has 1 cycle too, but rank 2, because the permutation is lexicographical greater the frist so it becomes a greater rank. p:= (1,2,3) is a permutation with 3 cycles. (1),(2),(3) How can I efficently rank (permutation with one cycle to rank) and unrank

What is the formula for the minimum number of nodes in a red-black tree of height h?

一曲冷凌霜 提交于 2021-02-16 20:12:07
问题 I have read that it is log(n+1) <= h <= 2*log(n+1) where log is in base 2. However, when trying this out on a few known minimum heights, it does not always work out. So far I know that: For for h = 1, minimum # of nodes = 2. For h = 2, minimum nodes = 4. For h = 3, minimum nodes = 10. However these were done purely by tracing it out using the rules for red-black trees. Should I be taking note of the black-height when trying to find this or is my approach/calculations just completely wrong?

What is the formula for the minimum number of nodes in a red-black tree of height h?

守給你的承諾、 提交于 2021-02-16 20:11:28
问题 I have read that it is log(n+1) <= h <= 2*log(n+1) where log is in base 2. However, when trying this out on a few known minimum heights, it does not always work out. So far I know that: For for h = 1, minimum # of nodes = 2. For h = 2, minimum nodes = 4. For h = 3, minimum nodes = 10. However these were done purely by tracing it out using the rules for red-black trees. Should I be taking note of the black-height when trying to find this or is my approach/calculations just completely wrong?