From 9b42b0ad9093772f7465a4eed1c4784e6fa3d617 Mon Sep 17 00:00:00 2001 From: bovem Date: Tue, 7 Nov 2023 21:32:24 +0530 Subject: [PATCH 1/2] Updated RSS Feed link --- content/posts/dsa/arrays-strings-hashmaps/index.md | 2 +- content/posts/dsa/rabin-karp-substring-search/index.md | 2 +- content/posts/dsa/time-complexity/index.md | 2 +- content/posts/go/concurrency-in-go/index.md | 2 +- content/posts/go/file-handling-in-go/index.md | 2 +- content/posts/go/go-programming-language/index.md | 2 +- content/posts/go/rest-api-requests-in-go/index.md | 2 +- content/posts/homelab/building-your-own-homelab/index.md | 2 +- content/posts/kubernetes/building-container-images/index.md | 2 +- content/posts/kubernetes/container-architecture/index.md | 2 +- content/posts/kubernetes/container-images/index.md | 2 +- content/posts/kubernetes/container-lifecycle/index.md | 2 +- content/posts/kubernetes/container-network-interfaces/index.md | 2 +- content/posts/kubernetes/container-storage-interfaces/index.md | 2 +- content/posts/kubernetes/containers/index.md | 2 +- content/posts/kubernetes/helm-charts/index.md | 2 +- content/posts/kubernetes/kubernetes-operators/index.md | 2 +- content/posts/kubernetes/network-functions/index.md | 2 +- content/posts/kubernetes/operator-sdk/index.md | 2 +- content/posts/kubernetes/operators-on-openshift/index.md | 2 +- 20 files changed, 20 insertions(+), 20 deletions(-) diff --git a/content/posts/dsa/arrays-strings-hashmaps/index.md b/content/posts/dsa/arrays-strings-hashmaps/index.md index dbc1a05..180b6b9 100644 --- a/content/posts/dsa/arrays-strings-hashmaps/index.md +++ b/content/posts/dsa/arrays-strings-hashmaps/index.md @@ -393,7 +393,7 @@ func main(){ ```
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Hash Table Explained: What it Is and How to Implement It diff --git a/content/posts/dsa/rabin-karp-substring-search/index.md b/content/posts/dsa/rabin-karp-substring-search/index.md index cd44230..47e5c94 100644 --- a/content/posts/dsa/rabin-karp-substring-search/index.md +++ b/content/posts/dsa/rabin-karp-substring-search/index.md @@ -409,7 +409,7 @@ func main(){ ```
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Efficient randomized pattern-matching algorithms diff --git a/content/posts/dsa/time-complexity/index.md b/content/posts/dsa/time-complexity/index.md index 3beb782..b563865 100644 --- a/content/posts/dsa/time-complexity/index.md +++ b/content/posts/dsa/time-complexity/index.md @@ -325,7 +325,7 @@ Assuming the output of the time complexity function is the algorithm's execution With the same computation power, an algorithm with $O(2^n)$ time complexity will sort 100 elements in $3.99 \times 10^{22}$ years while an algorithm with $O(n \log_2{n})$ time complexity will take only 664.4 seconds .
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Big Theta and Asymptotic Notation Explained diff --git a/content/posts/go/concurrency-in-go/index.md b/content/posts/go/concurrency-in-go/index.md index 0430dd2..2ecba3b 100644 --- a/content/posts/go/concurrency-in-go/index.md +++ b/content/posts/go/concurrency-in-go/index.md @@ -511,7 +511,7 @@ func main(){ `WaitGroup` starts a counter at `0` and a delta (`int` value) is added to it using the `Add()` method. The `Done()` method decrements the counter by `1` and the `Wait()` method waits for the counter to reach `0` and moves to the next statements in the program.
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Concurrency vs Parallelism diff --git a/content/posts/go/file-handling-in-go/index.md b/content/posts/go/file-handling-in-go/index.md index 89ee110..68654a7 100644 --- a/content/posts/go/file-handling-in-go/index.md +++ b/content/posts/go/file-handling-in-go/index.md @@ -572,7 +572,7 @@ func main() { ```
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Go by Example diff --git a/content/posts/go/go-programming-language/index.md b/content/posts/go/go-programming-language/index.md index 8163866..e6905d4 100644 --- a/content/posts/go/go-programming-language/index.md +++ b/content/posts/go/go-programming-language/index.md @@ -889,7 +889,7 @@ func main() { To catch an unexpected error and exit the program we call the `panic()` function with a string or the error as its argument. If we don't want the program to abort on `panic()` we can follow it up with a `recover()` function.
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Go by Example diff --git a/content/posts/go/rest-api-requests-in-go/index.md b/content/posts/go/rest-api-requests-in-go/index.md index d16b7b1..18945f0 100644 --- a/content/posts/go/rest-api-requests-in-go/index.md +++ b/content/posts/go/rest-api-requests-in-go/index.md @@ -393,7 +393,7 @@ func main() { ```
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources REST API Tutorial diff --git a/content/posts/homelab/building-your-own-homelab/index.md b/content/posts/homelab/building-your-own-homelab/index.md index f1feb01..94d2b56 100644 --- a/content/posts/homelab/building-your-own-homelab/index.md +++ b/content/posts/homelab/building-your-own-homelab/index.md @@ -269,7 +269,7 @@ I selected RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources G Suite legacy free edition diff --git a/content/posts/kubernetes/building-container-images/index.md b/content/posts/kubernetes/building-container-images/index.md index 9c11ca9..4ce4be7 100644 --- a/content/posts/kubernetes/building-container-images/index.md +++ b/content/posts/kubernetes/building-container-images/index.md @@ -164,7 +164,7 @@ Images built by `buildah` could then be executed using Podman and Docker or any
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources About the Open Container Initiative diff --git a/content/posts/kubernetes/container-architecture/index.md b/content/posts/kubernetes/container-architecture/index.md index 7bedcba..4ef7210 100644 --- a/content/posts/kubernetes/container-architecture/index.md +++ b/content/posts/kubernetes/container-architecture/index.md @@ -177,7 +177,7 @@ SELinux checks the *SELinux context* of the file or process to make decisions re
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Cgroups, namespaces, and beyond: what are containers made from? diff --git a/content/posts/kubernetes/container-images/index.md b/content/posts/kubernetes/container-images/index.md index f457b5f..69fb825 100644 --- a/content/posts/kubernetes/container-images/index.md +++ b/content/posts/kubernetes/container-images/index.md @@ -259,7 +259,7 @@ docker commit webserver-container quay.io/username/webserver:latest
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources About the Open Container Initiative diff --git a/content/posts/kubernetes/container-lifecycle/index.md b/content/posts/kubernetes/container-lifecycle/index.md index f11d952..326fb45 100644 --- a/content/posts/kubernetes/container-lifecycle/index.md +++ b/content/posts/kubernetes/container-lifecycle/index.md @@ -233,7 +233,7 @@ We can also attach a terminal to the container from the **Terminal** tab.
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Podman Installation Instructions diff --git a/content/posts/kubernetes/container-network-interfaces/index.md b/content/posts/kubernetes/container-network-interfaces/index.md index 8d9f4b1..1e58093 100644 --- a/content/posts/kubernetes/container-network-interfaces/index.md +++ b/content/posts/kubernetes/container-network-interfaces/index.md @@ -141,7 +141,7 @@ Features of Weave:
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Kubernetes CNI Explained diff --git a/content/posts/kubernetes/container-storage-interfaces/index.md b/content/posts/kubernetes/container-storage-interfaces/index.md index a8bc8ff..10a15c1 100644 --- a/content/posts/kubernetes/container-storage-interfaces/index.md +++ b/content/posts/kubernetes/container-storage-interfaces/index.md @@ -149,7 +149,7 @@ Using this driver the developers can take advantage of Optane memory in their ca
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Container storage 101: What is CSI and how does it work? | Computer Weekly diff --git a/content/posts/kubernetes/containers/index.md b/content/posts/kubernetes/containers/index.md index 701c66e..7ed06cd 100644 --- a/content/posts/kubernetes/containers/index.md +++ b/content/posts/kubernetes/containers/index.md @@ -126,7 +126,7 @@ It also has other security-focused features like the containers don't run as roo
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Oracle VM VirtualBox diff --git a/content/posts/kubernetes/helm-charts/index.md b/content/posts/kubernetes/helm-charts/index.md index 8f887a3..53a7310 100644 --- a/content/posts/kubernetes/helm-charts/index.md +++ b/content/posts/kubernetes/helm-charts/index.md @@ -260,7 +260,7 @@ helm pull oci://docker.io/bovem/test-helm-chart --version 0.0.1
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Using the DNF software package manager diff --git a/content/posts/kubernetes/kubernetes-operators/index.md b/content/posts/kubernetes/kubernetes-operators/index.md index 13744be..6e36758 100644 --- a/content/posts/kubernetes/kubernetes-operators/index.md +++ b/content/posts/kubernetes/kubernetes-operators/index.md @@ -88,7 +88,7 @@ It could be limited to a single namespace or deployed cluster-wide. It is recomm
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Operator Pattern diff --git a/content/posts/kubernetes/network-functions/index.md b/content/posts/kubernetes/network-functions/index.md index 5d5b3ae..d0e3148 100644 --- a/content/posts/kubernetes/network-functions/index.md +++ b/content/posts/kubernetes/network-functions/index.md @@ -95,7 +95,7 @@ The **CNF Test Suite** is defined by the Telecom User Group (TUG) and Cloud Nati It provisions the infrastructure for a Kubernetes and OpenStack cluster. After that, it deploys similar workloads on both environments to benchmark their performance using testing tools such as NFVbench.
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources What is telco cloud? diff --git a/content/posts/kubernetes/operator-sdk/index.md b/content/posts/kubernetes/operator-sdk/index.md index 86a4395..2d78f84 100644 --- a/content/posts/kubernetes/operator-sdk/index.md +++ b/content/posts/kubernetes/operator-sdk/index.md @@ -241,7 +241,7 @@ operator-sdk scorecard docker.io/bovem/memcached-operator-bundle:v0.0.1
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources Operator Bundle diff --git a/content/posts/kubernetes/operators-on-openshift/index.md b/content/posts/kubernetes/operators-on-openshift/index.md index 2d8a582..7ee1c61 100644 --- a/content/posts/kubernetes/operators-on-openshift/index.md +++ b/content/posts/kubernetes/operators-on-openshift/index.md @@ -132,7 +132,7 @@ $ oc delete csv/openshift-pipelines-operator-rh.v1.10.0 -n openshift-operators
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources OperatorHub From dfcc827381528358bfe6d8f043e6bb9764ef84e4 Mon Sep 17 00:00:00 2001 From: bovem Date: Tue, 7 Nov 2023 22:47:03 +0530 Subject: [PATCH 2/2] Added space complexity analysis --- content/posts/dsa/contains-duplicate/index.md | 39 ++++++---- .../encoding-and-decoding-strings/index.md | 23 ++++-- content/posts/dsa/group-anagrams/index.md | 48 +++++++----- content/posts/dsa/is-anagram/index.md | 41 +++++----- content/posts/dsa/is-valid-sudoku/index.md | 15 ++-- .../posts/dsa/product-except-self/index.md | 34 +++++---- content/posts/dsa/top-k-frequent/index.md | 76 ++++++++++--------- content/posts/dsa/two-sums/index.md | 75 +++++++++--------- 8 files changed, 204 insertions(+), 147 deletions(-) diff --git a/content/posts/dsa/contains-duplicate/index.md b/content/posts/dsa/contains-duplicate/index.md index 83497ac..5e24f62 100644 --- a/content/posts/dsa/contains-duplicate/index.md +++ b/content/posts/dsa/contains-duplicate/index.md @@ -34,32 +34,35 @@ loop index1 in array return true return false ``` - -## Best Case Scenario +## Time Complexity Analysis +### Best Case Scenario The best-case scenario for the brute-force solution would be when the first and second elements are duplicated. In this scenario, the outer and inner loop will execute only once so the time complexity will be $O(1)$.

Best Case scenario for containsDuplicate

-## Worst Case Scenario +### Worst Case Scenario If all the elements in the array are unique then the brute-force algorithm will take $O(n^2)$ time for completion, where $n$ is the size of the array.

Worst Case scenario for containsDuplicate

-## Code for Brute Force Solution +## Space Complexity Analysis +Since the brute-force solution does not use any data structures other than the input array, its space complexity will be $O(1)$. + +## Code for the Brute Force Solution ```Go package main import "fmt" -func containsDuplicate(array []int)(bool){ - for i:=0;i -Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources 217. Contains Duplicate diff --git a/content/posts/dsa/encoding-and-decoding-strings/index.md b/content/posts/dsa/encoding-and-decoding-strings/index.md index 422c321..6bef041 100644 --- a/content/posts/dsa/encoding-and-decoding-strings/index.md +++ b/content/posts/dsa/encoding-and-decoding-strings/index.md @@ -81,15 +81,20 @@ func decode(encodedString) return decodedList ``` -## Best Case Scenario + +## Time Complexity Analysis +### Best Case Scenario The best-case input for the brute force solution would be a string full of delimiters since the decoding process will finish earlier (the `index` is incremented by 2 while encountering the escape character `\`). The time complexity of encoding and decoding in the best-case scenario would be $O(k \times n)$ where $k$ is the average size of the string and $n$ is the size of the input array. -## Worst Case Scenario +### Worst Case Scenario For the worst-case time complexity of the brute-force solution, the input should not contain the delimiter or escape characters. The time complexity of encoding and decoding would still be $O(k \times n)$. -## Code for Brute Force Solution +## Space Complexity Analysis +The additional space required to store the `encodedString` and `decodedList` will be $O(kn)$. + +## Code for the Brute Force Solution ```Go package main @@ -233,19 +238,23 @@ func decode(encodedString) return decodedList ``` -## Best Case Scenario +## Time Complexity Analysis +### Best Case Scenario The best-case input for the optimized solution will contain strings with length < 9. The time complexity of the encoding loop will be $O(n)$ because it will iterate over all the elements in the array. For the decoding loop, the time complexity appears to be $O(kn)$ but we are incrementing the `index` by the length of string value ($k$) on every iteration. Thus, the time complexity of decoding is also $O({kn \over k}) = O(n)$. -## Worst Case Scenario +### Worst Case Scenario The worst-case time complexity of encoding is the same as the best-case i.e. $O(n)$. The time complexity of the decoding function will depend on the number of digits (in the length of the largest string). For example: If the length of the largest individual string is `199990` then the time complexity of decoding the string will be $O(6 \times n)$ (because `199990` has 6 digits). -## Code for Optimized Solution +## Space Complexity Analysis +The space complexity of the optimized solution will be the same as the brute force solution ($O(kn)$) as no additional data structures are used. + +## Code for the Optimized Solution ```Go package main @@ -331,7 +340,7 @@ func main(){
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources 659 ยท Encode and Decode Strings diff --git a/content/posts/dsa/group-anagrams/index.md b/content/posts/dsa/group-anagrams/index.md index 2c9e301..8d0d4f4 100644 --- a/content/posts/dsa/group-anagrams/index.md +++ b/content/posts/dsa/group-anagrams/index.md @@ -52,19 +52,23 @@ loop key, value in hashmap return returnedList ``` -## Best Case Scenario +## Time Complexity Analysis +### Best Case Scenario The time taken to sort individual strings is assumed to be $O(k \log(k))$ (the time complexity of the best sorting algorithm) where $k$ is the size of the string. Since we have to perform this operation on every element the sorting operation will be repeated $n$ time, where $n$ is the size of the array, resulting in $O(n \times k \log(k))$ time.

Best case scenario for Brute Force solution for groupAnagrams

The best-case input will contain just a single set of anagrams i.e. the hashmap will contain just one key. The loop over hashmap values will be executed only once. Thus, the total time complexity of grouping anagrams will be $O(n \times k \log(k))$. -## Worst Case Scenario +### Worst Case Scenario

Worst case scenario for Brute Force solution for groupAnagrams

The worst-case input array will contain zero anagrams and the hashmap will contain a key for each array element i.e. $n$ keys. Thus, the total time complexity of the function in the worst-case scenario will be $O(n + n \times k \log(k))$, which could be simplified to $O(n \times k \log(k))$. -## Code for Brute Force Solution +## Space Complexity Analysis +The additional space required to store the `hashmap` and `returnedList` will be $O(n)+O(n)$ or simply $O(n)$. + +## Code for the Brute Force Solution ```Go package main @@ -80,24 +84,24 @@ func sortString(inputString string)(string){ return strings.Join(listInputString, "") } -func groupAnagrams(inputArray []string)([][]string){ +func groupAnagrams(strs []string)([][]string){ hashmap := make(map[string][]string) - // Loop over the inputArray and sort every string element - for index:=0;index -Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources 49. Group Anagrams -Group Anagrams - Categorize Strings by Count - Leetcode 49 \ No newline at end of file +Group Anagrams - Categorize Strings by Count - Leetcode 49 diff --git a/content/posts/dsa/is-anagram/index.md b/content/posts/dsa/is-anagram/index.md index f444ef2..98524a8 100644 --- a/content/posts/dsa/is-anagram/index.md +++ b/content/posts/dsa/is-anagram/index.md @@ -53,18 +53,21 @@ loop index in string1 return true ``` - -## Best Case Scenario +## Time Complexity Analysis +### Best Case Scenario The best time complexity of a sorting function is $O(n \log(n))$, so the total time complexity of sorting operations is $O(s \log(s)) + O(t \log(t))$ where $s$ and $t$ are the sizes of strings. Since both strings are of the same length (for anagrams) we can generalize time complexity to $O(s \log(s))$. If the inputs are of different lengths (non-anagrams) then the time complexity of the complete function will be: $O(1)$ as the function will exit preemptively. Even though we have to compare characters in a loop ($O(s)$) the sorting time will scale relatively slower. Thus, the total time complexity of the best-case scenario will be $O(s \log(s))$. -## Worst Case Scenario +### Worst Case Scenario The worst-case scenario for a brute force solution will compare all the characters in sorted strings to find that they aren't anagrams. Its total time complexity will be $2*O(s \log(s)) + O(s)$ but we can generalize it to $O(s \log(s))$. -## Code for Brute Force Solution +## Space Complexity Analysis +The sorting of input strings is performed in-place. Thus, there is no requirement for additional memory space by the brute-force solution and its space complexity will be $O(1)$. + +## Code for the Brute Force Solution ```Go package main @@ -84,7 +87,7 @@ func sortString(inputString string)(string){ return strings.Join(str, "") } -func validAnagram(string1 string, string2 string)(bool){ +func isAnagram(string1 string, string2 string)(bool){ if len(string1) != len(string2){ return false } @@ -107,15 +110,15 @@ func main(){ string1 := "counter" string2 := "trounce" fmt.Println(string1, "is an Anagram of", - string2, ":", validAnagram(string1, string2)) + string2, ":", isAnagram(string1, string2)) string2 = "trouncee" fmt.Println(string1, "is an Anagram of", - string2, ":", validAnagram(string1, string2)) + string2, ":", isAnagram(string1, string2)) string2 = "trounc" fmt.Println(string1, "is an Anagram of", - string2, ":", validAnagram(string1, string2)) + string2, ":", isAnagram(string1, string2)) } // Output @@ -156,19 +159,23 @@ else return false ``` -## Best Case Scenario +## Time Complexity Analysis +### Best Case Scenario In the best-case scenario for an optimized solution both strings will be anagram and the time complexity will be $O(s)$ (generalized from $O(s) + O(s)$). -## Worst Case Scenario +### Worst Case Scenario The time complexity of the worst-case scenario will be the same as the best-case scenario i.e. $O(s)$ but the input strings won't be anagrams. -## Code for Optimized Solution +## Space Complexity Analysis +The `counter` hashmap will take additional $O(n)$ memory space. + +## Code for the Optimized Solution ```Go package main import "fmt" -func validAnagram(inputString1 string, inputString2 string)(bool){ +func isAnagram(inputString1 string, inputString2 string)(bool){ if len(inputString1) != len(inputString2){ return false } @@ -209,15 +216,15 @@ func main(){ string1 := "counter" string2 := "trounce" fmt.Println(string1, "is an Anagram of", - string2, ":", validAnagram(string1, string2)) + string2, ":", isAnagram(string1, string2)) string2 = "trouncee" fmt.Println(string1, "is an Anagram of", - string2, ":", validAnagram(string1, string2)) + string2, ":", isAnagram(string1, string2)) string2 = "trounc" fmt.Println(string1, "is an Anagram of", - string2, ":", validAnagram(string1, string2)) + string2, ":", isAnagram(string1, string2)) } // Output @@ -227,8 +234,8 @@ func main(){ ```
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources -242. Valid Anagram +242. Valid Anagram Valid Anagram - Leetcode 242 - Python diff --git a/content/posts/dsa/is-valid-sudoku/index.md b/content/posts/dsa/is-valid-sudoku/index.md index 42745d4..70cc588 100644 --- a/content/posts/dsa/is-valid-sudoku/index.md +++ b/content/posts/dsa/is-valid-sudoku/index.md @@ -82,13 +82,18 @@ loop row_index from 0 to 6 with step 3 sub_matrix_hashmap[r_value][c_value] = 1 ``` -## Best Case Scenario + +## Time Complexity Analysis +### Best Case Scenario The best-case input for the optimal solution will be a completed Sudoku grid. The time taken to return the result will be $9 \times O(9) + 9 \times O(9) + O(81)$ which could be simplified to $O(1)$. -## Worst Case Scenario +### Worst Case Scenario In the worst-case scenario, the input will be an invalid Sudoku grid. The time taken to produce the result will be the same as the best-case scenario i.e. O(1). -## Code for Optimal Solution +## Space Complexity Analysis +The space complexity for the optimal solution will be the sum of memory space required by the `row_hashmap`, `column_hashmap`, and `sub_matrix_hashmap` i.e. $O(9)+O(9)+O(9)$ which could be simplified to $O(1)$. + +## Code for the Optimal Solution First, we will implement the `validCellValue` function to check if the value in a cell is within 1 to 9. ```Go func validCellValue(inputValue byte)(bool){ @@ -272,7 +277,7 @@ func isValidSudoku(board [][]byte)(bool){ } ``` -### Complete Code for Optimal Solution +### Complete Code for the Optimal Solution ```Go package main @@ -530,7 +535,7 @@ func main(){
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources How to Play Sudoku? diff --git a/content/posts/dsa/product-except-self/index.md b/content/posts/dsa/product-except-self/index.md index ed247f4..4d6ab2a 100644 --- a/content/posts/dsa/product-except-self/index.md +++ b/content/posts/dsa/product-except-self/index.md @@ -2,7 +2,7 @@ author: "Avnish" title: "Building a Product Array without the Element Itself" date: "2023-10-28" -description: "Implementing a productExceptSelf function that returns an array of products of all the elements without the element at same index in input array" +description: "Implementing a productExceptSelf function that returns an array of products of all the elements without the element at the same index in input array" tags: ["data-structures", "arrays", "go", "neetcode-150", "leetcode-medium"] categories: ["Data Structures"] series: ["Data Structures and Algorithms"] @@ -14,7 +14,7 @@ comments: false cover: image: "product-except-self-cover.png" linkFullImages: true - alt: "The productExceptSelf function will returns an array of products of all the elements without the element at same index in input array" + alt: "The productExceptSelf function will returns an array of products of all the elements without the element at the same index in input array" caption: "" relative: false responsiveImages: false @@ -47,15 +47,19 @@ loop index on nums return answer ``` -## Best Case Scenario +## Time Complexity Analysis +### Best Case Scenario The brute-force solution will return the `answer` array in $O(n^2)$ time for the best-case input. Since both the loops will iterate completely over the `nums` array. -## Worst Case Scenario +### Worst Case Scenario The worst-case scenario will also take $O(n^2)$ to return the solution. -## Code for Brute Force Solution +## Space Complexity Analysis +The `answer` array will require additional $O(n)$ memory. + +## Code for the Brute Force Solution ```Go package main @@ -129,17 +133,19 @@ loop index on prefix return answer ``` -## Best Case Scenario - +## Time Complexity Analysis +### Best Case Scenario The optimized solution will return the `answer` array in $O(n)$ time (generalized from $O(3n)$) since the time complexity of iterating over all three arrays (`prefix`, `suffix`, and `answer`) is $O(n)$. -We can improve the space complexity further if we use a single array for storing products of `prefix` and `suffix` and return it as the `answer`. - -## Worst Case Scenario +### Worst Case Scenario The time complexity of iterating over arrays in worst-case input is the same as in the best-case scenario. Thus, the total time complexity of the function remains the same i.e. $O(n)$. -## Code for Optimized Solution +## Space Complexity Analysis +To store the `prefix`, `suffix`, and `answer` array we will need additional $O(3n)$ memory. +We can improve the space complexity if we use a single array for storing the products of `prefix` and `suffix` and return it as the `answer`. + +## Code for the Optimized Solution ```Go package main @@ -190,9 +196,9 @@ func main(){ // Product Except Self: [4505884 5963670 3026340 2357730 8815860] ``` -## Code for Optimized Solution (Constant Space Complexity) +## Code for the Optimized Solution (Constant Space Complexity) -We aren't considering the memory space acquired by the `answer` array. +Assuming the memory space required by the `answer` array is constant i.e. $O(1)$. ```Go package main @@ -236,7 +242,7 @@ func main(){
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources diff --git a/content/posts/dsa/top-k-frequent/index.md b/content/posts/dsa/top-k-frequent/index.md index e8b6504..7a3182d 100644 --- a/content/posts/dsa/top-k-frequent/index.md +++ b/content/posts/dsa/top-k-frequent/index.md @@ -2,7 +2,7 @@ author: "Avnish" title: "Finding Most Frequent Elements in an Array" date: "2023-10-26" -description: "Implementing a topKFrequent(inputArray, k) function that takes an array of integers and a value k as inputs and returns top k most frequent elements." +description: "Implementing a topKFrequent(nums, k) function that takes an array of integers and a value k as inputs and returns top k most frequent elements." tags: [ "data-structures", @@ -77,7 +77,8 @@ loop index on freq return topKElements ``` -## Best Case Scenario +## Time Complexity Analysis +### Best Case Scenario The first loop over the array has $O(n)$ time complexity, where $n$ is the size of the input array. @@ -89,11 +90,14 @@ The final loop (over `freq`) will execute `k` times. On each iteration, it will Thus, the total time complexity of the best-case scenario for brute force solution will be $O(n) + O(1) + O(n \log(n)) + O(k)$, which could be generalized to $O(n \log n)$. -## Worst Case Scenario +### Worst Case Scenario In the worst-case scenario, all the elements in the input array will have distinct frequencies. Thus, the length of `counter` and `reverseCounter` will be the same resulting in total time complexity $O(n) + O(n) + O(n \log(n)) + O(k)$, which could also be generalized to $O(n \log n)$. -## Code for Brute Force Solution +## Space Complexity Analysis +The memory space used by `counter` and `reverseCounter` will be $O(n)+O(n)$. We are assuming that the `freq` array is sorted in place so it won't require additional memory space. Finally, the space complexity of the `topKElements` array will be $O(n)$ if `k=length(nums)`. Thus, the total space complexity of the brute-force solution could be simplified to $O(n)$. + +## Code for the Brute Force Solution ```Go package main @@ -103,18 +107,18 @@ import ( "sort" ) -func topKElements(inputArray []int, k int)([]int){ +func topKElements(nums []int, k int)([]int){ // counter hashmap for storing frequency data - // of elements in inputArray + // of elements in nums // Time Complexity: O(n) counter := make(map[int]int) - for index:=0;index0;freq--{ + for freq:=len(nums);freq>0;freq--{ value, key_exists := reverseCounter[freq] if key_exists{ for _, val := range value{ @@ -263,19 +271,19 @@ func topKElements(inputArray []int, k int)([]int){ } func main(){ - inputArray := []int{1, 1, 1, 2, 2, 3, 3, 4} + nums := []int{1, 1, 1, 2, 2, 3, 3, 4} k := 3 - fmt.Println("Top", k, "elements in inputArray:", topKElements(inputArray, k)) + fmt.Println("Top", k, "elements in nums:", topKElements(nums, k)) - inputArray = []int{1, 2} + nums = []int{1, 2} k = 2 - fmt.Println("Top", k, "elements in inputArray:", topKElements(inputArray, k)) + fmt.Println("Top", k, "elements in nums:", topKElements(nums, k)) } ```
-Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources diff --git a/content/posts/dsa/two-sums/index.md b/content/posts/dsa/two-sums/index.md index d219d35..6646a77 100644 --- a/content/posts/dsa/two-sums/index.md +++ b/content/posts/dsa/two-sums/index.md @@ -2,7 +2,7 @@ author: "Avnish" title: "Finding Elements in an Array that Sum Up to a Target Value" date: "2023-10-15" -description: "Implementing a twoSums(inputArray, targetValue) function that returns the indices of two elements in inputArray which could be summed up to the targetValue" +description: "Implementing a twoSums(nums, targetValue) function that returns the indices of two elements in nums which could be summed up to the targetValue" tags: ["data-structures", "arrays", "hashmaps", "go", "neetcode-150", "leetcode-easy"] categories: ["Data Structures"] series: ["Data Structures and Algorithms"] @@ -40,23 +40,26 @@ loop index1 in array return none, none ``` -## Best Case Scenario +## Time Complexity Analysis +### Best Case Scenario If the sum of the first and second elements in the input array is equal to the target value, that would be the best-case runtime scenario. Since the inner and outer loop will be executed only once the time complexity will be $O(1)$. -## Worst Case Scenario +### Worst Case Scenario In the worst-case scenario, the input array won't contain any valid pair of elements. Thus, both loops will be executed completely resulting in $O(n^2)$ time complexity, where $n$ is the size of the input array. -## Code for Brute Force Solution +## Space Complexity Analysis +There isn't any additional memory used by the brute-force solution. Thus, the total space complexity will be $O(1)$. +## Code for the Brute Force Solution ```Go package main import "fmt" -func twoSums(inputArray []int, target int)([]int){ - for i:=0;i -Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. # Resources 1. Two Sum