├── Algorithms ├── Arrays │ ├── 2D Array DS │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ ├── description 4.png │ │ └── main.go │ ├── Array Manipulation │ │ ├── description 1.png │ │ ├── description 2.png │ │ └── main.go │ ├── Arrays Left Rotation │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ └── main.go │ ├── Minimum Swaps 2 │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ └── main.go │ └── New Year Chaos │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ └── main.go ├── Dictionaries and Hashmaps │ ├── Frequency Queries │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ ├── description 4.png │ │ └── main.go │ ├── Hash Tables Ransom Notes │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ └── main.go │ └── Two Strings │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ └── main.go ├── Dynamic Programming │ └── Candies │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ └── main.go ├── Graphs │ ├── Find the nearest clone │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ ├── description 4.png │ │ └── main.go │ └── Roads and Libraries │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ ├── description 4.png │ │ └── main.go ├── Greedy Algorithms │ └── Max Min │ │ ├── img 1.png │ │ ├── img 2.png │ │ ├── img 3.png │ │ ├── img 4.png │ │ └── main.go ├── Recursion and Backtracking │ └── Recursive Digit Sum │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ └── main.go ├── Search │ ├── Maximum Subarray Sum │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ └── main.go │ └── Pairs │ │ ├── description 1.png │ │ ├── description 2.png │ │ └── main.go ├── Sorting │ ├── Bubble Sort │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ ├── description 4.png │ │ └── main.go │ ├── Mark and Toys │ │ ├── description 1.png │ │ ├── description 2.png │ │ └── main.go │ └── Merge Sort: Counting Inversions │ │ ├── description 1.png │ │ ├── description 2.png │ │ └── main.go ├── Stacks │ └── Largest Rectangle │ │ ├── description 1.png │ │ ├── description 2.png │ │ └── main.go ├── String Manipulation │ ├── Sherlock and the Valid String │ │ ├── description 1.png │ │ ├── description 2.png │ │ └── main.go │ └── Strings: Making Anagrams │ │ ├── description 1.png │ │ ├── description 2.png │ │ └── main.go └── Warm-up │ ├── Counting Valleys │ ├── image 1.png │ ├── image 2.png │ └── main.go │ ├── Jumping On The Cloud │ ├── img 1.png │ ├── img 2.png │ ├── img 3.png │ └── main.go │ ├── Repeated String │ ├── description 1.png │ ├── description 2.png │ ├── description 3.png │ └── main.go │ └── Sock Merchant │ ├── image 1.png │ ├── image 2.png │ └── main.go ├── Artificial Intelligence ├── Bot Building │ ├── bot-clean-large │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ └── main.go │ ├── bot-clean-stochastic │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ └── main.go │ ├── bot-clean │ │ ├── description 1.png │ │ ├── description 2.png │ │ ├── description 3.png │ │ ├── description 4.png │ │ └── main.go │ └── bot-saves-princess │ │ ├── description 1.png │ │ ├── description 2.png │ │ └── main.go └── Statistics and Machine Learning │ └── Matching Questions with their Answers │ ├── description 1.png │ ├── description 2.png │ └── main.go ├── README.md └── SQL └── Weather Observation Station 5 ├── description 1.png ├── description 2.png └── query.sql /Algorithms/Arrays/2D Array DS/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/2D Array DS/description 1.png -------------------------------------------------------------------------------- /Algorithms/Arrays/2D Array DS/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/2D Array DS/description 2.png -------------------------------------------------------------------------------- /Algorithms/Arrays/2D Array DS/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/2D Array DS/description 3.png -------------------------------------------------------------------------------- /Algorithms/Arrays/2D Array DS/description 4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/2D Array DS/description 4.png -------------------------------------------------------------------------------- /Algorithms/Arrays/2D Array DS/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | // Complete the hourglassSum function below. 13 | func hourglassSum(arr [][]int32) int32 { 14 | hTotal := len(arr) 15 | if hTotal < 3 || hTotal > 6 { 16 | return 0 17 | } 18 | 19 | rst := int32(-100) 20 | for i := 1; i < hTotal-1; i++ { 21 | sum, ch := countSum(arr[i-1], arr[i], arr[i+1]) 22 | if !ch { 23 | return 0 24 | } 25 | if sum > rst { 26 | rst = sum 27 | } 28 | } 29 | return rst 30 | } 31 | 32 | func countSum(aRr, bRr, cRr []int32) (int32, bool) { 33 | rst := int32(-100) 34 | for i := 1; i < len(aRr)-1; i++ { 35 | a1 := aRr[i-1] 36 | a2 := aRr[i] 37 | a3 := aRr[i+1] 38 | b2 := bRr[i] 39 | c1 := cRr[i-1] 40 | c2 := cRr[i] 41 | c3 := cRr[i+1] 42 | if !validValue(a1) || !validValue(a2) || !validValue(a3) || !validValue(b2) || !validValue(c1) || !validValue(c2) || !validValue(c3) { 43 | return 0, false 44 | } 45 | sum := a1 + a2 + a3 + b2 + c1 + c2 + c3 46 | if sum > rst { 47 | rst = sum 48 | } 49 | } 50 | return rst, true 51 | } 52 | 53 | func validValue(n int32) bool { 54 | min := -9 55 | max := 9 56 | return int(n) >= min && int(n) <= max 57 | } 58 | 59 | func main() { 60 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 61 | 62 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 63 | checkError(err) 64 | 65 | defer stdout.Close() 66 | 67 | writer := bufio.NewWriterSize(stdout, 1024*1024) 68 | 69 | var arr [][]int32 70 | for i := 0; i < 6; i++ { 71 | arrRowTemp := strings.Split(readLine(reader), " ") 72 | 73 | var arrRow []int32 74 | for _, arrRowItem := range arrRowTemp { 75 | arrItemTemp, err := strconv.ParseInt(arrRowItem, 10, 64) 76 | checkError(err) 77 | arrItem := int32(arrItemTemp) 78 | arrRow = append(arrRow, arrItem) 79 | } 80 | 81 | if len(arrRow) != int(6) { 82 | panic("Bad input") 83 | } 84 | 85 | arr = append(arr, arrRow) 86 | } 87 | 88 | result := hourglassSum(arr) 89 | 90 | fmt.Fprintf(writer, "%d\n", result) 91 | 92 | writer.Flush() 93 | } 94 | 95 | func readLine(reader *bufio.Reader) string { 96 | str, _, err := reader.ReadLine() 97 | if err == io.EOF { 98 | return "" 99 | } 100 | 101 | return strings.TrimRight(string(str), "\r\n") 102 | } 103 | 104 | func checkError(err error) { 105 | if err != nil { 106 | panic(err) 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /Algorithms/Arrays/Array Manipulation/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/Array Manipulation/description 1.png -------------------------------------------------------------------------------- /Algorithms/Arrays/Array Manipulation/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/Array Manipulation/description 2.png -------------------------------------------------------------------------------- /Algorithms/Arrays/Array Manipulation/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println(arrayMaximum()) 7 | } 8 | 9 | func arrayMaximum() uint64 { 10 | var sz, opTt uint64 11 | if _, err := fmt.Scanf("%d %d", &sz, &opTt); err != nil { 12 | return 0 13 | } 14 | 15 | arr := make([]uint64, sz+1) 16 | for { 17 | var k1, k2, val uint64 18 | if _, err := fmt.Scanf("%d %d %d", &k1, &k2, &val); err != nil { 19 | break 20 | } 21 | 22 | k1-- 23 | arr[k1] += val 24 | arr[k2] -= val 25 | } 26 | 27 | var val, mx uint64 28 | for k := range arr { 29 | val += arr[k] 30 | if val > mx { 31 | mx = val 32 | } 33 | } 34 | 35 | return mx 36 | } 37 | -------------------------------------------------------------------------------- /Algorithms/Arrays/Arrays Left Rotation/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/Arrays Left Rotation/description 1.png -------------------------------------------------------------------------------- /Algorithms/Arrays/Arrays Left Rotation/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/Arrays Left Rotation/description 2.png -------------------------------------------------------------------------------- /Algorithms/Arrays/Arrays Left Rotation/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/Arrays Left Rotation/description 3.png -------------------------------------------------------------------------------- /Algorithms/Arrays/Arrays Left Rotation/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | // Complete the rotLeft function below. 13 | func rotLeft(a []int32, d int32) []int32 { 14 | var rst []int32 15 | aMin := 1 16 | aMax := 100000 17 | aTotal := len(a) 18 | if aTotal < aMin || aTotal > aMax || int(d) < aMin || int(d) > aTotal { 19 | return rst 20 | } 21 | if aTotal == int(d) { 22 | return a 23 | } 24 | 25 | iMax := 1000000 26 | left := a[d:] 27 | right := a[:d] 28 | for _, i := range left { 29 | if i < 1 || i > int32(iMax) { 30 | return nil 31 | } 32 | rst = append(rst, i) 33 | } 34 | for _, i := range right { 35 | if i < 1 || i > int32(iMax) { 36 | return nil 37 | } 38 | rst = append(rst, i) 39 | } 40 | 41 | return rst 42 | } 43 | 44 | func main() { 45 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 46 | 47 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 48 | checkError(err) 49 | 50 | defer stdout.Close() 51 | 52 | writer := bufio.NewWriterSize(stdout, 1024*1024) 53 | 54 | nd := strings.Split(readLine(reader), " ") 55 | 56 | nTemp, err := strconv.ParseInt(nd[0], 10, 64) 57 | checkError(err) 58 | n := int32(nTemp) 59 | 60 | dTemp, err := strconv.ParseInt(nd[1], 10, 64) 61 | checkError(err) 62 | d := int32(dTemp) 63 | 64 | aTemp := strings.Split(readLine(reader), " ") 65 | 66 | var a []int32 67 | 68 | for i := 0; i < int(n); i++ { 69 | aItemTemp, err := strconv.ParseInt(aTemp[i], 10, 64) 70 | checkError(err) 71 | aItem := int32(aItemTemp) 72 | a = append(a, aItem) 73 | } 74 | 75 | result := rotLeft(a, d) 76 | 77 | for i, resultItem := range result { 78 | fmt.Fprintf(writer, "%d", resultItem) 79 | 80 | if i != len(result)-1 { 81 | fmt.Fprintf(writer, " ") 82 | } 83 | } 84 | 85 | fmt.Fprintf(writer, "\n") 86 | 87 | writer.Flush() 88 | } 89 | 90 | func readLine(reader *bufio.Reader) string { 91 | str, _, err := reader.ReadLine() 92 | if err == io.EOF { 93 | return "" 94 | } 95 | 96 | return strings.TrimRight(string(str), "\r\n") 97 | } 98 | 99 | func checkError(err error) { 100 | if err != nil { 101 | panic(err) 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /Algorithms/Arrays/Minimum Swaps 2/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/Minimum Swaps 2/description 1.png -------------------------------------------------------------------------------- /Algorithms/Arrays/Minimum Swaps 2/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/Minimum Swaps 2/description 2.png -------------------------------------------------------------------------------- /Algorithms/Arrays/Minimum Swaps 2/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/Minimum Swaps 2/description 3.png -------------------------------------------------------------------------------- /Algorithms/Arrays/Minimum Swaps 2/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | func main() { 13 | fmt.Println(minSwaps()) 14 | } 15 | 16 | func minSwaps() uint64 { 17 | var tt uint64 18 | if _, err := fmt.Scanf("%d", &tt); err != nil { 19 | return 0 20 | } 21 | 22 | line := readLine(bufio.NewReaderSize(os.Stdin, 1024*1024)) 23 | numsStr := strings.Split(line, " ") 24 | var nums []uint64 25 | for _, nStr := range numsStr { 26 | n, err := strconv.ParseInt(nStr, 10, 64) 27 | if err != nil { 28 | return 0 29 | } 30 | nums = append(nums, uint64(n)) 31 | } 32 | 33 | _, s := minSort(nums, tt) 34 | 35 | return s 36 | } 37 | 38 | func findMin(nums []uint64) uint64 { 39 | var min uint64 40 | for _, n := range nums { 41 | if min == 0 || n < min { 42 | min = n 43 | } 44 | } 45 | return min 46 | } 47 | 48 | func minSort(nums []uint64, tt uint64) ([]uint64, uint64) { 49 | min := findMin(nums) 50 | var sorted bool 51 | var s, sNow uint64 52 | 53 | for !sorted { 54 | sNow = 0 55 | for i := uint64(0); i < tt; i++ { 56 | if nums[i] > i+min { 57 | s++ 58 | sNow++ 59 | nums[nums[i]-min], nums[i] = nums[i], nums[nums[i]-min] 60 | } 61 | } 62 | 63 | if sNow == 0 { 64 | sorted = true 65 | } 66 | } 67 | 68 | return nums, s 69 | } 70 | 71 | func readLine(reader *bufio.Reader) string { 72 | str, _, err := reader.ReadLine() 73 | if err == io.EOF { 74 | return "" 75 | } 76 | 77 | return strings.TrimRight(string(str), "\r\n") 78 | } 79 | -------------------------------------------------------------------------------- /Algorithms/Arrays/New Year Chaos/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/New Year Chaos/description 1.png -------------------------------------------------------------------------------- /Algorithms/Arrays/New Year Chaos/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/New Year Chaos/description 2.png -------------------------------------------------------------------------------- /Algorithms/Arrays/New Year Chaos/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Arrays/New Year Chaos/description 3.png -------------------------------------------------------------------------------- /Algorithms/Arrays/New Year Chaos/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | minimumBribes([]int32{1, 2, 5, 3, 7, 8, 6, 4}) 9 | } 10 | 11 | func minimumBribes(q []int32) { 12 | count := int32(0) 13 | for i := int32(len(q)) - 1; i >= 0; i-- { 14 | if q[i]-(i+1) > 2 { 15 | fmt.Println("Too chaotic") 16 | return 17 | } 18 | for j := max(0, q[i]-2); j < i; j++ { 19 | if q[j] > q[i] { 20 | count++ 21 | } 22 | } 23 | } 24 | fmt.Println(count) 25 | } 26 | 27 | func max(a, b int32) int32 { 28 | if a >= b { 29 | return a 30 | } 31 | return b 32 | } 33 | -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Frequency Queries/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dictionaries and Hashmaps/Frequency Queries/description 1.png -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Frequency Queries/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dictionaries and Hashmaps/Frequency Queries/description 2.png -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Frequency Queries/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dictionaries and Hashmaps/Frequency Queries/description 3.png -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Frequency Queries/description 4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dictionaries and Hashmaps/Frequency Queries/description 4.png -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Frequency Queries/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | type Query struct { 13 | ByNum map[int32]int32 14 | ByFreq map[int32]*Freq 15 | Result []int32 16 | } 17 | 18 | type Freq struct { 19 | Nums map[int32]bool 20 | } 21 | 22 | func freqQuery(queries [][]int32) []int32 { 23 | var fq Query 24 | fq.init() 25 | for _, q := range queries { 26 | if len(q) == 1 { 27 | continue 28 | } 29 | fq.process(q[0], q[1]) 30 | } 31 | return fq.Result 32 | } 33 | 34 | func (fq *Query) init() { 35 | fq.ByNum = make(map[int32]int32) 36 | fq.ByFreq = make(map[int32]*Freq) 37 | } 38 | 39 | func (fq *Query) process(q, num int32) { 40 | switch q { 41 | case 1: 42 | fq.insert(num) 43 | case 2: 44 | fq.remove(num) 45 | case 3: 46 | fq.query(num) 47 | } 48 | } 49 | 50 | func (fq *Query) insert(num int32) { 51 | if _, ok := fq.ByNum[num]; !ok { 52 | fq.ByNum[num] = 1 53 | fq.insertFreq(num, 1) 54 | return 55 | } 56 | 57 | freq := fq.ByNum[num] 58 | (*fq.ByFreq[freq]).remove(num) 59 | if (*fq.ByFreq[freq]).count() == 0 { 60 | var f Freq 61 | f.init() 62 | fq.ByFreq[freq] = &f 63 | } 64 | 65 | fq.ByNum[num]++ 66 | fq.insertFreq(num, fq.ByNum[num]) 67 | } 68 | 69 | func (fq *Query) insertFreq(num, freq int32) { 70 | if _, ok := fq.ByFreq[freq]; !ok { 71 | var f Freq 72 | f.init() 73 | f.insert(num) 74 | fq.ByFreq[freq] = &f 75 | } 76 | (*fq.ByFreq[freq]).insert(num) 77 | } 78 | 79 | func (f *Freq) init() { 80 | f.Nums = make(map[int32]bool) 81 | } 82 | 83 | func (f *Freq) insert(num int32) { 84 | if _, ok := f.Nums[num]; !ok { 85 | f.Nums[num] = true 86 | } 87 | } 88 | 89 | func (f Freq) count() int32 { 90 | return int32(len(f.Nums)) 91 | } 92 | 93 | func (fq *Query) query(freq int32) { 94 | if _, ok := fq.ByFreq[freq]; ok && (*fq.ByFreq[freq]).count() > 0 { 95 | fq.Result = append(fq.Result, 1) 96 | return 97 | } 98 | fq.Result = append(fq.Result, 0) 99 | } 100 | 101 | func (f *Freq) remove(num int32) { 102 | delete(f.Nums, num) 103 | } 104 | 105 | func (fq *Query) remove(num int32) { 106 | if _, ok := fq.ByNum[num]; !ok { 107 | return 108 | } 109 | 110 | freq := fq.ByNum[num] 111 | (*fq.ByFreq[freq]).remove(num) 112 | if (*fq.ByFreq[freq]).count() == 0 { 113 | var f Freq 114 | f.init() 115 | fq.ByFreq[freq] = &f 116 | } 117 | 118 | fq.ByNum[num]-- 119 | if fq.ByNum[num] == 0 { 120 | delete(fq.ByNum, num) 121 | return 122 | } 123 | fq.insertFreq(num, fq.ByNum[num]) 124 | } 125 | 126 | func main() { 127 | reader := bufio.NewReaderSize(os.Stdin, 16*1024*1024) 128 | 129 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 130 | checkError(err) 131 | 132 | defer stdout.Close() 133 | 134 | writer := bufio.NewWriterSize(stdout, 16*1024*1024) 135 | 136 | qTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64) 137 | checkError(err) 138 | q := int32(qTemp) 139 | 140 | var queries [][]int32 141 | for i := 0; i < int(q); i++ { 142 | queriesRowTemp := strings.Split(strings.TrimRight(readLine(reader), " \t\r\n"), " ") 143 | 144 | var queriesRow []int32 145 | for _, queriesRowItem := range queriesRowTemp { 146 | queriesItemTemp, err := strconv.ParseInt(queriesRowItem, 10, 64) 147 | checkError(err) 148 | queriesItem := int32(queriesItemTemp) 149 | queriesRow = append(queriesRow, queriesItem) 150 | } 151 | 152 | if len(queriesRow) != 2 { 153 | panic("Bad input") 154 | } 155 | 156 | queries = append(queries, queriesRow) 157 | } 158 | 159 | ans := freqQuery(queries) 160 | 161 | for i, ansItem := range ans { 162 | fmt.Fprintf(writer, "%d", ansItem) 163 | 164 | if i != len(ans)-1 { 165 | fmt.Fprintf(writer, "\n") 166 | } 167 | } 168 | 169 | fmt.Fprintf(writer, "\n") 170 | 171 | writer.Flush() 172 | } 173 | 174 | func readLine(reader *bufio.Reader) string { 175 | str, _, err := reader.ReadLine() 176 | if err == io.EOF { 177 | return "" 178 | } 179 | 180 | return strings.TrimRight(string(str), "\r\n") 181 | } 182 | 183 | func checkError(err error) { 184 | if err != nil { 185 | panic(err) 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Hash Tables Ransom Notes/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dictionaries and Hashmaps/Hash Tables Ransom Notes/description 1.png -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Hash Tables Ransom Notes/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dictionaries and Hashmaps/Hash Tables Ransom Notes/description 2.png -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Hash Tables Ransom Notes/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dictionaries and Hashmaps/Hash Tables Ransom Notes/description 3.png -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Hash Tables Ransom Notes/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "hash/fnv" 7 | "io" 8 | "os" 9 | "strconv" 10 | "strings" 11 | ) 12 | 13 | // Complete the checkMagazine function below. 14 | func checkMagazine(magazine []string, note []string) { 15 | yes := "Yes" 16 | no := "No" 17 | magazineT := len(magazine) 18 | noteT := len(note) 19 | min := 1 20 | maxWord := 5 21 | maxWords := 30000 22 | if magazineT < min || magazineT > maxWords || noteT < min || noteT > maxWords { 23 | fmt.Println(no) 24 | return 25 | } 26 | 27 | magazineM, ok := createMap(magazine) 28 | if !ok { 29 | fmt.Println(no) 30 | return 31 | } 32 | 33 | for _, word := range note { 34 | wLength := len(word) 35 | if wLength < min || wLength > maxWord { 36 | fmt.Println(no) 37 | return 38 | } 39 | wHash := stringHash(word) 40 | if c, exists := magazineM[wHash]; exists && c > 0 { 41 | magazineM[wHash]-- 42 | continue 43 | } 44 | fmt.Println(no) 45 | return 46 | } 47 | fmt.Println(yes) 48 | } 49 | 50 | func createMap(items []string) (map[uint32]int, bool) { 51 | min := 1 52 | max := 5 53 | rslt := make(map[uint32]int) 54 | for _, it := range items { 55 | length := len(it) 56 | if length < min || length > max { 57 | return rslt, false 58 | } 59 | rslt[stringHash(it)]++ 60 | } 61 | return rslt, true 62 | } 63 | 64 | func stringHash(str string) uint32 { 65 | h := fnv.New32a() 66 | h.Write([]byte(str)) 67 | return h.Sum32() 68 | } 69 | 70 | func main() { 71 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 72 | 73 | mn := strings.Split(readLine(reader), " ") 74 | 75 | mTemp, err := strconv.ParseInt(mn[0], 10, 64) 76 | checkError(err) 77 | m := int32(mTemp) 78 | 79 | nTemp, err := strconv.ParseInt(mn[1], 10, 64) 80 | checkError(err) 81 | n := int32(nTemp) 82 | 83 | magazineTemp := strings.Split(readLine(reader), " ") 84 | 85 | var magazine []string 86 | 87 | for i := 0; i < int(m); i++ { 88 | magazineItem := magazineTemp[i] 89 | magazine = append(magazine, magazineItem) 90 | } 91 | 92 | noteTemp := strings.Split(readLine(reader), " ") 93 | 94 | var note []string 95 | 96 | for i := 0; i < int(n); i++ { 97 | noteItem := noteTemp[i] 98 | note = append(note, noteItem) 99 | } 100 | 101 | checkMagazine(magazine, note) 102 | } 103 | 104 | func readLine(reader *bufio.Reader) string { 105 | str, _, err := reader.ReadLine() 106 | if err == io.EOF { 107 | return "" 108 | } 109 | 110 | return strings.TrimRight(string(str), "\r\n") 111 | } 112 | 113 | func checkError(err error) { 114 | if err != nil { 115 | panic(err) 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Two Strings/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dictionaries and Hashmaps/Two Strings/description 1.png -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Two Strings/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dictionaries and Hashmaps/Two Strings/description 2.png -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Two Strings/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dictionaries and Hashmaps/Two Strings/description 3.png -------------------------------------------------------------------------------- /Algorithms/Dictionaries and Hashmaps/Two Strings/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | // Complete the twoStrings function below. 13 | func twoStrings(s1 string, s2 string) string { 14 | yes := "YES" 15 | no := "NO" 16 | min := 1 17 | max := 100000 18 | s1T := len(s1) 19 | s2T := len(s2) 20 | if s1T < min || s1T > max || s2T < min || s2T > max { 21 | return no 22 | } 23 | 24 | s1M, ok := stringToMap(s1) 25 | if !ok { 26 | return no 27 | } 28 | 29 | for _, s := range s2 { 30 | if _, exists := s1M[int(s)]; exists { 31 | return yes 32 | } 33 | } 34 | 35 | return no 36 | } 37 | 38 | func stringToMap(str string) (map[int]bool, bool) { 39 | min := 97 40 | max := 122 41 | rst := make(map[int]bool) 42 | for _, s := range str { 43 | sInt := int(s) 44 | if sInt < min || sInt > max { 45 | return rst, false 46 | } 47 | rst[sInt] = true 48 | } 49 | return rst, true 50 | } 51 | 52 | func main() { 53 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 54 | 55 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 56 | checkError(err) 57 | 58 | defer stdout.Close() 59 | 60 | writer := bufio.NewWriterSize(stdout, 1024*1024) 61 | 62 | qTemp, err := strconv.ParseInt(readLine(reader), 10, 64) 63 | checkError(err) 64 | q := int32(qTemp) 65 | 66 | for qItr := 0; qItr < int(q); qItr++ { 67 | s1 := readLine(reader) 68 | 69 | s2 := readLine(reader) 70 | 71 | result := twoStrings(s1, s2) 72 | 73 | fmt.Fprintf(writer, "%s\n", result) 74 | } 75 | 76 | writer.Flush() 77 | } 78 | 79 | func readLine(reader *bufio.Reader) string { 80 | str, _, err := reader.ReadLine() 81 | if err == io.EOF { 82 | return "" 83 | } 84 | 85 | return strings.TrimRight(string(str), "\r\n") 86 | } 87 | 88 | func checkError(err error) { 89 | if err != nil { 90 | panic(err) 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /Algorithms/Dynamic Programming/Candies/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dynamic Programming/Candies/description 1.png -------------------------------------------------------------------------------- /Algorithms/Dynamic Programming/Candies/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dynamic Programming/Candies/description 2.png -------------------------------------------------------------------------------- /Algorithms/Dynamic Programming/Candies/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Dynamic Programming/Candies/description 3.png -------------------------------------------------------------------------------- /Algorithms/Dynamic Programming/Candies/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | candies() 7 | } 8 | 9 | func candies() { 10 | var tt int 11 | if _, err := fmt.Scanf("%d", &tt); err != nil { 12 | return 13 | } 14 | 15 | rts := make([]int, tt) 16 | for i := 0; i < tt; i++ { 17 | if _, err := fmt.Scanf("%d", &rts[i]); err != nil { 18 | return 19 | } 20 | } 21 | fmt.Println(minRequired(rts)) 22 | } 23 | 24 | func minRequired(rts []int) int { 25 | tt := len(rts) 26 | minR := make([]int, tt) 27 | minL := make([]int, tt) 28 | 29 | for i := 0; i < tt; i++ { 30 | minR[i] = 1 31 | minL[i] = 1 32 | } 33 | 34 | for i := tt - 2; i >= 0; i-- { 35 | if rts[i] > rts[i+1] { 36 | minR[i] = 1 + minR[i+1] 37 | } 38 | } 39 | 40 | for i := 1; i < tt; i++ { 41 | if rts[i-1] < rts[i] { 42 | minL[i] = 1 + minL[i-1] 43 | } 44 | } 45 | 46 | var c int 47 | for i := 0; i < tt; i++ { 48 | if minL[i] < minR[i] { 49 | c = c + minR[i] 50 | continue 51 | } 52 | c = c + minL[i] 53 | } 54 | return c 55 | } 56 | -------------------------------------------------------------------------------- /Algorithms/Graphs/Find the nearest clone/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Graphs/Find the nearest clone/description 1.png -------------------------------------------------------------------------------- /Algorithms/Graphs/Find the nearest clone/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Graphs/Find the nearest clone/description 2.png -------------------------------------------------------------------------------- /Algorithms/Graphs/Find the nearest clone/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Graphs/Find the nearest clone/description 3.png -------------------------------------------------------------------------------- /Algorithms/Graphs/Find the nearest clone/description 4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Graphs/Find the nearest clone/description 4.png -------------------------------------------------------------------------------- /Algorithms/Graphs/Find the nearest clone/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | func main() { 12 | fmt.Println(findNearest()) 13 | } 14 | 15 | func findNearest() int64 { 16 | rst := int64(-1) 17 | bufin := bufio.NewReader(os.Stdin) 18 | 19 | var q, eg int 20 | if _, err := fmt.Sscanf(readLine(bufin), "%d%d", &q, &eg); err != nil { 21 | return rst 22 | } 23 | 24 | if eg <= 1 { 25 | return rst 26 | } 27 | 28 | for i := 0; i < eg; i++ { 29 | readLine(bufin) 30 | } 31 | 32 | crs := readLine(bufin) 33 | var fCr string 34 | if _, err := fmt.Sscanf(readLine(bufin), "%s", &fCr); err != nil { 35 | return rst 36 | } 37 | 38 | var dist, minDist int64 39 | for _, cl := range strings.Split(crs, " ") { 40 | if cl == fCr { 41 | if dist == 0 { 42 | dist++ 43 | continue 44 | } 45 | 46 | if dist == 1 { 47 | minDist = dist 48 | break 49 | } 50 | 51 | if minDist > 0 { 52 | minDist = min(minDist, dist) 53 | dist = 1 54 | continue 55 | } 56 | 57 | minDist = dist 58 | dist = 1 59 | continue 60 | } 61 | 62 | if dist != 0 { 63 | dist++ 64 | } 65 | } 66 | 67 | if minDist == 0 { 68 | return rst 69 | } 70 | 71 | return minDist 72 | } 73 | 74 | func min(a, b int64) int64 { 75 | if a < b { 76 | return a 77 | } 78 | return b 79 | } 80 | 81 | func readLine(reader *bufio.Reader) string { 82 | str, _, err := reader.ReadLine() 83 | if err == io.EOF { 84 | return "" 85 | } 86 | 87 | return strings.TrimRight(string(str), "\r\n") 88 | } 89 | -------------------------------------------------------------------------------- /Algorithms/Graphs/Roads and Libraries/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Graphs/Roads and Libraries/description 1.png -------------------------------------------------------------------------------- /Algorithms/Graphs/Roads and Libraries/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Graphs/Roads and Libraries/description 2.png -------------------------------------------------------------------------------- /Algorithms/Graphs/Roads and Libraries/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Graphs/Roads and Libraries/description 3.png -------------------------------------------------------------------------------- /Algorithms/Graphs/Roads and Libraries/description 4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Graphs/Roads and Libraries/description 4.png -------------------------------------------------------------------------------- /Algorithms/Graphs/Roads and Libraries/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | roadsAndLibraries() 7 | } 8 | 9 | func roadsAndLibraries() { 10 | var q int 11 | if _, err := fmt.Scanf("%d", &q); err != nil { 12 | return 13 | } 14 | 15 | for line := 0; line < q; line++ { 16 | var n, m int 17 | var pRoad, pLib int64 18 | if _, err := fmt.Scanf("%d%d%d%d", &n, &m, &pLib, &pRoad); err != nil { 19 | return 20 | } 21 | processProvince(n, m, pLib, pRoad) 22 | } 23 | } 24 | 25 | func find(p []int, v int) int { 26 | if p[v] == v { 27 | return v 28 | } 29 | p[v] = find(p, p[v]) 30 | return p[v] 31 | } 32 | 33 | func min(a, b int64) int64 { 34 | if a < b { 35 | return a 36 | } 37 | return b 38 | } 39 | 40 | func processProvince(n, m int, pLib, pRoad int64) { 41 | var p []int 42 | for i := 0; i < n; i++ { 43 | p = append(p, i) 44 | } 45 | var cRoad int64 46 | for i := 0; i < m; i++ { 47 | u, v := 0, 0 48 | 49 | if _, err := fmt.Scanf("%d%d", &u, &v); err != nil { 50 | return 51 | } 52 | 53 | u, v = u-1, v-1 54 | if (i & 1) == 0 { 55 | u, v = v, u 56 | } 57 | u, v = find(p, u), find(p, v) 58 | if u != v { 59 | p[u] = v 60 | cRoad++ 61 | } 62 | } 63 | fmt.Printf("%d\n", min(int64(n)*pLib, cRoad*pRoad+(int64(n)-cRoad)*pLib)) 64 | } 65 | -------------------------------------------------------------------------------- /Algorithms/Greedy Algorithms/Max Min/img 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Greedy Algorithms/Max Min/img 1.png -------------------------------------------------------------------------------- /Algorithms/Greedy Algorithms/Max Min/img 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Greedy Algorithms/Max Min/img 2.png -------------------------------------------------------------------------------- /Algorithms/Greedy Algorithms/Max Min/img 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Greedy Algorithms/Max Min/img 3.png -------------------------------------------------------------------------------- /Algorithms/Greedy Algorithms/Max Min/img 4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Greedy Algorithms/Max Min/img 4.png -------------------------------------------------------------------------------- /Algorithms/Greedy Algorithms/Max Min/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | ) 7 | 8 | func main() { 9 | fmt.Println(minMax()) 10 | } 11 | 12 | func minMax() int { 13 | var tt, k int 14 | if _, err := fmt.Scanf("%d", &tt); err != nil { 15 | return 0 16 | } 17 | if _, err := fmt.Scanf("%d", &k); err != nil { 18 | return 0 19 | } 20 | 21 | var nums []int 22 | for { 23 | var n int 24 | if _, err := fmt.Scanf("%d", &n); err != nil { 25 | break 26 | } 27 | nums = append(nums, n) 28 | } 29 | if len(nums) < k { 30 | return 0 31 | } 32 | 33 | sort.Ints(nums) 34 | return minimumUnfairness(nums, k) 35 | } 36 | 37 | func minimumUnfairness(nums []int, k int) int { 38 | tt := len(nums) 39 | unf := nums[tt-1] 40 | 41 | for i := 0; i <= tt-k; i++ { 42 | mm := nums[i+k-1] - nums[i] 43 | if mm < unf { 44 | unf = mm 45 | } 46 | } 47 | return unf 48 | } 49 | -------------------------------------------------------------------------------- /Algorithms/Recursion and Backtracking/Recursive Digit Sum/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Recursion and Backtracking/Recursive Digit Sum/description 1.png -------------------------------------------------------------------------------- /Algorithms/Recursion and Backtracking/Recursive Digit Sum/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Recursion and Backtracking/Recursive Digit Sum/description 2.png -------------------------------------------------------------------------------- /Algorithms/Recursion and Backtracking/Recursive Digit Sum/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Recursion and Backtracking/Recursive Digit Sum/description 3.png -------------------------------------------------------------------------------- /Algorithms/Recursion and Backtracking/Recursive Digit Sum/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | func main() { 9 | fmt.Println(recursiveSum()) 10 | } 11 | 12 | func recursiveSum() int64 { 13 | var dgsStr string 14 | var rpt int64 15 | if _, err := fmt.Scanf("%s%d", &dgsStr, &rpt); err != nil { 16 | return 0 17 | } 18 | 19 | cut := 18 20 | if len(dgsStr) < cut { 21 | dgs, err := strconv.ParseInt(dgsStr, 10, 64) 22 | if err != nil { 23 | return 0 24 | } 25 | 26 | return sumDigits(sumDigits(dgs) * rpt) 27 | } 28 | 29 | var past bool 30 | for !past { 31 | dgsStr = sumDigitsHuge(dgsStr) 32 | if dgsStr == "" { 33 | return 0 34 | } 35 | 36 | if len(dgsStr) < cut { 37 | past = true 38 | continue 39 | } 40 | } 41 | 42 | dgsPart, err := strconv.ParseInt(dgsStr, 10, 64) 43 | if err != nil { 44 | return 0 45 | } 46 | 47 | return sumDigits(sumDigits(dgsPart) * rpt) 48 | } 49 | 50 | func sumDigitsHuge(dgsStr string) string { 51 | tt := int64(len(dgsStr)) 52 | cut := int64(18) 53 | var oneSum string 54 | var cutFrom int64 55 | cutTo := cut 56 | var past bool 57 | for !past { 58 | dgsPart, err := strconv.ParseInt(dgsStr[cutFrom:cutTo], 10, 64) 59 | if err != nil { 60 | return "" 61 | } 62 | oneSum += fmt.Sprintf("%d", sumDigits(dgsPart)) 63 | 64 | if tt == cutTo { 65 | past = true 66 | } 67 | 68 | if tt-cut >= cutTo { 69 | cutFrom += cut 70 | cutTo += cut 71 | continue 72 | } 73 | cutFrom = cutTo 74 | cutTo = tt 75 | } 76 | return oneSum 77 | } 78 | 79 | func sumDigits(n int64) int64 { 80 | if n < 10 { 81 | return n 82 | } 83 | 84 | var sum int64 85 | for n != 0 { 86 | sum += n % 10 87 | n = n / 10 88 | } 89 | return sumDigits(sum) 90 | } 91 | -------------------------------------------------------------------------------- /Algorithms/Search/Maximum Subarray Sum/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Search/Maximum Subarray Sum/description 1.png -------------------------------------------------------------------------------- /Algorithms/Search/Maximum Subarray Sum/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Search/Maximum Subarray Sum/description 2.png -------------------------------------------------------------------------------- /Algorithms/Search/Maximum Subarray Sum/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Search/Maximum Subarray Sum/description 3.png -------------------------------------------------------------------------------- /Algorithms/Search/Maximum Subarray Sum/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | "strconv" 8 | ) 9 | 10 | type Node struct { 11 | Key int64 12 | Left *Node 13 | Right *Node 14 | height int64 15 | } 16 | 17 | func main() { 18 | r := bufio.NewScanner(bufio.NewReader(os.Stdin)) 19 | r.Split(bufio.ScanWords) 20 | t := ReadLong(r) 21 | 22 | for i := int64(0); i < t; i++ { 23 | n := ReadLong(r) 24 | m := ReadLong(r) 25 | 26 | a := make([]int64, n) 27 | for j := int64(0); j < n; j++ { 28 | a[j] = ReadLong(r) 29 | } 30 | 31 | fmt.Println(MaximumSum(a, m)) 32 | } 33 | } 34 | 35 | func MaximumSum(arr []int64, m int64) int64 { 36 | var max, sm int64 37 | var r *Node 38 | 39 | for _, aVal := range arr { 40 | sm = (sm + aVal) % m 41 | r = insertOrIgnore(r, sm) 42 | if sm > max { 43 | max = sm 44 | } 45 | 46 | nd := r.next(sm) 47 | if nd == nil { 48 | continue 49 | } 50 | 51 | if d := (sm - nd.Key + m) % m; d > max { 52 | max = d 53 | } 54 | } 55 | 56 | return max 57 | } 58 | 59 | func ReadLong(s *bufio.Scanner) int64 { 60 | s.Scan() 61 | i, _ := strconv.ParseInt(s.Text(), 10, 64) 62 | return i 63 | } 64 | 65 | func balance(n *Node, key int64) *Node { 66 | b := n.Left.treeHeight() - n.Right.treeHeight() 67 | 68 | if b > 1 && key < n.Left.Key { 69 | return n.rotateRight() 70 | } 71 | 72 | if b < -1 && key > n.Right.Key { 73 | return n.rotateLeft() 74 | } 75 | 76 | if b > 1 && key > n.Left.Key { 77 | n.Left = n.Left.rotateLeft() 78 | return n.rotateRight() 79 | } 80 | 81 | if b < -1 && key < n.Right.Key { 82 | n.Right = n.Right.rotateRight() 83 | return n.rotateLeft() 84 | } 85 | 86 | return n 87 | } 88 | 89 | func createNode(key int64) *Node { 90 | n := &Node{} 91 | n.Key = key 92 | n.height = 1 93 | return n 94 | } 95 | 96 | func insertOrIgnore(n *Node, key int64) *Node { 97 | if n == nil { 98 | return createNode(key) 99 | } 100 | 101 | if key < n.Key { 102 | n.Left = insertOrIgnore(n.Left, key) 103 | } else if key > n.Key { 104 | n.Right = insertOrIgnore(n.Right, key) 105 | } else if key == n.Key { 106 | return n 107 | } 108 | 109 | n.height = max(n.Left.treeHeight(), n.Right.treeHeight()) + 1 110 | 111 | return balance(n, key) 112 | } 113 | 114 | func leftMost(n *Node) *Node { 115 | for { 116 | if n.Left == nil { 117 | return n 118 | } 119 | n = n.Left 120 | } 121 | } 122 | 123 | func max(a int64, b int64) int64 { 124 | if a > b { 125 | return a 126 | } 127 | return b 128 | } 129 | 130 | // next returns node with the next value or nil if the key is maximum 131 | func (n *Node) next(key int64) *Node { 132 | var prevRight *Node 133 | c := n 134 | for { 135 | if c.Key == key { 136 | if c.Right != nil { 137 | return leftMost(c.Right) 138 | } 139 | if prevRight != nil { 140 | return prevRight 141 | } 142 | break 143 | } 144 | if c.Key < key { 145 | c = c.Right 146 | continue 147 | } 148 | if c.Key > key { 149 | prevRight = c 150 | c = c.Left 151 | continue 152 | } 153 | } 154 | 155 | return nil 156 | } 157 | 158 | func (n *Node) rotateLeft() *Node { 159 | r := n.Right 160 | t2 := r.Left 161 | r.Left = n 162 | n.Right = t2 163 | n.height = max(n.Left.treeHeight(), n.Right.treeHeight()) + 1 164 | r.height = max(r.Left.treeHeight(), r.Right.treeHeight()) + 1 165 | return r 166 | } 167 | 168 | func (n *Node) rotateRight() *Node { 169 | r := n.Left 170 | t2 := r.Right 171 | r.Right = n 172 | n.Left = t2 173 | n.height = max(n.Left.treeHeight(), n.Right.treeHeight()) + 1 174 | r.height = max(r.Left.treeHeight(), r.Right.treeHeight()) + 1 175 | return r 176 | } 177 | 178 | func (n *Node) treeHeight() int64 { 179 | if n == nil { 180 | return 0 181 | } 182 | return n.height 183 | } 184 | -------------------------------------------------------------------------------- /Algorithms/Search/Pairs/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Search/Pairs/description 1.png -------------------------------------------------------------------------------- /Algorithms/Search/Pairs/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Search/Pairs/description 2.png -------------------------------------------------------------------------------- /Algorithms/Search/Pairs/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | func pairs(k int64, arr []int64) int64 { 13 | var rst int64 14 | arrMp := arrayToMap(arr) 15 | for _, n := range arr { 16 | diff := n - k 17 | 18 | if _, ok := arrMp[diff]; ok { 19 | rst++ 20 | } 21 | } 22 | return rst 23 | } 24 | 25 | func arrayToMap(arr []int64) map[int64]bool { 26 | mp := make(map[int64]bool) 27 | for _, a := range arr { 28 | if _, ok := mp[a]; !ok { 29 | mp[a] = true 30 | } 31 | } 32 | return mp 33 | } 34 | 35 | func main() { 36 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 37 | 38 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 39 | checkError(err) 40 | 41 | defer stdout.Close() 42 | 43 | writer := bufio.NewWriterSize(stdout, 1024*1024) 44 | 45 | nk := strings.Split(readLine(reader), " ") 46 | 47 | nTemp, err := strconv.ParseInt(nk[0], 10, 64) 48 | checkError(err) 49 | n := int(nTemp) 50 | 51 | kTemp, err := strconv.ParseInt(nk[1], 10, 64) 52 | checkError(err) 53 | k := kTemp 54 | 55 | arrTemp := strings.Split(readLine(reader), " ") 56 | 57 | var arr []int64 58 | 59 | for i := 0; i < int(n); i++ { 60 | arrItemTemp, err := strconv.ParseInt(arrTemp[i], 10, 64) 61 | checkError(err) 62 | arrItem := arrItemTemp 63 | arr = append(arr, arrItem) 64 | } 65 | 66 | result := pairs(k, arr) 67 | 68 | fmt.Fprintf(writer, "%d\n", result) 69 | 70 | writer.Flush() 71 | } 72 | 73 | func readLine(reader *bufio.Reader) string { 74 | str, _, err := reader.ReadLine() 75 | if err == io.EOF { 76 | return "" 77 | } 78 | 79 | return strings.TrimRight(string(str), "\r\n") 80 | } 81 | 82 | func checkError(err error) { 83 | if err != nil { 84 | panic(err) 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Algorithms/Sorting/Bubble Sort/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Sorting/Bubble Sort/description 1.png -------------------------------------------------------------------------------- /Algorithms/Sorting/Bubble Sort/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Sorting/Bubble Sort/description 2.png -------------------------------------------------------------------------------- /Algorithms/Sorting/Bubble Sort/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Sorting/Bubble Sort/description 3.png -------------------------------------------------------------------------------- /Algorithms/Sorting/Bubble Sort/description 4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Sorting/Bubble Sort/description 4.png -------------------------------------------------------------------------------- /Algorithms/Sorting/Bubble Sort/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | countSwaps([]int32{3, 2, 1}) 9 | } 10 | 11 | func countSwaps(a []int32) { 12 | rst, swCount := swap(a, 0) 13 | fmt.Printf("Array is sorted in %d swaps.\n", swCount) 14 | fmt.Printf("First Element: %d\n", rst[0]) 15 | fmt.Printf("Last Element: %d\n", rst[len(rst)-1]) 16 | } 17 | 18 | func swap(a []int32, swCount int32) ([]int32, int32) { 19 | total := int32(len(a)) 20 | swC := int32(0) 21 | for i := int32(0); i < total; i++ { 22 | iNext := i + 1 23 | if iNext < total && a[i] > a[iNext] { 24 | sw := a[i] 25 | a[i] = a[iNext] 26 | a[iNext] = sw 27 | swC++ 28 | } 29 | } 30 | 31 | if swC == 0 { 32 | return a, swCount 33 | } 34 | return swap(a, swCount+swC) 35 | } 36 | -------------------------------------------------------------------------------- /Algorithms/Sorting/Mark and Toys/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Sorting/Mark and Toys/description 1.png -------------------------------------------------------------------------------- /Algorithms/Sorting/Mark and Toys/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Sorting/Mark and Toys/description 2.png -------------------------------------------------------------------------------- /Algorithms/Sorting/Mark and Toys/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "sort" 9 | "strconv" 10 | "strings" 11 | ) 12 | 13 | func main() { 14 | sortToys() 15 | } 16 | 17 | type Data struct { 18 | canBuy int32 19 | totalToys int32 20 | totalFunds int32 21 | toys []int 22 | } 23 | 24 | func sortToys() { 25 | var d Data 26 | 27 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 28 | d.setTotals(readLine(reader)) 29 | if err := d.setToys(readLine(reader)); err != nil { 30 | d.printResult() 31 | return 32 | } 33 | 34 | if d.totalFunds == 0 || len(d.toys) == 0 { 35 | d.printResult() 36 | } 37 | 38 | d.countToBuy() 39 | d.printResult() 40 | } 41 | 42 | func (d *Data) countToBuy() { 43 | var sum int32 44 | for _, toy := range d.toys { 45 | sum += int32(toy) 46 | if sum <= d.totalFunds { 47 | d.canBuy++ 48 | } 49 | 50 | if sum >= d.totalFunds { 51 | return 52 | } 53 | } 54 | } 55 | 56 | func (d Data) printResult() { 57 | _, err := fmt.Fprintf(os.Stdout, "%d", d.canBuy) 58 | if err != nil { 59 | return 60 | } 61 | } 62 | 63 | func (d *Data) setTotals(input string) { 64 | data := strings.Split(input, " ") 65 | tTotal, err := strconv.ParseInt(data[0], 10, 32) 66 | if err != nil { 67 | return 68 | } 69 | fTotal, err := strconv.ParseInt(data[1], 10, 32) 70 | if err != nil { 71 | return 72 | } 73 | 74 | d.totalToys = int32(tTotal) 75 | d.totalFunds = int32(fTotal) 76 | } 77 | 78 | func (d *Data) setToys(input string) error { 79 | for _, toy := range strings.Split(input, " ") { 80 | toyP, err := strconv.ParseInt(toy, 10, 64) 81 | if err != nil { 82 | return err 83 | } 84 | d.toys = append(d.toys, int(toyP)) 85 | } 86 | sort.Ints(d.toys) 87 | return nil 88 | } 89 | 90 | func readLine(reader *bufio.Reader) string { 91 | str, _, err := reader.ReadLine() 92 | if err == io.EOF { 93 | return "" 94 | } 95 | 96 | return strings.TrimRight(string(str), "\r\n") 97 | } 98 | -------------------------------------------------------------------------------- /Algorithms/Sorting/Merge Sort: Counting Inversions/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Sorting/Merge Sort: Counting Inversions/description 1.png -------------------------------------------------------------------------------- /Algorithms/Sorting/Merge Sort: Counting Inversions/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Sorting/Merge Sort: Counting Inversions/description 2.png -------------------------------------------------------------------------------- /Algorithms/Sorting/Merge Sort: Counting Inversions/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | "strconv" 8 | "strings" 9 | ) 10 | 11 | func main() { 12 | countInversions() 13 | } 14 | 15 | func countInversions() { 16 | in := bufio.NewReaderSize(os.Stdin, 1024*1024) 17 | for { 18 | inputBt, _, err := in.ReadLine() 19 | input := string(inputBt) 20 | if err != nil { 21 | break 22 | } 23 | 24 | processLine(input) 25 | } 26 | } 27 | 28 | func processLine(input string) { 29 | numsStr := strings.Split(strings.TrimSpace(input), " ") 30 | if len(numsStr) < 2 { 31 | return 32 | } 33 | 34 | _, inv := mergeSortInversions(toNumArray(numsStr)) 35 | if _, err := fmt.Fprintf(os.Stdout, "%d\n", inv); err != nil { 36 | return 37 | } 38 | } 39 | 40 | func toNumArray(numsStr []string) []uint64 { 41 | var nums []uint64 42 | for _, numStr := range numsStr { 43 | n, err := strconv.Atoi(numStr) 44 | if err != nil { 45 | return []uint64{} 46 | } 47 | nums = append(nums, uint64(n)) 48 | } 49 | return nums 50 | } 51 | 52 | func mergeSortInversions(arr []uint64) ([]uint64, uint64) { 53 | if len(arr) == 1 { 54 | return arr, 0 55 | } 56 | 57 | l := arr[:len(arr)/2] 58 | r := arr[len(arr)/2:] 59 | 60 | l, ai := mergeSortInversions(l) 61 | r, bi := mergeSortInversions(r) 62 | 63 | var rst []uint64 64 | var i, j uint64 65 | inv := 0 + ai + bi 66 | 67 | for i < uint64(len(l)) && j < uint64(len(r)) { 68 | if l[i] <= r[j] { 69 | rst = append(rst, l[i]) 70 | i++ 71 | continue 72 | } 73 | rst = append(rst, r[j]) 74 | inv += uint64(len(l)) - i 75 | j++ 76 | } 77 | 78 | rst = append(rst, l[i:]...) 79 | rst = append(rst, r[j:]...) 80 | 81 | return rst, inv 82 | } 83 | -------------------------------------------------------------------------------- /Algorithms/Stacks/Largest Rectangle/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Stacks/Largest Rectangle/description 1.png -------------------------------------------------------------------------------- /Algorithms/Stacks/Largest Rectangle/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Stacks/Largest Rectangle/description 2.png -------------------------------------------------------------------------------- /Algorithms/Stacks/Largest Rectangle/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | func main() { 13 | fmt.Println(rectangle()) 14 | } 15 | 16 | func rectangle() int { 17 | var tt int 18 | if _, err := fmt.Scanf("%d", &tt); err != nil { 19 | return 0 20 | } 21 | 22 | line := readLine(bufio.NewReaderSize(os.Stdin, 1024*1024)) 23 | numsStr := strings.Split(line, " ") 24 | var max int 25 | for i := 1; i < tt-1; i++ { 26 | n, err := strconv.ParseInt(numsStr[i], 10, 64) 27 | if err != nil { 28 | return 0 29 | } 30 | 31 | lst := largest(i, int(n), tt, numsStr) 32 | rct := int(n) * lst 33 | if rct > max { 34 | max = rct 35 | } 36 | } 37 | 38 | return max 39 | } 40 | 41 | func largest(k, n, tt int, numsStr []string) int { 42 | c := 1 43 | for i := k - 1; i > 0; i-- { 44 | ln, err := strconv.ParseInt(numsStr[i], 10, 64) 45 | if err != nil { 46 | return 0 47 | } 48 | if int(ln) >= n { 49 | c++ 50 | continue 51 | } 52 | break 53 | } 54 | 55 | for i := k + 1; i < tt; i++ { 56 | rn, err := strconv.ParseInt(numsStr[i], 10, 64) 57 | if err != nil { 58 | return 0 59 | } 60 | if int(rn) >= n { 61 | c++ 62 | continue 63 | } 64 | break 65 | } 66 | 67 | return c 68 | } 69 | 70 | func readLine(reader *bufio.Reader) string { 71 | str, _, err := reader.ReadLine() 72 | if err == io.EOF { 73 | return "" 74 | } 75 | 76 | return strings.TrimRight(string(str), "\r\n") 77 | } 78 | -------------------------------------------------------------------------------- /Algorithms/String Manipulation/Sherlock and the Valid String/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/String Manipulation/Sherlock and the Valid String/description 1.png -------------------------------------------------------------------------------- /Algorithms/String Manipulation/Sherlock and the Valid String/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/String Manipulation/Sherlock and the Valid String/description 2.png -------------------------------------------------------------------------------- /Algorithms/String Manipulation/Sherlock and the Valid String/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | func isValidSherlock(a string) string { 12 | if len(a) == 0 { 13 | return "NO" 14 | } 15 | if len(a) == 1 { 16 | return "YES" 17 | } 18 | 19 | aInt := mergeSort(toNumArray(a)) 20 | rst := countValidSherlock(aInt, 0, 0, 0, 0, false) 21 | 22 | if rst { 23 | return "YES" 24 | } 25 | return "NO" 26 | } 27 | 28 | func countValidSherlock(a []uint64, freq, cFreq, prev, cur uint64, removed bool) bool { 29 | if len(a) == 0 { 30 | return freq == cFreq || (!removed && (freq == cFreq-1 || freq == cFreq+1 || cFreq == 1)) 31 | } 32 | 33 | if prev == 0 && a[0] != cur { 34 | return countValidSherlock(a[1:], cFreq, 1, cur, a[0], false) 35 | } 36 | 37 | if a[0] == cur { 38 | if freq == 0 || freq > cFreq { 39 | return countValidSherlock(a[1:], freq, cFreq+1, prev, cur, removed) 40 | } 41 | 42 | // when freq == cFreq 43 | if removed { 44 | return false 45 | } 46 | return countValidSherlock(a[1:], freq, cFreq+1, prev, cur, true) 47 | } 48 | 49 | // when a[0] != cur 50 | if freq > cFreq+1 { 51 | return false 52 | } 53 | if freq > cFreq { 54 | return countValidSherlock(a[1:], freq, 1, cur, a[0], true) 55 | } 56 | return countValidSherlock(a[1:], freq, 1, cur, a[0], removed) 57 | } 58 | 59 | func toNumArray(numsStr string) []uint64 { 60 | var nums []uint64 61 | for _, numStr := range numsStr { 62 | nums = append(nums, uint64(numStr)) 63 | } 64 | return nums 65 | } 66 | 67 | func mergeSort(arr []uint64) []uint64 { 68 | if len(arr) <= 1 { 69 | return arr 70 | } 71 | 72 | l := arr[:len(arr)/2] 73 | r := arr[len(arr)/2:] 74 | 75 | l = mergeSort(l) 76 | r = mergeSort(r) 77 | 78 | var rst []uint64 79 | var i, j uint64 80 | 81 | for i < uint64(len(l)) && j < uint64(len(r)) { 82 | if l[i] <= r[j] { 83 | rst = append(rst, l[i]) 84 | i++ 85 | continue 86 | } 87 | rst = append(rst, r[j]) 88 | j++ 89 | } 90 | 91 | if i < uint64(len(l)) { 92 | rst = append(rst, l[i:]...) 93 | } 94 | if j < uint64(len(r)) { 95 | rst = append(rst, r[j:]...) 96 | } 97 | 98 | return rst 99 | } 100 | 101 | func main() { 102 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 103 | 104 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 105 | checkError(err) 106 | 107 | defer stdout.Close() 108 | 109 | writer := bufio.NewWriterSize(stdout, 1024*1024) 110 | 111 | s := readLine(reader) 112 | 113 | result := isValidSherlock(s) 114 | 115 | fmt.Fprintf(writer, "%s\n", result) 116 | 117 | writer.Flush() 118 | } 119 | 120 | func readLine(reader *bufio.Reader) string { 121 | str, _, err := reader.ReadLine() 122 | if err == io.EOF { 123 | return "" 124 | } 125 | 126 | return strings.TrimRight(string(str), "\r\n") 127 | } 128 | 129 | func checkError(err error) { 130 | if err != nil { 131 | panic(err) 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /Algorithms/String Manipulation/Strings: Making Anagrams/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/String Manipulation/Strings: Making Anagrams/description 1.png -------------------------------------------------------------------------------- /Algorithms/String Manipulation/Strings: Making Anagrams/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/String Manipulation/Strings: Making Anagrams/description 2.png -------------------------------------------------------------------------------- /Algorithms/String Manipulation/Strings: Making Anagrams/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | func makeAnagram(a string, b string) int32 { 12 | if len(a) == 0 || len(b) == 0 { 13 | return 0 14 | } 15 | if (len(a) == 1 && len(b) == 1) && a[0] == b[0] { 16 | return 0 17 | } 18 | 19 | aInt := mergeSort(toNumArray(a)) 20 | bInt := mergeSort(toNumArray(b)) 21 | 22 | return int32(countAnagram(aInt, bInt, 0)) 23 | } 24 | 25 | func countAnagram(a, b []uint64, c uint64) uint64 { 26 | if len(a) == 0 { 27 | return c + uint64(len(b)) 28 | } 29 | if len(b) == 0 { 30 | return c + uint64(len(a)) 31 | } 32 | if (len(a) == 1 && len(b) == 1) && a[0] == b[0] { 33 | return c 34 | } 35 | 36 | if a[0] == b[0] { 37 | return countAnagram(a[1:], b[1:], c) 38 | } 39 | if a[0] > b[0] { 40 | return countAnagram(a, b[1:], c+1) 41 | } 42 | return countAnagram(a[1:], b, c+1) 43 | } 44 | 45 | func toNumArray(numsStr string) []uint64 { 46 | var nums []uint64 47 | for _, numStr := range numsStr { 48 | nums = append(nums, uint64(numStr)) 49 | } 50 | return nums 51 | } 52 | 53 | func mergeSort(arr []uint64) []uint64 { 54 | if len(arr) == 1 { 55 | return arr 56 | } 57 | 58 | l := arr[:len(arr)/2] 59 | r := arr[len(arr)/2:] 60 | 61 | l = mergeSort(l) 62 | r = mergeSort(r) 63 | 64 | var rst []uint64 65 | var i, j uint64 66 | 67 | for i < uint64(len(l)) && j < uint64(len(r)) { 68 | if l[i] <= r[j] { 69 | rst = append(rst, l[i]) 70 | i++ 71 | continue 72 | } 73 | rst = append(rst, r[j]) 74 | j++ 75 | } 76 | 77 | rst = append(rst, l[i:]...) 78 | rst = append(rst, r[j:]...) 79 | 80 | return rst 81 | } 82 | 83 | func main() { 84 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 85 | 86 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 87 | checkError(err) 88 | 89 | defer stdout.Close() 90 | 91 | writer := bufio.NewWriterSize(stdout, 1024*1024) 92 | 93 | a := readLine(reader) 94 | 95 | b := readLine(reader) 96 | 97 | res := makeAnagram(a, b) 98 | 99 | fmt.Fprintf(writer, "%d\n", res) 100 | 101 | writer.Flush() 102 | } 103 | 104 | func readLine(reader *bufio.Reader) string { 105 | str, _, err := reader.ReadLine() 106 | if err == io.EOF { 107 | return "" 108 | } 109 | 110 | return strings.TrimRight(string(str), "\r\n") 111 | } 112 | 113 | func checkError(err error) { 114 | if err != nil { 115 | panic(err) 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Algorithms/Warm-up/Counting Valleys/image 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Warm-up/Counting Valleys/image 1.png -------------------------------------------------------------------------------- /Algorithms/Warm-up/Counting Valleys/image 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Warm-up/Counting Valleys/image 2.png -------------------------------------------------------------------------------- /Algorithms/Warm-up/Counting Valleys/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | // Complete the countingValleys function below. 13 | func countingValleys(n int32, s string) int32 { 14 | c := 0 15 | if len(s) == 0 { 16 | return int32(c) 17 | } 18 | 19 | isNewValley := false 20 | level := 0 21 | for _, l := range s { 22 | if l == 68 { 23 | if level == 0 { 24 | isNewValley = true 25 | } 26 | level-- 27 | } else { 28 | level++ 29 | } 30 | 31 | if isNewValley { 32 | c++ 33 | isNewValley = false 34 | } 35 | } 36 | return int32(c) 37 | } 38 | 39 | func main() { 40 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 41 | 42 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 43 | checkError(err) 44 | 45 | defer stdout.Close() 46 | 47 | writer := bufio.NewWriterSize(stdout, 1024*1024) 48 | 49 | nTemp, err := strconv.ParseInt(readLine(reader), 10, 64) 50 | checkError(err) 51 | n := int32(nTemp) 52 | 53 | s := readLine(reader) 54 | 55 | result := countingValleys(n, s) 56 | 57 | fmt.Fprintf(writer, "%d\n", result) 58 | 59 | writer.Flush() 60 | } 61 | 62 | func readLine(reader *bufio.Reader) string { 63 | str, _, err := reader.ReadLine() 64 | if err == io.EOF { 65 | return "" 66 | } 67 | 68 | return strings.TrimRight(string(str), "\r\n") 69 | } 70 | 71 | func checkError(err error) { 72 | if err != nil { 73 | panic(err) 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Algorithms/Warm-up/Jumping On The Cloud/img 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Warm-up/Jumping On The Cloud/img 1.png -------------------------------------------------------------------------------- /Algorithms/Warm-up/Jumping On The Cloud/img 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Warm-up/Jumping On The Cloud/img 2.png -------------------------------------------------------------------------------- /Algorithms/Warm-up/Jumping On The Cloud/img 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Warm-up/Jumping On The Cloud/img 3.png -------------------------------------------------------------------------------- /Algorithms/Warm-up/Jumping On The Cloud/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | // Complete the jumpingOnClouds function below. 13 | func jumpingOnClouds(c []int32) int32 { 14 | jumps := int32(0) 15 | if len(c) == 0 { 16 | return 0 17 | } 18 | if len(c) == 1 { 19 | if c[0] == 0 { 20 | return 1 21 | } 22 | return 0 23 | } 24 | totalR := len(c) - 1 25 | 26 | for i := 1; i <= totalR; i++ { 27 | if i < totalR && c[i+1] == 0 { 28 | i++ 29 | jumps++ 30 | continue 31 | } 32 | if c[i] == 0 { 33 | jumps++ 34 | continue 35 | } 36 | return 0 37 | } 38 | 39 | return jumps 40 | } 41 | 42 | func main() { 43 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 44 | 45 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 46 | checkError(err) 47 | 48 | defer stdout.Close() 49 | 50 | writer := bufio.NewWriterSize(stdout, 1024*1024) 51 | 52 | nTemp, err := strconv.ParseInt(readLine(reader), 10, 64) 53 | checkError(err) 54 | n := int32(nTemp) 55 | 56 | cTemp := strings.Split(readLine(reader), " ") 57 | 58 | var c []int32 59 | 60 | for i := 0; i < int(n); i++ { 61 | cItemTemp, err := strconv.ParseInt(cTemp[i], 10, 64) 62 | checkError(err) 63 | cItem := int32(cItemTemp) 64 | c = append(c, cItem) 65 | } 66 | 67 | result := jumpingOnClouds(c) 68 | 69 | fmt.Fprintf(writer, "%d\n", result) 70 | 71 | writer.Flush() 72 | } 73 | 74 | func readLine(reader *bufio.Reader) string { 75 | str, _, err := reader.ReadLine() 76 | if err == io.EOF { 77 | return "" 78 | } 79 | 80 | return strings.TrimRight(string(str), "\r\n") 81 | } 82 | 83 | func checkError(err error) { 84 | if err != nil { 85 | panic(err) 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /Algorithms/Warm-up/Repeated String/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Warm-up/Repeated String/description 1.png -------------------------------------------------------------------------------- /Algorithms/Warm-up/Repeated String/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Warm-up/Repeated String/description 2.png -------------------------------------------------------------------------------- /Algorithms/Warm-up/Repeated String/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Warm-up/Repeated String/description 3.png -------------------------------------------------------------------------------- /Algorithms/Warm-up/Repeated String/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | // Complete the repeatedString function below. 13 | func repeatedString(s string, n int64) int64 { 14 | min := 1 15 | maxS := 100 16 | maxN := int64(1000000000000) 17 | sTotal := len(s) 18 | if sTotal < min || sTotal > maxS || n < 1 || n > maxN { 19 | return 0 20 | } 21 | 22 | asCount := countA(sTotal, s) 23 | if asCount == 0 { 24 | return 0 25 | } 26 | if asCount == sTotal { 27 | return n 28 | } 29 | 30 | rst := (n / int64(sTotal)) * int64(asCount) 31 | modT := n % int64(sTotal) 32 | if modT > 0 { 33 | rst += int64(countA(int(modT), s[:modT])) 34 | } 35 | return rst 36 | } 37 | 38 | func countA(sTotal int, s string) int { 39 | return sTotal - len(strings.Replace(s, "a", "", -1)) 40 | } 41 | 42 | func main() { 43 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 44 | 45 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 46 | checkError(err) 47 | 48 | defer stdout.Close() 49 | 50 | writer := bufio.NewWriterSize(stdout, 1024*1024) 51 | 52 | s := readLine(reader) 53 | 54 | n, err := strconv.ParseInt(readLine(reader), 10, 64) 55 | checkError(err) 56 | 57 | result := repeatedString(s, n) 58 | 59 | fmt.Fprintf(writer, "%d\n", result) 60 | 61 | writer.Flush() 62 | } 63 | 64 | func readLine(reader *bufio.Reader) string { 65 | str, _, err := reader.ReadLine() 66 | if err == io.EOF { 67 | return "" 68 | } 69 | 70 | return strings.TrimRight(string(str), "\r\n") 71 | } 72 | 73 | func checkError(err error) { 74 | if err != nil { 75 | panic(err) 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /Algorithms/Warm-up/Sock Merchant/image 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Warm-up/Sock Merchant/image 1.png -------------------------------------------------------------------------------- /Algorithms/Warm-up/Sock Merchant/image 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Algorithms/Warm-up/Sock Merchant/image 2.png -------------------------------------------------------------------------------- /Algorithms/Warm-up/Sock Merchant/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | // Complete the sockMerchant function below. 13 | func sockMerchant(n int32, ar []int32) int32 { 14 | count := 0 15 | check := make(map[int32]bool) 16 | for _, num := range ar { 17 | if val, exists := check[num]; exists { 18 | if val { 19 | count++ 20 | check[num] = false 21 | continue 22 | } 23 | } 24 | check[num] = true 25 | } 26 | return int32(count) 27 | } 28 | 29 | func main() { 30 | reader := bufio.NewReaderSize(os.Stdin, 1024*1024) 31 | 32 | stdout, err := os.Create(os.Getenv("OUTPUT_PATH")) 33 | checkError(err) 34 | 35 | defer stdout.Close() 36 | 37 | writer := bufio.NewWriterSize(stdout, 1024*1024) 38 | 39 | nTemp, err := strconv.ParseInt(readLine(reader), 10, 64) 40 | checkError(err) 41 | n := int32(nTemp) 42 | 43 | arTemp := strings.Split(readLine(reader), " ") 44 | 45 | var ar []int32 46 | 47 | for i := 0; i < int(n); i++ { 48 | arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64) 49 | checkError(err) 50 | arItem := int32(arItemTemp) 51 | ar = append(ar, arItem) 52 | } 53 | 54 | result := sockMerchant(n, ar) 55 | 56 | fmt.Fprintf(writer, "%d\n", result) 57 | 58 | writer.Flush() 59 | } 60 | 61 | func readLine(reader *bufio.Reader) string { 62 | str, _, err := reader.ReadLine() 63 | if err == io.EOF { 64 | return "" 65 | } 66 | 67 | return strings.TrimRight(string(str), "\r\n") 68 | } 69 | 70 | func checkError(err error) { 71 | if err != nil { 72 | panic(err) 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean-large/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Bot Building/bot-clean-large/description 1.png -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean-large/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Bot Building/bot-clean-large/description 2.png -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean-large/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Bot Building/bot-clean-large/description 3.png -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean-large/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "math" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | type Bot struct { 13 | action string 14 | line int32 15 | position int32 16 | } 17 | 18 | type Dust struct { 19 | line int32 20 | position int32 21 | } 22 | 23 | func main() { 24 | cleanBot() 25 | } 26 | 27 | func cleanBot() { 28 | in := bufio.NewReader(os.Stdin) 29 | var b Bot 30 | mDust := make(map[int32]map[int32]bool) 31 | 32 | line := int32(0) 33 | sline := int32(0) 34 | for { 35 | inputBt, _, err := in.ReadLine() 36 | input := string(inputBt) 37 | if err != nil { 38 | break 39 | } 40 | 41 | if sline == 0 { 42 | b.setPosition(input) 43 | sline++ 44 | continue 45 | } 46 | if sline == 1 { 47 | sline++ 48 | continue 49 | } 50 | 51 | lDust := findDust(input) 52 | if len(lDust) > 0 { 53 | mDust[line] = lDust 54 | } 55 | line++ 56 | } 57 | 58 | if len(mDust) == 0 { 59 | return 60 | } 61 | 62 | if b.isCleanNow(mDust) { 63 | b.act() 64 | return 65 | } 66 | 67 | nextDust := b.getNextDust(mDust) 68 | if (Dust{}) == nextDust { 69 | return 70 | } 71 | 72 | b.moveToDust(nextDust) 73 | b.act() 74 | } 75 | 76 | func (b *Bot) moveToDust(d Dust) { 77 | if b.line != 0 && d.line < b.line { 78 | b.action = "UP" 79 | return 80 | } 81 | if d.line > b.line { 82 | b.action = "DOWN" 83 | return 84 | } 85 | if d.position < b.position { 86 | b.action = "LEFT" 87 | return 88 | } 89 | b.action = "RIGHT" 90 | } 91 | 92 | func (b Bot) act() { 93 | _, err := fmt.Fprint(os.Stdout, b.action) 94 | if err != nil { 95 | return 96 | } 97 | } 98 | 99 | func (b Bot) calculateEuclidean(dl, dp int32) int32 { 100 | return int32(math.Pow(float64(dl-b.line), 2) + math.Pow(float64(dp-b.position), 2)) 101 | } 102 | 103 | func (b Bot) getNextDust(mDust map[int32]map[int32]bool) Dust { 104 | var dust Dust 105 | dist := int32(0) 106 | for l, lDust := range mDust { 107 | for p, _ := range lDust { 108 | dDist := b.calculateEuclidean(l, p) 109 | if dist == 0 { 110 | dist = dDist 111 | dust.set(l, p) 112 | continue 113 | } 114 | if dDist < dist { 115 | dist = dDist 116 | dust.set(l, p) 117 | } 118 | } 119 | } 120 | return dust 121 | } 122 | 123 | func (b *Bot) isCleanNow(mDust map[int32]map[int32]bool) bool { 124 | if lDust, ok := mDust[b.line]; ok { 125 | if _, ok := lDust[b.position]; ok { 126 | b.action = "CLEAN" 127 | return true 128 | } 129 | } 130 | return false 131 | } 132 | 133 | func (d *Dust) set(l, p int32) { 134 | d.line = l 135 | d.position = p 136 | } 137 | 138 | func (b *Bot) setPosition(line string) { 139 | words := strings.Fields(line) 140 | l, err := strconv.Atoi(words[0]) 141 | if err != nil { 142 | os.Exit(1) 143 | } 144 | p, err := strconv.Atoi(words[1]) 145 | if err != nil { 146 | os.Exit(1) 147 | } 148 | b.line = int32(l) 149 | b.position = int32(p) 150 | } 151 | 152 | func findDust(line string) map[int32]bool { 153 | ds := make(map[int32]bool) 154 | for i, p := range line { 155 | if p == 100 { 156 | ds[int32(i)] = true 157 | } 158 | } 159 | return ds 160 | } 161 | -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean-stochastic/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Bot Building/bot-clean-stochastic/description 1.png -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean-stochastic/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Bot Building/bot-clean-stochastic/description 2.png -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean-stochastic/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Bot Building/bot-clean-stochastic/description 3.png -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean-stochastic/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | "strconv" 8 | "strings" 9 | ) 10 | 11 | type Bot struct { 12 | action string 13 | line int32 14 | position int32 15 | positionated bool 16 | priority string 17 | total int32 18 | } 19 | 20 | func main() { 21 | cleanBot() 22 | } 23 | 24 | func cleanBot() { 25 | in := bufio.NewReader(os.Stdin) 26 | var b Bot 27 | mDust := make(map[int32]map[int32]bool) 28 | 29 | line := int32(0) 30 | for { 31 | inputBt, _, err := in.ReadLine() 32 | input := string(inputBt) 33 | if err != nil { 34 | break 35 | } 36 | 37 | if line == 0 && !b.positionated { 38 | b.positionate(input) 39 | continue 40 | } 41 | 42 | b.setTotal(input) 43 | mDust[line] = findDust(input) 44 | 45 | if b.line == line-1 && len(mDust[line-1]) > 0 { 46 | b.cleanLine(mDust) 47 | break 48 | } 49 | 50 | line++ 51 | } 52 | 53 | b.cleanLine(mDust) 54 | if b.action == "" { 55 | b.changeLine(mDust) 56 | } 57 | if b.action == "" { 58 | return 59 | } 60 | 61 | _, err := fmt.Fprint(os.Stdout, b.action) 62 | if err != nil { 63 | return 64 | } 65 | } 66 | 67 | func (b *Bot) changeLine(mDust map[int32]map[int32]bool) { 68 | if b.line == 0 { 69 | if b.isDustThere(1, mDust) { 70 | b.action = "DOWN" 71 | } 72 | return 73 | } 74 | 75 | if b.line == (b.total - 1) { 76 | if b.isDustThere(b.total-2, mDust) { 77 | b.action = "UP" 78 | } 79 | return 80 | } 81 | 82 | if b.line < (b.total / 2) { 83 | if b.isDustThere(b.line-1, mDust) { 84 | b.action = "UP" 85 | return 86 | } 87 | if b.isDustThere(b.line+1, mDust) { 88 | b.action = "DOWN" 89 | } 90 | return 91 | } 92 | 93 | if b.isDustThere(b.line+1, mDust) { 94 | b.action = "DOWN" 95 | return 96 | } 97 | if b.isDustThere(b.line-1, mDust) { 98 | b.action = "UP" 99 | } 100 | } 101 | 102 | func (b *Bot) checkDustInNextLine(mDust map[int32]map[int32]bool) { 103 | if b.priority == "DOWN" && b.line < b.total-1 { 104 | if _, ok := mDust[b.line+1][b.position]; ok { 105 | b.action = "DOWN" 106 | } 107 | } 108 | if b.line > 0 { 109 | if _, ok := mDust[b.line-1][b.position]; ok { 110 | b.action = "UP" 111 | } 112 | } 113 | } 114 | 115 | func (b *Bot) cleanLine(mDust map[int32]map[int32]bool) { 116 | ds := mDust[b.line] 117 | if len(ds) == 0 { 118 | return 119 | } 120 | if _, ok := ds[b.position]; ok { 121 | b.action = "CLEAN" 122 | return 123 | } 124 | 125 | b.checkDustInNextLine(mDust) 126 | if b.action != "" { 127 | return 128 | } 129 | 130 | left := b.getSteps(b.position-1, 1, ds) 131 | right := b.getSteps(b.position+1, 1, ds) 132 | 133 | if left == 0 { 134 | b.action = "RIGHT" 135 | return 136 | } 137 | 138 | if right == 0 { 139 | b.action = "LEFT" 140 | return 141 | } 142 | 143 | if left == right { 144 | b.action = "LEFT" 145 | return 146 | } 147 | 148 | if left < right { 149 | b.action = "LEFT" 150 | return 151 | } 152 | b.action = "RIGHT" 153 | } 154 | 155 | func findDust(line string) map[int32]bool { 156 | ds := make(map[int32]bool) 157 | for i, p := range line { 158 | if p == 100 { 159 | ds[int32(i)] = true 160 | } 161 | } 162 | return ds 163 | } 164 | 165 | func (b *Bot) getSteps(i, done int32, ds map[int32]bool) int32 { 166 | if _, ok := ds[i]; ok { 167 | return done 168 | } 169 | 170 | if i <= 0 || i >= (b.total-1) { 171 | return 0 172 | } 173 | 174 | done++ 175 | if i < b.position { 176 | return b.getSteps(i-1, done, ds) 177 | } 178 | return b.getSteps(i+1, done, ds) 179 | } 180 | 181 | func (b Bot) isDustThere(l int32, mDust map[int32]map[int32]bool) bool { 182 | if len(mDust[l]) > 0 { 183 | return true 184 | } 185 | 186 | if l == 0 || l == b.total-1 { 187 | return false 188 | } 189 | if l < b.line { 190 | return b.isDustThere(l-1, mDust) 191 | } 192 | return b.isDustThere(l+1, mDust) 193 | } 194 | 195 | func (b *Bot) positionate(line string) { 196 | words := strings.Fields(line) 197 | l, err := strconv.Atoi(words[0]) 198 | if err != nil { 199 | os.Exit(1) 200 | } 201 | p, err := strconv.Atoi(words[1]) 202 | if err != nil { 203 | os.Exit(1) 204 | } 205 | b.line = int32(l) 206 | b.position = int32(p) 207 | b.positionated = true 208 | } 209 | 210 | func (b *Bot) setPriority() { 211 | if b.line < (b.total / 2) { 212 | b.priority = "DOWN" 213 | return 214 | } 215 | b.priority = "UP" 216 | } 217 | 218 | func (b *Bot) setTotal(line string) { 219 | if b.total == 0 { 220 | b.total = int32(len(line)) 221 | b.setPriority() 222 | } 223 | } 224 | -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Bot Building/bot-clean/description 1.png -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Bot Building/bot-clean/description 2.png -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean/description 3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Bot Building/bot-clean/description 3.png -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean/description 4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Bot Building/bot-clean/description 4.png -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-clean/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "math" 7 | "os" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | type Bot struct { 13 | action string 14 | line int32 15 | position int32 16 | } 17 | 18 | type Dust struct { 19 | line int32 20 | position int32 21 | } 22 | 23 | func main() { 24 | cleanBot() 25 | } 26 | 27 | func cleanBot() { 28 | in := bufio.NewReader(os.Stdin) 29 | var b Bot 30 | mDust := make(map[int32]map[int32]bool) 31 | 32 | line := int32(0) 33 | sline := int32(0) 34 | for { 35 | inputBt, _, err := in.ReadLine() 36 | input := string(inputBt) 37 | if err != nil { 38 | break 39 | } 40 | 41 | if sline == 0 { 42 | b.setPosition(input) 43 | sline++ 44 | continue 45 | } 46 | 47 | lDust := findDust(input) 48 | if len(lDust) > 0 { 49 | mDust[line] = lDust 50 | } 51 | line++ 52 | } 53 | 54 | if len(mDust) == 0 { 55 | return 56 | } 57 | 58 | if b.isCleanNow(mDust) { 59 | b.act() 60 | return 61 | } 62 | 63 | nextDust := b.getNextDust(mDust) 64 | if (Dust{}) == nextDust { 65 | return 66 | } 67 | 68 | b.moveToDust(nextDust) 69 | b.act() 70 | } 71 | 72 | func (b *Bot) moveToDust(d Dust) { 73 | if b.line != 0 && d.line < b.line { 74 | b.action = "UP" 75 | return 76 | } 77 | if d.line > b.line { 78 | b.action = "DOWN" 79 | return 80 | } 81 | if d.position < b.position { 82 | b.action = "LEFT" 83 | return 84 | } 85 | b.action = "RIGHT" 86 | } 87 | 88 | func (b Bot) act() { 89 | _, err := fmt.Fprint(os.Stdout, b.action) 90 | if err != nil { 91 | return 92 | } 93 | } 94 | 95 | func (b Bot) calculateEuclidean(dl, dp int32) int32 { 96 | return int32(math.Pow(float64(dl-b.line), 2) + math.Pow(float64(dp-b.position), 2)) 97 | } 98 | 99 | func (b Bot) getNextDust(mDust map[int32]map[int32]bool) Dust { 100 | var dust Dust 101 | dist := int32(0) 102 | for l, lDust := range mDust { 103 | for p, _ := range lDust { 104 | dDist := b.calculateEuclidean(l, p) 105 | if dist == 0 { 106 | dist = dDist 107 | dust.set(l, p) 108 | continue 109 | } 110 | if dDist < dist { 111 | dist = dDist 112 | dust.set(l, p) 113 | } 114 | } 115 | } 116 | return dust 117 | } 118 | 119 | func (b *Bot) isCleanNow(mDust map[int32]map[int32]bool) bool { 120 | if lDust, ok := mDust[b.line]; ok { 121 | if _, ok := lDust[b.position]; ok { 122 | b.action = "CLEAN" 123 | return true 124 | } 125 | } 126 | return false 127 | } 128 | 129 | func (d *Dust) set(l, p int32) { 130 | d.line = l 131 | d.position = p 132 | } 133 | 134 | func (b *Bot) setPosition(line string) { 135 | words := strings.Fields(line) 136 | l, err := strconv.Atoi(words[0]) 137 | if err != nil { 138 | os.Exit(1) 139 | } 140 | p, err := strconv.Atoi(words[1]) 141 | if err != nil { 142 | os.Exit(1) 143 | } 144 | b.line = int32(l) 145 | b.position = int32(p) 146 | } 147 | 148 | func findDust(line string) map[int32]bool { 149 | ds := make(map[int32]bool) 150 | for i, p := range line { 151 | if p == 100 { 152 | ds[int32(i)] = true 153 | } 154 | } 155 | return ds 156 | } 157 | -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-saves-princess/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Bot Building/bot-saves-princess/description 1.png -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-saves-princess/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Bot Building/bot-saves-princess/description 2.png -------------------------------------------------------------------------------- /Artificial Intelligence/Bot Building/bot-saves-princess/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "math" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | type Person struct { 12 | line int 13 | position int 14 | found bool 15 | } 16 | 17 | func main() { 18 | in := bufio.NewReader(os.Stdin) 19 | var m, p Person 20 | 21 | line := 0 22 | for { 23 | inputBt, _, err := in.ReadLine() 24 | input := string(inputBt) 25 | if err != nil { 26 | break 27 | } 28 | 29 | if mi := strings.Index(input, "m"); mi != -1 { 30 | m.line = line 31 | m.position = mi 32 | m.found = true 33 | } 34 | if pi := strings.Index(input, "p"); pi != -1 { 35 | p.line = line 36 | p.position = pi 37 | p.found = true 38 | } 39 | 40 | if m.found && p.found { 41 | break 42 | } 43 | line++ 44 | } 45 | 46 | if !m.found || !p.found { 47 | return 48 | } 49 | 50 | ver := getVerticalSteps(int32(p.line-m.line), "UP", "DOWN") 51 | hor := getVerticalSteps(int32(p.position-m.position), "LEFT", "RIGHT") 52 | _, err := fmt.Fprint(os.Stdout, ver) 53 | if err != nil { 54 | return 55 | } 56 | _, err = fmt.Fprint(os.Stdout, hor) 57 | if err != nil { 58 | return 59 | } 60 | } 61 | 62 | func getVerticalSteps(n int32, txtf, txts string) string { 63 | var rst string 64 | if n == 0 { 65 | return rst 66 | } 67 | 68 | txt := txts 69 | if n < 0 { 70 | txt = txtf 71 | } 72 | n = int32(math.Abs(float64(n))) 73 | for i := int32(0); i < n; i++ { 74 | rst += fmt.Sprintf("%s\n", txt) 75 | } 76 | return rst 77 | } 78 | -------------------------------------------------------------------------------- /Artificial Intelligence/Statistics and Machine Learning/Matching Questions with their Answers/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Statistics and Machine Learning/Matching Questions with their Answers/description 1.png -------------------------------------------------------------------------------- /Artificial Intelligence/Statistics and Machine Learning/Matching Questions with their Answers/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/Artificial Intelligence/Statistics and Machine Learning/Matching Questions with their Answers/description 2.png -------------------------------------------------------------------------------- /Artificial Intelligence/Statistics and Machine Learning/Matching Questions with their Answers/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | "strings" 8 | ) 9 | 10 | type Question struct { 11 | text string 12 | order int32 13 | words []string 14 | uniqueWordsID []string 15 | uniqueWords []string 16 | answer string 17 | } 18 | 19 | type Sentence struct { 20 | text string 21 | wordsID []string 22 | words []string 23 | wordsHash string 24 | } 25 | 26 | type Sentences []Sentence 27 | 28 | func main() { 29 | reply() 30 | } 31 | 32 | func reply() { 33 | in := bufio.NewReader(os.Stdin) 34 | var lines []string 35 | for { 36 | inputBt, _, err := in.ReadLine() 37 | input := string(inputBt) 38 | if err != nil { 39 | break 40 | } 41 | 42 | lines = append(lines, input) 43 | } 44 | 45 | if len(lines) == 0 { 46 | return 47 | } 48 | 49 | ses := splitSentences(lines[0]) 50 | responses := strings.Split(lines[len(lines)-1], ";") 51 | questions := lines[1 : len(lines)-1] 52 | 53 | dic, dicRev, err := ses.createDictionary() 54 | if err != nil { 55 | return 56 | } 57 | qs := formalizeQuestions(questions, dic, dicRev) 58 | 59 | for _, q := range qs { 60 | q.findResponse(ses, responses) 61 | _, err := fmt.Fprint(os.Stdout, q.answer+"\n") 62 | if err != nil { 63 | return 64 | } 65 | } 66 | } 67 | 68 | func (q *Question) findResponse(ses Sentences, responses []string) { 69 | var max int32 70 | var seRes Sentence 71 | for _, se := range ses { 72 | rst := q.searchSentence(se.wordsHash) 73 | if max == 0 { 74 | max = rst 75 | seRes = se 76 | } 77 | if rst > max { 78 | max = rst 79 | seRes = se 80 | } 81 | } 82 | if max == 0 { 83 | return 84 | } 85 | 86 | var maxAns int32 87 | var ans string 88 | for _, resp := range responses { 89 | if strings.Contains(seRes.text, resp) { 90 | lenght := int32(len(resp)) 91 | if maxAns == 0 { 92 | maxAns = lenght 93 | ans = resp 94 | } 95 | if lenght > maxAns { 96 | maxAns = lenght 97 | ans = resp 98 | } 99 | } 100 | } 101 | if maxAns == 0 { 102 | return 103 | } 104 | q.answer = ans 105 | } 106 | 107 | func (q Question) searchSentence(seHash string) int32 { 108 | words := int32(len(q.uniqueWordsID)) 109 | return q.searchPart(seHash, 0, words, words) 110 | } 111 | 112 | func (q Question) searchPart(seHash string, from, to, words int32) int32 { 113 | if words == 0 { 114 | return 0 115 | } 116 | if strings.Contains(seHash, q.getPartHash(from, to)) { 117 | return words 118 | } 119 | 120 | from = from + 1 121 | to = to + 1 122 | if to > int32(len(q.uniqueWordsID)) { 123 | from = 0 124 | words = words - 1 125 | to = words 126 | } 127 | return q.searchPart(seHash, from, to, words) 128 | } 129 | 130 | func (q *Question) getPartHash(from, to int32) string { 131 | var hash string 132 | for _, wID := range q.uniqueWordsID[from:to] { 133 | hash += wID 134 | } 135 | return hash 136 | } 137 | 138 | func (se Sentence) countrySame(wsIDs []string) int32 { 139 | var c int32 140 | for _, wsID := range wsIDs { 141 | for _, wID := range se.wordsID { 142 | if wsID == wID { 143 | c++ 144 | break 145 | } 146 | } 147 | } 148 | 149 | return c 150 | } 151 | 152 | func formalizeQuestions(questionsTxt []string, dic map[string]string, dicRev map[string]string) []Question { 153 | var qs []Question 154 | for key, queTxt := range questionsTxt { 155 | q := Question{ 156 | text: queTxt, 157 | order: int32(key), 158 | } 159 | q.setWords(dic) 160 | qs = append(qs, q) 161 | } 162 | 163 | for k := range qs { 164 | qs[k].setUnique(qs, dicRev) 165 | } 166 | return qs 167 | } 168 | 169 | func (q *Question) setWords(dic map[string]string) { 170 | for _, word := range getWords(q.text) { 171 | word := strings.ToLower(word) 172 | if wID, ok := dic[word]; ok { 173 | q.words = append(q.words, wID) 174 | } 175 | } 176 | } 177 | 178 | func (que Question) isWordUnique(wID string, qs []Question) bool { 179 | for _, q := range qs { 180 | if q.order == que.order { 181 | continue 182 | } 183 | for _, qwID := range q.words { 184 | if qwID == wID { 185 | return false 186 | } 187 | } 188 | } 189 | return true 190 | } 191 | 192 | func (q *Question) setUnique(qs []Question, dicRev map[string]string) { 193 | for _, wID := range q.words { 194 | if q.isWordUnique(wID, qs) { 195 | q.uniqueWordsID = append(q.uniqueWordsID, wID) 196 | q.uniqueWords = append(q.uniqueWords, dicRev[wID]) 197 | } 198 | } 199 | } 200 | 201 | func splitSentences(text string) Sentences { 202 | var ses []Sentence 203 | if strings.Contains(text, "!") { 204 | text = strings.ReplaceAll(text, "!", ".") 205 | } 206 | for _, sTxt := range strings.Split(text, ".") { 207 | se := Sentence{ 208 | text: sTxt, 209 | } 210 | ses = append(ses, se) 211 | } 212 | return ses 213 | } 214 | 215 | func (se *Sentence) setHash() { 216 | for _, wId := range se.wordsID { 217 | se.wordsHash += wId 218 | } 219 | } 220 | 221 | func (ses *Sentences) createDictionary() (map[string]string, map[string]string, error) { 222 | dic := make(map[string]string) 223 | dicRev := make(map[string]string) 224 | c := int32(1) 225 | for sek, se := range *ses { 226 | for _, word := range getWords(se.text) { 227 | word = strings.TrimSpace(strings.ToLower(word)) 228 | dID, ok := dic[word] 229 | if ok { 230 | (*ses)[sek].wordsID = append((*ses)[sek].wordsID, dID) 231 | (*ses)[sek].words = append((*ses)[sek].words, word) 232 | continue 233 | } 234 | cID := toID(c) 235 | dic[word] = cID 236 | dicRev[cID] = word 237 | (*ses)[sek].wordsID = append((*ses)[sek].wordsID, cID) 238 | (*ses)[sek].words = append((*ses)[sek].words, word) 239 | c++ 240 | } 241 | (*ses)[sek].setHash() 242 | } 243 | return dic, dicRev, nil 244 | } 245 | 246 | func toID(num int32) string { 247 | return fmt.Sprintf("000000%d", num) 248 | } 249 | 250 | func getWords(sentence string) []string { 251 | return strings.Split(strip(sentence), " ") 252 | } 253 | 254 | func strip(s string) string { 255 | var result strings.Builder 256 | for i := 0; i < len(s); i++ { 257 | b := s[i] 258 | if ('a' <= b && b <= 'z') || 259 | ('A' <= b && b <= 'Z') || 260 | ('0' <= b && b <= '9') || 261 | b == ' ' { 262 | result.WriteByte(b) 263 | } 264 | } 265 | return result.String() 266 | } 267 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | 5 |
6 | 7 | This repository contains challenges of site [HackerRank](https://www.hackerrank.com). 8 | 9 | 10 | ### Algorithms 11 | 12 | | Subdomain | Challenge | Level | Max Score | Score Obtained | 13 | | :------------------------- | :----------------------------------------------------------- | :----: | :-------: | :------------: | 14 | | Arrays | [2D Array - DS](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Arrays/2D%20Array%20DS/main.go) | Easy | 15 | 15 | 15 | | Arrays | [Arrays: Left Rotation](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Arrays/Arrays%20Left%20Rotation/main.go) | Easy | 20 | 20 | 16 | | Arrays | [Array Manipulation](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Arrays/Array%20Manipulation/main.go) | Hard | 60 | 60 | 17 | | Arrays | [Minimum Swaps 2](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Arrays/Minimum%20Swaps%202/main.go) | Medium | 40 | 40 | 18 | | Arrays | [New Year Chaos](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Arrays/New%20Year%20Chaos/main.go) | Medium | 40 | 40 | 19 | | Dictionaries and Hashmaps | [Frequency Queries](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Dictionaries%20and%20Hashmaps/Frequency%20Queries/main.go) | Medium | 40 | 40 | 20 | | Dictionaries and Hashmaps | [Hash Tables: Ransom Note](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Dictionaries%20and%20Hashmaps/Hash%20Tables%20Ransom%20Notes/main.go) | Easy | 25 | 25 | 21 | | Dictionaries and Hashmaps | [Two Strings](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Dictionaries%20and%20Hashmaps/Two%20Strings/main.go) | Easy | 25 | 25 | 22 | | Dynamic Programming | [Candies](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Dynamic%20Programming/Candies/main.go) | Medium | 50 | 50 | 23 | | Graph | [Find the nearest clone](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Graphs/Find%20the%20nearest%20clone/main.go) | Medium | 45 | 45 | 24 | | Graph | [Roads and Libraries](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Graphs/Roads%20and%20Libraries/main.go) | Medium | 30 | 30 | 25 | | Greedy Algorithms | [Max Min](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Greedy%20Algorithms/Max%20Min/main.go) | Medium | 35 | 35 | 26 | | Recursion and Backtracking | [Recursive Digit Sum](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Recursion%20and%20Backtracking/Recursive%20Digit%20Sum/main.go) | Medium | 30 | 30 | 27 | | Search | [Maximum Subarray Sum](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Search/Maximum%20Subarray%20Sum/main.go) | Hard | 65 | 65 | 28 | | Search | [Pairs](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Search/Pairs/main.go) | Hard | 50 | 50 | 29 | | Sorting | [Bubble Sort](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Sorting/Bubble%20Sort/main.go) | Easy | 30 | 30 | 30 | | Sorting | [Mark and Toys](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Sorting/Mark%20and%20Toys/main.go) | Easy | 35 | 35 | 31 | | Sorting | [Merge Sort: Counting Inversions](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Sorting/Merge%20Sort:%20Counting%20Inversions/main.go) | Hard | 45 | 45 | 32 | | Stacks | [Largest Rectangle](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Stacks/Largest%20Rectangle/main.go) | Medium | 50 | 50 | 33 | | String Manipulation | [Sherlock and the Valid String](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/String%20Manipulation/Sherlock%20and%20the%20Valid%20String/main.go) | Medium | 35 | 35 | 34 | | String Manipulation | [Strings: Making Anagrams](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/String%20Manipulation/Strings:%20Making%20Anagrams/main.go) | Easy | 25 | 25 | 35 | | Warm-up | [Counting Valleys](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Warm-up/Counting%20Valleys/main.go) | Easy | 15 | 15 | 36 | | Warm-up | [Jumping on the Clouds](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Warm-up/Jumping%20On%20The%20Cloud/main.go) | Easy | 20 | 20 | 37 | | Warm-up | [Repeated String](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Warm-up/Repeated%20String/main.go) | Easy | 20 | 20 | 38 | | Warm-up | [Sock Merchant](https://github.com/dpcat237/hackerrank-golang/blob/master/Algorithms/Warm-up/Sock%20Merchant/main.go) | Easy | 10 | 10 | 39 | 40 | 41 | ### Artificial Intelligence 42 | 43 | | Subdomain | Challenge | Level | Max Score | Score Obtained | 44 | | :------------------------------ | :----------------------------------------------------------- | :---: | :-------: | :------------: | 45 | | Bot Building | [BotClean](https://github.com/dpcat237/hackerrank-golang/blob/master/Artificial%20Intelligence/Bot%20Building/bot-clean/main.go) | Easy | 17.83 | 17.83 | 46 | | Bot Building | [BotClean Stochastic](https://github.com/dpcat237/hackerrank-golang/blob/master/Artificial%20Intelligence/Bot%20Building/bot-clean-stochastic/main.go) | Easy | 10 | 10 | 47 | | Bot Building | [BotClean Large](https://github.com/dpcat237/hackerrank-golang/blob/master/Artificial%20Intelligence/Bot%20Building/bot-clean-large/main.go) | Hard | 56.40 | 54.70 | 48 | | Bot Building | [Bot saves princess](https://github.com/dpcat237/hackerrank-golang/blob/master/Artificial%20Intelligence/Bot%20Building/bot-saves-princess/main.go) | Easy | 13.9 | 13.9 | 49 | | Statistics and Machine Learning | [Matching Questions with their Answers](https://github.com/dpcat237/hackerrank-golang/blob/master/Artificial%20Intelligence/Statistics%20and%20Machine%20Learning/Matching%20Questions%20with%20their%20Answers/main.go) | Hard | 50 | 26 | 50 | 51 | ### SQL 52 | 53 | | Challenge | Level | Max Score | Score Obtained | 54 | | :----------------------------------------------------------- | :---: | :-------: | :------------: | 55 | | [Weather Observation Station 5](https://github.com/dpcat237/hackerrank-golang/blob/master/SQL/Weather%20Observation%20Station%205/query.sql) | Easy | 15 | 15 | -------------------------------------------------------------------------------- /SQL/Weather Observation Station 5/description 1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/SQL/Weather Observation Station 5/description 1.png -------------------------------------------------------------------------------- /SQL/Weather Observation Station 5/description 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpcat237/hackerrank-golang/e856910929a56540ed44b533513fa61953375038/SQL/Weather Observation Station 5/description 2.png -------------------------------------------------------------------------------- /SQL/Weather Observation Station 5/query.sql: -------------------------------------------------------------------------------- 1 | SELECT CITY, CHAR_LENGTH(CITY) 2 | FROM STATION 3 | ORDER BY CHAR_LENGTH(CITY) ASC, CITY ASC 4 | LIMIT 1; 5 | 6 | SELECT CITY, CHAR_LENGTH(CITY) 7 | FROM STATION 8 | ORDER BY CHAR_LENGTH(CITY) DESC, CITY ASC 9 | LIMIT 1; --------------------------------------------------------------------------------