id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n long long count = 0;\n unordered_set<string> sett(ideas.begin(),ideas.end());\n vector<vector<int>> vec(26,vector<int>(26,0));\n for(int i=0;i<ideas.size();i++){\n string word=ideas[i];\n char ori=word[0];\n for(char ch='a';ch<='z';ch++){\n word[0]=ch;\n if(sett.find(word)==sett.end()){\n vec[ori-'a'][ch-'a']++;\n }\n }\n }\n long long ans=0;\n for(int i = 0; i < 26; i++){\n for(int j = 0; j < 26; j++){\n if(i == j) continue; \n ans += (vec[i][j] * vec[j][i]);\n }\n }\n return ans;\n}\n\n \n \n};\n",
"memory": "88100"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_set<string> mp;\n long long dp[26][26]={0};\n long long res=0;\n for(auto& el: ideas)\n {\n mp.insert(el);\n }\n\n for(int i=0; i<ideas.size(); i++)\n {\n char c=ideas[i][0];\n \n for(char j='a'; j<='z'; j++)\n {\n if(j==c) continue;\n ideas[i][0]=j;\n if(mp.find(ideas[i])==mp.end())\n {\n res+=dp[c-'a'][j-'a'];\n dp[j-'a'][c-'a']++;\n }\n }\n }\n return 2*res;\n }\n};",
"memory": "88200"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 1 | {
"code": "typedef long long ll;\nclass Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_set<string> st(ideas.begin(),ideas.end());\n int n=ideas.size();\n vector<vector<ll>> hash(26,vector<ll>(26,0));\n for(auto &it:ideas){\n char f=it[0];\n string s=it.substr(1);\n for(char ch='a';ch<='z';ch++){\n if(ch==f) continue;\n string str=ch + s;\n if(st.count(str)==0) hash[f-'a'][ch-'a']++;\n }\n }\n ll ans=0;\n for(int i=0;i<26;i++){\n for(int j=0;j<26;j++){\n ans+=hash[i][j]*hash[j][i];\n }\n }\n return ans;\n }\n};",
"memory": "88300"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& a) {\n unordered_set<string>s;\n for(auto it : a) s.insert(it);\n\n vector<vector<long long>>v(26,vector<long long>(26,0));\n for(auto it : a){\n char tmp=it[0];\n for(char c='a';c<='z';c++){\n if(c==tmp) continue;\n it[0]=c;\n if(s.find(it)==s.end()) v[tmp-'a'][it[0]-'a']++;\n }\n }\n long long ans=0;\n for(int i=0;i<26;i++){\n for(int j=0;j<26;j++){\n if(i==j) continue;\n ans+=(v[i][j]*v[j][i]);\n }\n }\n return ans;\n }\n};",
"memory": "88400"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n vector<set<string>> hash(26);\n for(auto it:ideas) {\n hash[it[0]-'a'].insert(it.substr(1, it.size()-1));\n }\n long long ans = 0;\n for(int i=0;i<26;i++) {\n for(int j=i+1;j<26;j++) {\n long long l = hash[i].size();\n long long r = hash[j].size();\n for(auto it:hash[i]) {\n if(hash[j].find(it)!=hash[j].end()) {\n l--;\n r--;\n }\n }\n ans+=(l*r*2);\n }\n }\n return ans;\n }\n};",
"memory": "89400"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas){\n vector<set<string>>um(26);\n for(auto word:ideas)um[word[0]-'a'].insert(word);\n long long ans=0LL;\n for(int i=0;i<25;i++){\n for(int j=i+1;j<26;j++){\n long long intersect=0;\n for(auto temp:um[i]){\n temp[0]=j+'a';\n if(um[j].find(temp)!=um[j].end())intersect++;\n }\n long long d1=um[i].size()-intersect;\n long long d2=um[j].size()-intersect;\n ans+=2*d1*d2;\n }\n }\n return ans;\n }\n};",
"memory": "89500"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n set<string> vec[26];\n long long ans = 0;\n\n for(auto j:ideas){\n vec[j[0]-'a'].insert(j.substr(1,j.size()-1));\n }\n\n // for(auto j:vec[2])cout<<j<<\"\\n\";\n for(int i=0;i<26;i++){\n for(int j=0;j<26;j++){\n long long cnt = 0;\n for(auto k:vec[i]){\n if(vec[j].find(k)!=vec[j].end()){\n cnt++;\n }\n }\n ans = ans + (vec[j].size()-cnt) * (vec[i].size()-cnt); \n }\n }\n\n return ans; \n }\n};",
"memory": "89600"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_map<char,set<string>> mp; //creating a unordered_map w[0] as key and w[1 to n] as value stored in a set.\n for(int i=0;i<ideas.size();i++){\n mp[ideas[i][0]].insert(ideas[i].substr(1));\n } \n long long ans =0,samesuffix=0;\n for(int i =97;i<122;i++){ //iterating through 26 alphabets\n for(int j=i+1;j<123;j++){\n samesuffix=0;\n for(auto it3:mp[i]){ // checking if 2 map value set words have same w[1 to n ]\n if( mp[j].find(it3) != mp[j].end() ) \n samesuffix++; // incrementing samesuffix\n }\n ans += 2*( mp[i].size()-samesuffix)*(mp[j].size()-samesuffix); //updating the ans by removing the samesuffix words .\n }\n }\n\n return ans;\n }\n};",
"memory": "89700"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution\n{\npublic:\n long long distinctNames(vector<string> &ideas)\n {\n unordered_map<char, set<string>> um;\n set<char> starts;\n for (string idea : ideas)\n {\n um[idea[0]].insert(idea.substr(1));\n starts.insert(idea[0]);\n }\n vector<char> arr(starts.begin(), starts.end());\n int n = arr.size();\n long long res=0;\n for (int i = 0; i < n; i++)\n {\n for (int j = i + 1; j < n; j++)\n {\n char ch1 = arr[i];\n char ch2 = arr[j];\n int exc_count = 0;\n for (string stx : um[ch2])\n if (um[ch1].find(stx) != um[ch1].end())\n exc_count++;\n res += (um[ch1].size() - exc_count) * (um[ch2].size() - exc_count);\n }\n }\n return res*2;\n }\n};",
"memory": "89800"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n map<char,set<string>> m;\n int n=ideas.size();\n for(int i=0;i<n;i++){\n m[ideas[i][0]].insert(ideas[i].substr(1,ideas[i].size()-1));\n }\n long long ans=0;\n for(auto i=m.begin();i!=m.end();i++){\n for(auto j=next(i);j!=m.end();j++){\n int sameSuffix=0;\n for(auto &suffix:i->second){\n if(j->second.count(suffix)){\n sameSuffix++;\n }\n }\n int diff1=i->second.size()-sameSuffix,diff2=j->second.size()-sameSuffix;\n ans+=(long long)diff1*diff2*2;\n }\n }\n return ans;\n }\n};",
"memory": "90100"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n int n=ideas.size();\n map<char,set<string>> m;\n for(int i=0;i<n;i++){\n m[ideas[i][0]].insert(ideas[i].substr(1));\n }\n long long res=0;\n for(int i=0;i<26;i++){\n for(int j=0;j<26;j++){\n if(i==j)continue;\n int inter=0;\n for(auto s:m['a'+i]){\n if(m['a' + j].find(s) != m['a' + j].end()){\n inter++;\n }\n }\n int u=m['a'+i].size()-inter;\n int v=m['a'+j].size()-inter;\n res+=u*v;\n }\n }\n return res;\n }\n};",
"memory": "90200"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n long long ans = 0;\n vector<vector<long long>>cnt(26,vector<long long>(26,0));\n set<string>st(ideas.begin(),ideas.end());\n for(auto it:ideas){\n int i = it[0]-'a';\n for(int j=0;j<26;j++){\n it[0]='a'+j;\n if(st.find(it)==st.end()){\n ans+=cnt[j][i];\n cnt[i][j]++;\n }\n }\n }\n return ans*2;\n }\n};",
"memory": "90300"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\n unordered_map<string, bitset<26>> umsv;\npublic:\n Solution() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n umsv.reserve(10000);\n\n }\n\nlong long distinctNames(const vector<string> &ideas) {\n unordered_map<string, bitset<26>> umsv;\n for (const string &s : ideas)\n umsv[s.substr(1)]._Unchecked_set(s.front() - 'a');\n\n vector<bitset<26>> vvc;\n vvc.reserve(umsv.size());\n\n vector<long long> single_stuff(26, 0);\n for (const auto &p : umsv) {\n if (p.second.count() == 1)\n single_stuff[p.second._Find_first()]++;\n else\n vvc.emplace_back(p.second);\n }\n\n long long sum = 0;\n const int vsize = vvc.size();\n\n for (int i = 0; i < vsize; i++) {\n for (int k = 0; k < 26; k++)\n if (!vvc[i][k])\n sum += vvc[i].count() * single_stuff[k];\n\n for (int j = i + 1; j < vsize; j++) {\n const auto &a = vvc[i];\n const auto &b = vvc[j];\n const auto aXorB = a ^ b;\n const auto c = a & aXorB;\n const auto d = b & aXorB;\n\n sum += c.count() * d.count();\n }\n }\n\n for (int i = 0; i < 26; i++)\n for (int j = i + 1; j < 26; j++)\n sum += single_stuff[i] * single_stuff[j];\n\n return sum * 2;\n}\n};",
"memory": "93200"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n int n = ideas.size();\n map<string,int> mp;\n for(auto it:ideas){\n mp[it]++;\n }\n vector<vector<int>> dp(26,vector<int>(26,0));\n\n for(int i=0; i<n; i++){\n string ut = ideas[i];\n char ch = ut[0];\n\n for(int j= 0; j<26; j++){\n ut[0] = j+'a';\n\n if(mp.find(ut)==mp.end()){\n dp[ch-'a'][j]++;\n }\n }\n }\n long long ans=0;\n for(int i=0;i<26; i++){\n for(int j=0; j<26; j++){\n ans+= (dp[i][j]*dp[j][i]);\n }\n }\n return ans;\n\n }\n};",
"memory": "94600"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n int n = ideas.size();\n map<string,int> mp;\n for(auto it:ideas){\n mp[it]++;\n }\n vector<vector<int>> dp(26,vector<int>(26,0));\n\n for(int i=0; i<n; i++){\n string ut = ideas[i];\n char ch = ut[0];\n\n for(int j= 0; j<26; j++){\n ut[0] = j+'a';\n\n if(mp.find(ut)==mp.end()){\n dp[ch-'a'][j]++;\n }\n }\n }\n long long ans=0;\n for(int i=0;i<26; i++){\n for(int j=0; j<26; j++){\n ans+= (dp[i][j]*dp[j][i]);\n }\n }\n return ans;\n\n }\n};",
"memory": "94700"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_set<string> ump(ideas.begin(), ideas.end());\n vector<vector<int>> cur(128, vector<int>(128,0));\n \n long long ans = 0;\n for (string& s : ideas) {\n for (char c='a'; c<='z'; c++){\n string word = s;\n word[0] = c;\n if (ump.find(word) == ump.end()){\n ans += cur[s[0]][c];\n cur[c][s[0]]++;\n }\n }\n }\n return ans * 2;\n }\n};",
"memory": "95900"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_set<string> ump(ideas.begin(), ideas.end());\n vector<vector<int>> cur(128, vector<int>(128,0));\n \n long long ans = 0;\n for (string& s : ideas) {\n for (char c='a'; c<='z'; c++){\n string word = s;\n word[0] = c;\n if (ump.find(word) == ump.end()){\n ans += cur[s[0]][c];\n cur[c][s[0]]++;\n }\n }\n }\n return ans * 2;\n }\n};",
"memory": "96000"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n // ios_base::sync_with_stdio(false);\n // cin.tie(NULL);\n\n // unordered_map<char, vector<string>> map;\n vector<unordered_map<string, bool>> map;\n for(int i=0; i<26; i++){ \n map.push_back(unordered_map<string, bool>());\n map[i].clear();\n }\n\n long long ans = 0;\n for(string& i: ideas){\n int index = i[0] - 'a';\n map[index][i.substr(1)] = true;\n }\n \n for (int i = 0; i < 26; ++i) {\n if (map[i].empty()) continue;\n for (int j = 0; j < 26; ++j) {\n if (i != j && !map[j].empty()) {\n long long duplicates = 0;\n for (auto& m1 : map[j]) {\n if (map[i].find(m1.first) != map[i].end()) {\n ++duplicates;\n }\n }\n ans += 1LL * (map[i].size() - duplicates) * (map[j].size() - duplicates);\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "96800"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_map<string, bool> mp;\n vector<bool> store(26, 0);\n\n for(auto s : ideas){\n mp[s] = true;\n store[s[0] - 'a'] = 1;\n }\n\n vector<vector<int>> DP(26, vector<int>(26, 0));\n\n for(auto &s : ideas){\n char ch = s[0];\n\n for(int i=0; i<26; i++){\n if(store[i] == 0) continue;\n if(i+'a' == ch) continue;\n\n char c = i + 'a';\n s[0] = c;\n if(mp.find(s) == mp.end()) DP[ch-'a'][c-'a']++;\n }\n }\n\n long long ans = 0;\n\n for(int i=0; i<26; i++){\n for(int j=0; j<26; j++){\n if(DP[i][j] == 0) continue; \n \n ans = ans + (DP[i][j] * DP[j][i] * 2);\n DP[i][j] = 0;\n DP[j][i] = 0;\n }\n }\n\n return ans;\n }\n};",
"memory": "97100"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n\n vector<unordered_map<string, bool>> map;\n for(int i=0; i<26; i++){ \n map.push_back(unordered_map<string, bool>());\n map[i].clear();\n }\n\n long long ans = 0;\n for(string& i: ideas){\n int index = i[0] - 'a';\n map[index][i.substr(1)] = true;\n }\n \n for (int i = 0; i < 26; ++i) {\n if (map[i].empty()) continue;\n for (int j = 0; j < 26; ++j) {\n if (i != j && !map[j].empty()) {\n long long duplicates = 0;\n for (auto& m1 : map[j]) {\n if (map[i].find(m1.first) != map[i].end()) {\n ++duplicates;\n }\n }\n ans += 1LL * (map[i].size() - duplicates) * (map[j].size() - duplicates);\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "97300"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_map<string, bool> mp;\n vector<bool> store(26, 0);\n\n for(auto s : ideas){\n mp[s] = true;\n store[s[0] - 'a'] = 1;\n }\n\n vector<vector<int>> DP(26, vector<int>(26, 0));\n\n for(auto &s : ideas){\n char ch = s[0];\n\n for(int i=0; i<26; i++){\n if(store[i] == 0) continue;\n if(i+'a' == ch) continue;\n\n char c = i + 'a';\n s[0] = c;\n if(mp.find(s) == mp.end()) DP[ch-'a'][c-'a']++;\n }\n }\n\n long long ans = 0;\n\n for(int i=0; i<26; i++){\n for(int j=0; j<26; j++){\n if(DP[i][j] == 0) continue; \n \n ans = ans + (DP[i][j] * DP[j][i] * 2);\n DP[i][j] = 0;\n DP[j][i] = 0;\n }\n }\n\n return ans;\n }\n};",
"memory": "97400"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& s) {\n int n = s.size();\n unordered_map<string,bool> mp;\n for(auto x:s)\n {\n mp[x]=true;\n }\n vector<vector<long long >> dict(26,vector<long long> (26,0));\n for(int i=0;i<n;i++)\n {\n string str=s[i].substr(1);\n int in=s[i][0]-'a';\n cout<<in<<endl;\n for(int j=0;j<26;j++){\n char ch=j+'a';\n string res=ch+str;\n if(mp.count(res)==0){\n dict[in][j]+=1;\n }\n }\n }\n long long cnt=0;\n for(int i=0;i<26;i++)\n {\n for(int j=0;j<26;j++){\n if(dict[i][j]>0 and i!=j)\n {\n cnt+=dict[i][j]*dict[j][i];\n }\n }\n }\n return cnt;\n }\n};",
"memory": "97700"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_set<string>ump(ideas.begin(),ideas.end());\n vector<vector<long long>>cur(128,vector<long long>(128,0));\n long long ans=0;\n for(string &s:ideas){\n for(char j='a';j<='z';j++){\n string word=s;\n word[0]=j;\n if(ump.find(word)==ump.end()){\n ans+=cur[s[0]][j];\n cur[j][s[0]]++;\n }\n }\n }\n return ans*2;\n }\n};",
"memory": "104100"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n long long ans = 0;\n map<char, long long> map1;\n map<string, vector<char>> map2;\n for(int i=0;i<ideas.size();i++){\n map1[ideas[i][0]]++;\n map2[ideas[i].substr(1)].push_back(ideas[i][0]);\n }\n for(auto it : map1){\n cout<<it.first<<\" \"<<it.second<<endl;\n }\n vector<vector<int>> cnt(26, vector<int>(26, 0));\n for(auto it : map2){\n cout<<it.first<<\" ->\";\n vector<char> v = it.second;\n for(int i=0;i<v.size();i++) cout<<v[i]<<\" \";\n cout<<endl;\n for(int i=0;i<v.size();i++){\n for(int j=0;j<v.size();j++){\n cnt[v[i]-'a'][v[j]-'a']++;\n }\n }\n }\n for(int i=0;i<26;i++){\n for(int j=0;j<26;j++){\n cout<<cnt[i][j]<<\" \";\n }\n cout<<endl;\n }\n for(int i=0;i<26;i++){\n if(map1.find('a'+i) == map1.end()) continue;\n for(int j=0;j<26;j++){\n if(map1.find('a'+j) == map1.end() || i == j) continue;\n long long temp = map1['a'+i]*map1['a'+j];\n temp = temp - (cnt[i][j]*map1['a'+j] + (map1['a'+i]-cnt[i][j])*cnt[i][j]);\n ans += temp;\n }\n }\n return ans;\n // long long ans = 0;\n // map<char, long long> map1;\n // map<string, set<char>> st;\n // for(int i=0;i<ideas.size();i++){\n // map1[ideas[i][0]]++;\n // st[ideas[i].substr(1)].insert(ideas[i][0]);\n // }\n // vector<map<char, int>> cnt(26);\n // for(auto it : st){\n // vector<char> v(it.second.begin(), it.second.end());\n // for(int i=0;i<v.size();i++){\n // for(int j=0;j<v.size();j++){\n // cnt[v[i]-'a'][v[j]]++;\n // }\n // } \n // }\n // cout<<st.size()<<\" \"<<ideas.size()<<endl;\n // for(int i=0;i<26;i++){\n // if(map1.find('a'+i) == map1.end()) continue;\n // for(int j=0;j<26;j++){\n // if(cnt[i].find(j+'a') != cnt[i].end() || map1.find('a'+j) == map1.end()) continue;\n // ans = ans + (map1['a'+i] * map1['a'+j]);\n // }\n // }\n // return ans;\n }\n};",
"memory": "110200"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n long long ans = 0;\n map<char, long long> map1;\n map<string, vector<char>> map2;\n for(int i=0;i<ideas.size();i++){\n map1[ideas[i][0]]++;\n map2[ideas[i].substr(1)].push_back(ideas[i][0]);\n }\n for(auto it : map1){\n cout<<it.first<<\" \"<<it.second<<endl;\n }\n vector<vector<int>> cnt(26, vector<int>(26, 0));\n for(auto it : map2){\n cout<<it.first<<\" ->\";\n vector<char> v = it.second;\n for(int i=0;i<v.size();i++) cout<<v[i]<<\" \";\n cout<<endl;\n for(int i=0;i<v.size();i++){\n for(int j=0;j<v.size();j++){\n cnt[v[i]-'a'][v[j]-'a']++;\n }\n }\n }\n for(int i=0;i<26;i++){\n for(int j=0;j<26;j++){\n cout<<cnt[i][j]<<\" \";\n }\n cout<<endl;\n }\n for(int i=0;i<26;i++){\n if(map1.find('a'+i) == map1.end()) continue;\n for(int j=0;j<26;j++){\n if(map1.find('a'+j) == map1.end() || i == j) continue;\n long long temp = map1['a'+i]*map1['a'+j];\n temp = temp - (cnt[i][j]*map1['a'+j] + (map1['a'+i]-cnt[i][j])*cnt[i][j]);\n ans += temp;\n }\n }\n return ans;\n // long long ans = 0;\n // map<char, long long> map1;\n // map<string, set<char>> st;\n // for(int i=0;i<ideas.size();i++){\n // map1[ideas[i][0]]++;\n // st[ideas[i].substr(1)].insert(ideas[i][0]);\n // }\n // vector<map<char, int>> cnt(26);\n // for(auto it : st){\n // vector<char> v(it.second.begin(), it.second.end());\n // for(int i=0;i<v.size();i++){\n // for(int j=0;j<v.size();j++){\n // cnt[v[i]-'a'][v[j]]++;\n // }\n // } \n // }\n // cout<<st.size()<<\" \"<<ideas.size()<<endl;\n // for(int i=0;i<26;i++){\n // if(map1.find('a'+i) == map1.end()) continue;\n // for(int j=0;j<26;j++){\n // if(cnt[i].find(j+'a') != cnt[i].end() || map1.find('a'+j) == map1.end()) continue;\n // ans = ans + (map1['a'+i] * map1['a'+j]);\n // }\n // }\n // return ans;\n }\n};",
"memory": "110300"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\nlong long distinctNames(vector<string>& ideas) {\n map<int ,unordered_set<string> > mp;\n long long ans =0;\n for(auto it: ideas){\n mp[it[0]-'a'].insert(it.substr(1));\n }\n//******************************************\n\nfor(auto it: mp){\n auto it1=mp.find(it.first);\n it1++;\n while (it1!= mp.end()){\n long long vl1=0,vl2=0;\n\n for(auto v1: it.second){\n if(it1->second.find(v1)== it1->second.end())\n vl1++; \n }\n for(auto v1: it1->second){\n if(it.second.find(v1)== it.second.end())\n vl2++; \n }\n ans += (vl1*vl2);\n it1++;\n } \n}\nans *=2;\nreturn ans;\n}\n};",
"memory": "112200"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_set<string> ideaSet(ideas.begin(), ideas.end());\n vector<unordered_set<string>> suffixes(26);\n for (const string& idea : ideas) {\n suffixes[idea[0] - 'a'].insert(idea.substr(1));\n }\n long long result = 0;\n for (int i = 0; i < 25; ++i) {\n for (int j = i + 1; j < 26; ++j) {\n int common = 0;\n for (const string& suffix : suffixes[i]) {\n if (suffixes[j].count(suffix)) {\n ++common;\n }\n }\n result += 2 * (suffixes[i].size() - common) * (suffixes[j].size() - common);\n }\n }\n return result;\n }\n};",
"memory": "115600"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& arr){\n map<char, set<string>> mp;\n for(auto i : arr){\n mp[i[0]].insert(i.substr(1));\n }\n vector<string> res(1e4);\n long long cnt = 0;\n for(char c='a' ; c<='z' ; c++){\n for(char d=c ; d<='z' ; d++){\n if(c == d) continue;\n auto it = set_intersection(mp[c].begin(), mp[c].end(), mp[d].begin(), mp[d].end(), res.begin());\n int size = it - res.begin();\n cnt += 2 * (mp[c].size() - size) * (mp[d].size() - size);\n }\n }\n return cnt;\n }\n};",
"memory": "122400"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n vector<set<string>> vis(26);\n map<pair<int,string>, int> mp;\n for(auto v: ideas){\n char c=v[0];\n string s=v.substr(1,v.length()-1);\n vis[c-'a'].insert(s);\n mp[{c-'a',s}]++;\n }\n \n long long ans=0;\n for(int i=0;i<26;i++){\n for(int j=i+1;j<26;j++){\n long long count1=0;\n long long count2=0;\n for(auto v:vis[i]){\n if(mp.find({j,v})!=mp.end()) continue;\n count1++;\n }\n\n for(auto v:vis[j]){\n if(mp.find({i,v})!=mp.end()) continue;\n count2++;\n }\n\n\n ans+= (count1*1l*count2*1l*2);\n }\n }\n\n\n return ans;\n }\n};\n/*\n\n\n*/",
"memory": "124300"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_map<string,int> m1;\n for(string s:ideas)\n m1[s]++;\n vector<unordered_map<string,int>> strs(26);\n for(auto x:m1){\n strs[x.first[0]-'a'][x.first.substr(1)]=1;\n }\n long long ans=0;\n for(int i=0;i<26;i++){\n for(int j=0;j<26;j++){\n if(i==j)\n continue;\n int ct=0;\n for(auto x:strs[i]){\n if(strs[j].find(x.first)!=strs[j].end())\n ct++;\n }\n ans+=(strs[i].size()-ct)*(strs[j].size()-ct);\n }\n }\n return ans;\n }\n};",
"memory": "133800"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n \n int n = ideas.size();\n map<char, unordered_set<string>> mp;\n\n for(int i = 0; i < n; i++) {\n mp[ideas[i][0]].insert(ideas[i].substr(1));\n }\n long long ans = 0;\n for(auto i : mp) {\n char c = i.first;\n unordered_set<string> temp = i.second;\n for(char ch = 'a'; ch <= 'z'; ch++) {\n if(ch == c) continue; \n int cnt = 0;\n\n for(auto w : temp) {\n if(mp[ch].find(w) != mp[ch].end()) cnt++;\n }\n\n ans +=(temp.size() - cnt) * (mp[ch].size() - cnt);\n }\n }\n\n return ans;\n }\n};",
"memory": "136800"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n long long ret=0;\n unordered_map<string,unordered_set<char>> last;\n for(auto &s:ideas){\n char tmp=s[0];\n s[0]='_';\n last[s].insert(tmp);\n s[0]=tmp;\n }\n unordered_map<char,int> first;\n unordered_map<char,unordered_map<char,int>> unioned;\n for(auto &[key,val]:last){\n for(auto &a:val){\n for(auto &b:val){\n unioned[a][b]++;\n }\n first[a]++;\n }\n }\n for(auto &[key,val]:last){\n for(auto &n:val){\n for(char c='a';c<='z';c++){\n if(c!=n && val.count(c)==0){\n if(first[c]!=0){\n // cout << n<< \" \"<<key <<\" \"<< c <<\" \"<<first[c]<<\" \"<<unioned[n][c]<<endl;\n ret+=first[c]-unioned[n][c];\n }\n }\n }\n }\n }\n return ret;\n }\n};\n\n// t and c are part of a union, if swapping you find a t with a c you cant swap \n\n// but uhhh what about \n\n// so uhh total count - num in a union",
"memory": "147900"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "using namespace std;\n\nclass Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n // Step 1: Group ideas by their first letter\n unordered_map<char, unordered_set<string>> groups;\n for (const auto& idea : ideas) {\n groups[idea[0]].insert(idea.substr(1));\n }\n\n long long count = 0;\n\n // Step 2: Compare each pair of groups\n for (auto it1 = groups.begin(); it1 != groups.end(); ++it1) {\n for (auto it2 = next(it1); it2 != groups.end(); ++it2) {\n // Step 3: Find the number of common suffixes\n unordered_set<string> common_suffixes;\n for (const auto& suffix : it1->second) {\n if (it2->second.count(suffix)) {\n common_suffixes.insert(suffix);\n }\n }\n int num_common = common_suffixes.size();\n\n // Step 4: Calculate the number of distinct valid names\n count += (it1->second.size() - num_common) * (it2->second.size() - num_common) * 2;\n }\n }\n\n return count;\n }\n};",
"memory": "167100"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_map<string,int> m1,m2;\n for(string s:ideas){\n m1[s]++;\n m2[s]=1;\n }\n vector<unordered_map<string,int>> strs(26);\n for(auto x:m1){\n strs[x.first[0]-'a'][x.first.substr(1)]=1;\n }\n long long ans=0;\n for(int i=0;i<26;i++){\n for(int j=0;j<26;j++){\n if(i==j)\n continue;\n int ct=0;\n for(auto x:strs[i]){\n if(strs[j].find(x.first)!=strs[j].end())\n ct++;\n }\n ans+=(strs[i].size()-ct)*(strs[j].size()-ct);\n }\n }\n return ans;\n }\n};",
"memory": "171200"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_map<string, unordered_set<char>> suffixGroups;\n vector<unordered_set<string>> prefixGroups(26);\n\n for (auto& idea : ideas) {\n string suffix = idea.substr(1);\n char prefix = idea[0];\n suffixGroups[suffix].insert(prefix);\n prefixGroups[prefix - 'a'].insert(suffix);\n }\n\n long long result = 0;\n for (int i = 0; i < 26; ++i) {\n for (int j = i + 1; j < 26; ++j) {\n int common = 0;\n for (const auto& suffix : prefixGroups[i]) {\n if (prefixGroups[j].count(suffix)) {\n common++;\n }\n }\n result += 2LL * (prefixGroups[i].size() - common) * (prefixGroups[j].size() - common);\n }\n }\n return result;\n }\n};\n",
"memory": "173200"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n long long ret = 0;\n\n array<vector<string>, 26> has;\n for (auto const& s: ideas)\n {\n has[s[0] - 'a'].push_back(s.substr(1));\n }\n\n for (int i = 0; i < 26; i++)\n {\n set<string> now(has[i].begin(), has[i].end());\n // for (auto &x: now)\n // cout << x << \" \";\n // cout << endl;\n\n\n if (has[i].empty())\n continue;\n for (int j = i + 1; j < 26; j++)\n {\n if (has[j].empty())\n continue;\n vector<string> com;\n com.reserve(min(has[i].size(), has[j].size()));\n for (auto &s: has[j])\n {\n if (now.find(s) != now.end())\n com.push_back(s);\n }\n\n // cout << i << \" \" << j << \" \" << com.size() << endl;;\n\n ret += 2ll * (has[i].size() - com.size() * 1ll) * (has[j].size() - com.size());\n }\n }\n\n return ret;\n }\n};",
"memory": "180400"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_set<string> filterSet(ideas.begin(),ideas.end());\n vector<string> filter(filterSet.begin(),filterSet.end());\n long long ret=0;\n unordered_map<string,unordered_set<char>> last;\n for(auto &s:filter){\n char tmp=s[0];\n s[0]='_';\n last[s].insert(tmp);\n s[0]=tmp;\n }\n unordered_map<char,int> first;\n unordered_map<char,unordered_map<char,int>> unioned;\n for(auto &[key,val]:last){\n for(auto &a:val){\n for(auto &b:val){\n unioned[a][b]++;\n }\n first[a]++;\n }\n }\n for(auto &[key,val]:last){\n for(auto &n:val){\n for(char c='a';c<='z';c++){\n if(c!=n && val.count(c)==0){\n if(first[c]!=0){\n // cout << n<< \" \"<<key <<\" \"<< c <<\" \"<<first[c]<<\" \"<<unioned[n][c]<<endl;\n ret+=first[c]-unioned[n][c];\n }\n }\n }\n }\n }\n return ret;\n }\n};\n\n// t and c are part of a union, if swapping you find a t with a c you cant swap \n\n// but uhhh what about \n\n// so uhh total count - num in a union",
"memory": "185400"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n Solution() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n // umsv.reserve(10000);\n\n }\n int numCommonEle(vector<string>& v1,vector<string>& v2){\n unordered_set<string> st;\n for(auto x:v1)st.insert(x);\n int ans=0;\n for(auto x:v2){\n if(st.count(x))ans++;\n }\n return ans;\n }\n long long getCombo(int a,int b){\n return 1LL*2*a*b;\n }\n long long distinctNames(vector<string>& ideas) {\n unordered_map<char,vector<string>> mp;\n for(auto x:ideas){\n char str1 = x[0];\n string str2 = x.substr(1);\n mp[str1].emplace_back(str2);\n }\n long long ans=0;\n for(auto ch=mp.begin();ch!=mp.end();ch++){\n // auto it = ch;\n // if(ch+1 == mp.end())continue;\n for(auto ch1=next(ch);ch1!=mp.end();ch1++){\n int num = numCommonEle(ch->second,ch1->second);\n int num1 = ch->second.size()-num;\n int num2 = ch1->second.size()-num;\n ans+=getCombo(num1,num2);\n }\n }\n return ans;\n }\n};",
"memory": "383900"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n Solution() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n // umsv.reserve(10000);\n\n }\n int numCommonEle(vector<string>& v1,vector<string>& v2){\n unordered_set<string> st;\n for(auto x:v1)st.insert(x);\n int ans=0;\n for(auto x:v2){\n if(st.count(x))ans++;\n }\n return ans;\n }\n long long getCombo(int a,int b){\n return 1LL*2*a*b;\n }\n long long distinctNames(vector<string>& ideas) {\n map<char,vector<string>> mp;\n for(auto x:ideas){\n char str1 = x[0];\n string str2 = x.substr(1);\n mp[str1].emplace_back(str2);\n }\n long long ans=0;\n for(char ch='a';ch<='z';ch++){\n for(char ch1=ch+1;ch1<='z';ch1++){\n if(mp.count(ch) && mp.count(ch1)){\n int num = numCommonEle(mp[ch],mp[ch1]);\n int num1 = mp[ch].size()-num;\n int num2 = mp[ch1].size()-num;\n ans+=getCombo(num1,num2);\n }\n }\n }\n return ans;\n }\n};",
"memory": "384900"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numCommonEle(vector<string>& v1,vector<string>& v2){\n unordered_set<string> st;\n for(auto x:v1)st.insert(x);\n int ans=0;\n for(auto x:v2){\n if(st.count(x))ans++;\n }\n return ans;\n }\n long long getCombo(int a,int b){\n return 1LL*2*a*b;\n }\n long long distinctNames(vector<string>& ideas) {\n map<char,vector<string>> mp;\n for(auto x:ideas){\n char str1 = x[0];\n string str2 = x.substr(1);\n mp[str1].emplace_back(str2);\n }\n long long ans=0;\n for(char ch='a';ch<='z';ch++){\n for(char ch1=ch+1;ch1<='z';ch1++){\n if(mp.count(ch) && mp.count(ch1)){\n int num = numCommonEle(mp[ch],mp[ch1]);\n int num1 = mp[ch].size()-num;\n int num2 = mp[ch1].size()-num;\n ans+=getCombo(num1,num2);\n }\n }\n }\n return ans;\n }\n};",
"memory": "385000"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n Solution() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n // umsv.reserve(10000);\n\n }\n int numCommonEle(vector<string>& v1,vector<string>& v2){\n unordered_set<string> st;\n for(auto x:v1)st.insert(x);\n int ans=0;\n for(auto x:v2){\n if(st.count(x))ans++;\n }\n return ans;\n }\n long long getCombo(int a,int b){\n return 1LL*2*a*b;\n }\n long long distinctNames(vector<string>& ideas) {\n map<char,vector<string>> mp;\n for(auto x:ideas){\n char str1 = x[0];\n string str2 = x.substr(1);\n mp[str1].emplace_back(str2);\n }\n long long ans=0;\n for(auto ch=mp.begin();ch!=mp.end();ch++){\n // auto it = ch;\n // if(ch+1 == mp.end())continue;\n for(auto ch1=next(ch);ch1!=mp.end();ch1++){\n int num = numCommonEle(ch->second,ch1->second);\n int num1 = ch->second.size()-num;\n int num2 = ch1->second.size()-num;\n ans+=getCombo(num1,num2);\n }\n }\n return ans;\n }\n};",
"memory": "385000"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_map<char, unordered_set<string>> wordMap;\n long long count = 0;\n\n for (string idea : ideas) {\n wordMap[idea[0]].insert(idea.substr(1));\n }\n\n for (auto curr = wordMap.begin(); curr != wordMap.end(); curr++) {\n int groupCount = 0;\n unordered_set<string> currSet = curr->second;\n\n for (auto it = curr; it != wordMap.end(); it++) {\n if (it == curr) {\n continue;\n }\n\n unordered_set<string> itSet = it->second;\n int invalidWords = 0;\n\n for (auto setPtr = itSet.begin(); setPtr != itSet.end(); setPtr++) {\n string word = *setPtr;\n\n if (currSet.contains(word) == true) {\n invalidWords += 1;\n }\n\n }\n\n count += (2 * ((currSet.size() - invalidWords) * (itSet.size() - invalidWords)));\n }\n }\n\n return count;\n }\n\n};\n\n\n\n//BRUTE FORCE N^2:\n\n\n// unordered_set<string> set;\n// long long count = 0;\n\n// for (string idea : ideas) {\n// set.insert(idea);\n// }\n\n// for (int i = 0; i < ideas.size(); i++) {\n// for (int j = i + 1; j < ideas.size(); j++) {\n// string word1 = ideas[i];\n// string word2 = ideas[j];\n// string newName1 = \"\";\n// string newName2 = \"\";\n\n// newName1 += word2[0] + word1.substr(1);\n// newName2 += word1[0] + word2.substr(1);\n\n// if (set.contains(newName1) == false && set.contains(newName2) == false) {\n// count += 2;\n// set.insert(newName1 + \" \" + newName2);\n// set.insert(newName2 + \" \" + newName1);\n// }\n// }\n// }\n\n// return count;",
"memory": "385400"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n map<char, vector<string>> m;\n \n\n for (int i = 0; i < ideas.size(); i++) {\n char firstChar = ideas[i][0];\n string suffix = ideas[i].substr(1, ideas[i].size() - 1);\n m[firstChar].push_back(suffix);\n }\n\n long long result = 0;\n\n\n for (auto it1 = m.begin(); it1 != m.end(); ++it1) {\n map<string, int> repeat;\n\n\n for (int i = 0; i < it1->second.size(); i++) {\n repeat[it1->second[i]]++;\n }\n\n \n for (auto it2 = next(it1); it2 != m.end(); ++it2) {\n int count = 0;\n\n\n for (int i = 0; i < it2->second.size(); i++) {\n if (repeat[it2->second[i]]) {\n count++;\n }\n }\n\n \n result += (2LL * (it1->second.size() - count) * (it2->second.size() - count));\n }\n }\n\n return result;\n }\n};\n",
"memory": "405800"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "#define ll long long\nclass Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_map<char, unordered_set<string>> m;\n for (int i = 0; i < ideas.size(); i++) {\n m[ideas[i][0]].insert(ideas[i]);\n }\n ll cnt =0;\n for (auto it = m.begin(); it != m.end(); it++) {\n auto it2 = it;\n it2++;\n for (;it2 != m.end(); it2++) {\n char first1 = it->first;\n char first2 = it2->first;\n auto strs1 = it->second;\n auto strs2 = it2->second;\n if (first1 == first2) continue;\n ll cnt1 = 0, cnt2 = 0;\n for (auto s: strs1) {\n string ns = \"\";\n ns += first2;\n ns += s.substr(1, -1);\n // cout << ns << endl;\n if (strs2.find(ns) == strs2.end()) cnt1++;\n }\n\n for (auto s: strs2) {\n string ns = \"\";\n ns += first1;\n ns += s.substr(1, -1);\n // cout << ns << endl;\n if (strs1.find(ns) == strs1.end()) cnt2++;\n }\n cnt += 2 * cnt1 * cnt2;\n }\n }\n\n return cnt; \n }\n};",
"memory": "423900"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int common_ele(unordered_set<string> s1, unordered_set<string> s2){\n int count = 0; \n for(auto str : s1){\n if (s2.find(str)!=s2.end()){\n count++;\n }\n }\n return count;\n }\n long long distinctNames(vector<string>& ideas) {\n long long count = 0,ce;\n // unordered_map< char, int > count_map;\n unordered_map< char, unordered_set<string> > prefix_map;\n for(auto str : ideas){\n if(str.size()!=1){\n (prefix_map[str[0]]).insert( str.substr(1,str.size()-1) );\n }else{\n (prefix_map[str[0]]).insert( \"\" );\n }\n }\n\n for(auto p1 = prefix_map.begin();p1 != prefix_map.end(); p1 ++){\n for(auto p2 = p1 ;p2 != prefix_map.end(); p2 ++){\n // if(p1->first==p2->first){\n // continue;\n // }\n if(p2 == p1){\n advance(p2, 1); \n }\n if(p2 == prefix_map.end()){\n break;\n }\n cout << \"1st iteration\" << endl;\n ce = common_ele(p1->second , p2->second);\n count = count + ((p1->second).size()-ce)*((p2->second).size()-ce);\n }\n }\n return 2*count;\n }\n};",
"memory": "424100"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas)\n {\n unordered_map<int, unordered_set<string>> first_letter_to_substrings;\n\n for (string idea : ideas)\n {\n int first_letter = idea[0] - 'a';\n\n string substring = idea.substr(1);\n first_letter_to_substrings[first_letter].insert(substring);\n }\n\n long long result = 0;\n\n for (int i = 0; i < 26; i++)\n {\n for (int j = i + 1; j < 26; j++)\n {\n unordered_set<string> substrings_starting_with_i = first_letter_to_substrings[i];\n unordered_set<string> substrings_starting_with_j = first_letter_to_substrings[j];\n int duplicate = 0;\n\n for (string substring : substrings_starting_with_i)\n {\n if (substrings_starting_with_j.count(substring) > 0)\n duplicate++;\n }\n\n result += (substrings_starting_with_i.size() - duplicate) * (substrings_starting_with_j.size() - duplicate);\n }\n }\n\n return result * 2;\n }\n};\n",
"memory": "425000"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_map<int, unordered_set<string>>mpp;\n long long res = 0;\n\n\n for(auto &it : ideas) mpp[it[0]].insert(it.substr(1,it.size()-1)); \n\n \n for(int i = 0; i < 26; i++)\n {\n for(int j = i+1; j < 26; j++)\n {\n unordered_set<string>temp1 = mpp[i+'a'], temp2 = mpp[j+'a'];\n \n long long val = 0;\n\n for(auto &it : temp1)\n {\n if(temp2.find(it) != temp2.end()) val++;\n }\n\n \n res += 1LL*((temp1.size()- val)*(temp2.size()- val))*2;\n }\n }\n\n return res;\n\n }\n};",
"memory": "425100"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_map<int,unordered_set<string>> st;\n\n for(string idea: ideas){\n int firstletter = idea[0]-'a';\n string sub = idea.substr(1);\n st[firstletter].insert(sub);\n }\n\n long long ans = 0;\n for(int i=0;i<26;i++){\n for(int j= i+1;j<26;j++){\n\n unordered_set<string> st1 = st[i];\n unordered_set<string> st2 = st[j];\n\n int duplicate =0;\n for(string substring : st1){\n if(st2.count(substring) > 0) duplicate++;\n }\n\n ans += (st1.size() - duplicate)*(st2.size()-duplicate);\n }\n }\n return ans*2;\n }\n};",
"memory": "425200"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n long long distinctNames(vector<string>& ideas){\n\n \n unordered_map<int, unordered_set<string>>mpp;\n long long res = 0;\n\n\n for(auto &it : ideas) mpp[it[0]].insert(it.substr(1,it.size()-1)); \n\n \n for(int i = 0; i < 26; i++)\n {\n for(int j = i+1; j < 26; j++)\n {\n unordered_set<string>temp1 = mpp[i+'a'], temp2 = mpp[j+'a'];\n \n long long val = 0;\n\n for(auto &it : temp1)\n {\n if(temp2.find(it) != temp2.end()) val++;\n }\n\n \n res += 1LL*((temp1.size()- val)*(temp2.size()- val))*2;\n }\n }\n\n return res;\n }\n};",
"memory": "425300"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n map<char, unordered_set<string>> m;\n \n for(int i = 0; i < ideas.size(); i++){\n m[ideas[i][0]].insert(ideas[i].substr(1, ideas[i].size() - 1));\n }\n long long ans = 0;\n for(auto it1 : m){\n for(auto it2 : m){\n long long cnt = 0;\n if(it1 != it2){\n for(auto it : it2.second){\n if(it1.second.find(it) != it1.second.end()){\n cnt++;\n }\n }\n ans += (it1.second.size() - cnt) * (it2.second.size() - cnt);\n }\n }\n }\n return ans;\n\n }\n};",
"memory": "430800"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "typedef long long ll;\nclass Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n \n\n int n=ideas.size();\n ll ans=0;\n unordered_map<char,unordered_set<string>> mp;\n\n for(auto s:ideas){\n mp[s[0]].insert(s.substr(1,s.size()-1));\n }\n\n for(auto p1:mp){\n for(auto p2:mp){\n \n auto c1 = p1.first;\n auto c2 = p2.first;\n\n if(c1 == c2) continue;\n\n int intersection =0;\n\n for(auto s:p1.second){\n if(p2.second.find(s) != p2.second.end()){\n intersection++;\n }\n }\n ans+=(ll)((p1.second.size()-intersection)*(p2.second.size()-intersection));\n }\n }\n return ans;\n }\n};",
"memory": "431000"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n long long ans = 0;\n unordered_map<char, unordered_set<string>> mp;\n for(string idea : ideas){\n mp[idea[0]].insert(idea.substr(1));\n }\n for(auto p1 : mp){\n for(auto p2 : mp){\n if(p2.first == p1.first) continue;\n //unordered_set<string> temp;\n long long a = 0, b = 0;\n for(auto s : p1.second){\n if(p2.second.find(s) == p2.second.end()) a++;\n }\n for(auto s : p2.second){\n if(p1.second.find(s) == p1.second.end()) b++;\n }\n ans += a * b;\n\n }\n }\n return ans;\n }\n};",
"memory": "431100"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n long long ans = 0;\n map<char, unordered_set<string>> mp;\n for(string idea : ideas){\n mp[idea[0]].insert(idea.substr(1));\n }\n for(auto p1 : mp){\n for(auto p2 : mp){\n if(p2.first == p1.first) continue;\n //unordered_set<string> temp;\n long long a = 0, b = 0;\n for(auto s : p1.second){\n if(p2.second.find(s) == p2.second.end()) a++;\n }\n for(auto s : p2.second){\n if(p1.second.find(s) == p1.second.end()) b++;\n }\n ans += a * b;\n\n }\n }\n return ans;\n }\n};",
"memory": "431200"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n long long count=0;\n vector<unordered_set<string>>arr(26);\n for(string i:ideas)\n arr[i[0]-'a'].insert(i.substr(1));\n for(int i=0; i<25;i++){\n for(int j=i+1;j<26;j++){\n unordered_set<string>set;\n set.insert(arr[i].begin(),arr[i].end());\n set.insert(arr[j].begin(),arr[j].end());\n count+=(arr[i].size()-set.size())*(arr[j].size()-set.size());\n }\n }\n return count*2;\n }\n};",
"memory": "441200"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n long long disName = 0;\n vector<unordered_set<string>> arr(26);\n for (string s : ideas) \n arr[s[0] - 'a'].insert(s.substr(1));\n \n for (int i = 0; i < 25; i++) {\n for (int j = i + 1; j < 26; j++) {\n unordered_set<string> set;\n set.insert(arr[i].begin(), arr[i].end());\n set.insert(arr[j].begin(), arr[j].end());\n disName += (arr[i].size() - set.size()) * (arr[j].size() - set.size());\n }\n }\n return disName * 2;\n }\n};",
"memory": "441300"
} |
2,390 | <p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>.</li>
<li>Swap the first letters of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>idea<sub>A</sub> idea<sub>B</sub></code> (the <strong>concatenation</strong> of <code>idea<sub>A</sub></code> and <code>idea<sub>B</sub></code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["coffee","donuts","time","toffee"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following selections are valid:
- ("coffee", "donuts"): The company name created is "doffee conuts".
- ("donuts", "coffee"): The company name created is "conuts doffee".
- ("donuts", "time"): The company name created is "tonuts dime".
- ("donuts", "toffee"): The company name created is "tonuts doffee".
- ("time", "donuts"): The company name created is "dime tonuts".
- ("toffee", "donuts"): The company name created is "doffee tonuts".
Therefore, there are a total of 6 distinct company names.
The following are some examples of invalid selections:
- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.
- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.
- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ideas = ["lack","back"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid selections. Therefore, 0 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= ideas.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= ideas[i].length <= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long distinctNames(vector<string>& ideas) {\n unordered_map<int, set<string>> appears;\n\n for (auto &idea: ideas) {\n int front = idea[0] - 'a';\n string back = idea.substr(1, idea.size() - 1);\n appears[front].insert(back);\n }\n\n auto intersection_count = [](set<string> &s1, set<string> &s2) -> int {\n int ans = 0;\n for (auto &cand: s1) {\n if (s2.find(cand) != s2.end()) {\n ans += 1;\n }\n }\n return ans;\n };\n\n int64_t ans = 0;\n\n for (int i = 0; i < 26; i++) {\n for (int j = i + 1; j < 26; j++) {\n auto s1 = appears[i], s2 = appears[j];\n int64_t k = intersection_count(s1, s2);\n ans += (s1.size() - k) * (s2.size() - k);\n }\n }\n\n return ans * 2;\n }\n};",
"memory": "454300"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n int count=0;\n int n=tickets.size();\n while(true){\n if(tickets[k]==0) \n break;\n for(int i=0;i<n;i++){\n if(tickets[k]==0)\n break;\n tickets[i]--;\n if(tickets[i]>=0)\n count++;\n }\n }\n return count;\n }\n};",
"memory": "9300"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n int ans = 0;\n\n for (int i = 0; i < tickets.size(); ++i)\n if (i <= k)\n ans += min(tickets[i], tickets[k]);\n else\n ans += min(tickets[i], tickets[k] - 1);\n\n return ans;\n }\n};",
"memory": "9400"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n int counter = 0, target = tickets[k];\n for(int i = 0; i < tickets.size(); i++){\n if(i > k && tickets[i] >= target){\n counter --;\n }\n counter += min(tickets[i], target);\n }\n return counter;\n }\n};",
"memory": "9500"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n int time = 0;\n int i = 0;\n while(tickets[k] != 0){\n if(tickets[i] != 0){\n time++;\n tickets[i]--;\n }\n i++;\n i = i % tickets.size();\n }\n return time;\n }\n};",
"memory": "9600"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n int c=0;\n while(true){\n if(tickets[k]==0)\n break;\n for(int i=0;i<tickets.size();i++){\n if(tickets[k]==0)\n break;\n if(tickets[i]>0){\n tickets[i]-=1;\n c+=1;\n }\n }\n }\n return c;\n }\n};",
"memory": "9700"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n int c=0,i=0;\n while(tickets[k]!=0){\n if(tickets[i]>0){\n c++;\n tickets[i]--;\n i=(i+1)%tickets.size();\n }\n else if(tickets[i]==0)i=(i+1)%tickets.size();\n }\n return c;\n }\n};",
"memory": "9700"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n int result =0 ;\n for(int i =0;i<=k;i++){\n tickets[i]--;\n result++;\n }\n int val = tickets[k];\n for(int i =0;i<tickets.size();i++){\n result += min(tickets[i],val);\n }\n return result;\n }\n};",
"memory": "9800"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 2 | {
"code": "class person {\n public:\n person* next;\n int index;\n int tickets; \n person(int position, int num): next{nullptr}, index{position}, tickets{num}{}\n};\n\nclass Queue{\n public:\n person* front;\n person* back;\n int size;\n Queue(){\n front = nullptr;\n back = nullptr;\n }\n void enqueue(int position, int num){\n person* temp = new person(position, num);\n if(size == 0){\n front = temp;\n back = temp;\n }\n else{\n back->next = temp;\n back = temp;\n }\n size++;\n }\n int requeue(){\n if(front->tickets == 1){\n person* temp = front;\n int val = temp->index;\n front = front->next;\n delete temp;\n size--;\n return val;\n }\n front->tickets--;\n back->next = front;\n back = front;\n front = front->next;\n back->next = nullptr;\n return back->index;\n }\n};\n\nclass Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n int counter = 0;\n Queue queue = Queue();\n for(int i = 0; i < tickets.size(); i ++){\n queue.enqueue(i,tickets[i]);\n }\n while(true){\n counter++;\n int check_tickets = queue.front->tickets;\n int index = queue.requeue();\n if(check_tickets == 1 && index == k)\n {\n return counter;\n }\n \n }\n }\n};",
"memory": "9900"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n vector<int> others(tickets[k] + 1);\n for (int i = 0; i < tickets.size(); ++i) {\n int num = min(tickets[k], tickets[i]); \n if (i > k) {\n num = min(tickets[k] - 1, num);\n }\n cout << num << '\\n';\n ++others[num];\n }\n int time = 0;\n for (int i = 1; i < others.size(); ++i) {\n time += i * others[i];\n }\n return time;\n }\n};",
"memory": "10000"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n int n=tickets.size();\n int time=0;\n queue<int>q;\n for(int i=0;i<n;i++){\n q.push(i);\n }\n while(tickets[k]!=0){\n tickets[q.front()]--;\n if(tickets[q.front()]){\n q.push(q.front());}\n q.pop();\n time++;\n }\n return time;\n }\n};",
"memory": "10100"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int>q;\n int t =0;\n for(int i=0;i<tickets.size();i++){\n q.push(i);\n } \n while(tickets[k]!=0){\n tickets[q.front()]--;\n if(tickets[q.front()] !=0){\n q.push(q.front());\n }\n q.pop();\n t++;\n }\n return t;\n }\n};",
"memory": "10200"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& arr, int k) {\n int n=arr.size();\n int count=0;\n queue<int>q;\n for(int i=0;i<n;i++){\n if(i==k)q.push(-arr[i]);\n else q.push(arr[i]);\n }\n while(q.size()>0){\n int a=q.front();\n if(a<0){\n a++;\n count++;\n q.pop();\n if(a==0)return count;\n q.push(a);\n }\n else if(a>0){\n a--;\n count++;\n q.pop();\n if(a!=0)q.push(a);\n }\n\n }\n return count;\n // while(arr[k]>0){\n // for(int i=0;i<n;i++){\n // if(arr[i]>0){\n // arr[i]--;\n // count++;\n\n // if(arr[k]==0){\n // return count;\n // }\n // }\n // }\n // }\n return count;\n }\n};",
"memory": "10300"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int>q;\n\n for( int i=0;i<tickets.size();i++){\n q.push(i);\n }\n\n int time =0;\n while(!q.empty()){\n time++;\n int index =q.front();\n q.pop();\n tickets[index]--;\n\n if(index==k && tickets[index]== 0){\n return time;\n }\n if(tickets[index]){\n q.push(index);\n }\n }\n \n return time;\n }\n};",
"memory": "10400"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int>q;\n int time=0;\n for(int i=0;i<tickets.size();i++){\n q.push(i);\n }\n\n while(tickets[k]!=0){\n tickets[q.front()]--;\n if(tickets[q.front()]){\n q.push(q.front());\n }\n q.pop();\n time++;\n }\n\n return time;\n }\n};",
"memory": "10400"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int>q;\n int n=tickets.size();\n int time=0;\n for(int i=0;i<n;i++)\n {\n q.push(i);\n }\n while(tickets[k]!=0)\n {\n tickets[q.front()]--;\n if(tickets[q.front()])\n q.push(q.front());\n\n q.pop();\n time++;\n }\nreturn time;\n }\n};",
"memory": "10500"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int>q;\n int n=tickets.size();\n for(int i=0;i<n;i++){\n q.push(i);\n }\n int time=0;\n while(tickets[k]!=0){\n tickets[q.front()]--;\n if(tickets[q.front()]){\n q.push(q.front());\n }\n q.pop();\n time++;\n }\n\n return time;\n \n }\n};",
"memory": "10500"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int>q;\n int n=tickets.size();\n \n for(int i=0;i<n;i++)\n q.push(i);\n int time=0;\n while(tickets[k]!=0){\n tickets[q.front()]--;\n if(tickets[q.front()])\n q.push(q.front());\n q.pop();\n time++;\n }\n \n return time;\n \n }\n};",
"memory": "10600"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n return simul(tickets, k);\n }\n int simul(vector<int>& tickets, int k) {\n std::queue<int> line;\n \n for (int i : tickets) line.push(i);\n \n int counter = 0;\n int time = 0;\n while (tickets[k]) {\n int front = line.front();\n if (front) {\n time++;\n front--;\n }\n line.pop();\n line.push(front);\n tickets[counter]--;\n counter++;\n if (counter == line.size()) counter = 0;\n }\n return time;\n }\n};",
"memory": "10700"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n int res = 0;\n queue<int> list;\n for (int i = 0; i < tickets.size(); i++) {\n --tickets[i];\n res++;\n list.push(tickets[i]);\n if (k == i && tickets[i] == 0)\n return res;\n }\n int count=0,item; \n while (true) {\n cout<<count<<\" \";\n item=list.front();\n if (item==0) {\n list.pop();\n list.push(item);\n if (count==tickets.size()) count=tickets.size()-count;\n count++;\n continue;\n }\n item--;\n res++;\n if (count==tickets.size()) count=tickets.size()-count;\n if (count==k) {\n // cout<<item<<endl;\n if (item==0) break;\n } \n count++;\n list.pop();\n list.push(item);\n }\n return res;\n }\n};",
"memory": "10800"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n int res = 0;\n queue<int> list;\n for (int i = 0; i < tickets.size(); i++) {\n --tickets[i];\n res++;\n list.push(tickets[i]);\n if (k == i && tickets[i] == 0)\n return res;\n }\n int count=0,item; \n while (true) {\n cout<<count<<\" \";\n item=list.front();\n if (item==0) {\n list.pop();\n list.push(item);\n if (count==tickets.size()) count=tickets.size()-count;\n count++;\n continue;\n }\n item--;\n res++;\n if (count==tickets.size()) count=tickets.size()-count;\n if (count==k && item==0) break;\n count++;\n list.pop();\n list.push(item);\n }\n return res;\n }\n};",
"memory": "10800"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int> q;\n\n for (int i = 0; i < tickets.size(); i++) {\n q.push(tickets[i]);\n }\n\n int index = k, ans = 1;\n while (!(index == 0 && q.front() == 1)) {\n if (q.front() == 1) q.pop();\n else {\n q.push(q.front() - 1);\n q.pop();\n }\n\n if (index == 0)\n index += q.size() - 1;\n else\n index--;\n\n ans++;\n }\n\n return ans;\n }\n};",
"memory": "10900"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int> q;\n\n for (int i = 0; i < tickets.size(); i++) {\n q.push(tickets[i]);\n }\n\n int index = k, ans = 1;\n while (!(index == 0 && q.front() == 1)) {\n if (q.front() == 1) q.pop();\n else {\n q.push(q.front() - 1);\n q.pop();\n }\n\n if (index == 0)\n index += q.size() - 1;\n else\n index--;\n\n ans++;\n }\n\n return ans;\n }\n};",
"memory": "11000"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int> q;\n\n for (int i = 0; i < tickets.size(); i++) {\n q.push(tickets[i]);\n }\n\n int index = k, ans = 1;\n while (!(index == 0 && q.front() == 1)) {\n if (q.front() == 1) q.pop();\n else {\n q.push(q.front() - 1);\n q.pop();\n }\n\n if (index == 0)\n index += q.size() - 1;\n else\n index--;\n\n ans++;\n }\n\n return ans;\n }\n};",
"memory": "11100"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int> q;\n\n for (int i = 0; i < tickets.size(); i++) {\n q.push(tickets[i]);\n }\n\n int index = k, ans = 1;\n while (!(index == 0 && q.front() == 1)) {\n if (q.front() == 1) q.pop();\n else {\n q.push(q.front() - 1);\n q.pop();\n }\n\n if (index == 0)\n index += q.size() - 1;\n else\n index--;\n\n ans++;\n }\n\n return ans;\n }\n};",
"memory": "11100"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int> q;\n\n for (int i = 0; i < tickets.size(); i++) {\n q.push(tickets[i]);\n }\n\n int index = k, ans = 1;\n while (!(index == 0 && q.front() == 1)) {\n if (q.front() == 1) q.pop();\n else {\n q.push(q.front() - 1);\n q.pop();\n }\n\n if (index == 0)\n index += q.size() - 1;\n else\n index--;\n\n ans++;\n }\n\n return ans;\n }\n};",
"memory": "11200"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n vector<int> temp(tickets.size(), 0);\n unordered_map<int, int> time;\n int sum = 0;\n int answer = 0;\n while (tickets != temp) {\n\n for (int i = 0; i < tickets.size(); i++) {\n\n if (tickets[i] > 0) {\n sum++;\n tickets[i] = tickets[i] - 1;\n time[i] = sum;\n }\n }\n }\n for (auto i : time) {\n if (i.first == k) {\n answer = i.second;\n break;\n }\n }\n return answer;\n }\n};",
"memory": "11300"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int> q;\n int n = tickets.size();\n int time = 0;\n for(int i = 0; i < n; i++) {\n q.push(tickets[i]);\n }\n int count = 0;\n while(true) {\n \n if(count == k && q.front() == 1){\n break;\n }\n \n if(q.front() != 0) {\n\n time++;\n }\n q.push(q.front() != 0 ? q.front()-1 : 0);\n q.pop();\n count = (count+1)%n;\n }\n return time+1;\n }\n};",
"memory": "11400"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int> qt;\n int time = 0;\n\n int i = 0;\n\n for (int i =0 ; i< tickets.size(); i++) {\n qt.push(tickets[i]);\n }\n while (tickets[k] != 0) {\n if (qt.front() != 0) {\n cout << \"qt.front()\" << qt.front();\n qt.push(qt.front() - 1);\n tickets[i]--;\n qt.pop();\n time++;\n\n cout << \"time\" << time;\n } else if (qt.front() == 0) {\n qt.push(qt.front());\n qt.pop();\n // tickets is 0\n }\n\n if (i == tickets.size() - 1) {\n i = 0;\n } else {\n i++;\n }\n }\n\n return time;\n }\n};",
"memory": "11500"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<int> qt;\n int time = 0;\n\n int i = 0;\n\n for (int i =0 ; i< tickets.size(); i++) {\n qt.push(tickets[i]);\n }\n while (tickets[k] != 0) {\n if (qt.front() != 0) {\n qt.push(qt.front() - 1);\n tickets[i]--;\n qt.pop();\n time++;\n } else if (qt.front() == 0) {\n qt.push(qt.front());\n qt.pop();\n // tickets is 0\n }\n\n if (i == tickets.size() - 1) {\n i = 0;\n } else {\n i++;\n }\n }\n\n return time;\n }\n};",
"memory": "11500"
} |
2,195 | <p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tickets = [2,3,2], k = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
<strong>Output:</strong> 8
<strong>Explanation:</strong>
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int timeRequiredToBuy(vector<int>& tickets, int k) {\n queue<pair<int,int>> Q;\n for(int i = 0 ;i<tickets.size();i++)\n {\n Q.push({tickets[i],i});\n }\n int Second = 0;\n while(!Q.empty())\n {\n auto Front = Q.front();\n if(Front.second==k && Front.first==1) return Second+1;\n else if(Front.second!=k && Front.first==1) {Q.pop();Second++;continue;}\n Q.pop();\n Front.first--;\n Q.push(Front);\n Second++;\n }\n return -1;\n }\n};",
"memory": "11600"
} |
152 | <p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>
<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-10 <= nums[i] <= 10</code></li>
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n int maxi1=INT_MIN;\n int maxi2=INT_MIN;\n int prod=1;\n for(int i=0;i<nums.size();i++){\n prod=prod*nums[i];\n maxi1=max(prod,maxi1);\n if(prod==0){\n prod=1;\n }\n }\n prod=1;\n for(int i=nums.size()-1;i>=0;i--){\n prod=prod*nums[i];\n maxi2=max(prod,maxi2);\n if(prod==0){\n prod=1;\n }\n }\n return max(maxi1,maxi2);\n }\n};",
"memory": "16200"
} |
152 | <p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>
<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-10 <= nums[i] <= 10</code></li>
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n\n int n = nums.size();\n\n int m1 = INT_MIN;\n int m2 = INT_MIN;\n \n int prefix = 1;\n int suffix = 1;\n\n int j = n-1;\n\n for(int i = 0; i < n; i ++)\n {\n prefix *= nums[i];\n m1 = max(m1, prefix);\n\n if(prefix == 0) prefix = 1;\n\n suffix *= nums[j];\n m2 = max(m2, suffix);\n\n if(suffix == 0) suffix = 1;\n\n j --;\n }\n\n return max(m1,m2);\n \n }\n};",
"memory": "16300"
} |
152 | <p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>
<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-10 <= nums[i] <= 10</code></li>
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n int n=nums.size();\n if(n==0){return 0;}\n if(n==1){return nums[0];}\n int ans=INT_MIN;\n for(int i=0;i<n;i++)\n {\n int prod=nums[i];\n for(int j=i+1;j<n;j++)\n {\n ans=max(ans,prod);\n prod*=nums[j];\n ans=max(ans,prod);\n }\n }\n return max(ans,nums[n-1]);\n\n }\n};",
"memory": "16400"
} |
152 | <p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>
<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-10 <= nums[i] <= 10</code></li>
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n int maxProduct = nums[0];\n int minProduct = nums[0];\n int result = nums[0];\n for(int i=1; i<nums.size(); i++){\n if(nums[i] < 0){\n swap(maxProduct, minProduct);\n }\n maxProduct = max(nums[i], nums[i]*maxProduct);\n minProduct = min(nums[i], nums[i]*minProduct);\n result = max(maxProduct, result);\n }\n return result;\n }\n};",
"memory": "16400"
} |
152 | <p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>
<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-10 <= nums[i] <= 10</code></li>
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n int pre = 1, suff=1;\n int ans = INT_MIN;\n int n = nums.size();\n for(int i = 0;i<n;i++)\n {\n if(pre==0) pre = 1;\n if(suff==0) suff = 1;\n\n pre = pre * nums[i];\n suff = suff * nums[n-i-1];\n ans = max(ans,max(pre,suff));\n }\n return ans;\n }\n};",
"memory": "16500"
} |
152 | <p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>
<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-10 <= nums[i] <= 10</code></li>
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n // Intuitive Approah\n int n = nums.size();\n int prefix = nums[0];\n int prod = nums[0];\n for (int i = 1; i < n; i++) {\n if (prod == 0)\n prod = 1;\n prod *= nums[i];\n prefix = max(prefix, prod);\n }\n prod = nums[n - 1];\n int suffix = nums[n - 1];\n for (int i = n - 2; i >= 0; i--) {\n if (prod == 0)\n prod = 1;\n prod *= nums[i];\n suffix = max(suffix, prod);\n }\n return max(prefix, suffix);\n }\n};",
"memory": "16500"
} |
152 | <p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>
<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-10 <= nums[i] <= 10</code></li>
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n int result = nums[0];\n int currmax = nums[0], currmin = nums[0];\n\n for (int i = 1; i < nums.size(); i++) {\n int tempmax = currmax;\n currmax = max(nums[i], max(currmax * nums[i], currmin * nums[i]));\n currmin = min(nums[i], min(tempmax * nums[i], currmin * nums[i]));\n result = max(result, currmax);\n }\n return result;\n }\n};",
"memory": "16600"
} |
152 | <p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>
<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-10 <= nums[i] <= 10</code></li>
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n int n=nums.size();\n int maxi=INT_MIN;\n int pre=1;\n int suff=1;\n for(int i=0;i<n;i++){\n if(pre==0){\n pre=1;\n }\n if(suff==0){\n suff=1;\n }\n pre=pre*nums[i];\n suff=suff*nums[n-i-1];\n maxi=max(maxi,max(pre,suff));\n }\n return maxi;\n }\n};",
"memory": "16600"
} |
152 | <p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>
<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-10 <= nums[i] <= 10</code></li>
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n int ans=INT_MIN;\n for(int i=0;i<nums.size();i++){\n int p=nums[i];\n for(int j=i+1;j<nums.size();j++){\n ans=max(ans,p);\n p*=nums[j];\n }\n ans=max(ans,p);\n }\n return ans;\n }\n};",
"memory": "16700"
} |
152 | <p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>
<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-10 <= nums[i] <= 10</code></li>
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) \n {\n int n=nums.size();\n int pro1=1,pro2=1,ans1=-1e9,ans2=-1e9;\n for(int i=0;i<n;i++)\n {\n pro1=pro1*nums[i];\n ans1=max(ans1,pro1);\n if(pro1==0){\n pro1=1;\n }\n }\n for(int i=n-1;i>=0;i--)\n {\n pro2=pro2*nums[i];\n ans2=max(ans2,pro2);\n if(pro2==0){\n pro2=1;\n }\n }\n return max(ans1,ans2);\n }\n};",
"memory": "16700"
} |
152 | <p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>
<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-10 <= nums[i] <= 10</code></li>
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int> A) {\n int n = A.size(), res = A[0], l = 0, r = 0;\n for (int i = 0; i < n; i++) {\n l = (l ? l : 1) * A[i];\n r = (r ? r : 1) * A[n - 1 - i];\n res = max(res, max(l, r));\n }\n return res;\n }\n \n};",
"memory": "16800"
} |
152 | <p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p>
<p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,3,-2,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [2,3] has the largest product 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-2,0,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-10 <= nums[i] <= 10</code></li>
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n int prefix=1,suffix=1,n=nums.size();\n int ans=INT_MIN;\n for(int i=0;i<n;i++){\n if(prefix==0) prefix=1;\n if(suffix==0) suffix=1;\n prefix*=nums[i];\n suffix*=nums[n-i-1];\n ans=max(ans,max(suffix,prefix));\n }\n return ans;\n }\n};",
"memory": "16800"
} |
153 | <p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,2,4,5,6,7]</code> might become:</p>
<ul>
<li><code>[4,5,6,7,0,1,2]</code> if it was rotated <code>4</code> times.</li>
<li><code>[0,1,2,4,5,6,7]</code> if it was rotated <code>7</code> times.</li>
</ul>
<p>Notice that <strong>rotating</strong> an array <code>[a[0], a[1], a[2], ..., a[n-1]]</code> 1 time results in the array <code>[a[n-1], a[0], a[1], a[2], ..., a[n-2]]</code>.</p>
<p>Given the sorted rotated array <code>nums</code> of <strong>unique</strong> elements, return <em>the minimum element of this array</em>.</p>
<p>You must write an algorithm that runs in <code>O(log n) time</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,1,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The original array was [1,2,3,4,5] rotated 3 times.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,5,6,7,0,1,2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [11,13,15,17]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The original array was [11,13,15,17] and it was rotated 4 times.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= n <= 5000</code></li>
<li><code>-5000 <= nums[i] <= 5000</code></li>
<li>All the integers of <code>nums</code> are <strong>unique</strong>.</li>
<li><code>nums</code> is sorted and rotated between <code>1</code> and <code>n</code> times.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findMin(vector<int>& nums) {\n int low = 0, high = nums.size() - 1, middle;\n if (nums[high] >= nums[low])\n return nums[low];\n if (nums.empty()) return -1;\n\n while (low <= high) {\n middle = low + (high - low) / 2;\n if (middle < nums.size() - 1 && nums[middle] > nums[middle + 1])\n return nums[middle + 1];\n if (middle >= 0 && nums[middle] < nums[middle - 1]) {\n return nums[middle];\n }\n\n if (nums[middle] >= nums[low]) {\n low = middle + 1;\n } else\n high = middle - 1;\n }\n return -1;\n }\n};",
"memory": "13000"
} |
153 | <p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,2,4,5,6,7]</code> might become:</p>
<ul>
<li><code>[4,5,6,7,0,1,2]</code> if it was rotated <code>4</code> times.</li>
<li><code>[0,1,2,4,5,6,7]</code> if it was rotated <code>7</code> times.</li>
</ul>
<p>Notice that <strong>rotating</strong> an array <code>[a[0], a[1], a[2], ..., a[n-1]]</code> 1 time results in the array <code>[a[n-1], a[0], a[1], a[2], ..., a[n-2]]</code>.</p>
<p>Given the sorted rotated array <code>nums</code> of <strong>unique</strong> elements, return <em>the minimum element of this array</em>.</p>
<p>You must write an algorithm that runs in <code>O(log n) time</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,1,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The original array was [1,2,3,4,5] rotated 3 times.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,5,6,7,0,1,2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [11,13,15,17]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The original array was [11,13,15,17] and it was rotated 4 times.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= n <= 5000</code></li>
<li><code>-5000 <= nums[i] <= 5000</code></li>
<li>All the integers of <code>nums</code> are <strong>unique</strong>.</li>
<li><code>nums</code> is sorted and rotated between <code>1</code> and <code>n</code> times.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findMin(vector<int>& nums) {\n int l = 0;\n int h = nums.size()-1;\n int res = INT_MAX;\n\n while(l<=h){\n if(nums[l]<nums[h]){\n return min(res, nums[l]);\n }\n\n int m = (l+h)/2;\n res = min(res, nums[m]);\n if(nums[l]>nums[m]){\n h = m-1;\n }else{\n l = m+1;\n }\n }\n return res;\n }\n};",
"memory": "13000"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.