From 4228ce24c619a1ffe573b1b83fe4c06c3101fd5a Mon Sep 17 00:00:00 2001 From: Venkat Palampally Date: Tue, 5 Jun 2018 11:02:23 -0700 Subject: [PATCH 01/11] Minor modifications --- strings/lengthOfLongestSubstring.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/strings/lengthOfLongestSubstring.go b/strings/lengthOfLongestSubstring.go index c36cd22..ab4c312 100644 --- a/strings/lengthOfLongestSubstring.go +++ b/strings/lengthOfLongestSubstring.go @@ -9,16 +9,19 @@ import ("fmt" func lengthOfLongestSubstring(s string) int { charHash := make(map[string]int) - start := -1 + start := 0 maxLen := 0 for idx,rune := range s { if valIdx,ok := charHash[string(rune)] ; ok { - start = valIdx + for _,rune := range s[start:valIdx] { + delete(charHash,string(rune)) + } + start = valIdx + 1 charHash[string(rune)] = idx } else { charHash[string(rune)] = idx - if maxLen < (idx - start) { - maxLen = idx - start + if maxLen < (idx - start + 1) { + maxLen = idx - start + 1 } } } @@ -28,6 +31,7 @@ func lengthOfLongestSubstring(s string) int { func main() { //s1 := "abcabcbb" s2 := "abcdecdfghi" + //s2 := "tmmzuxt" length := lengthOfLongestSubstring(s2) fmt.Println("Length of Longest Substring in the string ",s2,"is :",length) } From 5d41b1273a76e72d75df2c43993fdc6675195987 Mon Sep 17 00:00:00 2001 From: Venkat Palampally Date: Tue, 5 Jun 2018 16:26:05 -0700 Subject: [PATCH 02/11] Kadanes Algorithm --- MathAndStats/Kadanes.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 MathAndStats/Kadanes.go diff --git a/MathAndStats/Kadanes.go b/MathAndStats/Kadanes.go new file mode 100644 index 0000000..53dab5b --- /dev/null +++ b/MathAndStats/Kadanes.go @@ -0,0 +1,29 @@ +/* +Largest Sum Contiguous SubArray +*/ + +package main + +import ("fmt") + +func Kadanes(array *[]int) int { + MaxSoFar := 0 + GlobalMax := 0 + for _, val := range *array { + if (MaxSoFar + val > val) { + MaxSoFar += val + } else { + MaxSoFar = val + } + if MaxSoFar >= GlobalMax{ + GlobalMax = MaxSoFar + } + } + return GlobalMax +} + +func main() { + array := []int{-5,6,7,1,4,-8,16} + MaxSum := Kadanes(&array) + fmt.Println("Largest Sum in the given contiguous Subarray is:",MaxSum) +} From efd4b1d131c1278e073e0ee5e57b3e284606ab1a Mon Sep 17 00:00:00 2001 From: Venkat Palampally Date: Tue, 5 Jun 2018 23:47:07 -0700 Subject: [PATCH 03/11] Added Fibonacci, GameScoring --- .gitignore | 1 + DynamicProgramming/Fibonacci.go | 36 +++++++++++++ DynamicProgramming/GameScoring.go | 64 ++++++++++++++++++++++++ DynamicProgramming/KnapSack.go | 2 +- MathAndStats/AllSubset.go | 50 ++++++++++++++++++ MathAndStats/FindKthPermutation.go | 1 - README.md | 7 ++- arrays/MergeOverlappingIntervals.go | 35 +++++++++++++ linkedList/deleteNodeLinkedList.go | 16 ++++++ linkedList/mergeTwoLinkedLists.go | 38 ++++++++++++++ linkedList/src/github.com/uudashr/gopkgs | 1 + 11 files changed, 248 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 DynamicProgramming/Fibonacci.go create mode 100644 DynamicProgramming/GameScoring.go create mode 100644 MathAndStats/AllSubset.go create mode 100644 arrays/MergeOverlappingIntervals.go create mode 100644 linkedList/deleteNodeLinkedList.go create mode 100644 linkedList/mergeTwoLinkedLists.go create mode 160000 linkedList/src/github.com/uudashr/gopkgs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/DynamicProgramming/Fibonacci.go b/DynamicProgramming/Fibonacci.go new file mode 100644 index 0000000..486e873 --- /dev/null +++ b/DynamicProgramming/Fibonacci.go @@ -0,0 +1,36 @@ +/* +Fibonacci sequence using recursion and Memoization +Returns the value of the nth Fibonacci number +*/ + +package main + +import ("fmt") + +// Recursion +func FibonacciRecursion(n int) int { + if n == 0 || n == 1 { + return 1 + } + return FibonacciRecursion(n-1) + FibonacciRecursion(n-2) +} + +// Memoization can be done using hashmap or also by using an array +func FibonacciMemoization(n int) int { + var MemoizedArray = []int{1,1} + if n == 0 || n == 1 { + return 1 + } + for i:=2;i= 0 { + NumWays += MemoizedArray[tot - val] + } else { + continue + } + } + MemoizedArray = append(MemoizedArray,NumWays) + } + return MemoizedArray[Total] +} + +func main() { + PossibleRuns := []int{5,6,7} + Total := 11 + + + // Bottom up Approach + NumWaysBottomUp := GameScoringBottomUp(Total, PossibleRuns) + fmt.Println(fmt.Sprintf("Number of ways of getting to %d with possible runs %v and BottomUp Approach is %d",Total,PossibleRuns,NumWaysBottomUp)) + + + // Create a MemoizedMap and pass it to the Memoization Function + var MemoizedMap = make(map[int]int) + MemoizedMap[0] = 1 + NumWaysMemoization := GameScoringMemoization(Total, PossibleRuns, MemoizedMap) + fmt.Println(fmt.Sprintf("Number of ways of getting to %d with possible runs %v and Memoization Approach is %d",Total,PossibleRuns,NumWaysMemoization)) + +} \ No newline at end of file diff --git a/DynamicProgramming/KnapSack.go b/DynamicProgramming/KnapSack.go index b854ad8..f94235d 100644 --- a/DynamicProgramming/KnapSack.go +++ b/DynamicProgramming/KnapSack.go @@ -24,7 +24,7 @@ func KnapSackRecursion(ItemWeight,ItemValue []int,n,c int) int { } } -// Memoized version of the problem, which minimizes recusrsion +// Memoized version of the problem, which minimizes recursion func KnapSackMemoization(MemoizedMap map[string]int,ItemWeight,ItemValue []int,n,c int) int { nc := fmt.Sprintf("%d:%d",n,c) if val,ok := MemoizedMap[nc];ok { diff --git a/MathAndStats/AllSubset.go b/MathAndStats/AllSubset.go new file mode 100644 index 0000000..6e5d6fb --- /dev/null +++ b/MathAndStats/AllSubset.go @@ -0,0 +1,50 @@ +/* +Find all the subsets of a given set +Two methods will be discussed here +1) Using the bit representation approach +2) Using a recursion tree type of approach +*/ + +package main + +import ("fmt" + "math" +) + +func GetBit(num int,bit uint) uint { + temp := 1 << bit + temp = temp & num + if temp == 0 { + return 0 + } else { + return 1 + } +} + +func AllSubsetsBit(array []int) *[][]int { + var SubsetsArray [][]int + var ArrayLen uint = uint(len(array)) + NumSubsets := math.Pow(float64(2),float64(len(array))) + for i:=0;i Date: Wed, 6 Jun 2018 00:55:25 -0700 Subject: [PATCH 04/11] CoinChange example --- DynamicProgramming/CoinChange.go | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 DynamicProgramming/CoinChange.go diff --git a/DynamicProgramming/CoinChange.go b/DynamicProgramming/CoinChange.go new file mode 100644 index 0000000..0587154 --- /dev/null +++ b/DynamicProgramming/CoinChange.go @@ -0,0 +1,34 @@ +/* +Coin Change Problem, Given a set of Coin denominations find the number of ways of making total amount +This problem is much like the problem in GameScoring except that we dont consider the permutations as in GameScoring +For Example in this problem (1,1,2) is the same as (2,1,1) and (1,2,1) +*/ + +package main + +import ("fmt") + +func Max(a,b int) int { + if a>=b { + return a + } + return b +} + +func CoinChange(Amount int, Denomination []int) int { + var CoinAmountArray = make([]int,Amount+1) + CoinAmountArray[0] = 1 + for _, den := range Denomination { + for amt:=den;amt Date: Wed, 6 Jun 2018 09:25:02 -0700 Subject: [PATCH 05/11] Frog Jump using Hashmap and Hashset --- DynamicProgramming/FrogJump.go | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 DynamicProgramming/FrogJump.go diff --git a/DynamicProgramming/FrogJump.go b/DynamicProgramming/FrogJump.go new file mode 100644 index 0000000..20d00ce --- /dev/null +++ b/DynamicProgramming/FrogJump.go @@ -0,0 +1,59 @@ +/* +LEETCODE Problem (403.Frog Jump) +A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. + +Given a list of stones' positions (in units) in sorted ascending order, determine if the frog is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit. + +If the frog's last jump was k units, then its next jump must be either k - 1, k, or k + 1 units. Note that the frog can only jump in the forward direction. + +Note: +The number of stones is ≥ 2 and is < 1,100. +Each stone's position will be a non-negative integer < 231. +The first stone's position is always 0. + +Test Cases: +[0,1,3,5,6,8,12,17] - True +[0,1,2,3,4,8,9,11] - False +*/ + +package main + +import ("fmt") + +// Using HashMap and Set +func canCross(stone []int) bool { + var m = make(map[int]map[int]bool) //key:stoneValue Value:Set of k's to reach that value + // The array holds k values to get to the key, initialize all the keys values to empty arrays + for _,val := range stone { + m[val] = make(map[int]bool) + } + m[0][0] = true + for _,val := range stone { + for lastStep,_ := range m[val] { + for nextStep:=lastStep-1;nextStep<=lastStep+1;nextStep++ { + if _,ok := m[val+nextStep]; ok && nextStep>0 { + m[val+nextStep][nextStep] = true + } + } + } + } + // If the last val array is empty we return false, it's because we were not able to reach the last stone + //fmt.Println(m) + lastVal := m[stone[len(stone) - 1]] + if len(lastVal) != 0 { + return true + } + return false +} + +func main() { + //array1 := []int{0,1,3,5,6,8,12,17} + //array1 := []int{0,1,2,3,4,8,9,11} + array1 := []int{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,530,531,532,533,534,535,536,537,538,539,540,541,542,543,544,545,546,547,548,549,550,551,552,553,554,555,556,557,558,559,560,561,562,563,564,565,566,567,568,569,570,571,572,573,574,575,576,577,578,579,580,581,582,583,584,585,586,587,588,589,590,591,592,593,594,595,596,597,598,599,600,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,668,669,670,671,672,673,674,675,676,677,678,679,680,681,682,683,684,685,686,687,688,689,690,691,692,693,694,695,696,697,698,699,700,701,702,703,704,705,706,707,708,709,710,711,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,735,736,737,738,739,740,741,742,743,744,745,746,747,748,749,750,751,752,753,754,755,756,757,758,759,760,761,762,763,764,765,766,767,768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799,800,801,802,803,804,805,806,807,808,809,810,811,812,813,814,815,816,817,818,819,820,821,822,823,824,825,826,827,828,829,830,831,832,833,834,835,836,837,838,839,840,841,842,843,844,845,846,847,848,849,850,851,852,853,854,855,856,857,858,859,860,861,862,863,864,865,866,867,868,869,870,871,872,873,874,875,876,877,878,879,880,881,882,883,884,885,886,887,888,889,890,891,892,893,894,895,896,897,898,899,900,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927,928,929,930,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,948,949,950,951,952,953,954,955,956,957,958,959,960,961,962,963,964,965,966,967,968,969,970,971,972,973,974,975,976,977,978,979,980,981,982,983,984,985,986,987,988,989,990,991,992,993,994,995,996,997,998,999} + canCros := canCross(array1) + if canCros { + fmt.Println("The Frog can cross the river") + } else { + fmt.Println("The Frog cannot cross the river") + } +} \ No newline at end of file From e9babb0e586a75e162c575dfdd0c67aa9031f933 Mon Sep 17 00:00:00 2001 From: Venkat Palampally Date: Wed, 6 Jun 2018 09:28:03 -0700 Subject: [PATCH 06/11] Frog Jump using Hashmap and Hashset --- DynamicProgramming/FrogJump.go | 41 ++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/DynamicProgramming/FrogJump.go b/DynamicProgramming/FrogJump.go index 20d00ce..205faea 100644 --- a/DynamicProgramming/FrogJump.go +++ b/DynamicProgramming/FrogJump.go @@ -49,8 +49,45 @@ func canCross(stone []int) bool { func main() { //array1 := []int{0,1,3,5,6,8,12,17} //array1 := []int{0,1,2,3,4,8,9,11} - array1 := []int{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,530,531,532,533,534,535,536,537,538,539,540,541,542,543,544,545,546,547,548,549,550,551,552,553,554,555,556,557,558,559,560,561,562,563,564,565,566,567,568,569,570,571,572,573,574,575,576,577,578,579,580,581,582,583,584,585,586,587,588,589,590,591,592,593,594,595,596,597,598,599,600,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,668,669,670,671,672,673,674,675,676,677,678,679,680,681,682,683,684,685,686,687,688,689,690,691,692,693,694,695,696,697,698,699,700,701,702,703,704,705,706,707,708,709,710,711,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,735,736,737,738,739,740,741,742,743,744,745,746,747,748,749,750,751,752,753,754,755,756,757,758,759,760,761,762,763,764,765,766,767,768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799,800,801,802,803,804,805,806,807,808,809,810,811,812,813,814,815,816,817,818,819,820,821,822,823,824,825,826,827,828,829,830,831,832,833,834,835,836,837,838,839,840,841,842,843,844,845,846,847,848,849,850,851,852,853,854,855,856,857,858,859,860,861,862,863,864,865,866,867,868,869,870,871,872,873,874,875,876,877,878,879,880,881,882,883,884,885,886,887,888,889,890,891,892,893,894,895,896,897,898,899,900,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927,928,929,930,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,948,949,950,951,952,953,954,955,956,957,958,959,960,961,962,963,964,965,966,967,968,969,970,971,972,973,974,975,976,977,978,979,980,981,982,983,984,985,986,987,988,989,990,991,992,993,994,995,996,997,998,999} - canCros := canCross(array1) + array1 := []int{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, + 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66, + 67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100, + 101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126, + 127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152, + 153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178, + 179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204, + 205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230, + 231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256, + 257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282, + 283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309, + 310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336, + 337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363, + 364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390, + 391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417, + 418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444, + 445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471, + 472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498, + 499,500,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524,525, + 526,527,528,529,530,531,532,533,534,535,536,537,538,539,540,541,542,543,544,545,546,547,548,549,550,551,552, + 553,554,555,556,557,558,559,560,561,562,563,564,565,566,567,568,569,570,571,572,573,574,575,576,577,578,579, + 580,581,582,583,584,585,586,587,588,589,590,591,592,593,594,595,596,597,598,599,600,601,602,603,604,605,606, + 607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631,632,633, + 634,635,636,637,638,639,640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660, + 661,662,663,664,665,666,667,668,669,670,671,672,673,674,675,676,677,678,679,680,681,682,683,684,685,686,687, + 688,689,690,691,692,693,694,695,696,697,698,699,700,701,702,703,704,705,706,707,708,709,710,711,712,713,714, + 715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,735,736,737,738,739,740,741, + 742,743,744,745,746,747,748,749,750,751,752,753,754,755,756,757,758,759,760,761,762,763,764,765,766,767,768, + 769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795, + 796,797,798,799,800,801,802,803,804,805,806,807,808,809,810,811,812,813,814,815,816,817,818,819,820,821,822, + 823,824,825,826,827,828,829,830,831,832,833,834,835,836,837,838,839,840,841,842,843,844,845,846,847,848,849, + 850,851,852,853,854,855,856,857,858,859,860,861,862,863,864,865,866,867,868,869,870,871,872,873,874,875,876, + 877,878,879,880,881,882,883,884,885,886,887,888,889,890,891,892,893,894,895,896,897,898,899,900,901,902,903, + 904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927,928,929,930, + 931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,948,949,950,951,952,953,954,955,956,957, + 958,959,960,961,962,963,964,965,966,967,968,969,970,971,972,973,974,975,976,977,978,979,980,981,982,983,984, + 985,986,987,988,989,990,991,992,993,994,995,996,997,998,999} + + canCros := canCross(array1) if canCros { fmt.Println("The Frog can cross the river") } else { From 47cfaaf4da146b0529c85c68acb6fae67fbf2643 Mon Sep 17 00:00:00 2001 From: Venkat Palampally Date: Wed, 6 Jun 2018 09:30:16 -0700 Subject: [PATCH 07/11] removed trash files --- linkedList/src/github.com/uudashr/gopkgs | 1 - 1 file changed, 1 deletion(-) delete mode 160000 linkedList/src/github.com/uudashr/gopkgs diff --git a/linkedList/src/github.com/uudashr/gopkgs b/linkedList/src/github.com/uudashr/gopkgs deleted file mode 160000 index f5479ab..0000000 --- a/linkedList/src/github.com/uudashr/gopkgs +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f5479abf3b43b1fd739014debddd4b341a18e963 From 1041fab693cc444c10f6990285424f5d43390290 Mon Sep 17 00:00:00 2001 From: Venkat Palampally Date: Wed, 6 Jun 2018 11:50:54 -0700 Subject: [PATCH 08/11] Merge Overlapping Intervals with sorting code --- arrays/MergeOverlappingIntervals.go | 82 ++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 18 deletions(-) diff --git a/arrays/MergeOverlappingIntervals.go b/arrays/MergeOverlappingIntervals.go index e1cba73..59a4b8d 100644 --- a/arrays/MergeOverlappingIntervals.go +++ b/arrays/MergeOverlappingIntervals.go @@ -1,35 +1,81 @@ /* -Merge overlapping intervals -List of intervals is given in sorted order and we need to merge the overlapping intervals to give a larger interval -If the intervals are not in sorted order we need to sort them first and then we can use the below approach -Input: {{1,5},{3,7},{4,6},{6,8},{10,12},{11,15}} -Output: {{1,8},{10,15}} +LEET CODE PROBLEM +56. Merge Intervals + +Given a collection of intervals, merge all overlapping intervals. + +Example 1: + +Input: [[1,3],[2,6],[8,10],[15,18]] +Output: [[1,6],[8,10],[15,18]] +Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. +Example 2: + +Input: [[1,4],[4,5]] +Output: [[1,5]] +Explanation: Intervals [1,4] and [4,5] are considerred overlapping. + +APPROACH: +Sort the intervals on Start +Then merge */ package main import ("fmt" + "sort" ) // Defining a tuple type to hold the intervals as go doesn't have a tuple by default -type tuple struct { - first,second interface{} +type Interval struct { + Start,End int } -func MergeOverlappingIntervals(arr *[]tuple) *[]tuple { - outputArray := []tuple - if len(*arr) == 0 { - return arr +func SortIntervalsByStart(intervals []Interval) []Interval { + var indexKeyMap = make(map[int]Interval) //Each item is mapped to an index which is the key + var keyIndexMap = make(map[int][]int) //Each key is mapped to multiple indices, the values at these indices contain the intervals + var sortedIntervals []Interval + var keyArray sort.IntSlice + for idx,interval := range intervals { + indexKeyMap[idx] = interval + keyIndexMap[interval.Start] = append(keyIndexMap[interval.Start],idx) + keyArray = append(keyArray,interval.Start) } - for _,val := range *arr { - + // Sort the key array and iterate over it to create the sorted intervals array + keyArray.Sort() + for _,val := range keyArray { + intervalIdx := keyIndexMap[val][len(keyIndexMap[val])-1] + interval := indexKeyMap[intervalIdx] + keyIndexMap[val] = keyIndexMap[val][:len(keyIndexMap[val])-1] + sortedIntervals = append(sortedIntervals,interval) } + return sortedIntervals +} +func MergeOverlappingIntervals(intervals []Interval) []Interval { + // Sort the array based on Start value of each interval + intervals = SortIntervalsByStart(intervals) + if len(intervals) == 0 { + return intervals + } + var master Interval = intervals[0] + var intervalsArray []Interval + for _,val := range intervals { + if (val.Start <= master.End && val.Start >= master.Start && val.End >= master.End) { + master.End = val.End + } else if (val.Start > master.End) { + intervalsArray = append(intervalsArray,master) + master = val + } + } + intervalsArray = append(intervalsArray,master) + return intervalsArray } + func main() { - array := []tuple{{1,5},{3,7},{4,6},{6,8},{10,12},{11,15}} - fmt.Println(array) - MergeOverlappingIntervals(&array) - -} + array := []Interval{{1,5},{0,7},{2,6},{5,8},{4,12},{0,15}} + //fmt.Println(array) + mergedIntervals := MergeOverlappingIntervals(array) + fmt.Println(mergedIntervals) +} \ No newline at end of file From d4431ee838af944a61c59c02072fe8bd0b7cac85 Mon Sep 17 00:00:00 2001 From: Venkat Palampally Date: Tue, 17 Jul 2018 09:12:15 -0700 Subject: [PATCH 09/11] A bunch of edits --- .../MaxSumSubsequenceNonAdjacent.go | 40 ++++++ README.md | 13 ++ StackAndQueue/StackUsingQueue.go | 43 ++++++ arrays/maxInSlidingWindow.go | 9 +- arrays/maxSingleSellProfit.go | 51 +++++++ arrays/mergeSort.go | 17 +++ binarySearchTree/BST.go | 133 ++++++++++++++++++ linkedList/RotateLinkedList.go | 93 ++++++++++++ 8 files changed, 395 insertions(+), 4 deletions(-) create mode 100644 DynamicProgramming/MaxSumSubsequenceNonAdjacent.go create mode 100644 StackAndQueue/StackUsingQueue.go create mode 100644 arrays/maxSingleSellProfit.go create mode 100644 arrays/mergeSort.go create mode 100644 binarySearchTree/BST.go create mode 100644 linkedList/RotateLinkedList.go diff --git a/DynamicProgramming/MaxSumSubsequenceNonAdjacent.go b/DynamicProgramming/MaxSumSubsequenceNonAdjacent.go new file mode 100644 index 0000000..c826b5a --- /dev/null +++ b/DynamicProgramming/MaxSumSubsequenceNonAdjacent.go @@ -0,0 +1,40 @@ +/* +Maximum Subsequence of Non Adjacent Elements + +Example - [1,3,10,14,-5,-3,2,-2,3] +Output - 22 from elements [3,14,2,3] + +We will use dynamic programming to solve this problem +*/ + +package main + +import ("fmt") + +func Max(a,b int) int { + if a>=b { + return a + } + return b +} + +func MaxSubsequenceNonAdjacent(array []int) int { + var maxSoFarArray = make([]int,len(array)) + maxSoFarArray[0] = array[0] + maxSoFarArray[1] = Max(maxSoFarArray[0],array[1]) + for idx,val := range array { + if (idx == 0 || idx == 1) { + continue + } else { + maxSoFarArray[idx] = Max(maxSoFarArray[idx-1],maxSoFarArray[idx-2]+val) + } + } + return maxSoFarArray[len(array)-1] +} + +func main() { + array := []int{1,6,10,14,-5,-1,2,-1,3} + //array := []int{1,-1,6,-4,2,2} + maxSubsequenceNonAdjacentSum := MaxSubsequenceNonAdjacent(array) + fmt.Println("The Maximum sum of Non adjacent subsequence is:",maxSubsequenceNonAdjacentSum) +} \ No newline at end of file diff --git a/README.md b/README.md index 8c53a23..34fbe68 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,19 @@ # AlgorithmsGolang +I started working on this repo as a way to showcase my skills in algorithms and data structures. During the process I realized that there are some common pattern which can be extended to many type of algorithms. Any problem can be converted into one of these base patterns. + +The number of problems I have here is more than the base patterns. + I have created this repository to provide those important patterns that will help people to prepare for interviews. The algorithms here are not comprehensive and the repo is still a work in progress +## Patterns + +### Dynamic Programming +- Fibonacci Problem +- Frog Jumping Problem +- Coin Change Problem +- Game Scoring Problem + +### \ No newline at end of file diff --git a/StackAndQueue/StackUsingQueue.go b/StackAndQueue/StackUsingQueue.go new file mode 100644 index 0000000..f179ef5 --- /dev/null +++ b/StackAndQueue/StackUsingQueue.go @@ -0,0 +1,43 @@ +/* +Implemet a Stack using Queues + +Method 1: Use two Queues to implement a stack. PUSH:O() POP:O() + +Method 2: Use two Queues to implement a stack. PUSH:O() POP:O() + +"github.com/golang-collections/collections/stack" +type Stack +func New() *Stack +func (this *Stack) Len() int +func (this *Stack) Peek() interface{} +func (this *Stack) Pop() interface{} +func (this *Stack) Push(value interface{}) + +"github.com/golang-collections/go-datastructures/queue" +type Queue +func New(hint int64) *Queue +func (q *Queue) Dispose() +func (q *Queue) Disposed() bool +func (q *Queue) Empty() bool +func (q *Queue) Get(number int64) ([]interface{}, error) +func (q *Queue) Len() int64 +func (q *Queue) Put(items ...interface{}) error +func (q *Queue) TakeUntil(checker func(item interface{}) bool) ([]interface{}, error) + +*/ + +package main + +import ("github.com/golang-collections/go-datastructures/queue" + "github.com/golang-collections/collections/stack" +) + +type StackUsingQueue struct { + // Initialized +} + +func (*) + +func main() { + +} \ No newline at end of file diff --git a/arrays/maxInSlidingWindow.go b/arrays/maxInSlidingWindow.go index 372a22d..9e38a8a 100644 --- a/arrays/maxInSlidingWindow.go +++ b/arrays/maxInSlidingWindow.go @@ -62,10 +62,11 @@ func maxInSlidingWindow(arr *[]int,windowSize int) { } func main() { - arr := []int{-4,2,-5,1,-1,6} - - - maxInSlidingWindow(&arr,3) + //arr := []int{-4,2,-5,1,-1,6} + arr := []int{1,3,-1,-3,5,3,6,7} + k := 3 + + maxInSlidingWindow(&arr,k) //fmt.Println(dll) //fmt.Println(l.Front()) diff --git a/arrays/maxSingleSellProfit.go b/arrays/maxSingleSellProfit.go new file mode 100644 index 0000000..0a0b0ee --- /dev/null +++ b/arrays/maxSingleSellProfit.go @@ -0,0 +1,51 @@ +/* +Find maximum single sell profit +Given a list of stock prices for n days, find the maximum profit with a single buy/sell activity. + +Description: +Given a list of day stock prices (integers for simplicity), find the maximum single sell profit. + +We need to maximize the single buy/sell profit and in case we can't make any profit, we'll try to minimize the loss. +For below examples, buy and sell prices for making maximum profit are highlighted. +*/ + +package main + +import ("fmt" + "math" +) + +type BuySell struct { + buy,sell int +} + +func maxSingleSellProfit(array []int) *BuySell { + currentBuy := array[0] + globalSell := array[1] + globalProfit := globalSell - currentBuy + currentProfit := math.MinInt64 + var BS = new(BuySell) + + for i:=1;i globalProfit) { + globalProfit = currentProfit + globalSell = array[i] + } + + if (array[i] < currentBuy) { + currentBuy = array[i] + } + } + BS.buy = globalSell - globalProfit + BS.sell = globalSell + return BS +} + + +func main() { + array := []int{21,12,11,9,6,3} + //array := []int{12,5,9,19} + BS := maxSingleSellProfit(array) + fmt.Println("BuyPrice:",BS.buy,"SellPrice:",BS.sell) +} \ No newline at end of file diff --git a/arrays/mergeSort.go b/arrays/mergeSort.go new file mode 100644 index 0000000..19b28d1 --- /dev/null +++ b/arrays/mergeSort.go @@ -0,0 +1,17 @@ +/* +Sorting an Array through Merge Sort +*/ + +package main + +import ("fmt") + +func MergeSort(array []int) []int { + +} + +func main() { + array := []int{7,2,4,9,10,1,3,1,24,12} + sortedArray := MergeSort(array) + fmt.Println(sortedArray) +} \ No newline at end of file diff --git a/binarySearchTree/BST.go b/binarySearchTree/BST.go new file mode 100644 index 0000000..2886df2 --- /dev/null +++ b/binarySearchTree/BST.go @@ -0,0 +1,133 @@ +/* +Implementation of a binary search tree operations (Insert, Search, Delete) +*/ + +package main + +import ("fmt" + //"container/list" +) + +type Element struct { + + // The value in stored in the element + Value interface{} + left, right *Element + + // The binary search tree to which the element belongs + bst *Bst +} + +type Bst struct { + root Element +} + +// Creates a new Bst Element and initializes it +func New() *Bst { + return new(Bst).Init() +} + +func (b *Bst) Init() *Bst { + b.root.right = nil + b.root.left = nil + return b +} + +func (b *Bst) Insert(val interface{}) { + temp := &b.root + for true { + if (temp.Value == nil) { + temp.Value = val + temp.bst = b + return + } else if (val.(int) >= temp.Value.(int) && temp.right == nil) { + temp.right = new(Element) + temp.right.Value = val + temp.right.bst = b + return + } else if (val.(int) < temp.Value.(int) && temp.left == nil) { + temp.left = new(Element) + temp.left.Value = val + temp.left.bst = b + return + } else if (val.(int) >= temp.Value.(int) && temp.right != nil) { + temp = temp.right + } else { + temp = temp.left + } + } +} + +// Returns if a value exists in the Binary Search Tree +func (b *Bst) Search(val interface{}) bool { + temp := &b.root + for true { + if (temp.Value.(int) == val.(int)) { + return true + } else if (val.(int) >= temp.Value.(int) && temp.right != nil) { + temp = temp.right + } else if (val.(int) < temp.Value.(int) && temp.left != nil) { + temp = temp.left + } else { + return false + } + } + return false +} + +func (b *Bst) Remove(val interface{}) { + +} + +// Inorder Traversal (Left, Root, Right) +func (b *Bst) Inorder(root *Element) { + if (root == nil) { + return + } + b.Inorder(root.left) + fmt.Println(root.Value) + b.Inorder(root.right) +} + +func (b *Bst) Preorder(root *Element) { + if (root == nil) { + return + } + fmt.Println(root.Value) + b.Preorder(root.left) + b.Preorder(root.right) +} + +func (b *Bst) Postorder(root *Element) { + if (root == nil) { + return + } + b.Postorder(root.left) + b.Postorder(root.right) + fmt.Println(root.Value) +} + +func (b *Bst) Levelorder() { + +} + +/* +func ArrayToBST(array []int) { + +} +*/ + +func main() { + // Instantiate a random BST with the root element + array := []int{5,10,12,7,6,21,35,45,22,100} + b := New() + for _, val := range array { + b.Insert(val) + } + fmt.Println(b.Search(7)) + b.Inorder(&b.root) + b.Preorder(&b.root) + b.Postorder(&b.root) + //fmt.Println(b.root) + //fmt.Println(b.root.right) +} diff --git a/linkedList/RotateLinkedList.go b/linkedList/RotateLinkedList.go new file mode 100644 index 0000000..12569a1 --- /dev/null +++ b/linkedList/RotateLinkedList.go @@ -0,0 +1,93 @@ +/* +LEET CODE PROBLEM +61. Rotate List + +Given a linked list, rotate the list to the right by k places, where k is non-negative. + +Example 1: + +Input: 1->2->3->4->5->NULL, k = 2 +Output: 4->5->1->2->3->NULL +Explanation: +rotate 1 steps to the right: 5->1->2->3->4->NULL +rotate 2 steps to the right: 4->5->1->2->3->NULL +Example 2: + +Input: 0->1->2->NULL, k = 4 +Output: 2->0->1->NULL +Explanation: +rotate 1 steps to the right: 2->0->1->NULL +rotate 2 steps to the right: 1->2->0->NULL +rotate 3 steps to the right: 0->1->2->NULL +rotate 4 steps to the right: 2->0->1->NUL +*/ + +package main + +import "fmt" + +type ListNode struct { + Val int + Next *ListNode +} + +func rotateRight(head *ListNode, k int) *ListNode { + listLen := 1 + var n_k int + // make an initial pass through the list and get the length of the list + temp := head + //fmt.Println(temp) + for temp.Next != nil { + listLen++ + temp = temp.Next + } + // This will tell us the real value of k keeping view the list length + k = k%listLen + if listLen == 0 || k == 0 { + return head + } else if (listLen == k) { + return head + } + n_k = listLen - k + + // Traverse n_k elements ahead and sever the connection + temp = head + for i:=0;i Date: Fri, 19 Jun 2020 18:03:55 +0000 Subject: [PATCH 10/11] Add .ApplyDouble files to .gitignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e43b0f9..4f83207 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.DS_Store +**/.DS_Store +**/.AppleDouble + From 85dc3bc1d02be7ffdc084587c4cb0411f58b0967 Mon Sep 17 00:00:00 2001 From: Venkat Gokul Reddy Palampally Date: Fri, 19 Jun 2020 18:23:43 +0000 Subject: [PATCH 11/11] Add .DSStore and .AppleDouble to .gitignore --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 4f83207..2f65dce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -**/.DS_Store -**/.AppleDouble +.DS_Store +.AppleDouble pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy