├── .gitignore ├── README.md └── tasks ├── 1.go ├── 10.go ├── 1004.go ├── 101.go ├── 11.go ├── 125.go ├── 146.go ├── 1493.go ├── 150.go ├── 19.go ├── 2.go ├── 20.go ├── 200.go ├── 206.go ├── 21.go ├── 22.go ├── 228.go ├── 23.go ├── 232.go ├── 234.go ├── 238.go ├── 268.go ├── 279.go ├── 28.go ├── 281.py ├── 283.go ├── 3.go ├── 33.go ├── 332.go ├── 34.go ├── 350.go ├── 356.go ├── 380.go ├── 392.go ├── 4.go ├── 42.go ├── 443.go ├── 449.go ├── 48.go ├── 49.go ├── 5.go ├── 56.go ├── 560.go ├── 567.go ├── 59.go ├── 6.go ├── 658.go ├── 7.go ├── 71.go ├── 8.go ├── 849.go ├── 85.go ├── 88.go ├── 9.go ├── 933.go ├── 974.go ├── 977.go └── 98.go /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | tmp.go 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode tasks for Yandex 2 | 3 | | Status | # | Title | **Difficulty** | Solution | 4 | | :-----: | :--: | :-----------------------------------------------------------------------------------------------------------------------------------------------: | :------------: | :-------------------------------------------------------------------------------: | 5 | | ☑ | 228 | [summary ranges](https://leetcode.com/problems/summary-ranges/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/228.go) | 6 | | ☑ | 443 | [string compression](https://leetcode.com/problems/string-compression/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/443.go) | 7 | | ☑ | 206 | [reverse linked list](https://leetcode.com/problems/reverse-linked-list/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/206.go) | 8 | | ☑ | 22 | [generate parentheses](https://leetcode.com/problems/generate-parentheses/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/22.go) | 9 | | ☑ | 146 | [lru cache](https://leetcode.com/problems/lru-cache/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/146.go) | 10 | | ☑ | 283 | [move zeroes](https://leetcode.com/problems/move-zeroes/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/283.go) | 11 | | ☑ | 125 | [valid palindrome](https://leetcode.com/problems/valid-palindrome/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/125.go) | 12 | | ☑ | 49 | [group anagrams](https://leetcode.com/problems/group-anagrams/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/49.go) | 13 | | ☑ | 1493 | [longest subarray](https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/1493.go) | 14 | | ☑ | 20 | [valid parentheses](https://leetcode.com/problems/valid-parentheses/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/20.go) | 15 | | ☑ | 560 | [subarray sum equals k](https://leetcode.com/problems/subarray-sum-equals-k/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/560.go) | 16 | | ☑ | 380 | [insert delete getrandom o1](https://leetcode.com/problems/insert-delete-getrandom-o1/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/380.go) | 17 | | ☑ | 1 | [two sum](https://leetcode.com/problems/two-sum/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/1.go) | 18 | | ☑ | 23 | [merge k sorted lists](https://leetcode.com/problems/merge-k-sorted-lists/) | **Hard** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/23.go) | 19 | | ☑ | 567 | [permutation in string](https://leetcode.com/problems/permutation-in-string/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/567.go) | 20 | | ☑ | 56 | [merge intervals](https://leetcode.com/problems/merge-intervals/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/56.go) | 21 | | ☑ | 933 | [number of recent calls](https://leetcode.com/problems/number-of-recent-calls/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/933.go) | 22 | | ☑ | 98 | [validate binary search tree](https://leetcode.com/problems/validate-binary-search-tree/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/98.go) | 23 | | ☑ | 232 | [implement queue using stacks](https://leetcode.com/problems/implement-queue-using-stacks/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/232.go) | 24 | | ☑ | 59 | [spiral matrix ii](https://leetcode.com/problems/spiral-matrix-ii/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/59.go) | 25 | | ☑ | 1004 | [max consecutive ones iii](https://leetcode.com/problems/max-consecutive-ones-iii/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/1004.go) | 26 | | ☑ | 42 | [trapping rain water](https://leetcode.com/problems/trapping-rain-water/) | **Hard** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/42.go) | 27 | | ☑ | 3 | [longest substring without repeating characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/3.go) | 28 | | ☑ | 2 | [add two numbers](https://leetcode.com/problems/add-two-numbers/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/2.go) | 29 | | ☑ | 21 | [merge two sorted lists](https://leetcode.com/problems/merge-two-sorted-lists/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/21.go) | 30 | | ☑ | 88 | [merge sorted array](https://leetcode.com/problems/merge-sorted-array/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/88.go) | 31 | | ☑ | 101 | [symmetric tree](https://leetcode.com/problems/symmetric-tree/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/101.go) | 32 | | ☑ | 268 | [missing number](https://leetcode.com/problems/missing-number/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/268.go) | 33 | | ☑ | 48 | [rotate image](https://leetcode.com/problems/rotate-image/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/48.go) | 34 | | ☑ | 5 | [longest palindromic substring](https://leetcode.com/problems/longest-palindromic-substring/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/5.go) | 35 | | ☑ | 332 | [reconstruct itinerary](https://leetcode.com/problems/reconstruct-itinerary/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/332.go) | 36 | | ☑ | 658 | [find k closest elements](https://leetcode.com/problems/find-k-closest-elements/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/658.go) | 37 | | ☑ | 200 | [number of islands](https://leetcode.com/problems/number-of-islands/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/200.go) | 38 | | ☑ | 392 | [is subsequence](https://leetcode.com/problems/is-subsequence/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/392.go) | 39 | | ☑ | 849 | [maximize distance to closest person](https://leetcode.com/problems/maximize-distance-to-closest-person/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/849.go) | 40 | | ☑ | 4 | [median of two sorted arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | **Hard** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/4.go) | 41 | | ☑ | 974 | [subarray sums divisible by k](https://leetcode.com/problems/subarray-sums-divisible-by-k/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/974.go) | 42 | | ☑ | 33 | [search in rotated sorted array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/33.go) | 43 | | ☑ | 449 | [serialize and deserialize bst](https://leetcode.com/problems/serialize-and-deserialize-bst/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/449.go) | 44 | | ☑ | 977 | [squares of a sorted array](https://leetcode.com/problems/squares-of-a-sorted-array/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/977.go) | 45 | | ☑ | 350 | [intersection of two arrays ii](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/350.go) | 46 | | ☑ | 19 | [remove nth node from end of list](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/19.go) | 47 | | ☑ | 85 | [maximal rectangle](https://leetcode.com/problems/maximal-rectangle/) | **Hard** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/85.go) | 48 | | ☑ | 34 | [find first and last position of element in sorted array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/34.go) | 49 | | ☑ | 150 | [evaluate reverse polish notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/150.go) | 50 | | ☑ | 279 | [perfect squares](https://leetcode.com/problems/perfect-squares/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/279.go) | 51 | | ☑ | 28 | [implement strstr](https://leetcode.com/problems/implement-strstr/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/28.go) | 52 | | ☑ | 238 | [product of array except self](https://leetcode.com/problems/product-of-array-except-self/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/238.go) | 53 | | ☑ | 71 | [simplify path](https://leetcode.com/problems/simplify-path/) | **Medium** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/71.go) | 54 | | ☑ | 234 | [palindrome linked list](https://leetcode.com/problems/palindrome-linked-list/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/234.go) | 55 | 56 | # LeetCode tasks for practice 57 | 58 | | Status | # | Title | **Difficulty** | Solution | 59 | | :-----: | :-: | :---------------------------------------------------------------: | :------------: | :----------------------------------------------------------------------------: | 60 | | ☐ | 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | **Easy** | [here](https://github.com/VanjaRo/LeetCodeTasksForYandex/blob/main/tasks/7.go) | 61 | -------------------------------------------------------------------------------- /tasks/1.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | func twoSum(nums []int, target int) []int { 8 | // [rem]ind 9 | remMap := make(map[int]int) 10 | for i, v := range nums { 11 | if val, ok := remMap[v]; ok { 12 | return []int{val, i} 13 | } 14 | rem := target - v 15 | remMap[rem] = i 16 | } 17 | return []int{-1, -1} 18 | } 19 | -------------------------------------------------------------------------------- /tasks/10.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Print(isMatch("absabs", "abs*")) 9 | } 10 | 11 | // abab bas 12 | // ab* b.* 13 | 14 | func isMatch(s string, p string) bool { 15 | m := make(map[[2]int]bool) 16 | var dp func(i, j int) bool 17 | dp = func(i, j int) bool { 18 | if _, ok := m[[2]int{i, j}]; !ok { 19 | var ans bool 20 | if j == len(p) { 21 | ans = i == len(s) 22 | } else { 23 | firstMatch := i < len(s) && (p[j] == s[i] || p[j] == '.') 24 | if j+1 < len(p) && p[j+1] == '*' { 25 | ans = dp(i, j+2) || firstMatch && dp(i+1, j) 26 | } else { 27 | ans = firstMatch && dp(i+1, j+1) 28 | } 29 | } 30 | m[[2]int{i, j}] = ans 31 | } 32 | return m[[2]int{i, j}] 33 | } 34 | return dp(0, 0) 35 | } 36 | -------------------------------------------------------------------------------- /tasks/1004.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | func longestOnes(nums []int, k int) int { 8 | windowStart := 0 9 | zeroCounter := 0 10 | windowEnd := 0 11 | // maxLen := 0 12 | for windowEnd < len(nums) { 13 | if nums[windowEnd] == 0 { 14 | zeroCounter++ 15 | } 16 | if zeroCounter > k { 17 | if nums[windowStart] == 0 { 18 | zeroCounter-- 19 | } 20 | windowStart++ 21 | } 22 | windowEnd++ 23 | } 24 | return windowEnd - windowStart 25 | } 26 | -------------------------------------------------------------------------------- /tasks/101.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | type TreeNode struct { 8 | Val int 9 | Left *TreeNode 10 | Right *TreeNode 11 | } 12 | 13 | func isSymmetric(root *TreeNode) bool { 14 | return areSymmetricNodes(root.Left, root.Right) 15 | } 16 | 17 | func areSymmetricNodes(lSubTree *TreeNode, rSubTree *TreeNode) bool { 18 | if lSubTree == nil && rSubTree == nil { 19 | return true 20 | } 21 | 22 | if lSubTree == nil || rSubTree == nil || lSubTree.Val != rSubTree.Val { 23 | return false 24 | } 25 | 26 | return areSymmetricNodes(lSubTree.Left, rSubTree.Right) && areSymmetricNodes(lSubTree.Right, rSubTree.Left) 27 | 28 | } 29 | -------------------------------------------------------------------------------- /tasks/11.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | func maxArea(height []int) int { 8 | var i, j, mx int 9 | j = len(height) - 1 10 | for i <= j { 11 | minDimY := min(height[i], height[j]) 12 | space := (j - i) * minDimY 13 | if space > mx { 14 | mx = space 15 | } 16 | if height[j] > height[i] { 17 | i++ 18 | } else { 19 | j-- 20 | } 21 | } 22 | return mx 23 | } 24 | 25 | func min(i, j int) int { 26 | if i < j { 27 | return i 28 | } 29 | return j 30 | } 31 | 32 | func max(i, j int) int { 33 | if i < j { 34 | return j 35 | } 36 | return i 37 | } 38 | -------------------------------------------------------------------------------- /tasks/125.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Print(isPalindrome("A man, a plan, a canal: Panama")) 9 | } 10 | 11 | func isPalindrome(s string) bool { 12 | for left, right := 0, len(s)-1; left <= right; left, right = left+1, right-1 { 13 | vLeft := value(s[left]) 14 | vRight := value(s[right]) 15 | if vLeft < 0 { 16 | right++ 17 | continue 18 | } 19 | if vRight < 0 { 20 | left-- 21 | continue 22 | } 23 | if vLeft != vRight { 24 | return false 25 | } 26 | 27 | } 28 | return true 29 | } 30 | 31 | func value(m byte) int { 32 | if m >= '0' && m <= '9' { 33 | return int((m - '0') + 100) 34 | } 35 | if m >= 'A' && m <= 'Z' { 36 | return int(m - 'A') 37 | } 38 | if m >= 'a' && m <= 'z' { 39 | return int(m - 'a') 40 | } 41 | return -1 42 | } 43 | 44 | // Same func but with usage of regexp and strings modules 45 | 46 | // func isPalindrome(s string) bool { 47 | // reg, _ := regexp.Compile("[^a-zA-Z]+") 48 | // formattedStr := reg.ReplaceAllString(s, "") 49 | // formattedStr = strings.ToLower(strings.Join(strings.Split(formattedStr, " "), "")) 50 | // var revesedStr string 51 | // for _, c := range formattedStr { 52 | // revesedStr = string(c) + revesedStr 53 | // } 54 | // fmt.Println(formattedStr) 55 | // fmt.Println(revesedStr) 56 | // return revesedStr == formattedStr 57 | // } 58 | -------------------------------------------------------------------------------- /tasks/146.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | lru := Constructor(2) 7 | lru.Put(2, 1) 8 | lru.Put(1, 1) 9 | lru.Put(2, 3) 10 | lru.Put(4, 1) 11 | fmt.Println(lru.head.next) 12 | fmt.Println(lru.tail.prev) 13 | // lru.Put(2, 2) 14 | // // fmt.Println(lru.dLList.head) 15 | // lru.Get(1) 16 | // lru.Put(3, 3) 17 | // fmt.Println(lru.dLList.tail.value) 18 | // fmt.Println(lru.Get(2)) 19 | 20 | } 21 | 22 | type LRUCache struct { 23 | cacheMap map[int]*DLNode 24 | capacity int 25 | head, tail *DLNode 26 | } 27 | 28 | type DLNode struct { 29 | prev *DLNode 30 | next *DLNode 31 | key int 32 | value int 33 | } 34 | 35 | func Constructor(capacity int) LRUCache { 36 | lru := LRUCache{ 37 | cacheMap: make(map[int]*DLNode), 38 | capacity: capacity, 39 | head: &DLNode{}, 40 | tail: &DLNode{}, 41 | } 42 | lru.head.next, lru.tail.prev = lru.tail, lru.head 43 | return lru 44 | } 45 | 46 | func (this *LRUCache) Get(key int) int { 47 | n, ok := this.cacheMap[key] 48 | if !ok { 49 | return -1 50 | } 51 | this.delete(n) 52 | this.pushAfter(this.head, n) 53 | return n.value 54 | } 55 | 56 | func (this *LRUCache) Put(key int, value int) { 57 | n, ok := this.cacheMap[key] 58 | if !ok { 59 | if len(this.cacheMap) == this.capacity { 60 | endNode := this.tail.prev 61 | delete(this.cacheMap, endNode.key) 62 | this.delete(endNode) 63 | endNode.key, endNode.value = key, value 64 | n = endNode 65 | } else { 66 | n = &DLNode{ 67 | value: value, 68 | key: key, 69 | } 70 | } 71 | this.cacheMap[key] = n 72 | } else { 73 | n.value = value 74 | this.delete(n) 75 | } 76 | 77 | this.pushAfter(this.head, n) 78 | } 79 | 80 | func (this *LRUCache) pushAfter(current, new *DLNode) { 81 | next := current.next 82 | new.prev, new.next = current, next 83 | current.next, next.prev = new, new 84 | } 85 | 86 | func (this *LRUCache) delete(curr *DLNode) { 87 | prev, next := curr.prev, curr.next 88 | prev.next, next.prev = next, prev 89 | } 90 | -------------------------------------------------------------------------------- /tasks/1493.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println(longestSubarray([]int{0, 1, 1, 1, 0, 1, 1, 0, 1})) 7 | } 8 | 9 | func longestSubarray(nums []int) int { 10 | var ret int 11 | start, end := 0, 0 12 | for prev := -1; start <= end && end < len(nums); end++ { 13 | if nums[end] != 1 { 14 | if prev == -1 { 15 | prev = end 16 | } else { 17 | ret = max(ret, end-start-1) 18 | start = prev + 1 19 | prev = end 20 | } 21 | } 22 | } 23 | ret = max(ret, end-start-1) 24 | return ret 25 | } 26 | 27 | func max(a, b int) int { 28 | if a > b { 29 | return a 30 | } 31 | return b 32 | } 33 | -------------------------------------------------------------------------------- /tasks/150.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | func main() { 9 | arr := []string{"4", "13", "5", "/", "+"} 10 | fmt.Print(evalRPN(arr)) 11 | // fmt.Print(13 / 5) 12 | } 13 | 14 | func evalRPN(tokens []string) int { 15 | st := Stack{} 16 | for _, token := range tokens { 17 | 18 | switch token { 19 | case "+": 20 | a, b := st.Pop(), st.Pop() 21 | st.Push(a + b) 22 | 23 | case "-": 24 | a, b := st.Pop(), st.Pop() 25 | st.Push(b - a) 26 | case "*": 27 | a, b := st.Pop(), st.Pop() 28 | st.Push(a * b) 29 | 30 | case "/": 31 | a, b := st.Pop(), st.Pop() 32 | st.Push(b / a) 33 | default: 34 | el, _ := strconv.Atoi(token) 35 | st.Push(el) 36 | 37 | } 38 | 39 | } 40 | return st.Pop() 41 | } 42 | 43 | type Stack struct { 44 | stack []int 45 | } 46 | 47 | // func NewStack() Stack { 48 | // return stack 49 | // } 50 | 51 | func (st *Stack) Pop() int { 52 | ret := st.stack[len(st.stack)-1] 53 | st.stack = st.stack[:len(st.stack)-1] 54 | return ret 55 | } 56 | 57 | func (st *Stack) Push(el int) { 58 | st.stack = append(st.stack, el) 59 | } 60 | -------------------------------------------------------------------------------- /tasks/19.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | type ListNode struct { 8 | Val int 9 | Next *ListNode 10 | } 11 | 12 | func removeNthFromEnd(head *ListNode, n int) *ListNode { 13 | ret := recursiveBackCounterDelete(head, n) 14 | if ret == -1 { 15 | return head.Next 16 | } 17 | return head 18 | } 19 | 20 | // 0 – found 21 | // -1 – delete next 22 | func recursiveBackCounterDelete(head *ListNode, n int) int { 23 | if head == nil { 24 | return 1 25 | } 26 | ret := recursiveBackCounterDelete(head.Next, n) 27 | if ret == 0 { 28 | return 0 29 | } 30 | if ret == -1 { 31 | head.Next = head.Next.Next 32 | return 0 33 | } 34 | if ret == n { 35 | return -1 36 | } 37 | return ret++ 38 | } 39 | 40 | -------------------------------------------------------------------------------- /tasks/2.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | t := 10 7 | fmt.Print(t / 3) 8 | } 9 | 10 | type ListNode struct { 11 | Val int 12 | Next *ListNode 13 | } 14 | 15 | func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { 16 | ret := &ListNode{ 17 | Val: 0, 18 | } 19 | left := 0 20 | retHead := ret 21 | for l1 != nil || l2 != nil { 22 | sum := left 23 | if l1 != nil { 24 | sum += l1.Val 25 | l1 = l1.Next 26 | } 27 | if l2 != nil { 28 | sum += l2.Val 29 | l2 = l2.Next 30 | } 31 | tmp := &ListNode{ 32 | Val: sum % 10, 33 | } 34 | ret.Next = tmp 35 | ret = ret.Next 36 | left = sum / 10 37 | } 38 | if left == 1 { 39 | ret.Next = &ListNode{ 40 | Val: 1, 41 | } 42 | } 43 | 44 | return retHead.Next 45 | } 46 | -------------------------------------------------------------------------------- /tasks/20.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // 40 '(' 41 ')' 7 | // 91 '[' 93 ']' 8 | // 123 '{' 125 '}' 9 | fmt.Print(isValid("([)]")) 10 | } 11 | 12 | func isValid(s string) bool { 13 | // openedRound := 0 14 | // openedSquare := 0 15 | // openedFigure := 0 16 | var opened []byte 17 | if s[0] != ')' && s[0] != ']' && s[0] != '}' { 18 | opened = append(opened, s[0]) 19 | } else { 20 | return false 21 | } 22 | 23 | for i := 1; i < len(s); i++ { 24 | switch s[i] { 25 | case ')': 26 | if len(opened) > 0 && abs(')'-opened[len(opened)-1]) < 3 { 27 | opened = opened[:len(opened)-1] 28 | } else { 29 | return false 30 | } 31 | 32 | case ']': 33 | if len(opened) > 0 && abs(']'-opened[len(opened)-1]) < 3 { 34 | opened = opened[:len(opened)-1] 35 | } else { 36 | return false 37 | } 38 | 39 | case '}': 40 | if len(opened) > 0 && abs('}'-opened[len(opened)-1]) < 3 { 41 | opened = opened[:len(opened)-1] 42 | } else { 43 | return false 44 | } 45 | default: 46 | opened = append(opened, s[i]) 47 | } 48 | 49 | } 50 | if len(opened) > 0 { 51 | return false 52 | } 53 | return true 54 | 55 | } 56 | 57 | func abs(a byte) byte { 58 | if a > 0 { 59 | return a 60 | } 61 | return -a 62 | } 63 | -------------------------------------------------------------------------------- /tasks/200.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | func numIslands(grid [][]byte) int { 8 | ret := 0 9 | for i := range grid { 10 | for j := range grid[0] { 11 | if grid[i][j] == '1' { 12 | dfs(i, j, &grid) 13 | ret++ 14 | } 15 | } 16 | } 17 | return ret 18 | } 19 | 20 | func dfs(i, j int, grid *[][]byte) { 21 | if i < 0 || j < 0 || i >= len((*grid)) || j >= len((*grid)[0]) || (*grid)[i][j] == byte('0') { 22 | return 23 | } 24 | 25 | (*grid)[i][j] = '0' 26 | dfs(i-1, j, grid) 27 | dfs(i+1, j, grid) 28 | dfs(i, j-1, grid) 29 | dfs(i, j+1, grid) 30 | } 31 | -------------------------------------------------------------------------------- /tasks/206.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type ListNode struct { 4 | Val int 5 | Next *ListNode 6 | } 7 | 8 | func reverseList(head *ListNode) *ListNode { 9 | current := head 10 | var prev, next *ListNode 11 | for current != nil { 12 | next = current.Next 13 | current.Next = prev 14 | prev = current 15 | current = next 16 | } 17 | return prev 18 | } 19 | -------------------------------------------------------------------------------- /tasks/21.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | // * Definition for singly-linked list. 8 | type ListNode struct { 9 | Val int 10 | Next *ListNode 11 | } 12 | 13 | func mergeTwoLists(fList *ListNode, sList *ListNode) *ListNode { 14 | var head *ListNode 15 | var current *ListNode 16 | for fList != nil || sList != nil { 17 | min, isFirst := minValNode(fList, sList) 18 | if isFirst { 19 | fList = fList.Next 20 | } else { 21 | sList = sList.Next 22 | } 23 | if head == nil { 24 | head = min 25 | current = head 26 | continue 27 | } 28 | current.Next = min 29 | current = current.Next 30 | 31 | } 32 | return head 33 | } 34 | 35 | func minValNode(fNode, sNode *ListNode) (*ListNode, bool) { 36 | if sNode == nil { 37 | return fNode, true 38 | } 39 | if fNode == nil { 40 | return sNode, false 41 | } 42 | if sNode.Val < fNode.Val { 43 | return sNode, false 44 | } 45 | return fNode, true 46 | } 47 | -------------------------------------------------------------------------------- /tasks/22.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Print(generateParenthesis(3)) 7 | } 8 | 9 | func generateParenthesis(n int) []string { 10 | var ret []string 11 | backTrack(&ret, "", n, n, n*2) 12 | return ret 13 | 14 | } 15 | 16 | func backTrack(output *[]string, current string, opened, closed, max int) { 17 | if len(current) == max { 18 | *output = append(*output, current) 19 | } 20 | if opened > 0 { 21 | backTrack(output, current+"(", opened-1, closed, max) 22 | 23 | } 24 | if closed > opened { 25 | backTrack(output, current+")", opened, closed-1, max) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tasks/228.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func summaryRanges(nums []int) []string { 6 | if len(nums) == 0 { 7 | return []string{} 8 | } 9 | var ret []string 10 | start := nums[0] 11 | end := nums[0] 12 | for i := 1; i < len(nums); i++ { 13 | if nums[i]-1 == nums[i-1] { 14 | end = nums[i] 15 | } else { 16 | if start != end { 17 | ret = append(ret, fmt.Sprintf("%d->%d", start, end)) 18 | } else { 19 | ret = append(ret, fmt.Sprint(start)) 20 | } 21 | start = nums[i] 22 | end = nums[i] 23 | } 24 | } 25 | if start != end { 26 | ret = append(ret, fmt.Sprintf("%d->%d", start, end)) 27 | } else { 28 | ret = append(ret, fmt.Sprint(start)) 29 | } 30 | return ret 31 | } 32 | -------------------------------------------------------------------------------- /tasks/23.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | } 8 | 9 | // * Definition for singly-linked list. 10 | type ListNode struct { 11 | Val int 12 | Next *ListNode 13 | } 14 | 15 | func mergeKLists(lists []*ListNode) *ListNode { 16 | 17 | step, lenLists := 1, len(lists) 18 | if lenLists == 0 { 19 | return nil 20 | } 21 | for step < lenLists { 22 | for i := 0; i+step < lenLists; i += 2 * step { 23 | lists[i] = mergeTwoLists(lists[i], lists[i+step]) 24 | } 25 | step *= 2 26 | } 27 | return lists[0] 28 | } 29 | 30 | func (l *ListNode) Print() { 31 | tmp := l 32 | for tmp != nil { 33 | fmt.Print(tmp.Val) 34 | tmp = tmp.Next 35 | } 36 | } 37 | 38 | func mergeTwoLists(fList *ListNode, sList *ListNode) *ListNode { 39 | // var min *ListNode 40 | var head *ListNode 41 | var current *ListNode 42 | for fList != nil || sList != nil { 43 | min, isFirst := minValNode(fList, sList) 44 | if isFirst { 45 | fList = fList.Next 46 | } else { 47 | sList = sList.Next 48 | } 49 | if head == nil { 50 | head = min 51 | current = head 52 | continue 53 | } 54 | current.Next = min 55 | current = current.Next 56 | 57 | } 58 | return head 59 | } 60 | 61 | func minValNode(fNode, sNode *ListNode) (*ListNode, bool) { 62 | if sNode == nil { 63 | return fNode, true 64 | } 65 | if fNode == nil { 66 | return sNode, false 67 | } 68 | if sNode.Val < fNode.Val { 69 | return sNode, false 70 | } 71 | return fNode, true 72 | } 73 | -------------------------------------------------------------------------------- /tasks/232.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | obj := Constructor() 7 | obj.Push(1) 8 | obj.Push(2) 9 | param_2 := obj.Pop() 10 | param_3 := obj.Peek() 11 | param_4 := obj.Empty() 12 | fmt.Println(param_2) 13 | fmt.Println(param_3) 14 | fmt.Println(param_4) 15 | } 16 | 17 | type MyQueue struct { 18 | stack []int 19 | } 20 | 21 | /** Initialize your data structure here. */ 22 | func Constructor() MyQueue { 23 | return MyQueue{} 24 | } 25 | 26 | /** Push element x to the back of queue. */ 27 | func (this *MyQueue) Push(x int) { 28 | this.stack = append(this.stack, x) 29 | } 30 | 31 | /** Removes the element from in front of queue and returns that element. */ 32 | func (this *MyQueue) Pop() int { 33 | ret := this.stack[0] 34 | this.stack = this.stack[1:] 35 | return ret 36 | } 37 | 38 | /** Get the front element. */ 39 | func (this *MyQueue) Peek() int { 40 | return this.stack[0] 41 | } 42 | 43 | /** Returns whether the queue is empty. */ 44 | func (this *MyQueue) Empty() bool { 45 | lenSt := len(this.stack) 46 | if lenSt == 0 { 47 | return true 48 | } 49 | return false 50 | } 51 | -------------------------------------------------------------------------------- /tasks/234.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | head := &ListNode{ 7 | Val: 1, 8 | Next: &ListNode{ 9 | Val: 2, 10 | Next: &ListNode{ 11 | Val: 2, 12 | Next: &ListNode{ 13 | Val: 1, 14 | Next: nil, 15 | }, 16 | }, 17 | }, 18 | } 19 | fmt.Print(isPalindrome(head)) 20 | } 21 | 22 | type ListNode struct { 23 | Val int 24 | Next *ListNode 25 | } 26 | 27 | func isPalindrome(head *ListNode) bool { 28 | if head == nil { 29 | return true 30 | } 31 | 32 | l := 1 33 | curr := head 34 | for curr.Next != nil { 35 | curr = curr.Next 36 | l++ 37 | } 38 | 39 | curr = head 40 | for i := 0; i < l/2; i++ { 41 | curr = curr.Next 42 | } 43 | var prev *ListNode 44 | for curr.Next != nil { 45 | curr, curr.Next, prev = curr.Next, prev, curr 46 | } 47 | curr.Next = prev 48 | 49 | for i := 0; i < l/2; i++ { 50 | if head.Val != curr.Val { 51 | return false 52 | } 53 | curr, head = curr.Next, head.Next 54 | } 55 | return true 56 | } 57 | -------------------------------------------------------------------------------- /tasks/238.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | func productExceptSelf(nums []int) []int { 8 | prefix := 1 9 | suffix := 1 10 | n := len(nums) 11 | arr := make([]int, n) 12 | for i := range arr { 13 | arr[i] = 1 14 | } 15 | for i := 1; i <= len(nums); i++ { 16 | arr[i-1] *= prefix 17 | arr[n-i] *= suffix 18 | prefix *= nums[i-1] 19 | suffix *= nums[n-i] 20 | } 21 | return arr 22 | } 23 | -------------------------------------------------------------------------------- /tasks/268.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | func missingNumber(nums []int) int { 8 | n := len(nums) 9 | sum := int(float32(1+n) / float32(2) * float32(n)) 10 | for _, el := range nums { 11 | sum -= el 12 | } 13 | return sum 14 | } 15 | -------------------------------------------------------------------------------- /tasks/279.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | ) 7 | 8 | func main() { 9 | fmt.Print(numSquares(4)) 10 | } 11 | 12 | func numSquares(n int) int { 13 | count := make([]int, n+1) 14 | count[0] = 0 15 | count[1] = 1 16 | for i := 2; i <= n; i++ { 17 | count[i] = -1 18 | for j := 1; j <= int(math.Sqrt(float64(i))); j++ { 19 | count[i] = minIntModified(count[i], count[i-j*j]+1) 20 | } 21 | } 22 | return count[n] 23 | } 24 | 25 | func minIntModified(a, b int) int { 26 | if a == -1 || b < a { 27 | return b 28 | } 29 | return a 30 | } 31 | -------------------------------------------------------------------------------- /tasks/28.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | func strStr(haystack string, needle string) int { 8 | if len(needle) == 0 { 9 | return 0 10 | } 11 | for ind := range haystack { 12 | if len(haystack) < ind+len(needle) { 13 | break 14 | } 15 | if haystack[ind:ind+len(needle)] == needle { 16 | return ind 17 | } 18 | } 19 | return -1 20 | } 21 | -------------------------------------------------------------------------------- /tasks/281.py: -------------------------------------------------------------------------------- 1 | # zigzag iterator but for undifined numbers of vecs 2 | 3 | class ZigzagIterator2: 4 | """ 5 | @param: vecs: a list of 1d vectors 6 | """ 7 | 8 | def __init__(self, vecs): 9 | # do intialization if necessary 10 | self.vecs = vecs 11 | self.queue = [] 12 | for i in range(len(vecs)): 13 | if len(vecs[i]) > 0: 14 | self.queue.append((i, 0)) 15 | """ 16 | @return: An integer 17 | """ 18 | 19 | def _next(self): 20 | row, col = self.queue.pop(0) 21 | 22 | if col + 1 < len(self.vecs[row]): 23 | self.queue.append((row, col+1)) 24 | return self.vecs[row][col] 25 | """ 26 | @return: True if has next 27 | """ 28 | 29 | def hasNext(self): 30 | return len(self.queue) != 0 31 | -------------------------------------------------------------------------------- /tasks/283.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | nums := []int{0, 0, 1} 7 | moveZeroes(nums) 8 | fmt.Print(nums) 9 | } 10 | 11 | func moveZeroes(nums []int) { 12 | count := 0 13 | i := 0 14 | for i < len(nums) { 15 | 16 | if nums[i] == 0 && i < len(nums)-1 { 17 | fmt.Println(nums) 18 | fmt.Println(i) 19 | nums = append(nums[:i], nums[i+1:]...) 20 | count++ 21 | continue 22 | } 23 | i++ 24 | 25 | } 26 | zeros := make([]int, count) 27 | nums = append(nums, zeros...) 28 | } 29 | -------------------------------------------------------------------------------- /tasks/3.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Print(lengthOfLongestSubstring("aab")) 9 | } 10 | 11 | func lengthOfLongestSubstring(s string) int { 12 | maxLen := 0 13 | mp := make([]int, 128) 14 | for i := range mp { 15 | mp[i] = -1 16 | } 17 | windowStart := -1 18 | 19 | for windowEnd, r := range s { 20 | if mp[r] > windowStart { 21 | windowStart = mp[r] 22 | } 23 | mp[r] = windowEnd 24 | if maxLen < windowEnd-windowStart { 25 | maxLen = windowEnd - windowStart 26 | } 27 | } 28 | return maxLen 29 | } 30 | 31 | // func lengthOfLongestSubstring(s string) int { 32 | // counter := 0 33 | // mp := make(map[rune]int) 34 | // windowStart := 0 35 | 36 | // for windowEnd, r := range s { 37 | // mp[r]++ 38 | // for mp[r] > 1 { 39 | // mp[rune(s[windowStart])]-- 40 | // if mp[rune(s[windowStart])] == 0 { 41 | // delete(mp, rune(s[windowStart])) 42 | // } 43 | // windowStart++ 44 | // } 45 | // counter = maxInt(counter, windowEnd-windowStart+1) 46 | // } 47 | // return counter 48 | // } 49 | 50 | // func maxInt(a, b int) int { 51 | // if a > b { 52 | // return a 53 | // } 54 | // return b 55 | // } 56 | -------------------------------------------------------------------------------- /tasks/33.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | func search(nums []int, target int) int { 8 | if nums[0] <= target { 9 | for i := 0; i <= len(nums)-1 && nums[i] <= target; i++ { 10 | if nums[i] == target { 11 | return i 12 | } 13 | } 14 | return -1 15 | } 16 | 17 | for i := len(nums) - 1; i >= 0 && nums[i] >= target; i-- { 18 | if nums[i] == target { 19 | return i 20 | } 21 | } 22 | return -1 23 | } 24 | -------------------------------------------------------------------------------- /tasks/332.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | ) 7 | 8 | func main() { 9 | tickets := [][]string{{"JFK", "SFO"}, {"JFK", "ATL"}, {"SFO", "ATL"}, {"ATL", "JFK"}, {"ATL", "SFO"}} 10 | fmt.Print(findItinerary(tickets)) 11 | } 12 | 13 | func findItinerary(tickets [][]string) []string { 14 | graph := newGraph(tickets) 15 | 16 | ret := []string{} 17 | 18 | graph.dfs(&ret, "JFK") 19 | 20 | reverse(&ret) 21 | 22 | return ret 23 | 24 | } 25 | 26 | type Queue []string 27 | 28 | type Graph map[string]*Queue 29 | 30 | func newGraph(tickets [][]string) *Graph { 31 | g := make(Graph) 32 | 33 | // edges init 34 | ensureArray := func(node string) { 35 | if _, ok := g[node]; !ok { 36 | q := &Queue{} 37 | g[node] = q 38 | } 39 | } 40 | 41 | // edges add 42 | for _, tiket := range tickets { 43 | from, to := tiket[0], tiket[1] 44 | ensureArray(from) 45 | ensureArray(to) 46 | *g[from] = append(*g[from], to) 47 | } 48 | 49 | // edges sort 50 | for _, node := range g { 51 | sort.Strings(*node) 52 | } 53 | 54 | return &g 55 | } 56 | 57 | func (g *Graph) dfs(itinerary *[]string, from string) { 58 | q := (*g)[from] 59 | 60 | for len(*q) > 0 { 61 | child := q.deque() 62 | g.dfs(itinerary, child) 63 | } 64 | *itinerary = append(*itinerary, from) 65 | } 66 | 67 | func (q *Queue) deque() string { 68 | ret := (*q)[0] 69 | *q = (*q)[1:] 70 | return ret 71 | } 72 | 73 | func reverse(ss *[]string) { 74 | n := len(*ss) 75 | for i := 0; i < n/2; i++ { 76 | (*ss)[i], (*ss)[n-1-i] = (*ss)[n-1-i], (*ss)[i] 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /tasks/34.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | nums := []int{1} 7 | fmt.Print(searchRange(nums, 0)) 8 | } 9 | 10 | func searchRange(nums []int, target int) []int { 11 | ind := len(nums) / 2 12 | if len(nums) == 0 { 13 | return []int{-1, -1} 14 | } 15 | if target < nums[ind] { 16 | for ; ind >= 0 && target != nums[ind]; ind-- { 17 | if target > nums[ind] { 18 | return []int{-1, -1} 19 | } 20 | } 21 | if ind < 0 { 22 | return []int{-1, -1} 23 | } 24 | } 25 | if target > nums[ind] { 26 | for ; ind < len(nums) && target != nums[ind]; ind++ { 27 | if target < nums[ind] { 28 | return []int{-1, -1} 29 | } 30 | } 31 | if ind >= len(nums) { 32 | return []int{-1, -1} 33 | } 34 | } 35 | return expandFromMiddle(&nums, target, ind) 36 | } 37 | 38 | func expandFromMiddle(nums *[]int, target, ind int) []int { 39 | right := ind 40 | left := ind 41 | for ; right < len(*nums); right++ { 42 | if (*nums)[right] != target { 43 | break 44 | } 45 | } 46 | 47 | for ; left >= 0; left-- { 48 | if (*nums)[left] != target { 49 | break 50 | } 51 | } 52 | left++ 53 | right-- 54 | return []int{left, right} 55 | 56 | } 57 | -------------------------------------------------------------------------------- /tasks/350.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | arr1 := []int{4, 9, 5} 9 | arr2 := []int{9, 4, 9, 8, 4} 10 | fmt.Print(intersect(arr1, arr2)) 11 | } 12 | 13 | func intersect(nums1 []int, nums2 []int) []int { 14 | var ret []int 15 | numMap := make(map[int]int) 16 | for _, el := range nums1 { 17 | numMap[el]++ 18 | } 19 | for _, el := range nums2 { 20 | if _, ok := numMap[el]; ok { 21 | numMap[el]-- 22 | if numMap[el] == 0 { 23 | delete(numMap, el) 24 | } 25 | ret = append(ret, el) 26 | } 27 | 28 | } 29 | 30 | return ret 31 | } 32 | -------------------------------------------------------------------------------- /tasks/356.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | func isReflected(points [][]int) bool { 8 | if len(points) < 2 { 9 | return true 10 | } 11 | var min, max int 12 | var mid float32 13 | var m map[[2]int]bool 14 | m = make(map[[2]int]bool) 15 | min = points[0][0] 16 | max = min 17 | for i := 0; i < len(points); i++ { 18 | if points[i][0] < min { 19 | min = points[i][0] 20 | } else if points[i][0] > max { 21 | max = points[i][0] 22 | } 23 | m[[2]int{points[i][0], points[i][1]}] = true 24 | } 25 | mid = float32(min+max) / 2.0 26 | for el := 0; el < len(points); el++ { 27 | refX := int(2*mid) - points[el][0] 28 | if _, ok := m[[2]int{refX, points[el][1]}]; !ok { 29 | return false 30 | } 31 | } 32 | return true 33 | } 34 | -------------------------------------------------------------------------------- /tasks/380.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "math/rand" 4 | 5 | func main() { 6 | 7 | } 8 | 9 | type valEl struct { 10 | val int 11 | ind int 12 | } 13 | 14 | type RandomizedSet struct { 15 | vals []int 16 | valsSet map[int]valEl 17 | } 18 | 19 | /** Initialize your data structure here. */ 20 | func Constructor() RandomizedSet { 21 | return RandomizedSet{ 22 | valsSet: make(map[int]valEl), 23 | } 24 | 25 | } 26 | 27 | /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ 28 | func (this *RandomizedSet) Insert(val int) bool { 29 | if _, ok := this.valsSet[val]; !ok { 30 | this.valsSet[val] = valEl{val: val, ind: len(this.vals)} 31 | this.vals = append(this.vals, val) 32 | return true 33 | } 34 | return false 35 | } 36 | 37 | /** Removes a value from the set. Returns true if the set contained the specified element. */ 38 | func (this *RandomizedSet) Remove(val int) bool { 39 | if setIt, ok := this.valsSet[val]; ok { 40 | last := this.vals[len(this.vals)-1] 41 | this.vals[setIt.ind] = last 42 | this.vals = this.vals[:len(this.vals)-1] 43 | this.valsSet[last] = valEl{last, setIt.ind} 44 | delete(this.valsSet, val) 45 | return true 46 | } 47 | return false 48 | } 49 | 50 | /** Get a random element from the set. */ 51 | func (this *RandomizedSet) GetRandom() int { 52 | return this.vals[rand.Intn(len(this.vals))] 53 | } 54 | 55 | /** 56 | * Your RandomizedSet object will be instantiated and called as such: 57 | * obj := Constructor(); 58 | * param_1 := obj.Insert(val); 59 | * param_2 := obj.Remove(val); 60 | * param_3 := obj.GetRandom(); 61 | */ 62 | -------------------------------------------------------------------------------- /tasks/392.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println(isSubsequence("abc", "ahbgdc")) 7 | } 8 | 9 | func isSubsequence(s string, t string) bool { 10 | left := 0 11 | for i := 0; i < len(t) && left < len(s); i++ { 12 | if t[i] == s[left] { 13 | left++ 14 | } 15 | } 16 | return left == len(s) 17 | } 18 | -------------------------------------------------------------------------------- /tasks/4.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | ) 7 | 8 | func main() { 9 | arr1 := []int{1, 3} 10 | arr_2 := []int{2} 11 | fmt.Println(findMedianSortedArrays(arr1, arr_2)) 12 | } 13 | 14 | func findMedianSortedArrays(nums1 []int, nums2 []int) float64 { 15 | nums1 = append(nums1, nums2...) 16 | sort.Ints(nums1) 17 | medInd := len(nums1) / 2 18 | if len(nums1) == medInd*2 { 19 | return float64(nums1[medInd]+nums1[medInd-1]) / 2 20 | } else { 21 | return float64(nums1[medInd]) 22 | } 23 | } 24 | 25 | // func findMedianSortedArrays(nums1 []int, nums2 []int) float64 { 26 | // merged := []int{} 27 | // left1 := 0 28 | // left2 := 0 29 | // for left1 < len(nums1) { 30 | // if left2 < len(nums2) { 31 | // if nums1[left1] < nums2[left2] { 32 | // merged = append(merged, nums1[left1]) 33 | // left1++ 34 | // } else { 35 | // merged = append(merged, nums2[left2]) 36 | // left2++ 37 | // } 38 | // } else { 39 | // break 40 | // } 41 | // } 42 | // if left2 < len(nums2) { 43 | // merged = append(merged, nums2[left2:]...) 44 | // } 45 | // if left1 < len(nums1) { 46 | // merged = append(merged, nums1[left1:]...) 47 | // } 48 | 49 | // medInd := len(merged) / 2 50 | // if len(merged) == medInd*2 { 51 | // return float64(merged[medInd]+merged[medInd-1]) / 2 52 | // } else { 53 | // return float64(merged[medInd]) 54 | // } 55 | // } 56 | -------------------------------------------------------------------------------- /tasks/42.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Print(trap([]int{4, 2, 0, 3, 2, 5})) 7 | } 8 | 9 | func trap(height []int) int { 10 | left := 0 11 | right := len(height) - 1 12 | 13 | ret := 0 14 | 15 | leftMax := 0 16 | rightMax := 0 17 | for left < right { 18 | if height[left] < height[right] { 19 | if height[left] > leftMax { 20 | leftMax = height[left] 21 | } else { 22 | ret += leftMax - height[left] 23 | } 24 | left++ 25 | } else { 26 | if height[right] > rightMax { 27 | rightMax = height[right] 28 | } else { 29 | ret += rightMax - height[right] 30 | } 31 | right-- 32 | } 33 | } 34 | return ret 35 | } 36 | -------------------------------------------------------------------------------- /tasks/443.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func compress(chars []byte) int { 6 | if len(chars) == 0 { 7 | return 0 8 | } 9 | start := 0 10 | end := 0 11 | pivot := 0 12 | for i := 1; i < len(chars); i++ { 13 | if chars[i] != chars[i-1] { 14 | chars[pivot] = chars[start] 15 | pivot++ 16 | if start != end { 17 | for _, x := range fmt.Sprint(end - start + 1) { 18 | chars[pivot] = byte(x) 19 | pivot++ 20 | } 21 | } 22 | start = i 23 | } 24 | end = i 25 | } 26 | chars[pivot] = chars[start] 27 | pivot++ 28 | if start != end { 29 | for _, x := range fmt.Sprint(end - start + 1) { 30 | chars[pivot] = byte(x) 31 | pivot++ 32 | } 33 | } 34 | start = i 35 | return pivot 36 | } 37 | -------------------------------------------------------------------------------- /tasks/449.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strconv" 5 | "strings" 6 | ) 7 | 8 | func main() { 9 | } 10 | 11 | type TreeNode struct { 12 | Val int 13 | Left *TreeNode 14 | Right *TreeNode 15 | } 16 | 17 | type Codec struct { 18 | } 19 | 20 | func Constructor() Codec { 21 | return Codec{} 22 | } 23 | 24 | // Serializes a tree to a single string. 25 | func (this *Codec) serialize(root *TreeNode) string { 26 | var ret []string 27 | var dfs func(head *TreeNode) 28 | dfs = func(head *TreeNode) { 29 | if head == nil { 30 | ret = append(ret, "x") 31 | return 32 | } 33 | ret = append(ret, strconv.Itoa(head.Val)) 34 | dfs(head.Left) 35 | dfs(head.Right) 36 | } 37 | dfs(root) 38 | 39 | return strings.Join(ret, ",") 40 | } 41 | 42 | // Deserializes your encoded data to tree. 43 | func (this *Codec) deserialize(data string) *TreeNode { 44 | index := -1 45 | arr := strings.Split(data, ",") 46 | var dfs func() *TreeNode 47 | dfs = func() *TreeNode { 48 | index++ 49 | if index >= len(data) || arr[index] == "x" { 50 | 51 | return nil 52 | } 53 | val, _ := strconv.Atoi(arr[index]) 54 | 55 | head := &TreeNode{Val: val} 56 | head.Left = dfs() 57 | head.Right = dfs() 58 | return head 59 | } 60 | return dfs() 61 | } 62 | -------------------------------------------------------------------------------- /tasks/48.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | matrix := [][]int{{5, 1, 9, 11}, {2, 4, 8, 10}, {13, 3, 6, 7}, {15, 14, 12, 16}} 7 | for _, el := range matrix { 8 | fmt.Println(el) 9 | } 10 | fmt.Println("") 11 | rotate(matrix) 12 | 13 | for _, el := range matrix { 14 | fmt.Println(el) 15 | } 16 | fmt.Println("") 17 | 18 | } 19 | 20 | func rotate(matrix [][]int) { 21 | n := len(matrix) 22 | for i := 0; i < len(matrix)/2; i++ { 23 | for j := i; j < len(matrix)-i-1; j++ { 24 | matrix[j][n-i-1], matrix[n-i-1][n-j-1], matrix[n-j-1][i], matrix[i][j] = matrix[i][j], matrix[j][n-i-1], matrix[n-i-1][n-j-1], matrix[n-j-1][i] 25 | 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tasks/49.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | ) 7 | 8 | func main() { 9 | fmt.Print(groupAnagrams([]string{"eat", "tea", "tan", "ate", "nat", "bat"})) 10 | } 11 | 12 | func groupAnagrams(strs []string) [][]string { 13 | sortedHash := make(map[string][]string) 14 | var ret [][]string 15 | for i := 0; i < len(strs); i++ { 16 | sortedWord := sortString(strs[i]) 17 | sortedHash[sortedWord] = append(sortedHash[sortedWord], strs[i]) 18 | } 19 | for el := range sortedHash { 20 | ret = append(ret, sortedHash[el]) 21 | } 22 | return ret 23 | } 24 | 25 | type sortRunes []rune 26 | 27 | func (s sortRunes) Less(i, j int) bool { 28 | return s[i] < s[j] 29 | } 30 | 31 | func (s sortRunes) Swap(i, j int) { 32 | s[i], s[j] = s[j], s[i] 33 | } 34 | 35 | func (s sortRunes) Len() int { 36 | return len(s) 37 | } 38 | 39 | func sortString(s string) string { 40 | r := []rune(s) 41 | sort.Sort(sortRunes(r)) 42 | return string(r) 43 | } 44 | -------------------------------------------------------------------------------- /tasks/5.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | func longestPalindrome(s string) string { 8 | if len(s) < 1 { 9 | return "" 10 | } 11 | start, end := 0, 0 12 | for i := range s { 13 | len1 := expandAroundCenter(s, i, i) 14 | len2 := expandAroundCenter(s, i, i+1) 15 | lenMax := 0 16 | if len1 > len2 { 17 | lenMax = len1 18 | } else { 19 | lenMax = len2 20 | } 21 | 22 | if lenMax > end-start { 23 | start = i - (lenMax-1)/2 24 | end = i + (lenMax)/2 25 | } 26 | 27 | } 28 | return s[start : end+1] 29 | } 30 | 31 | func expandAroundCenter(s string, L, R int) int { 32 | for L >= 0 && R < len(s) && s[L] == s[R] { 33 | L-- 34 | R++ 35 | } 36 | return R - L - 1 37 | } 38 | -------------------------------------------------------------------------------- /tasks/56.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | 5 | } 6 | 7 | func merge(intervals [][]int) [][]int { 8 | mpStart := make(map[int]int) 9 | mpEnd := make(map[int]int) 10 | max := 0 11 | for _, interval := range intervals { 12 | mpStart[interval[0]]++ 13 | mpEnd[interval[1]]++ 14 | max = maxInt(max, interval[1]) 15 | } 16 | var ret [][]int 17 | for i := 0; i <= max; i++ { 18 | if mpStart[i] > 0 { 19 | sum := 0 20 | el := make([]int, 2) 21 | el[0] = i 22 | for { 23 | sum += mpStart[i] 24 | sum -= mpEnd[i] 25 | if sum == 0 { 26 | break 27 | } 28 | i++ 29 | } 30 | el[1] = i 31 | ret = append(ret, el) 32 | } 33 | } 34 | return ret 35 | } 36 | 37 | func maxInt(a, b int) int { 38 | if a > b { 39 | return a 40 | } 41 | return b 42 | } 43 | -------------------------------------------------------------------------------- /tasks/560.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println(subarraySum([]int{1, 1, 1}, 2)) 7 | } 8 | 9 | func subarraySum(nums []int, k int) int { 10 | sumFreq := map[int]int{0: 1} 11 | sum := 0 12 | count := 0 13 | for _, v := range nums { 14 | sum += v 15 | count += sumFreq[sum-k] 16 | sumFreq[sum]++ 17 | } 18 | return count 19 | } 20 | -------------------------------------------------------------------------------- /tasks/567.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | str1 := "adc" 9 | str2 := "sadadasdasdsadasdasdasdadacdadasadadadasd" 10 | fmt.Print(checkInclusion(str1, str2)) 11 | } 12 | 13 | func checkInclusion(s1 string, s2 string) bool { 14 | var mp1, mp2 [26]byte 15 | s2Len := len(s2) 16 | s1Len := len(s1) 17 | if s2Len < s1Len { 18 | return false 19 | } 20 | 21 | lPtr := 0 22 | rPtr := 0 23 | for i := range s1 { 24 | mp1[s1[i]-'a']++ 25 | mp2[s2[i]-'a']++ 26 | rPtr++ 27 | } 28 | for rPtr != s2Len { 29 | if equal(mp1, mp2) { 30 | return true 31 | } 32 | mp2[s2[lPtr]-'a']-- 33 | lPtr++ 34 | mp2[s2[rPtr]-'a']++ 35 | rPtr++ 36 | } 37 | return equal(mp1, mp2) 38 | } 39 | 40 | func equal(mp1, mp2 [26]byte) bool { 41 | for i := range mp1 { 42 | if mp1[i] != mp2[i] { 43 | return false 44 | } 45 | } 46 | return true 47 | } 48 | 49 | // func checkInclusion(s1 string, s2 string) bool { 50 | // s2Len := len(s2) 51 | // s1Len := len(s1) 52 | // if s2Len < s1Len { 53 | // return false 54 | // } 55 | // s1Sorted := sortString(s1) 56 | // for i := 0; i+s1Len-1 < s2Len; i++ { 57 | // s2SortedSlice := sortString(s2[i : i+s1Len]) 58 | // if s1Sorted == s2SortedSlice { 59 | // return true 60 | // } 61 | // } 62 | // return false 63 | // } 64 | 65 | // type sortRunes []rune 66 | 67 | // func (sr sortRunes) Less(i, j int) bool { 68 | // return sr[i] < sr[j] 69 | // } 70 | 71 | // func (sr sortRunes) Swap(i, j int) { 72 | // sr[i], sr[j] = sr[j], sr[i] 73 | // } 74 | 75 | // func (sr sortRunes) Len() int { 76 | // return len(sr) 77 | // } 78 | 79 | // func sortString(s string) string { 80 | // rs := []rune(s) 81 | // sort.Sort(sortRunes(rs)) 82 | // return string(rs) 83 | // } 84 | -------------------------------------------------------------------------------- /tasks/59.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Print(generateMatrix(3)) 7 | } 8 | 9 | func generateMatrix(n int) [][]int { 10 | matrix := make([][]int, n) 11 | for i := range matrix { 12 | matrix[i] = make([]int, n) 13 | } 14 | x := 0 15 | y := 0 16 | counter := 1 17 | for counter != n*n+1 { 18 | for x < n { 19 | if matrix[y][x] != 0 { 20 | break 21 | } 22 | matrix[y][x] = counter 23 | counter++ 24 | x++ 25 | } 26 | x-- 27 | y++ 28 | for y < n { 29 | if matrix[y][x] != 0 { 30 | break 31 | } 32 | matrix[y][x] = counter 33 | counter++ 34 | y++ 35 | } 36 | y-- 37 | x-- 38 | for x >= 0 { 39 | if matrix[y][x] != 0 { 40 | break 41 | } 42 | matrix[y][x] = counter 43 | counter++ 44 | x-- 45 | } 46 | x++ 47 | y-- 48 | for y >= 0 { 49 | if matrix[y][x] != 0 { 50 | break 51 | } 52 | matrix[y][x] = counter 53 | counter++ 54 | y-- 55 | } 56 | y++ 57 | x++ 58 | } 59 | return matrix 60 | } 61 | -------------------------------------------------------------------------------- /tasks/6.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println(convert("PAYPALISHIRING", 3)) 7 | } 8 | 9 | func convert(s string, numRows int) string { 10 | 11 | if len(s) <= numRows || numRows == 1 { 12 | return s 13 | } 14 | // min(diag) == 2 15 | // diag == numRows - 2 16 | maxStep := numRows + numRows - 2 17 | ret := make([]byte, len(s)) 18 | 19 | currInd := 0 20 | currRow := 0 21 | for currRow < numRows { 22 | 23 | // first and last are symmetrical 24 | if currRow == 0 || currRow == numRows-1 { 25 | for i := currRow; i < len(s); i += maxStep { 26 | ret[currInd] = s[i] 27 | currInd++ 28 | } 29 | } else { 30 | for i := currRow; i < len(s); i += maxStep { 31 | ret[currInd] = s[i] 32 | currInd++ 33 | pairedElInd := i + maxStep - 2*currRow 34 | if pairedElInd < len(s) { 35 | ret[currInd] = s[pairedElInd] 36 | currInd++ 37 | } 38 | } 39 | } 40 | currRow++ 41 | } 42 | return string(ret) 43 | } 44 | -------------------------------------------------------------------------------- /tasks/658.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | arr := []int{1, 2, 3, 4} 9 | fmt.Println(findClosestElements(arr, 2, 2)) 10 | } 11 | 12 | func findClosestElements(arr []int, k int, x int) []int { 13 | left, right := 0, len(arr)-k 14 | for left < right { 15 | mid := (left + right) / 2 16 | if x-arr[mid] > arr[mid+k]-x { 17 | left = mid + 1 18 | } else { 19 | right = mid 20 | } 21 | } 22 | return arr[left : left+k] 23 | } 24 | -------------------------------------------------------------------------------- /tasks/7.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | func main() { 9 | fmt.Println(reverse(-123)) 10 | } 11 | 12 | func reverse(x int) int { 13 | 14 | strX := fmt.Sprintf("%d", x) 15 | // last zero 16 | if strX[len(strX)-1] == '0' { 17 | strX = strX[:len(strX)-1] 18 | } 19 | 20 | lenAfter := len(strX) 21 | if lenAfter > 0 { 22 | sign := 1 23 | // sign 24 | if strX[0] == '-' { 25 | sign = -1 26 | lenAfter-- 27 | } 28 | 29 | ret := make([]byte, lenAfter) 30 | for i := 0; i < len(ret); i++ { 31 | ret[i] = strX[len(strX)-1-i] 32 | } 33 | val, _ := strconv.Atoi(string(ret)) 34 | if val > 1<<31-1 { 35 | val = 0 36 | } 37 | return val * sign 38 | } 39 | 40 | return 0 41 | } 42 | -------------------------------------------------------------------------------- /tasks/71.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | str := "/a//b////c/d//././/.." 9 | fmt.Print(simplifyPath(str)) 10 | } 11 | 12 | func simplifyPath(path string) string { 13 | start := 0 14 | var ret []string 15 | for start < len(path) { 16 | for ; start < len(path) && path[start] == '/'; start++ { 17 | } 18 | end := start 19 | for ; end < len(path) && path[end] != '/'; end++ { 20 | } 21 | s := path[start:end] 22 | if s == ".." { 23 | if len(ret) > 0 { 24 | ret = ret[:len(ret)-1] 25 | } 26 | } else { 27 | if s != "." && s != "" { 28 | ret = append(ret, s) 29 | } 30 | } 31 | start = end + 1 32 | } 33 | 34 | if len(ret) == 0 { 35 | return "/" 36 | } 37 | answ := "" 38 | for _, el := range ret { 39 | answ += "/" + el 40 | } 41 | return answ 42 | } 43 | 44 | // func putInRet(path, str &string, start, end int) { 45 | // path + 46 | // } 47 | 48 | // /home//. 49 | -------------------------------------------------------------------------------- /tasks/8.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | "strings" 7 | "unicode" 8 | ) 9 | 10 | func main() { 11 | fmt.Println(myAtoi((" "))) 12 | } 13 | 14 | func myAtoi(s string) int { 15 | // s should begin with number or sign 16 | 17 | // 1. Trim spaces 18 | sFormatted := strings.TrimLeft(s, " ") 19 | 20 | sign := 1 21 | startingInd := -1 22 | endingInd := -1 23 | signed := false 24 | // 2. Search and read sign 25 | for i, c := range sFormatted { 26 | if startingInd == -1 { 27 | if (c == rune('+') || c == rune('-')) && signed { 28 | return 0 29 | } 30 | if c == rune('+') { 31 | signed = true 32 | } else if c == rune('-') { 33 | sign = -1 34 | signed = true 35 | } else if unicode.IsDigit(c) { 36 | startingInd = i 37 | } 38 | } else { 39 | if !unicode.IsDigit(c) { 40 | endingInd = i 41 | break 42 | } 43 | } 44 | } 45 | if startingInd == -1 { 46 | return 0 47 | } 48 | if !signed && startingInd > 0 { 49 | return 0 50 | } 51 | if endingInd == -1 && startingInd != -1 { 52 | endingInd = len(sFormatted) 53 | } 54 | 55 | retNum, _ := strconv.Atoi(sFormatted[startingInd:endingInd]) 56 | if sign == -1 && retNum > 2<<31 { 57 | return 2 << 31 * -1 58 | } else if sign == 1 && retNum > 2<<31-1 { 59 | return 2<<31 - 1 60 | } else { 61 | return retNum * sign 62 | } 63 | } 64 | 65 | // FASTER REALISATION 66 | // func myAtoi(s string) int { 67 | // for len(s) > 0 && s[0] == ' ' { 68 | // s = s[1:] 69 | // } 70 | // if len(s) == 0 { 71 | // return 0 72 | // } 73 | // neg := false 74 | // switch s[0] { 75 | // case '-': 76 | // neg = true 77 | // fallthrough 78 | // case '+': 79 | // s = s[1:] 80 | // } 81 | // var val int64 82 | // for len(s) > 0 { 83 | // d := s[0] 84 | // if d < '0' || d > '9' { 85 | // break 86 | // } 87 | // val *= 10 88 | // val += int64(d-'0') 89 | // if val >= 1<<31 { 90 | // break 91 | // } 92 | // s = s[1:] 93 | // } 94 | // const ( 95 | // max = 1<<31-1 96 | // min = -1<<31 97 | // ) 98 | // if neg { 99 | // if val = -val; val < min { 100 | // val = min 101 | // } 102 | // } else if val > max { 103 | // val = max 104 | // } 105 | // return int(val) 106 | // } 107 | -------------------------------------------------------------------------------- /tasks/849.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | arr := []int{1, 0, 0, 0, 1, 0, 1} 7 | fmt.Println(maxDistToClosest(arr)) 8 | } 9 | 10 | func maxDistToClosest(seats []int) int { 11 | max := 0 12 | lastInd := len(seats) - 1 13 | prev := -1 14 | for i := range seats { 15 | if seats[i] == 1 { 16 | if prev == -1 { 17 | max = maxInt(max, i) 18 | } else { 19 | max = maxInt(max, (i-prev)/2) 20 | } 21 | prev = i 22 | } 23 | } 24 | 25 | if prev != lastInd { 26 | max = maxInt(max, lastInd-prev) 27 | } 28 | return max 29 | } 30 | 31 | func maxInt(a, b int) int { 32 | if a > b { 33 | return a 34 | } 35 | return b 36 | } 37 | -------------------------------------------------------------------------------- /tasks/85.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // matrix := [][]byte{ 7 | // {'1', '1', '1', '1', '1', '1', '1', '1'}, 8 | // {'1', '1', '1', '1', '1', '1', '1', '0'}, 9 | // {'1', '1', '1', '1', '1', '1', '1', '0'}, 10 | // {'1', '1', '1', '1', '1', '0', '0', '0'}, 11 | // {'0', '1', '1', '1', '1', '0', '0', '0'}} 12 | 13 | // matrix2 := [][]byte{ 14 | // {'1', '0', '0', '0', '1'}, 15 | // {'1', '1', '0', '1', '1'}, 16 | // {'1', '1', '1', '1', '1'}, 17 | // } 18 | 19 | matr3 := [][]byte{ 20 | {'1', '0', '1', '1', '1'}, 21 | {'0', '1', '0', '1', '0'}, 22 | {'1', '1', '0', '1', '1'}, 23 | {'1', '1', '0', '1', '1'}, 24 | {'0', '1', '1', '1', '1'}, 25 | } 26 | fmt.Print(maximalRectangle(matr3)) 27 | } 28 | 29 | func maximalRectangle(matrix [][]byte) int { 30 | area := 0 31 | for i := 0; i < len(matrix); i++ { 32 | for j := 0; j < len(matrix[0]); j++ { 33 | if matrix[i][j] == '1' { 34 | newArea := getArea(matrix, i, j) 35 | if area < newArea { 36 | area = newArea 37 | } 38 | } 39 | } 40 | } 41 | return area 42 | } 43 | 44 | func getArea(matrix [][]byte, x, y int) int { 45 | maxArea := 0 46 | minWidth := 0 47 | width := 0 48 | length := 0 49 | for i := x; i < len(matrix); i++ { 50 | if matrix[i][y] == '0' { 51 | break 52 | } 53 | for j := y; j < len(matrix[0]); j++ { 54 | if minWidth != 0 && j-y >= minWidth || matrix[i][j] == '0' { 55 | break 56 | } 57 | 58 | width++ 59 | } 60 | 61 | length++ 62 | if minWidth == 0 || width < minWidth { 63 | minWidth = width 64 | } 65 | if width*length > maxArea { 66 | maxArea = width * length 67 | } 68 | width = 0 69 | } 70 | return maxArea 71 | } 72 | -------------------------------------------------------------------------------- /tasks/88.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | nums1 := []int{1, 2, 3, 0, 0, 0} 7 | merge(nums1, 3, []int{4, 5, 6}, 3) 8 | fmt.Print(nums1) 9 | } 10 | 11 | func merge(nums1 []int, m int, nums2 []int, n int) { 12 | var i, j = m - 1, n - 1 13 | for l := len(nums1) - 1; l >= 0; l-- { 14 | if j < 0 || i >= 0 && nums1[i] >= nums2[j] { 15 | nums1[l] = nums1[i] 16 | i-- 17 | } else { 18 | nums1[l] = nums2[j] 19 | j-- 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tasks/9.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | } 8 | 9 | func isPalindrome(x int) bool { 10 | sX := fmt.Sprintf("%d", x) 11 | for i := 0; i < len(sX)/2; i++ { 12 | if sX[i] != sX[len(sX)-1-i] { 13 | return false 14 | } 15 | } 16 | return true 17 | } 18 | -------------------------------------------------------------------------------- /tasks/933.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | obj := Constructor() 7 | param_1 := obj.Ping(1) 8 | fmt.Print(param_1) 9 | param_2 := obj.Ping(100) 10 | fmt.Print(param_2) 11 | param_3 := obj.Ping(3001) 12 | fmt.Print(param_3) 13 | param_4 := obj.Ping(3002) 14 | fmt.Print(param_4) 15 | 16 | } 17 | 18 | type RecentCounter struct { 19 | pings []int 20 | } 21 | 22 | func Constructor() RecentCounter { 23 | return RecentCounter{} 24 | } 25 | 26 | func (this *RecentCounter) Ping(t int) int { 27 | this.pings = append(this.pings, t) 28 | lowest := 0 29 | for this.pings[lowest] < t-3000 { 30 | lowest++ 31 | } 32 | this.pings = this.pings[lowest:] 33 | return len(this.pings) 34 | } 35 | -------------------------------------------------------------------------------- /tasks/974.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | arr := []int{-1, 2, 9} 7 | fmt.Println(subarraysDivByK(arr, 2)) 8 | } 9 | 10 | func subarraysDivByK(nums []int, k int) int { 11 | ret, rem, sum := 0, 0, 0 12 | reminders := make([]uint16, k) 13 | reminders[0] = 1 14 | for _, x := range nums { 15 | sum += x 16 | 17 | rem = sum % k 18 | if rem < 0 { 19 | rem += k 20 | } 21 | ret += int(reminders[rem]) 22 | reminders[rem]++ 23 | } 24 | return ret 25 | } 26 | -------------------------------------------------------------------------------- /tasks/977.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | nums := []int{-4, -1, 0, 3, 10} 7 | fmt.Print(sortedSquares(nums)) 8 | } 9 | 10 | func sortedSquares(nums []int) []int { 11 | l := len(nums) 12 | ret := make([]int, l) 13 | i, j, k := 0, l-1, l-1 14 | for i <= j { 15 | if modInt(nums[i]) < modInt(nums[j]) { 16 | ret[k] = nums[j] * nums[j] 17 | j-- 18 | } else { 19 | ret[k] = nums[i] * nums[i] 20 | i++ 21 | } 22 | k-- 23 | } 24 | return ret 25 | } 26 | 27 | func modInt(a int) int { 28 | if a < 0 { 29 | return -a 30 | } 31 | return a 32 | } 33 | -------------------------------------------------------------------------------- /tasks/98.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | treeHead := &TreeNode{ 7 | Val: 5, 8 | Left: &TreeNode{ 9 | Val: 4, 10 | }, 11 | Right: &TreeNode{ 12 | Val: 6, 13 | Left: &TreeNode{ 14 | Val: 3, 15 | }, 16 | Right: &TreeNode{ 17 | Val: 7, 18 | }, 19 | }, 20 | } 21 | fmt.Print(isValidBST(treeHead)) 22 | } 23 | 24 | type TreeNode struct { 25 | Val int 26 | Left *TreeNode 27 | Right *TreeNode 28 | } 29 | 30 | func isValidBST(root *TreeNode) bool { 31 | var min *TreeNode 32 | var max *TreeNode 33 | return dfs(root, min, max) 34 | } 35 | 36 | func dfs(root, min, max *TreeNode) bool { 37 | if root == nil { 38 | return true 39 | } 40 | 41 | if min != nil && root.Val <= min.Val { 42 | return false 43 | } 44 | 45 | if max != nil && root.Val >= max.Val { 46 | return false 47 | } 48 | 49 | return dfs(root.Left, min, root) && dfs(root.Right, root, max) 50 | } 51 | --------------------------------------------------------------------------------