question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
time-based-key-value-store | C++ || Custom Binary Search Function || Easy to understand | c-custom-binary-search-function-easy-to-nz2tv | \nclass TimeMap {\npublic:\n unordered_map<string,vector<pair<int,string>>>mp;\n TimeMap() {\n \n }\n \n void set(string key, string value | puneet801 | NORMAL | 2022-10-06T08:00:45.677637+00:00 | 2022-10-06T08:00:45.677681+00:00 | 509 | false | ```\nclass TimeMap {\npublic:\n unordered_map<string,vector<pair<int,string>>>mp;\n TimeMap() {\n \n }\n \n void set(string key, string value, int timestamp) {\n mp[key].push_back({timestamp,value});\n }\n \n pair<int,string> searchEqualOrLesser(vector<pair<int,string>>&arr, int ke... | 5 | 0 | ['C', 'Binary Tree'] | 1 |
time-based-key-value-store | unordered map and upper bound | unordered-map-and-upper-bound-by-aniketp-r99r | \nclass TimeMap {\npublic:\n unordered_map<string,map<int,string> > um;\n TimeMap() {\n \n }\n \n void set(string key, string value, int t | aniketpawar | NORMAL | 2022-10-06T06:31:48.581864+00:00 | 2022-10-06T06:31:48.581932+00:00 | 137 | false | ```\nclass TimeMap {\npublic:\n unordered_map<string,map<int,string> > um;\n TimeMap() {\n \n }\n \n void set(string key, string value, int ts) {\n um[key][ts]=value;\n }\n \n string get(string key, int ts) {\n if(um.find(key)==um.end())return "";\n auto it=um[key].up... | 5 | 0 | [] | 0 |
time-based-key-value-store | [Typescript/Javascript] Binary Search + Hashmap | typescriptjavascript-binary-search-hashm-kr24 | Use map to store list of \'datums\' (value and timestamp).\nDescription says timestamps are set in increasing order, so that is hint to use binary search.\n\nIn | weakhero | NORMAL | 2022-04-16T15:36:50.758522+00:00 | 2024-09-21T17:54:20.818972+00:00 | 649 | false | Use map to store list of \'datums\' (value and timestamp).\nDescription says timestamps are set in increasing order, so that is hint to use binary search.\n\nIn a list of timestamps, given a target timestamp, we want to find the \'datum.timestamp\' <= timestamp. \nOnce our datum.timestamp <= timestamp (within the corre... | 5 | 0 | ['Binary Tree', 'TypeScript', 'JavaScript'] | 0 |
time-based-key-value-store | C# | Built in BinarySearch method for lists & maps | 97% | c-built-in-binarysearch-method-for-lists-s2a4 | Very good question! I learned alot about the BinarySearch method for Lists in C#. Very useful so you don\'t have to imlpement binary search from scratch and ris | sms009 | NORMAL | 2021-10-12T21:20:22.791707+00:00 | 2021-10-12T21:20:22.791735+00:00 | 256 | false | Very good question! I learned alot about the ```BinarySearch``` method for ```Lists``` in C#. Very useful so you don\'t have to imlpement binary search from scratch and risk error/debugging time.\n\nHighly recommend reading: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list... | 5 | 0 | ['Binary Tree'] | 2 |
time-based-key-value-store | Simple Java solution using Map of TreeMaps | simple-java-solution-using-map-of-treema-gckc | ```\nclass TimeMap {\n \n Map> map;\n\n /* Initialize your data structure here. /\n public TimeMap() {\n map = new HashMap<>();\n }\n \ | crisa | NORMAL | 2021-05-18T03:54:51.903427+00:00 | 2021-05-18T03:54:51.903472+00:00 | 327 | false | ```\nclass TimeMap {\n \n Map<String, TreeMap<Integer, String>> map;\n\n /** Initialize your data structure here. */\n public TimeMap() {\n map = new HashMap<>();\n }\n \n public void set(String key, String value, int timestamp) {\n TreeMap<Integer, String> values = map.getOrDefault(k... | 5 | 0 | [] | 0 |
time-based-key-value-store | JavaScript solution [96, 61] | Binary Search | Clean | javascript-solution-96-61-binary-search-bzkiy | I spent more time writing binary search algorithm than anyting else. That\'s where started to regret that JS misses binary search method on arrays.\n\nRuntime: | i-love-typescript | NORMAL | 2021-03-24T03:05:03.038105+00:00 | 2021-03-24T03:06:32.097395+00:00 | 902 | false | I spent more time writing binary search algorithm than anyting else. That\'s where started to regret that JS misses binary search method on arrays.\n\nRuntime: 404 ms, faster than 96.55% of JavaScript online submissions for Time Based Key-Value Store.\nMemory Usage: 79.4 MB, less than 61.49% of JavaScript online submis... | 5 | 1 | ['JavaScript'] | 1 |
time-based-key-value-store | Python Bisect | python-bisect-by-wsy19970125-3y4b | \tclass TimeMap:\n\n\t\tdef init(self):\n\t\t\t"""\n\t\t\tInitialize your data structure here.\n\t\t\t"""\n\t\t\tself.d = defaultdict(list)\n\n\t\tdef set(self, | wsy19970125 | NORMAL | 2020-07-08T00:05:53.617328+00:00 | 2020-07-08T00:05:53.617357+00:00 | 254 | false | \tclass TimeMap:\n\n\t\tdef __init__(self):\n\t\t\t"""\n\t\t\tInitialize your data structure here.\n\t\t\t"""\n\t\t\tself.d = defaultdict(list)\n\n\t\tdef set(self, key: str, value: str, timestamp: int) -> None:\n\t\t\tself.d[key].append((timestamp, value))\n\n\n\t\tdef get(self, key: str, timestamp: int) -> str:\n\t\t... | 5 | 0 | [] | 1 |
time-based-key-value-store | C++ Solution with inline code explanation | c-solution-with-inline-code-explanation-ig7rq | \nclass TimeMap {\npublic:\nunordered_map<string, map<int, string>> time_map;\n \n /** Initialize your data structure here. */\n TimeMap() {\n \ | linlin0208 | NORMAL | 2020-04-12T21:10:28.708455+00:00 | 2020-04-12T21:10:28.708489+00:00 | 393 | false | ```\nclass TimeMap {\npublic:\nunordered_map<string, map<int, string>> time_map;\n \n /** Initialize your data structure here. */\n TimeMap() {\n \n }\n \n void set(string key, string value, int timestamp) {\n // use hash map to lookup ordered {timestamp, value} pairs by key in O(1).\n ... | 5 | 0 | [] | 0 |
time-based-key-value-store | Javascript beat 100% solution. | javascript-beat-100-solution-by-jay_tsz_-rd13 | \nclass TimeMap{\n constructor(){\n this.map = {};\n }\n\n set(key, value, timestamp){\n if(!this.map[key]) this.map[key]= [];\n t | jay_tsz_wai | NORMAL | 2019-02-12T07:38:45.543458+00:00 | 2019-02-12T07:38:45.543525+00:00 | 866 | false | ```\nclass TimeMap{\n constructor(){\n this.map = {};\n }\n\n set(key, value, timestamp){\n if(!this.map[key]) this.map[key]= [];\n this.map[key].push({timestamp:timestamp, value:value});\n }\n\n get(key, timestamp){\n if(!this.map[key]) return undefined;\n return this.... | 5 | 1 | [] | 2 |
time-based-key-value-store | C++ || Binary Search || HashMap | c-binary-search-hashmap-by-190111052008-o1bh | Intuition\nThe problem involves storing values with timestamps for different keys, and we need to retrieve the value that corresponds to a particular timestamp | 190111052008 | NORMAL | 2024-09-11T11:05:07.771665+00:00 | 2024-09-18T03:18:36.124502+00:00 | 505 | false | # Intuition\nThe problem involves storing values with timestamps for different keys, and we need to retrieve the value that corresponds to a particular timestamp for a given key. To solve this, we can use an `unordered_map`, where each key is a `string`, and the value is a dynamic array (`vector`) of pairs. Each pair c... | 4 | 0 | ['String', 'Binary Search', 'C++'] | 0 |
time-based-key-value-store | Simple C++ Code || using Hash-Map || Faster than 90%+ users | simple-c-code-using-hash-map-faster-than-w70p | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | vishal_0410 | NORMAL | 2023-09-16T22:36:53.985455+00:00 | 2023-09-16T22:36:53.985484+00:00 | 322 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 4 | 0 | ['Hash Table', 'String', 'Binary Search', 'Design', 'C++'] | 1 |
time-based-key-value-store | c++ || hash map || | c-hash-map-by-adroitabhi205-xtn0 | The idea is simple and straight forward. \nstore the key,timestam,value in map \nIn the get function use a loop from the asked time stamp till 1 and check if we | adroitabhi205 | NORMAL | 2022-10-06T03:21:06.992263+00:00 | 2022-10-06T03:21:06.992305+00:00 | 1,304 | false | The idea is simple and straight forward. \nstore the key,timestam,value in map \nIn the get function use a loop from the asked time stamp till 1 and check if we find any value in the map[key] then return that one only. \nif the loop does not return any value then return empty string. \n\n\n**c++ code**\n```\nclass Tim... | 4 | 1 | ['Hash Table', 'C'] | 1 |
time-based-key-value-store | Java | treemap | binary search | java-treemap-binary-search-by-conchwu-bsay | List + binary search\n\n\t//Runtime: 181 ms, faster than 87.71% of Java online submissions for Time Based Key-Value Store.\n //Memory Usage: 117.3 MB, less t | conchwu | NORMAL | 2022-10-06T01:31:16.359896+00:00 | 2022-10-06T01:31:43.981963+00:00 | 556 | false | # List + binary search\n```\n\t//Runtime: 181 ms, faster than 87.71% of Java online submissions for Time Based Key-Value Store.\n //Memory Usage: 117.3 MB, less than 91.94% of Java online submissions for Time Based Key-Value Store.\n //List + binary search\n // If M is the number of set function calls\n // ... | 4 | 0 | ['Binary Search', 'Tree', 'Java'] | 0 |
time-based-key-value-store | 🗓️ Daily LeetCoding Challenge October, Day 6 | daily-leetcoding-challenge-october-day-6-rwcj | This problem is the Daily LeetCoding Challenge for October, Day 6. Feel free to share anything related to this problem here! You can ask questions, discuss what | leetcode | OFFICIAL | 2022-10-06T00:00:29.071475+00:00 | 2022-10-06T00:00:29.071570+00:00 | 3,184 | false | This problem is the Daily LeetCoding Challenge for October, Day 6.
Feel free to share anything related to this problem here!
You can ask questions, discuss what you've learned from this problem, or show off how many days of streak you've made!
---
If you'd like to share a detailed solution to the problem, please cr... | 4 | 0 | [] | 31 |
time-based-key-value-store | C++|| Using Hashmap and Binary Search Approach || easy-understanding code | c-using-hashmap-and-binary-search-approa-n026 | \ntypedef pair<int, string> pis;\nclass TimeMap {\npublic:\n unordered_map<string, vector<pis>> m; \n TimeMap() {\n \n }\n \n string great | HereToRule | NORMAL | 2022-08-12T11:36:08.823105+00:00 | 2022-08-12T11:36:08.823149+00:00 | 307 | false | ```\ntypedef pair<int, string> pis;\nclass TimeMap {\npublic:\n unordered_map<string, vector<pis>> m; \n TimeMap() {\n \n }\n \n string greatestValLessThan(int timestamp, vector<pis>& s){\n int start = 0;\n int end = s.size()-1;\n int ans = INT_MIN;\n string str = "";\n... | 4 | 0 | ['C', 'Binary Tree'] | 0 |
time-based-key-value-store | Binary Search + Hashmap Solution in Python, Very Easy to Understand!!! | binary-search-hashmap-solution-in-python-ojii | Read carefully and you will find that it is actually a search question combined with hashmap!\n\nUse binary search!\nThe question actually asks us to find out t | GeorgePot | NORMAL | 2022-05-13T18:46:37.930862+00:00 | 2022-05-13T18:46:37.930887+00:00 | 498 | false | Read carefully and you will find that it is actually a **search question** combined with **hashmap**!\n\nUse **binary search**!\nThe question actually asks us to find out **the largest number that is smaller or equal to some target value**.\n\nInitiate a **map**, storing (key, [(time1, value1), (time2, value2), ...]\nW... | 4 | 0 | ['Binary Tree', 'Python'] | 1 |
time-based-key-value-store | Python with bisect with one dict, easy to understand | python-with-bisect-with-one-dict-easy-to-z4q1 | \nfrom collections import defaultdict\nfrom bisect import bisect_right\nclass TimeMap:\n\n def __init__(self):\n self.tm = defaultdict(list)\n | mayankanand | NORMAL | 2022-04-18T08:05:49.942708+00:00 | 2022-04-18T08:05:49.942751+00:00 | 237 | false | ```\nfrom collections import defaultdict\nfrom bisect import bisect_right\nclass TimeMap:\n\n def __init__(self):\n self.tm = defaultdict(list)\n \n def set(self, key: str, value: str, timestamp: int) -> None:\n self.tm[key].append((timestamp, value))\n\n def get(self, key: str, timestamp:... | 4 | 0 | ['Binary Tree', 'Python'] | 0 |
time-based-key-value-store | Python: Using Dict and Binary Search | python-using-dict-and-binary-search-by-b-p20m | Create a dictionary of key => List of (timestamp, value)\nFor e.g: "foo" => [(1,"bar"), (5,"bar2")]\n\n"constantly increasing" property of timestamps makes inse | benslie | NORMAL | 2021-12-23T20:04:45.433839+00:00 | 2021-12-23T20:04:45.433880+00:00 | 656 | false | Create a dictionary of key => List of (timestamp, value)\nFor e.g: "foo" => [(1,"bar"), (5,"bar2")]\n\n"constantly increasing" property of timestamps makes insertion in "set" operation easy by just appending the values in the list. \n\nAs the list is sorted, use binary search to get the value in O(logn) time. \nTime ta... | 4 | 0 | ['Binary Tree', 'Python'] | 1 |
time-based-key-value-store | [python] binary search (82% faster) | python-binary-search-82-faster-by-mooris-cyca | \nclass TimeMap:\n\n def __init__(self):\n """\n Initialize your data structure here\n """\n self.d = collections.defaultdict(lis | moorissa | NORMAL | 2021-06-28T23:39:47.967937+00:00 | 2021-06-28T23:39:47.967987+00:00 | 597 | false | ```\nclass TimeMap:\n\n def __init__(self):\n """\n Initialize your data structure here\n """\n self.d = collections.defaultdict(list)\n\n\n def set(self, key: str, value: str, timestamp: int) -> None: # O(1)\n self.d[key].append((timestamp, value))\n \n\n def get(self, k... | 4 | 0 | ['Binary Tree', 'Python'] | 1 |
time-based-key-value-store | easy to understand hash map/ priority queue beats 99%. every detail commented :) | easy-to-understand-hash-map-priority-que-yjcq | //Idea is to maintain a hashmap of the key value pairs, but each key has a max heap as its value. This takes care of both having multiple values \n//mapped to a | aag479 | NORMAL | 2021-05-02T04:20:46.867099+00:00 | 2021-05-02T04:20:46.867133+00:00 | 922 | false | //Idea is to maintain a hashmap of the key value pairs, but each key has a max heap as its value. This takes care of both having multiple values \n//mapped to a key and efficiently returning the one with the most recent timestamp. I use a "timestamp" object which stores both the actual value and the timestamp\n//the ob... | 4 | 0 | ['Heap (Priority Queue)', 'Java'] | 1 |
time-based-key-value-store | Java Binary Search | java-binary-search-by-vikrant_pc-262d | \nclass TimeMap {\n Map<String, List<TimeValue>> map;\n\n /** Initialize your data structure here. */\n public TimeMap() {\n map = new HashMap<> | vikrant_pc | NORMAL | 2021-03-06T07:10:39.619092+00:00 | 2021-03-06T07:10:39.619126+00:00 | 342 | false | ```\nclass TimeMap {\n Map<String, List<TimeValue>> map;\n\n /** Initialize your data structure here. */\n public TimeMap() {\n map = new HashMap<>();\n }\n \n public void set(String key, String value, int timestamp) {\n map.putIfAbsent(key, new ArrayList<>());\n map.get(key).add(... | 4 | 0 | [] | 1 |
time-based-key-value-store | C++ std::unordered_map + std::upper_bound | c-stdunordered_map-stdupper_bound-by-igo-6yxi | \nclass TimeMap {\npublic:\n /** Initialize your data structure here. */\n unordered_map<string, vector<pair<string, int>>> mp; // [key, vec[val, time]]\n | igorb | NORMAL | 2020-06-02T23:41:31.122559+00:00 | 2020-06-03T04:34:54.170838+00:00 | 1,011 | false | ```\nclass TimeMap {\npublic:\n /** Initialize your data structure here. */\n unordered_map<string, vector<pair<string, int>>> mp; // [key, vec[val, time]]\n TimeMap() { \n }\n \n void set(string key, string value, int timestamp) {\n mp[key].push_back({value, timestamp});\n }\n \n str... | 4 | 0 | ['C', 'C++'] | 2 |
time-based-key-value-store | Java | Maxheap + HashMap | 100% Memory | 95% time (120 ms) | java-maxheap-hashmap-100-memory-95-time-bvz3v | The idea is to store a MaxHeap for every key. The Maxheap will work on the timestamp so as to get the max one on top. \n\n\nclass TimeMap {\n\n Map<String, P | techton | NORMAL | 2020-05-12T00:23:03.357591+00:00 | 2020-05-12T23:27:10.421914+00:00 | 319 | false | The idea is to store a MaxHeap for every key. The Maxheap will work on the timestamp so as to get the max one on top. \n\n```\nclass TimeMap {\n\n Map<String, PriorityQueue<Entry>> map;\n public TimeMap() {\n map = new HashMap<>();\n }\n \n public void set(String key, String value, int timestamp) ... | 4 | 0 | [] | 2 |
time-based-key-value-store | C# 99.57%/100% with Binary Search | c-9957100-with-binary-search-by-vasaka-194u | \npublic class TimeMap {\n Dictionary<string, List<(int, string)>> map = new Dictionary<string, List<(int, string)>>();\n \n /** Initialize your data s | vasaka | NORMAL | 2019-10-03T19:57:04.261503+00:00 | 2019-10-03T19:57:04.261573+00:00 | 383 | false | ```\npublic class TimeMap {\n Dictionary<string, List<(int, string)>> map = new Dictionary<string, List<(int, string)>>();\n \n /** Initialize your data structure here. */\n public TimeMap() {\n \n }\n \n public void Set(string key, string value, int timestamp) {\n if (map.TryGetValue... | 4 | 1 | ['Binary Search'] | 0 |
time-based-key-value-store | [CPP] A clear solution using STL( map and unordered_map) | cpp-a-clear-solution-using-stl-map-and-u-o1dc | Here is a clear c++ solution for this problem, and this solution only use map and unorder_map data structure in STL.\n\nFirst, we use a unordered_map to save Ke | wuxiaobai24 | NORMAL | 2019-09-16T12:26:42.485547+00:00 | 2019-09-16T12:26:42.485628+00:00 | 622 | false | Here is a clear c++ solution for this problem, and this solution only use `map` and `unorder_map` data structure in STL.\n\nFirst, we use a `unordered_map` to save Key and \'Value\'(not truely value). And then, we use a `map` to save Time and turely value in map\'s value, so that we can easy use the `lower_bound` api i... | 4 | 0 | ['C++'] | 0 |
time-based-key-value-store | simple c++ binary search solution | simple-c-binary-search-solution-by-xiliu-gvfi | \nclass TimeMap {\npublic:\n /** Initialize your data structure here. */\n TimeMap() {\n \n }\n \n void set(string key, string value, int | xiliuzju | NORMAL | 2019-07-13T19:39:41.570868+00:00 | 2019-07-13T19:39:41.570900+00:00 | 352 | false | ```\nclass TimeMap {\npublic:\n /** Initialize your data structure here. */\n TimeMap() {\n \n }\n \n void set(string key, string value, int timestamp) {\n map_[key].push_back(std::make_pair(value, timestamp));\n }\n \n string get(string key, int timestamp) {\n vector<pair<s... | 4 | 0 | [] | 0 |
time-based-key-value-store | C# Solution | c-solution-by-roccosen-2jyc | Here is the C# Solution.\n\n\npublic class TimeMap \n{\n Dictionary<string, SortedList<int, string>> map = new Dictionary<string, SortedList<int, string>>(); | roccosen | NORMAL | 2019-04-21T17:08:23.544333+00:00 | 2019-04-21T17:08:23.544401+00:00 | 869 | false | Here is the C# Solution.\n\n```\npublic class TimeMap \n{\n Dictionary<string, SortedList<int, string>> map = new Dictionary<string, SortedList<int, string>>();\n\n /** Initialize your data structure here. */\n public TimeMap()\n {\n\n }\n\n public void Set(string key, string value, int timestamp)\n ... | 4 | 0 | [] | 2 |
time-based-key-value-store | Swift two dictionaries | swift-two-dictionaries-by-wds8807-3jeh | Two dictionaries, one for (key: String, timestamp: Int), the other for (timeStamp: Int, value: String).\n\nclass TimeMap {\n\n /** Initialize your data struc | wds8807 | NORMAL | 2019-04-10T19:47:44.466165+00:00 | 2019-04-10T19:47:44.466255+00:00 | 388 | false | Two dictionaries, one for ```(key: String, timestamp: Int)```, the other for ```(timeStamp: Int, value: String)```.\n```\nclass TimeMap {\n\n /** Initialize your data structure here. */\n\tprivate var timestamps: [String: [Int]] = [:] \n\tprivate var values: [Int: String] = [:]\n\t\n\tinit() { }\n\t\n\tfunc set(_ ke... | 4 | 0 | [] | 0 |
time-based-key-value-store | O(N) solution beats 90% | Python 3 | Java | Javascript | on-solution-beats-90-python-3-java-javas-vpt7 | IntuitionNo thoughtsApproachCode Commented, just know that i had to submit the code multiple times to understand how it works, the describtion is incomprehsible | mawhadmd | NORMAL | 2025-01-02T23:38:01.880688+00:00 | 2025-01-02T23:38:01.880688+00:00 | 732 | false | # Intuition
No thoughts
# Approach
Code Commented, just know that i had to submit the code multiple times to understand how it works, the describtion is incomprehsible.
The`Set()` does one thing, which is checking if the key already exist in the `Self.map`, if so then it'll append the new `{value, stamp}`, `Self.map`... | 3 | 0 | ['Hash Table', 'Java', 'Python3', 'JavaScript'] | 0 |
time-based-key-value-store | Easy C++ Solution with Explanation | easy-c-solution-with-explanation-by-yash-c61i | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nExplained in the code u | YashodhanSonune | NORMAL | 2024-07-20T12:28:38.486655+00:00 | 2024-07-20T12:28:38.486677+00:00 | 624 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nExplained in the code using comments.\n\n# Complexity\n- Time complexity: Considering a single operation:-\n- \'set\' function -> O(1)\n- \'get\' function -> O(log n)\... | 3 | 0 | ['C++'] | 1 |
time-based-key-value-store | 32.1 (Approach 1) | O(log n)✅ | Python & C++(Step by step explanation)✅ | 321-approach-1-olog-n-python-cstep-by-st-zx62 | Approach\n\nThe TimeMap class is designed to store key-value pairs where each value has an associated timestamp. The primary operations include setting a key-va | monster0Freason | NORMAL | 2024-01-30T18:29:23.903490+00:00 | 2024-01-30T18:29:23.903517+00:00 | 698 | false | # Approach\n\nThe `TimeMap` class is designed to store key-value pairs where each value has an associated timestamp. The primary operations include setting a key-value pair with a timestamp and retrieving the value associated with a key at a given timestamp.\n\n1. **Initialization:**\n - The class initializes an unor... | 3 | 0 | ['String', 'Binary Search', 'Design', 'C++', 'Python3'] | 1 |
time-based-key-value-store | [Swift][Answer for the interview] | swiftanswer-for-the-interview-by-vilkis-4k8v | Code\n\n\nclass TimeMap {\n\n var dict = [String: [(Int, String)]]()\n\n init() {\n \n }\n func set(_ key: String, _ value: String, _ timesta | ViLkiS | NORMAL | 2023-10-16T07:42:52.374859+00:00 | 2023-10-30T07:05:50.886974+00:00 | 125 | false | # Code\n```\n\nclass TimeMap {\n\n var dict = [String: [(Int, String)]]()\n\n init() {\n \n }\n func set(_ key: String, _ value: String, _ timestamp: Int) {\n dict[key, default: []].append((timestamp, value))\n }\n \n func get(_ key: String, _ timestamp: Int) -> String {\n guar... | 3 | 0 | ['Swift'] | 1 |
time-based-key-value-store | OOP And Binary search | Beats 100% C++ Users in time | oop-and-binary-search-beats-100-c-users-rj768 | Intuition\n Describe your first thoughts on how to solve this problem. \n When I read that the inserted timestamps are always greater than the previous, and als | abdelazizSalah | NORMAL | 2023-09-13T12:24:38.976715+00:00 | 2023-09-13T12:34:35.137974+00:00 | 254 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n* When I read that the inserted timestamps are always greater than the previous, and also we are looking for the obejct with certain timestamp, I realized that it is a binary search problem.\n# Approach\n<!-- Describe your approach to sol... | 3 | 0 | ['Hash Table', 'String', 'Binary Search', 'Design', 'C++'] | 0 |
time-based-key-value-store | Time Based Key-Value Store | time-based-key-value-store-by-rissabh361-30jj | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | rissabh361 | NORMAL | 2023-09-05T17:48:32.754208+00:00 | 2023-09-05T17:48:32.754231+00:00 | 281 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['C++'] | 0 |
time-based-key-value-store | Easy C++ Solution | easy-c-solution-by-vishnu_kumar-srivasta-weqh | Intuition\n Describe your first thoughts on how to solve this problem. \nThe code implements a time-based key-value store using the TimeMap class. The set funct | vishnu_Kumar-Srivastava | NORMAL | 2023-06-16T04:26:16.917575+00:00 | 2023-06-16T04:26:16.917595+00:00 | 832 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe code implements a time-based key-value store using the TimeMap class. The set function is used to store a key-value pair with a corresponding timestamp, and the get function is used to retrieve the value associated with a key at a spe... | 3 | 0 | ['C++'] | 0 |
time-based-key-value-store | Solution | solution-by-deleted_user-nnm2 | C++ []\nclass TimeMap {\npublic:\n TimeMap()\n {\n }\n void set(const string& key, const string& value, const int timestamp)\n {\n data_[k | deleted_user | NORMAL | 2023-05-17T12:31:34.985653+00:00 | 2023-05-17T13:20:13.753076+00:00 | 1,194 | false | ```C++ []\nclass TimeMap {\npublic:\n TimeMap()\n {\n }\n void set(const string& key, const string& value, const int timestamp)\n {\n data_[key].emplace_back(timestamp, value);\n }\n string get(const string& key, const int timestamp)\n {\n const auto itData = data_.find(key);\n ... | 3 | 0 | ['C++', 'Java', 'Python3'] | 1 |
time-based-key-value-store | Pure BinarySearch | No fancy util functions | pure-binarysearch-no-fancy-util-function-jr2w | Intuition\n Describe your first thoughts on how to solve this problem. \n- whenever we any data which is in sorted order we always need to think about binarySea | napster01 | NORMAL | 2022-12-05T16:14:39.671488+00:00 | 2022-12-05T16:14:39.671533+00:00 | 610 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- whenever we any data which is in sorted order we always need to think about binarySearch\n- here we can see the data we are getting by timestamp, which will always be in increasing order, so that\'s the click.\n\n# Approach\n<!-- Descri... | 3 | 0 | ['Hash Table', 'String', 'Binary Search', 'Design', 'Go'] | 0 |
time-based-key-value-store | [ Python ] ✅✅ Simple Python Solution Using Hashmap | Dictionary 🥳✌👍 | python-simple-python-solution-using-hash-vk5i | If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F\n# Runtime: 1654 ms, faster than 40.05% of Python3 online submissi | ashok_kumar_meghvanshi | NORMAL | 2022-10-08T04:58:46.009968+00:00 | 2022-10-08T04:58:46.010008+00:00 | 684 | false | # If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F\n# Runtime: 1654 ms, faster than 40.05% of Python3 online submissions for Time Based Key-Value Store.\n# Memory Usage: 70 MB, less than 86.19% of Python3 online submissions for Time Based Key-Value Store.\n\n\tclass TimeMap:... | 3 | 0 | ['Python', 'Python3'] | 1 |
time-based-key-value-store | python3 the most efficient and shortest algorithm | python3-the-most-efficient-and-shortest-dabth | ```\nclass TimeMap:\n\n def init(self):\n self.d={}\n\n def set(self, key: str, value: str, timestamp: int) -> None:\n self.d[(key,timestamp | benon | NORMAL | 2022-10-06T21:57:40.535791+00:00 | 2022-10-06T21:57:40.535826+00:00 | 838 | false | ```\nclass TimeMap:\n\n def __init__(self):\n self.d={}\n\n def set(self, key: str, value: str, timestamp: int) -> None:\n self.d[(key,timestamp)]=value\n\n def get(self, key: str, timestamp: int) -> str:\n for i in range(timestamp,0,-1):\n if (key,i) in self.d:\n ... | 3 | 0 | ['Python', 'Python3'] | 4 |
time-based-key-value-store | 📌5 line code✅|Faster than 96.95% of CPP online submissions | HashMap | Binary Search | Map | 5-line-codefaster-than-9695-of-cpp-onlin-kvxk | \nclass TimeMap {\npublic:\n unordered_map<string,map<int,string>> umap;\n TimeMap() {}\n void set(string key, string value, int timestamp) {\n | code_hobby_2404 | NORMAL | 2022-10-06T18:24:53.861594+00:00 | 2022-10-06T18:27:57.626510+00:00 | 337 | false | ```\nclass TimeMap {\npublic:\n unordered_map<string,map<int,string>> umap;\n TimeMap() {}\n void set(string key, string value, int timestamp) {\n umap[key].insert({-1*timestamp,value});\n }\n string get(string key, int timestamp) {\n if(!umap.count(key)) return "";\n auto it=umap[ke... | 3 | 0 | ['Binary Tree', 'C++'] | 2 |
time-based-key-value-store | TreeMap | Simple | 85% Faster | treemap-simple-85-faster-by-evil_bios-akir | java\nclass TimeMap {\n Map <String, TreeMap<Integer, String>> map;\n public TimeMap() {\n map = new HashMap<>();\n }\n \n public void set | evil_bios | NORMAL | 2022-10-06T16:55:06.713087+00:00 | 2022-10-06T16:55:06.713131+00:00 | 369 | false | ```java\nclass TimeMap {\n Map <String, TreeMap<Integer, String>> map;\n public TimeMap() {\n map = new HashMap<>();\n }\n \n public void set(String key, String value, int timestamp) {\n if (map.get(key) == null)\n map.put(key, new TreeMap<>());\n map.get(key).put(timestamp,... | 3 | 0 | ['Tree', 'Java'] | 0 |
time-based-key-value-store | Easiest soln | Java | TreeMap | | easiest-soln-java-treemap-by-amandubeyy-ad2r | \nclass TimeMap {\n\n HashMap<String, TreeMap<Integer, String>> map;\n \n public TimeMap() {\n map = new HashMap<>();\n }\n \n public v | amandubeyy | NORMAL | 2022-10-06T13:36:09.543771+00:00 | 2022-10-06T13:36:09.543806+00:00 | 314 | false | ```\nclass TimeMap {\n\n HashMap<String, TreeMap<Integer, String>> map;\n \n public TimeMap() {\n map = new HashMap<>();\n }\n \n public void set(String key, String value, int timestamp) {\n \n if(map.containsKey(key) == false){\n map.put(key, new TreeMap<>());\n ... | 3 | 0 | ['Tree', 'Java'] | 0 |
time-based-key-value-store | c++ solution using map, simple and easy to understand | c-solution-using-map-simple-and-easy-to-zixzj | \nclass TimeMap {\n map<string, map<int, string, std::greater<int>>> dictionary;\npublic:\n TimeMap() {\n \n }\n \n void set(string key, s | MegaBeta | NORMAL | 2022-10-06T12:39:43.967351+00:00 | 2022-10-06T12:39:43.967392+00:00 | 599 | false | ```\nclass TimeMap {\n map<string, map<int, string, std::greater<int>>> dictionary;\npublic:\n TimeMap() {\n \n }\n \n void set(string key, string value, int timestamp) {\n dictionary[key][timestamp] = value;\n }\n \n string get(string key, int timestamp) {\n auto it = dict... | 3 | 0 | [] | 0 |
time-based-key-value-store | Java | HashMap+TreeMap | Searching | Easy to Understand | java-hashmaptreemap-searching-easy-to-un-grmf | \nclass TimeMap {\n HashMap<String, TreeMap<Integer, String>> map;\n public TimeMap() {\n map = new HashMap<String, TreeMap<Integer, String>>();\n | Suyash_25 | NORMAL | 2022-10-06T10:42:27.947566+00:00 | 2022-10-06T10:46:02.871419+00:00 | 546 | false | ```\nclass TimeMap {\n HashMap<String, TreeMap<Integer, String>> map;\n public TimeMap() {\n map = new HashMap<String, TreeMap<Integer, String>>();\n }\n\n public void set(String key, String value, int timestamp) {\n if (!map.containsKey(key)) map.put(key, new TreeMap<Integer, String>());\n ... | 3 | 0 | ['Tree', 'Java'] | 0 |
time-based-key-value-store | C++ || Map Implementation || Priority Queue | c-map-implementation-priority-queue-by-a-u1h9 | \nclass TimeMap {\npublic:\n unordered_map<string,string> mp;\n unordered_map<string,priority_queue<int>> mp2;\n \n TimeMap() {\n mp.clear(); | anujverma11062002 | NORMAL | 2022-10-06T08:52:25.583166+00:00 | 2022-10-06T08:52:46.666386+00:00 | 1,155 | false | ```\nclass TimeMap {\npublic:\n unordered_map<string,string> mp;\n unordered_map<string,priority_queue<int>> mp2;\n \n TimeMap() {\n mp.clear();\n mp2.clear();\n }\n \n void set(string key, string value, int timestamp) {\n string s = key + "_" + to_string(timestamp);\n m... | 3 | 0 | ['C', 'Heap (Priority Queue)', 'C++'] | 1 |
time-based-key-value-store | Java TreeMap Implementation | java-treemap-implementation-by-fllght-1kwm | Java\njava\nprivate final Map<String, TreeMap<Integer, String>> store;\n\n public TimeMap() {\n store = new HashMap<>();\n }\n\n public void set | FLlGHT | NORMAL | 2022-10-06T07:35:44.203451+00:00 | 2022-10-06T07:35:54.763443+00:00 | 209 | false | ##### Java\n```java\nprivate final Map<String, TreeMap<Integer, String>> store;\n\n public TimeMap() {\n store = new HashMap<>();\n }\n\n public void set(String key, String value, int timestamp) {\n if (!store.containsKey(key))\n store.put(key, new TreeMap<>());\n\n store.get(ke... | 3 | 0 | ['Java'] | 1 |
count-alternating-subarrays | Simple C++ Solution || Counting | simple-c-solution-counting-by-adityaraj_-4jho | Approach\n1. The method iterates through the array nums starting from index 1.\nAt each iteration, it checks if the current element is equal to the previous ele | AdityaRaj_cpp | NORMAL | 2024-03-31T04:18:30.835794+00:00 | 2024-03-31T04:39:45.582834+00:00 | 2,109 | false | # Approach\n1. The method iterates through the array nums starting from index 1.\nAt each iteration, it checks if the current element is equal to the previous element.\n2. If they are equal, it means the alternating pattern is broken, so it calculates the length of the subarray and adds the count of alternating subarra... | 16 | 2 | ['Sliding Window', 'C++'] | 3 |
count-alternating-subarrays | Python | DP | Easy to understand | python-dp-easy-to-understand-by-coder191-h26e | \nclass Solution:\n def countAlternatingSubarrays(self, nums: List[int]) -> int:\n n = len(nums)\n dp = [1] * n\n \n for i in ran | Coder1918 | NORMAL | 2024-03-31T04:04:49.703112+00:00 | 2024-03-31T04:04:49.703137+00:00 | 928 | false | ```\nclass Solution:\n def countAlternatingSubarrays(self, nums: List[int]) -> int:\n n = len(nums)\n dp = [1] * n\n \n for i in range(1, n):\n if nums[i-1] != nums[i]:\n dp[i] = dp[i - 1] + 1\n \n return sum(dp)\n``` | 12 | 0 | ['Dynamic Programming', 'Python3'] | 1 |
count-alternating-subarrays | Simple java solution | simple-java-solution-by-siddhant_1602-wdha | Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Code\n\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n | Siddhant_1602 | NORMAL | 2024-03-31T04:18:56.665673+00:00 | 2024-03-31T04:18:56.665694+00:00 | 1,058 | false | # Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n long maxi=0;\n int i=0,j=0;\n for(i=0,j=0;i<nums.length-1;i++)\n {\n if(nums[i]==nums[i+1])\n {\n ... | 11 | 1 | ['Java'] | 2 |
count-alternating-subarrays | Java Solution - Sliding Window | java-solution-sliding-window-by-makubex7-b375 | Intuition\n Describe your first thoughts on how to solve this problem. \n- Compare the current element with the previous element. \n- If they are different, inc | makubex74 | NORMAL | 2024-03-31T04:46:31.376653+00:00 | 2024-03-31T05:47:34.060207+00:00 | 443 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Compare the current element with the previous element. \n- If they are different, increase the length of the window. \n- If they are same, then calculate the number of subarrays that can be created.\n# Approach\n<!-- Describe your appro... | 10 | 0 | ['Two Pointers', 'Sliding Window', 'Java'] | 1 |
count-alternating-subarrays | Python 3 || 7 lines, simple count || T/S: 98% / 93% | python-3-7-lines-simple-count-ts-98-93-b-if5t | \nclass Solution:\n def countAlternatingSubarrays(self, nums: List[int]) -> int:\n\n ans, curr = 0, nums[0]\n\n for num in nums:\n | Spaulding_ | NORMAL | 2024-03-31T16:25:42.773859+00:00 | 2024-06-12T05:26:18.619355+00:00 | 509 | false | ```\nclass Solution:\n def countAlternatingSubarrays(self, nums: List[int]) -> int:\n\n ans, curr = 0, nums[0]\n\n for num in nums:\n prev, curr = curr, num\n \n if prev^curr == 0: cnt = 0\n\n cnt+= 1\n\n ans+= cnt\n \n return ans\n```\n[ht... | 8 | 0 | ['Python3'] | 1 |
count-alternating-subarrays | C++ | Sliding Window | Counting | Explained | c-sliding-window-counting-explained-by-d-gki8 | Approach\nWe know our subarray that contribute to answer must now have any adjacent values same. \n\tSo use a variable prev to store the last index whem element | dholiyoo | NORMAL | 2024-03-31T04:03:54.435093+00:00 | 2024-03-31T04:03:54.435116+00:00 | 1,254 | false | **Approach**\nWe know our subarray that contribute to answer must now have any adjacent values same. \n\tSo use a variable `prev` to store the last index whem element is different. Now iterate over the array if adjacent are not same, update `answer += (i - prev)`. If same set `prev = current` Index and iterate ahead. ... | 7 | 0 | ['Counting', 'C++'] | 2 |
count-alternating-subarrays | Simple Java solution - Two pointer/Slding Window Time: O(n), Space: O(1) | simple-java-solution-two-pointerslding-w-3yaz | Intuition\n Describe your first thoughts on how to solve this problem. \nStart from the begining and find the longest alternating Subarray. For an array of size | piyush_tilokani | NORMAL | 2024-03-31T06:02:37.989762+00:00 | 2024-03-31T06:02:37.989797+00:00 | 95 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nStart from the begining and find the longest alternating Subarray. For an array of size n, there are (1+2+3+...+n) **(i.e. sum of first n natural numbers)** subarrays that exist \n\nThat means for every alternating subarray of size `windo... | 6 | 0 | ['Java'] | 0 |
count-alternating-subarrays | SIMPLE SLIDING WINDOW SOLUTION | simple-sliding-window-solution-by-qbasic-0fv1 | Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Code\n\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& num | calm_porcupine | NORMAL | 2024-03-31T04:01:48.535223+00:00 | 2024-04-01T07:01:40.377890+00:00 | 1,656 | false | # Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n long long ans = 0;\n int prev = -1;\n int j = 0;\n for(int i = 0;i<nums.size();i++){\n if(nums[i]==pr... | 5 | 0 | ['Sliding Window', 'C++'] | 1 |
count-alternating-subarrays | Sliding window approach, easy to understand. | sliding-window-approach-easy-to-understa-y6f9 | Intuition\n Describe your first thoughts on how to solve this problem. \nwhen it comes to subarrays problem, i usually think of sliding window.\n# Approach\n De | kietng04 | NORMAL | 2024-04-03T06:58:20.196686+00:00 | 2024-04-03T06:58:20.196723+00:00 | 382 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwhen it comes to subarrays problem, i usually think of sliding window.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nwe definitely have nums.size() into our result first.\nif (nums[r] == nums[r-1]) then we have r -... | 4 | 0 | ['Two Pointers', 'Sliding Window', 'C++'] | 1 |
count-alternating-subarrays | aao solution samgho hindi me|| unique concept|| Bhut aasan h | aao-solution-samgho-hindi-me-unique-conc-13ew | Intuition\n Describe your first thoughts on how to solve this problem. \n# OR CODERS kya haal chaal h\nbhut hi aasan q puch liya \nmaine is question ko jab dekh | Aman028 | NORMAL | 2024-03-31T06:27:33.780612+00:00 | 2024-03-31T06:27:33.780642+00:00 | 78 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n# OR CODERS kya haal chaal h\nbhut hi aasan q puch liya \nmaine is question ko jab dekha toh hume number of alternating subarrays nikalana h toh maine socha kyu n hum ye concept lagaye\n```\nconcept h\nnums = [0,1,1,1]\n- agar hume index=... | 4 | 0 | ['Two Pointers', 'C++'] | 0 |
count-alternating-subarrays | Easy - Linear time and constant space | easy-linear-time-and-constant-space-by-s-wzdy | Intuition\n Describe your first thoughts on how to solve this problem. \nTo count the number of alternating subarrays in the given list of numbers, we can itera | slippinnjimmy | NORMAL | 2024-03-31T04:43:51.848586+00:00 | 2024-03-31T04:43:51.848605+00:00 | 295 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo count the number of alternating subarrays in the given list of numbers, we can iterate through the list while maintaining a count of consecutive alternating elements. Whenever we encounter a non-alternating element, we calculate the nu... | 4 | 0 | ['Python3'] | 1 |
count-alternating-subarrays | BEATS 100% || LEARN INTUITION BUILDING || DETAILED | beats-100-learn-intuition-building-detai-v8kz | Intuition\n Describe your first thoughts on how to solve this problem. \nApproaching problems like counting alternating subarrays involves thinking like a detec | Abhishekkant135 | NORMAL | 2024-03-31T04:11:20.093813+00:00 | 2024-03-31T04:11:20.093833+00:00 | 364 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nApproaching problems like counting alternating subarrays involves thinking like a detective. Imagine scanning the array for "clues" - elements differing by 1 alternately. A sliding window helps us focus on potential subarrays as we move a... | 4 | 0 | ['Array', 'Java'] | 1 |
count-alternating-subarrays | ✅ Java Solution | Easy to understand | java-solution-easy-to-understand-by-hars-xtzo | CODE\nJava []\npublic long countAlternatingSubarrays(int[] nums) {\n\tlong res = 0l;\n\tint n = nums.length;\n\tint i=0, j=1;\n\twhile(j < n){\n\t\tif(nums[j] = | Harsh__005 | NORMAL | 2024-03-31T04:02:22.677725+00:00 | 2024-03-31T04:02:22.677759+00:00 | 362 | false | ## **CODE**\n```Java []\npublic long countAlternatingSubarrays(int[] nums) {\n\tlong res = 0l;\n\tint n = nums.length;\n\tint i=0, j=1;\n\twhile(j < n){\n\t\tif(nums[j] == nums[j-1]) {\n\t\t\ti = j;\n\t\t} else {\n\t\t\tres += (j-i);\n\t\t}\n\t\tj++;\n\t}\n\treturn res+n;\n}\n``` | 4 | 0 | ['Java'] | 1 |
count-alternating-subarrays | Simple math solution JS || O(n) || O(1) | simple-math-solution-js-on-o1-by-dhnbrok-418u | Approach\n Describe your approach to solving the problem. \n- Declare a variable to count the consecutive number\n- If nums[i] !== nums[i - 1], increase the con | dhnbroken | NORMAL | 2024-04-02T04:35:04.147508+00:00 | 2024-04-02T04:38:59.801172+00:00 | 200 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\n- Declare a variable to count the consecutive number\n- If nums[i] !== nums[i - 1], increase the consecutive by 1\n- Else set consecutive = 1\n- Count will be increased by consecutive for each loop\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add yo... | 3 | 0 | ['JavaScript'] | 0 |
count-alternating-subarrays | Dynamic Programming🔥🔥 || Python | dynamic-programming-python-by-vinaypabba-fjsm | \n\n# Code\n\nclass Solution:\n def countAlternatingSubarrays(self, nums: List[int]) -> int:\n #dp=[1]*len(nums)\n s=1\n curr=1\n | vinaypabba48 | NORMAL | 2024-03-31T07:25:36.399676+00:00 | 2024-03-31T07:25:36.399698+00:00 | 32 | false | \n\n# Code\n```\nclass Solution:\n def countAlternatingSubarrays(self, nums: List[int]) -> int:\n #dp=[1]*len(nums)\n s=1\n curr=1\n for i in range(1,len(nums)):\n if nums[i]!=nums[i-1]:\n curr=curr+1\n s+=curr\n else:\n c... | 3 | 0 | ['Dynamic Programming', 'Python3'] | 0 |
count-alternating-subarrays | Beats 100% || Easy Soln - n*(n+1)/2 | beats-100-easy-soln-nn12-by-hina_bediya-jex3 | Intuition\n\n# Approach\n# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(1)\n# Code\n\nclass Solution {\n public long countAlternatingSubarray | Hina_Bediya | NORMAL | 2024-03-31T04:31:06.560051+00:00 | 2024-03-31T04:47:07.050947+00:00 | 219 | false | # Intuition\n\n# Approach\n# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(1)\n# Code\n```\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n if(nums.length==1)return 1;\n long ans=0;\n int i=0,j=0;\n \n while(j<nums.length-1){\n \n ... | 3 | 0 | ['Math', 'Sliding Window', 'Java'] | 1 |
count-alternating-subarrays | C++ Solution beats 100% during contest 31-03-2024 | c-solution-beats-100-during-contest-31-0-pmti | Intuition\n\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Tim | Edwards310 | NORMAL | 2024-03-31T04:17:24.701325+00:00 | 2024-03-31T04:17:24.701351+00:00 | 25 | false | # Intuition\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time compl... | 3 | 0 | ['C++'] | 1 |
count-alternating-subarrays | Beats 100% user || very easy to understand | beats-100-user-very-easy-to-understand-b-ovkg | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Tamradhwaj | NORMAL | 2024-03-31T04:08:53.386272+00:00 | 2024-03-31T04:08:53.386291+00:00 | 187 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['C++'] | 1 |
count-alternating-subarrays | Easiest Java Solution || Step by step | easiest-java-solution-step-by-step-by-1a-z9z6 | \n\n# Code\n\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n long count=1;\n int ref=0; // changed when we encounter | 1asthakhushi1 | NORMAL | 2024-03-31T04:06:38.387468+00:00 | 2024-03-31T04:28:06.176829+00:00 | 846 | false | \n\n# Code\n```\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n long count=1;\n int ref=0; // changed when we encounter that digit at previous index is same as digit at current index\n int prev=nums[0];\n for(int i=1;i<nums.length;i++){\n if(prev!=nums... | 3 | 0 | ['Java'] | 1 |
count-alternating-subarrays | Evaluate n(n + 1)/2 for each non repeating subarray | evaluate-nn-12-for-each-non-repeating-su-qayt | Intuition\n- Each set of equal value divide the array into two separate array\n- On each non repeating subarray of size n, then the total number of subarrays po | kreakEmp | NORMAL | 2024-03-31T04:01:33.951009+00:00 | 2024-03-31T04:01:33.951038+00:00 | 341 | false | # Intuition\n- Each set of equal value divide the array into two separate array\n- On each non repeating subarray of size n, then the total number of subarrays possible = n(n+1)/2\n- But in the solution, in place of calculating n(n + 1)/2, we can iterate and add all numbers from 1 to n that is equal to n(n+1)/2\n\n# Ap... | 3 | 0 | ['C++'] | 1 |
count-alternating-subarrays | C++ 100% Faster | Sliding Window--->O(1) Space | Line By Line Detailed Explanation | c-100-faster-sliding-window-o1-space-lin-5j2f | \n\n# Intuition\n- An alternating subarray has no consecutive elements with the same value. We can iterate through the array and keep track of the current subar | VYOM_GOYAL | NORMAL | 2024-03-31T04:00:45.407175+00:00 | 2024-03-31T04:04:22.807087+00:00 | 138 | false | \n\n# Intuition\n- An alternating subarray has no consecutive elements with the same value. We can iterate through the array and keep track of the current subarray length while checking if adjacent ... | 3 | 1 | ['Array', 'Sliding Window', 'C++'] | 2 |
count-alternating-subarrays | ✅💯🔥Simple Code🚀📌|| 🔥✔️Easy to understand🎯 || 🎓🧠Beats 100%🔥|| Beginner friendly💀💯 | simple-code-easy-to-understand-beats-100-78nn | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | atishayj4in | NORMAL | 2024-07-31T20:12:55.043138+00:00 | 2024-08-01T19:26:46.482244+00:00 | 101 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | 0 | ['Array', 'Math', 'C', 'Python', 'C++', 'Java'] | 0 |
count-alternating-subarrays | O(N) time and easy simple solution. | on-time-and-easy-simple-solution-by-21bc-brdc | Intuition\nCount number of continueous alternating number and all possible subarray that can be made using it. \n\n# Approach\nuse long to store the and take ca | 21bcs069 | NORMAL | 2024-04-03T07:55:25.792996+00:00 | 2024-04-03T07:55:25.793031+00:00 | 11 | false | # Intuition\nCount number of continueous alternating number and all possible subarray that can be made using it. \n\n# Approach\nuse long to store the and take care of overflow , count alternating 0 and 1 continueous if it break add answer to it and then at last add the last the left subarray. which i did after complet... | 2 | 0 | ['Array', 'Math', 'C++'] | 0 |
count-alternating-subarrays | Python || O(1) Space🔥🔥|| DP | python-o1-space-dp-by-vinaypabba48-y88v | \n# Approach 1 (Uses O(N) Time)\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity: O(N)\n Add your time complexity here, e.g. | vinaypabba48 | NORMAL | 2024-03-31T11:19:50.073596+00:00 | 2024-03-31T11:19:50.073616+00:00 | 22 | false | \n# Approach 1 (Uses O(N) Time)\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def countAltern... | 2 | 0 | ['Dynamic Programming', 'Python3'] | 0 |
count-alternating-subarrays | 🔥Easy to Understand | Clean Code | C++ | | easy-to-understand-clean-code-c-by-antim-aq6p | Code\n\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n long long left = 0;\n long long res = nums.size( | Antim_Sankalp | NORMAL | 2024-03-31T10:45:58.956021+00:00 | 2024-04-06T12:58:10.058843+00:00 | 215 | false | # Code\n```\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n long long left = 0;\n long long res = nums.size();\n long long i;\n for (i = 1; i < nums.size(); i++) \n {\n if (nums[i] != nums[i - 1])\n continue;\n\n ... | 2 | 0 | ['C++'] | 0 |
count-alternating-subarrays | simple | easy java solution | simple-easy-java-solution-by-rini03-v018 | Complexity\n- Time complexity:\nlinear time complexity O(n)\n\n- Space complexity:\nconstant space complexity O(1)\n\n# Code\n\nclass Solution {\n public lon | rini03 | NORMAL | 2024-03-31T07:48:40.441396+00:00 | 2024-03-31T07:51:01.996591+00:00 | 184 | false | # Complexity\n- Time complexity:\nlinear time complexity O(n)\n\n- Space complexity:\nconstant space complexity O(1)\n\n# Code\n```\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n long count = 1;\n int start = 0; \n int end = 0;\n\n for (int i = 1; i < nums.lengt... | 2 | 0 | ['Two Pointers', 'Sliding Window', 'Java'] | 1 |
count-alternating-subarrays | Dynamic programming|| Beats 100% | dynamic-programming-beats-100-by-niteshj-qunz | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | niteshjha01 | NORMAL | 2024-03-31T04:15:06.237790+00:00 | 2024-03-31T04:15:06.237818+00:00 | 367 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | 0 | ['Dynamic Programming', 'Java'] | 3 |
count-alternating-subarrays | Simple C++ || Beginner Friendly || Beats 100% | simple-c-beginner-friendly-beats-100-by-advcj | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\n1. Initialize variables n to store the size of the input array nums, cn | dibendu07 | NORMAL | 2024-03-31T04:03:40.193782+00:00 | 2024-03-31T04:03:40.193814+00:00 | 10 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n1. Initialize variables `n` to store the size of the input array `nums`, `cnt` to store the count of alternating subarrays, and `len` to store the length of the current alternating subarray, initially set to 1.\n\n2. Itera... | 2 | 0 | ['C++'] | 0 |
count-alternating-subarrays | Beats 100% | Easy Java Solution | ✅✅✅✅ | beats-100-easy-java-solution-by-ikapoor1-yp72 | Please Upvote \u2705\u2705\n\n# Code\n\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n long ans = 1;\n int count = 0; | Ikapoor123 | NORMAL | 2024-03-31T04:02:44.726336+00:00 | 2024-03-31T04:16:12.039542+00:00 | 136 | false | # Please Upvote \u2705\u2705\n\n# Code\n```\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n long ans = 1;\n int count = 0;\n \n for(int i=1;i<nums.length;i++){\n ans++;\n if(nums[i]+nums[i-1] == 1) {\n ans+=(i-count);\n ... | 2 | 0 | ['Math', 'Counting', 'Java'] | 0 |
count-alternating-subarrays | O(1) space and O(n) time - DP Approach Java | o1-space-and-on-time-dp-approach-java-by-m16r | IntuitionApproachDp approach using two variablesComplexity
Time complexity: O(n)
Space complexity: O(1)
Code | dhivya6613 | NORMAL | 2025-02-03T13:31:37.558455+00:00 | 2025-02-03T13:31:37.558455+00:00 | 67 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
Dp approach using two variables
# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space c... | 1 | 0 | ['Dynamic Programming', 'Java'] | 0 |
count-alternating-subarrays | Easy 5 lines C++ Solution | easy-5-lines-c-solution-by-pushuu-uwvq | Intuition\n Describe your first thoughts on how to solve this problem. \nYou are tasked with counting the number of alternating subarrays in an array, where alt | pushuu | NORMAL | 2024-09-14T16:24:43.087508+00:00 | 2024-09-14T16:24:43.087527+00:00 | 5 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nYou are tasked with counting the number of alternating subarrays in an array, where alternating subarrays are defined as subarrays whose adjacent elements alternate between two different values. The key insight is that if two adjacent ele... | 1 | 0 | ['Array', 'Math', 'C++'] | 0 |
count-alternating-subarrays | Beats 98% | beats-98-by-mdsrrkhan-qkvf | \n# Code\n\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n int prev = nums[0];\n long count = 1;\n long ans = | mdsrrkhan | NORMAL | 2024-08-08T13:55:40.263165+00:00 | 2024-08-08T13:55:40.263199+00:00 | 7 | false | \n# Code\n```\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n int prev = nums[0];\n long count = 1;\n long ans = 0;\n for(int i = 1;i<nums.length;i++){\n if(nums[i]==1-prev){\n count++; \n }else{\n ans = ans+... | 1 | 0 | ['Java'] | 0 |
count-alternating-subarrays | ONE FOR LOOP!!!! | one-for-loop-by-hitesh__raj-omwr | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Hitesh__Raj | NORMAL | 2024-08-03T05:05:24.616719+00:00 | 2024-08-03T05:05:24.616757+00:00 | 43 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['Python'] | 0 |
count-alternating-subarrays | SIMPLE SOLUTION!!!(PYTHON) | simple-solutionpython-by-hitesh__raj-yuzj | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Hitesh__Raj | NORMAL | 2024-08-03T05:04:30.506690+00:00 | 2024-08-03T05:04:30.506728+00:00 | 12 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['Python'] | 0 |
count-alternating-subarrays | Simple Maths || C++ Solution | simple-maths-c-solution-by-nehagupta_09-p5iv | \n\n# Code\n\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n long long ans=0;\n long long len=1;\n | NehaGupta_09 | NORMAL | 2024-07-07T06:22:37.936487+00:00 | 2024-07-07T06:22:37.936517+00:00 | 0 | false | \n\n# Code\n```\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n long long ans=0;\n long long len=1;\n for(int i=1;i<nums.size();i++)\n {\n if(nums[i]!=nums[i-1])\n {\n len++;\n }\n else\n ... | 1 | 0 | ['Array', 'Math', 'C++'] | 0 |
count-alternating-subarrays | Fastest and Easiest Answer with Explanation || Dynamic Programming || Beats 98.92% of users | fastest-and-easiest-answer-with-explanat-yr0b | Approach\n Describe your approach to solving the problem. \nSolving the problem by using Dynamic Programming\n# Complexity\n- Time complexity:O(n)\n Add your ti | Saidakbar_Zokirovich | NORMAL | 2024-04-19T12:11:53.639501+00:00 | 2024-04-19T12:12:42.104176+00:00 | 38 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\nSolving the problem by using Dynamic Programming\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n// Use Dynam... | 1 | 0 | ['Dynamic Programming', 'Go'] | 1 |
count-alternating-subarrays | Easy Solution using Dynamic Programming concept | easy-solution-using-dynamic-programming-5qp69 | \n# Approach\n Describe your approach to solving the problem. \nDynamic Programming\nConcept is same but without array adding them up right away\n# Complexity\n | kmuhammadjon | NORMAL | 2024-04-19T11:36:26.312844+00:00 | 2024-04-19T11:36:26.312874+00:00 | 21 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nDynamic Programming\nConcept is same but without array adding them up right away\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n... | 1 | 0 | ['Array', 'Math', 'Dynamic Programming', 'Go'] | 1 |
count-alternating-subarrays | Simple C++ Solution || Counting || Easy To Understand | simple-c-solution-counting-easy-to-under-ji9f | \n# Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(1)\n Add your space complexity here, e.g. O(n) \n\n# | Akshay1054 | NORMAL | 2024-04-04T15:48:27.315645+00:00 | 2024-04-04T15:48:27.315668+00:00 | 9 | false | \n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n int n=nums.size();\n l... | 1 | 0 | ['Sliding Window', 'Counting', 'C++'] | 0 |
count-alternating-subarrays | Easy and basic approach||O(n)||Beats 98% | easy-and-basic-approachonbeats-98-by-ank-j19p | Intuition\n\nIntuition:\nThe code aims to count the number of alternating subarrays within the given array nums. An alternating subarray is defined as a contigu | Ankit--kr | NORMAL | 2024-04-03T21:00:45.832728+00:00 | 2024-04-03T21:00:45.832756+00:00 | 19 | false | # Intuition\n\nIntuition:\nThe code aims to count the number of alternating subarrays within the given array nums. An alternating subarray is defined as a contiguous subarray where adjacent elements have different values.\n\n# Approach\nInitialize variables right and left to track the boundaries of the current subarray... | 1 | 0 | ['Java'] | 0 |
count-alternating-subarrays | AVS | avs-by-vishal1431-uss8 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Vishal1431 | NORMAL | 2024-04-02T22:34:42.048070+00:00 | 2024-04-02T22:34:42.048087+00:00 | 2 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['C++'] | 0 |
count-alternating-subarrays | C++ | Easy | c-easy-by-ak200199-29bo | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | AK200199 | NORMAL | 2024-04-02T09:12:21.238936+00:00 | 2024-04-02T09:12:21.238959+00:00 | 1 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['C++'] | 0 |
count-alternating-subarrays | Dynamic Programming: Count Alternating Subarrays Explained | dynamic-programming-count-alternating-su-8dkw | Intuition\nThe problem asks to count the number of alternating subarrays in the given array. An alternating subarray is defined as a contiguous subarray where t | pro_donkey | NORMAL | 2024-04-01T09:41:28.658381+00:00 | 2024-04-01T09:41:28.658401+00:00 | 94 | false | # Intuition\nThe problem asks to count the number of alternating subarrays in the given array. An alternating subarray is defined as a contiguous subarray where the elements alternate between being even and odd.\n\n# Approach\nTo solve this problem, we can use dynamic programming. We\'ll maintain a dynamic programming ... | 1 | 0 | ['Python3'] | 0 |
count-alternating-subarrays | C++ || sliding window approach || beats 100% | c-sliding-window-approach-beats-100-by-a-4eb0 | \n# Code\n\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n int l = 0, r = 0, n = nums.size();\n long lo | Adnan_Alkam | NORMAL | 2024-03-31T23:30:20.153292+00:00 | 2024-03-31T23:30:20.153345+00:00 | 39 | false | \n# Code\n```\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n int l = 0, r = 0, n = nums.size();\n long long ans = 0;\n \n while(r < n) {\n if (l != r && nums[r] == nums[r-1]) \n l = r;\n ans += r - l + 1;\n ... | 1 | 0 | ['Two Pointers', 'Sliding Window', 'C++'] | 0 |
count-alternating-subarrays | DP Solution if you need | dp-solution-if-you-need-by-___lovesh1808-vgxc | \n# Complexity\n- Time complexity:\nO(N)\n- Space complexity:\nO(1)\n\n# Code\n\nfunc countAlternatingSubarrays(nums []int) int64 {\n return foo(nums, 0)\n}\ | ___lovesh1808___ | NORMAL | 2024-03-31T20:52:39.360030+00:00 | 2024-03-31T20:52:39.360062+00:00 | 29 | false | \n# Complexity\n- Time complexity:\nO(N)\n- Space complexity:\nO(1)\n\n# Code\n```\nfunc countAlternatingSubarrays(nums []int) int64 {\n return foo(nums, 0)\n}\nfunc foo(nums []int, index int) int64 {\n if index == len(nums) {\n return 0\n }\n var sum int64 = 0\n for i := index; i < len(nums); i +... | 1 | 0 | ['Dynamic Programming', 'Recursion', 'Go'] | 0 |
count-alternating-subarrays | Sliding window , C++ | sliding-window-c-by-code12repeat-d36h | Intuition\nuse sliding window approach\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity:\n Add your time compl | code12repeat | NORMAL | 2024-03-31T18:11:40.948571+00:00 | 2024-03-31T18:11:40.948593+00:00 | 28 | false | # Intuition\nuse sliding window approach\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npubl... | 1 | 0 | ['C++'] | 1 |
count-alternating-subarrays | Super Easy C++ Solution | 4 lines codes | super-easy-c-solution-4-lines-codes-by-_-xsf0 | Code\n\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n if(nums.size() == 1) return 1;\n int i = 0, j = | _White_Rabbit_ | NORMAL | 2024-03-31T17:56:44.379249+00:00 | 2024-03-31T17:56:44.379289+00:00 | 8 | false | # Code\n```\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n if(nums.size() == 1) return 1;\n int i = 0, j = 0;\n long long count = 0;\n while(j < nums.size() ) {\n count += (j-i+1);\n if(j < nums.size() - 1 && nums[j] == nums[j... | 1 | 0 | ['C++'] | 0 |
count-alternating-subarrays | 1st Kotlin Solution | 1st-kotlin-solution-by-user3900b-0w0e | Complexity\n- Time complexity: O(n)\n- Space complexity: O(1)\n\n# Code\n\nclass Solution {\n fun countAlternatingSubarrays(nums: IntArray): Long {\n | user3900b | NORMAL | 2024-03-31T15:22:48.151383+00:00 | 2024-03-31T15:22:48.151402+00:00 | 12 | false | # Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution {\n fun countAlternatingSubarrays(nums: IntArray): Long {\n var result = 1L\n var size = 1\n\n for (i in 1..nums.size-1) {\n size = if (nums[i-1] == nums[i]) 1 else size + 1\n ... | 1 | 0 | ['Kotlin'] | 0 |
count-alternating-subarrays | Simple and easy solution with comments, beats 90% ✅✅ | simple-and-easy-solution-with-comments-b-vl1o | \n\n# Complexity\n- Time complexity: O(N)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n | vaibhav2112 | NORMAL | 2024-03-31T13:43:45.834437+00:00 | 2024-03-31T13:43:45.834474+00:00 | 21 | false | \n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n // long long countAlternatingSubarrays(vector<int>& arr) {\n // int i = 0 , j = 0, n ... | 1 | 0 | ['C++'] | 0 |
count-alternating-subarrays | Simple python3 solution | 766 ms - faster than 100.00% solutions | simple-python3-solution-766-ms-faster-th-xexe | Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n\n# Co | tigprog | NORMAL | 2024-03-31T13:04:14.533701+00:00 | 2024-03-31T13:04:14.533734+00:00 | 23 | false | # Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n``` python3 []\nclass Solution:\n def countAlternatingSubarrays(self, nums: List[int]) -> int:\n result = 0\n ... | 1 | 0 | ['Math', 'Greedy', 'Counting', 'Python3'] | 0 |
count-alternating-subarrays | C++ | Easy and Short Sliding Window Approach | c-easy-and-short-sliding-window-approach-iw77 | Code\n\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n long long ans=0,i=0,j=0,n=nums.size();\n vector< | Shrutika_Pandey | NORMAL | 2024-03-31T10:46:19.369599+00:00 | 2024-03-31T10:46:19.369617+00:00 | 21 | false | # Code\n```\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n long long ans=0,i=0,j=0,n=nums.size();\n vector<int> v;\n while(j<n){\n if(j==0 || nums[j]!=nums[j-1]){\n ans+=j-i+1; //counting all subarrays till the index\n ... | 1 | 0 | ['Two Pointers', 'Sliding Window', 'C++'] | 0 |
count-alternating-subarrays | Simple Java Solution | simple-java-solution-by-rrr34-u7hr | Code\n\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n int l=0;\n int r=0;\n long ans = 0;\n int p = -1 | rrr34 | NORMAL | 2024-03-31T09:40:07.207801+00:00 | 2024-03-31T09:40:07.207833+00:00 | 37 | false | # Code\n```\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n int l=0;\n int r=0;\n long ans = 0;\n int p = -1;\n while(r<nums.length){\n if(nums[r]==p){\n l = r;\n }\n ans += r-l+1;\n p =nums[r];\n ... | 1 | 0 | ['Two Pointers', 'Sliding Window', 'Java'] | 0 |
count-alternating-subarrays | Simple Java Solution | simple-java-solution-by-rrr34-u73x | Code\n\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n int l=0;\n int r=0;\n long ans = 0;\n int p = -1 | rrr34 | NORMAL | 2024-03-31T09:40:04.995860+00:00 | 2024-03-31T09:40:04.995889+00:00 | 17 | false | # Code\n```\nclass Solution {\n public long countAlternatingSubarrays(int[] nums) {\n int l=0;\n int r=0;\n long ans = 0;\n int p = -1;\n while(r<nums.length){\n if(nums[r]==p){\n l = r;\n }\n ans += r-l+1;\n p =nums[r];\n ... | 1 | 0 | ['Two Pointers', 'Sliding Window', 'Java'] | 0 |
count-alternating-subarrays | VERY EASY C++ SOLUTION || Single FOR loop || Beginner's Friendly | very-easy-c-solution-single-for-loop-beg-96nu | Intuition\nTo count the number of alternating subarrays within the given array, we iterate through the array, keeping track of the length of alternating subarra | ishajangir2002 | NORMAL | 2024-03-31T09:08:06.412643+00:00 | 2024-03-31T09:08:06.412678+00:00 | 31 | false | # Intuition\nTo count the number of alternating subarrays within the given array, we iterate through the array, keeping track of the length of alternating subarrays encountered so far. Whenever we encounter a different element compared to the previous one, we increase the length of the alternating subarray. When the el... | 1 | 0 | ['C++'] | 0 |
count-alternating-subarrays | Simply counting number of subarrays || 100% Beat || O(n) solution || C++ code | simply-counting-number-of-subarrays-100-erdsp | Keep counting number of subarrays ending at index i and keep adding to the answer. As soon as we get same adjacent number bring the counter back to 1.\n\nTime C | just_csk | NORMAL | 2024-03-31T08:52:49.220761+00:00 | 2024-03-31T09:02:37.009566+00:00 | 2 | false | **Keep counting number of subarrays ending at index i and keep adding to the answer. As soon as we get same adjacent number bring the counter back to 1.**\n\n**Time Complexity : O(n)\nSpace Complexity : O(1)**\n\n# Code\n```\nclass Solution {\npublic:\n long long countAlternatingSubarrays(vector<int>& nums) {\n ... | 1 | 0 | ['Array', 'Counting', 'C++'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.