diff --git a/Answer15.cpp b/Answer15.cpp new file mode 100644 index 0000000..036bdec --- /dev/null +++ b/Answer15.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int majorityElement(vector& nums) { + int count = 0; + int candidate = 0; + + for (int num : nums) { + if (count == 0) { + candidate = num; + } + if(num==candidate) count += 1; + else count -= 1; + } + + return candidate; + } +}; + +//Input Format: N = 3, nums[] = {3,2,3} +//Result: 3 + +// When we just count the occurrences of each number and compare with half of the size of the array, you will get 3 for the above solution. \ No newline at end of file diff --git a/Answer55.cpp b/Answer55.cpp new file mode 100644 index 0000000..ecf5597 --- /dev/null +++ b/Answer55.cpp @@ -0,0 +1,10 @@ +// It is the same structure but by using the in-built list STL data structures of C++ + +class Graph{ + int numVertices; + list *adjLists; + + public: + Graph(int V); + void addEdge(int src, int dest); +}; \ No newline at end of file