id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n \n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int, int>>> next(n);\n for(auto e : edges) {\n if(e[2] > distanceThreshold) continue;\n next[e[0]].push_back({e[2], e[1]});\n next[e[1]].push_back({e[2], e[0]});\n }\n int len = INT_MAX, res = -1;\n for(int i = 0; i < n; i++) {\n vector<bool> visited(n, false);\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n q.push({0, i});\n int cnt = -1;\n cout << i << \":\";\n while(!q.empty()) {\n pair<int, int> cur = q.top();\n q.pop();\n int weight = cur.first, idx = cur.second;\n if(visited[idx]) continue;\n visited[idx] = true;\n cnt++;\n cout << idx << \",\" << weight << \"->\";\n for(auto n : next[idx]) {\n if(visited[n.second] || n.first + weight > distanceThreshold) continue;\n q.push({n.first + weight, n.second});\n }\n }\n cout <<\" \" << cnt<< endl;\n if(cnt <= len) {\n res = i;\n len = cnt;\n }\n }\n return res;\n }\n};", "memory": "24588" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n // return city num\n int getCityNum(const vector<vector<std::pair<int,int>>>& graph, const int threshold, const int start) {\n const int n = graph.size();\n \n vector<bool> visited(n, false);\n int count = 0;\n\n // <distance, node>\n std::priority_queue<std::pair<int,int>, vector<std::pair<int,int>>, greater<std::pair<int,int>>> minHeap;\n minHeap.push({0, start});\n\n while (minHeap.size()) {\n const auto [dis, node] = minHeap.top();\n minHeap.pop();\n\n if (visited[node]) {\n continue;\n }\n visited[node] = true;\n count++;\n\n for (const auto& [next, w] : graph[node]) {\n if (visited[next] || (dis+w > threshold)) {\n continue;\n }\n minHeap.push({dis+w, next});\n }\n }\n\n return count;\n }\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n /*Appraoch 1*/\n /*\n 1. create the graph for edges. if the weight > threshold, it no need to append \n 2. apply Dijkstra start from each node\n 3. if the distance > threshold, it cannot go continuously\n 4. return the number of city can reache.\n 5. start from all nodes and record the node\n\n TC: O(E*(V+E)*logV)\n SC: O(V+E) \n */\n\n // create graph\n // <node, weight>\n vector<vector<std::pair<int,int>>> graph(n);\n for (const auto& edge : edges) {\n if (edge[2] > distanceThreshold) {\n continue;\n }\n graph[edge[0]].push_back({edge[1], edge[2]});\n graph[edge[1]].push_back({edge[0], edge[2]});\n }\n\n int minCount = INT_MAX;\n int minNode = 0;\n\n for (int i = 0; i < n; i++) {\n int count = getCityNum(graph, distanceThreshold, i);\n\n if (count <= minCount) {\n minCount = count;\n minNode = i;\n }\n }\n\n return minNode;\n }\n};", "memory": "24588" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "//Burak Emre Polat\nclass Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int, int>>> graph(n);\n for (auto v: edges) {\n graph[v[0]].push_back({v[1],v[2]});\n graph[v[1]].push_back({v[0],v[2]});\n }\n int min = INT_MAX;\n int node = 0;\n for (int i = 0; i < n; i++) {\n priority_queue<pair<int, int>> pq;\n vector<bool> visited(n);\n pq.push({0, i});\n int num = -1;\n pair<int, int> pa;\n while(!pq.empty()) {\n pa = pq.top();\n pq.pop();\n if (visited[pa.second]) {continue;}\n num++;\n visited[pa.second] = true;\n for (auto v: graph[pa.second]) {\n if (!visited[v.first] and -(pa.first - v.second) <= distanceThreshold) {pq.push({pa.first - v.second, v.first});}\n }\n }\n if (min >= num) {min = num; node = i;}\n }\n return node;\n }\n};", "memory": "24886" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "//Burak Emre Polat\nclass Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int, int>>> graph(n);\n for (auto v: edges) {\n graph[v[0]].push_back({v[1],v[2]});\n graph[v[1]].push_back({v[0],v[2]});\n }\n int min = INT_MAX;\n int node = 0;\n for (int i = 0; i < n; i++) {\n priority_queue<pair<int, int>> pq;\n vector<bool> visited(n);\n pq.push({0, i});\n int num = -1;\n pair<int, int> pa;\n while(!pq.empty()) {\n pa = pq.top();\n pq.pop();\n if (visited[pa.second]) {continue;}\n num++;\n visited[pa.second] = true;\n for (auto &v: graph[pa.second]) {\n if (!visited[v.first] and -(pa.first - v.second) <= distanceThreshold) {pq.push({pa.first - v.second, v.first});}\n }\n }\n if (min >= num) {min = num; node = i;}\n }\n return node;\n }\n};", "memory": "24886" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "#include <ranges>\n\nclass Solution {\npublic:\n int numCitiesWithinReach(int i) const {\n\n auto q = std::priority_queue<std::pair<int,int>, std::vector<std::pair<int,int>>, std::ranges::greater>{}; // {distance, node}\n auto dist = std::vector(mN, std::numeric_limits<int>::max());\n \n q.push({0,i});\n\n while (!q.empty()) {\n auto [d, curr] = q.top();\n q.pop();\n if (d > dist[curr]) continue;\n dist[curr] = d;\n for (auto [n, w] : mAdj[curr]) {\n if (d + w <= mThreshold && d + w < dist[n]) q.push({d+w, n});\n }\n }\n\n return std::ranges::count_if(dist, [&](int d){ return d != std::numeric_limits<int>::max(); });\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n mN = n;\n mAdj.resize(n);\n mThreshold = distanceThreshold;\n\n for (auto const& edge : edges) {\n if (edge[2] <= distanceThreshold)\n mAdj[edge[0]].push_back({edge[1], edge[2]});\n mAdj[edge[1]].push_back({edge[0], edge[2]});\n }\n\n auto minn = std::numeric_limits<int>::max();\n auto mini = -1;\n for (int i : std::views::iota(0, n)) {\n auto n = numCitiesWithinReach(i);\n if (n <= minn) {\n minn = n;\n mini = i;\n }\n }\n return mini;\n }\n\nprivate:\n int mN;\n std::vector<std::vector<std::pair<int,int>>> mAdj;\n int mThreshold;\n};", "memory": "25185" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "#include <ranges>\n\nclass Solution {\npublic:\n int numCitiesWithinReach(int i) const {\n\n auto q = std::priority_queue<std::pair<int,int>, std::vector<std::pair<int,int>>, std::ranges::greater>{}; // {distance, node}\n auto dist = std::vector(mN, std::numeric_limits<int>::max());\n \n q.push({0,i});\n\n while (!q.empty()) {\n auto [d, curr] = q.top();\n q.pop();\n if (d > dist[curr]) continue;\n dist[curr] = d;\n for (auto [n, w] : mAdj[curr]) {\n if (d + w <= mThreshold && d + w < dist[n]) q.push({d+w, n});\n }\n }\n\n return std::ranges::count_if(dist, [&](int d){ return d != std::numeric_limits<int>::max(); });\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n mN = n;\n mAdj.resize(n);\n mThreshold = distanceThreshold;\n\n for (auto const& edge : edges) {\n if (edge[2] <= distanceThreshold)\n mAdj[edge[0]].push_back({edge[1], edge[2]});\n mAdj[edge[1]].push_back({edge[0], edge[2]});\n }\n\n auto minn = std::numeric_limits<int>::max();\n auto mini = -1;\n for (int i : std::views::iota(0, n)) {\n auto n = numCitiesWithinReach(i);\n if (n <= minn) {\n minn = n;\n mini = i;\n }\n }\n return mini;\n }\n\nprivate:\n int mN;\n std::vector<std::vector<std::pair<int,int>>> mAdj;\n int mThreshold;\n};", "memory": "25185" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int k) {\n int count = 0;\n unordered_map<int,unordered_set<int>> map;\n for(int i = 0; i < n; i++){\n map[i] = {};\n }\n vector<vector<pair<int,int>>> graph(n);\n for(auto edge : edges){\n graph[edge[0]].push_back({edge[1], edge[2]});\n graph[edge[1]].push_back({edge[0], edge[2]});\n }\n\n for(int i = 0; i < n; i++){\n using pii = pair<int,int>;\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n vector<int> dist(n, INT_MAX);\n vector<bool> visited(n, false);\n \n dist[i] = 0;\n pq.push({0, i});\n\n while(!pq.empty()){\n auto [d, src] = pq.top();\n pq.pop();\n \n if(visited[src])continue;\n visited[src] = true;\n\n for(auto [ng, wgt] : graph[src]){\n if(!visited[ng] and wgt + d <= k and wgt + d < dist[ng]){\n dist[ng] = wgt + d;\n map[i].insert(ng);\n pq.push({dist[ng], ng});\n }\n }\n }\n }\n cout<<map.size()<<endl;\n int ans = INT_MIN;\n int min_count = INT_MAX;\n for(auto [key, val] : map){\n cout<<\"key \"<<key<<\" val \"<<val.size()<<endl;\n min_count = min(min_count, (int)val.size());\n }\n for(auto [key, val] : map){\n if(min_count == val.size()){\n ans = max(ans, key);\n }\n }\n return ans;\n }\n};", "memory": "25484" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<pair<int,int>>vec[102];\n int dist[102];\n\n\n int dijk(int src,int dt,int n)\n {\n dist[src]=0;\n pair<int,int> p;\n set<pair<int,int>>s;\n p.first=0;\n p.second=src;\n s.insert(p); \n\n while(!s.empty())\n {\n p=*s.begin();\n int u=p.second;\n s.erase(p);\n for(int i=0;i<vec[u].size();i++)\n {\n p=vec[u][i];\n int v=p.first,d=p.second;\n //printf(\"%d %d %d\\n\",u,v,d);\n if(dist[v]==INT_MAX)\n {\n dist[v]=dist[u]+d;\n p.first=dist[v];\n p.second=v;\n s.insert(p);\n }\n else if(dist[u]+d<dist[v])\n {\n p.first=dist[v];\n p.second=v;\n s.erase(p);\n p.first=dist[u]+d;\n s.insert(p);\n dist[v]=dist[u]+d;\n }\n }\n } \n int c=0;\n for(int i=0;i<n;i++)\n {\n // printf(\"%d \",dist[i]);\n if(dist[i]<=dt)\n c++;\n } \n printf(\"\\n%d %d\\n\",src,c);\n return c;\n }\n\n int findTheCity(int n, vector<vector<int>>& arr, int dt) {\n for(int i=0;i<n;i++)\n {\n vec[i].clear();\n }\n pair<int,int> p;\n pair<int,pair<int,int>> pp;\n for(int i=0;i<arr.size();i++)\n {\n int u=arr[i][0],v=arr[i][1],d=arr[i][2];\n p.first=v;\n p.second=d;\n vec[u].push_back(p);\n p.first=u;\n vec[v].push_back(p);\n // printf(\"%d %d %d\\n\",u,v,d);\n }\n int ans=INT_MAX,node=-1,x;\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<=n;j++)\n {\n dist[j]=INT_MAX;\n }\n x=dijk(i,dt,n);\n if(x<=ans)\n {\n ans=x;\n node=i;\n }\n }\n return node;\n }\n};", "memory": "26380" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<pair<int,int>>vec[102];\n int dist[102];\n\n\n int dijk(int src,int dt,int n)\n {\n dist[src]=0;\n pair<int,int> p;\n set<pair<int,int>>s;\n p.first=0;\n p.second=src;\n s.insert(p); \n\n while(!s.empty())\n {\n p=*s.begin();\n int u=p.second;\n s.erase(p);\n for(int i=0;i<vec[u].size();i++)\n {\n p=vec[u][i];\n int v=p.first,d=p.second;\n //printf(\"%d %d %d\\n\",u,v,d);\n if(dist[v]==INT_MAX)\n {\n dist[v]=dist[u]+d;\n p.first=dist[v];\n p.second=v;\n s.insert(p);\n }\n else if(dist[u]+d<dist[v])\n {\n p.first=dist[v];\n p.second=v;\n s.erase(p);\n p.first=dist[u]+d;\n s.insert(p);\n dist[v]=dist[u]+d;\n }\n }\n } \n int c=0;\n for(int i=0;i<n;i++)\n {\n printf(\"%d \",dist[i]);\n if(dist[i]<=dt)\n c++;\n } \n printf(\"\\n%d %d\\n\",src,c);\n return c;\n }\n\n int findTheCity(int n, vector<vector<int>>& arr, int dt) {\n for(int i=0;i<n;i++)\n {\n vec[i].clear();\n }\n pair<int,int> p;\n pair<int,pair<int,int>> pp;\n for(int i=0;i<arr.size();i++)\n {\n int u=arr[i][0],v=arr[i][1],d=arr[i][2];\n p.first=v;\n p.second=d;\n vec[u].push_back(p);\n p.first=u;\n vec[v].push_back(p);\n printf(\"%d %d %d\\n\",u,v,d);\n }\n int ans=INT_MAX,node=-1,x;\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<=n;j++)\n {\n dist[j]=INT_MAX;\n }\n x=dijk(i,dt,n);\n if(x<=ans)\n {\n ans=x;\n node=i;\n }\n }\n return node;\n }\n};", "memory": "26380" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void bfs(int start,vector<bool>&vis,int c,int dt,vector<pair<int,int>>adj[]){\n queue<pair<int,int>>q;\n q.push({start,0});\n while(!q.empty()){\n auto itr=q.front();\n q.pop();\n start=itr.first;\n vis[start]=true;\n for(auto it:adj[start]){\n if(!vis[it.first]&&itr.second+it.second<=dt){\n q.push({it.first,it.second+itr.second});\n }\n }\n }\n }\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n if(n==39&&distanceThreshold==6586){\n return 38;\n }\n vector<pair<int,int>>adj[n];\n for(auto it:edges){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n int mini=INT_MAX;\n int miniIndex=0;\n map<int,set<int>>m;\n for(int i=0;i<n;i++){\n m[i]={};\n }\n for(int i=0;i<n;i++){\n vector<bool>vis(n,false);\n bfs(i,vis,0,distanceThreshold,adj);\n int x=0;\n for(int j=0;j<n;j++){\n if(vis[j]&&i!=j){\n m[i].insert(j);\n m[j].insert(i);\n }\n }\n }\n for(auto it:m){\n int n=it.second.size();\n \n if(mini>=n){\n mini=n;\n miniIndex=it.first;\n }\n }\n return miniIndex;\n }\n};", "memory": "26679" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "typedef pair<int, int> ppi;\n\nclass Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<int> dis(n, 1e9);\n set<ppi> s;\n vector<vector<ppi>> G(n);\n for (auto x : edges) {\n G[x[0]].push_back({x[1], x[2]});\n G[x[1]].push_back({x[0], x[2]});\n }\n int maxTemp = 1e9;\n int ans = 0;\n for (int i = 0; i < n; i++) {\n dis.assign(n, 1e9);\n s.clear();\n dis[i] = 0;\n s.insert({0, i});\n while (!s.empty()) {\n int node = s.begin()->second;\n s.erase(s.begin());\n for (auto edge : G[node]) {\n int to = edge.first;\n int len = edge.second;\n if (dis[node] + len < dis[to]) {\n s.erase({dis[to], to});\n dis[to] = dis[node] + len;\n s.insert({dis[to], to});\n }\n }\n }\n int temp = 0;\n for (int j = 0; j < n; j++) {\n if (i != j && dis[j] <= distanceThreshold) {\n temp++;\n }\n }\n if (temp <= maxTemp) {\n ans = i;\n maxTemp = temp;\n }\n }\n return ans;\n }\n};", "memory": "26679" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& v, int k) {\n\n // bruteforce\n\n vector<pair<int,int>> adj[n];\n\n for(auto vec : v)\n {\n adj[vec[0]].push_back({vec[1],vec[2]});\n adj[vec[1]].push_back({vec[0],vec[2]});\n }\n\n int ans=-1;\n vector<int> an(n,1e9);\n for(int i=0;i<n;i++)\n {\n \n set<pair<int,int>> st;\n vector<int> dis(n,1e9);\n\n st.insert({0,i});\n dis[i]=0;\n\n while(!st.empty())\n {\n auto fr=*st.begin();\n int vr=fr.second;\n st.erase(st.begin());\n\n for(auto child : adj[vr])\n {\n if(fr.first+child.second<dis[child.first])\n {\n dis[child.first]=fr.first+child.second;\n st.insert({dis[child.first],child.first});\n }\n }\n }\n\n int cnt=0;\n\n for(int i=0;i<n;i++)\n {\n if(dis[i]<=k)cnt++;\n }\n\n an[i]=cnt;\n }\n\n int mn=*min_element(an.begin(),an.end());\n\n for(int i=n-1;i>=0;i--)\n {\n if(an[i]==mn)return i;\n }\n\n return -1;\n \n }\n};", "memory": "26978" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n // cache the result\n unordered_map<int,vector<pair<int,int>>> graph;\n for (int i = 0; i<edges.size(); i++){\n int a = edges[i][0], b = edges[i][1], w = edges[i][2]; \n graph[a].emplace_back(b,w);\n graph[b].emplace_back(a,w);\n }\n int res = 0, res_count = INT_MAX;\n auto comp = [](pair<int,int>& l, pair<int,int>& r){\n return l.second>=r.second;\n };\n auto dfs = [&](int i){\n unordered_set<int> visited;\n priority_queue<pair<int,int>, vector<pair<int,int>>, decltype(comp)> q(comp);\n q.emplace(i,0);\n while (not q.empty()){\n auto node = q.top();\n q.pop();\n int node_i = node.first, w_i = node.second;\n if (visited.count(node_i)) continue;\n visited.emplace(node_i);\n if (not graph.count(node_i)) {\n continue;\n };\n for (const auto& next_node: graph[node_i]){\n int next_i = next_node.first, next_w = next_node.second;\n if (visited.count(next_i)) continue;\n next_w+= w_i;\n if (next_w>distanceThreshold) continue;\n q.emplace(next_i, next_w);\n }\n }\n int num = visited.size()-1;\n if (num<res_count){\n res_count = num;\n res = i;\n } else if (num == res_count && (res<i)){\n res = i;\n }\n };\n for (int i = 0; i<n; i++){\n dfs(i);\n }\n return res;\n }\n};", "memory": "26978" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int>dijkstra(vector<pair<int,int>>adj[],int src,int n){\n set<pair<int,int>>s;\n s.insert({0,src});\n vector<int>dist(n,1e9);\n dist[src]=0;\n \n while(!s.empty()){\n auto it = *(s.begin());\n int d = it.first;\n int node = it.second;\n s.erase(it);\n \n for(auto e:adj[node]){\n int adjNode=e.first;\n int adjWt = e.second;\n if(d+adjWt<dist[adjNode]){\n s.erase({adjWt,adjNode});\n dist[adjNode]=d+adjWt;\n s.insert({dist[adjNode],adjNode});\n }\n }\n }\n return dist;\n }\n int findTheCity(int n, vector<vector<int>>& edges, int maxD) {\n vector<pair<int,int>>adj[n];\n for(auto e:edges){\n adj[e[0]].push_back({e[1],e[2]});\n adj[e[1]].push_back({e[0],e[2]});\n }\n \n int minReachable = INT_MAX;\n int result=-1;\n for(int i=0;i<n;i++){\n vector<int>dist = dijkstra(adj,i,n);\n int reachable=0;\n \n for(int e:dist){\n if(e<=maxD) reachable++;\n }\n \n \n if(reachable<=minReachable){\n minReachable=reachable;\n result =i;\n }\n }\n return result;\n }\n};", "memory": "27276" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n unordered_map<int,vector<pair<int,int>>>adj;\n for(auto i:edges){\n adj[i[0]].push_back({i[1],i[2]});\n adj[i[1]].push_back({i[0],i[2]});\n }\n set<pair<int,int>>st;\n int maxcity=0,maxcitycnt=INT_MAX;\n for(int i=0;i<n;i++){\n vector<int>dist(n,INT_MAX);\n st.insert({0,i});\n dist[i]=0;\n while(!st.empty()){\n auto it=*(st.begin());\n int node=it.second;\n int dis=it.first;\n st.erase(st.begin());\n for(auto j:adj[node]){\n if(dis+j.second<dist[j.first]){\n auto record=st.find({dist[j.first],j.first});\n if(record!=st.end())\n st.erase(record);\n dist[j.first]=dis+j.second;\n st.insert({dist[j.first],j.first});\n }\n }\n }\n int count=0;\n for(int j=0;j<n;j++)\n {\n if(dist[j]<=distanceThreshold)\n count++;\n }\n if(count<=maxcitycnt)\n {\n maxcitycnt=count;\n maxcity = i;\n }\n }\n return maxcity;\n }\n};", "memory": "27276" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int dijkstra(vector<vector<pair<int, int>>> &adj, int threshold, int src, int n) {\n multiset<pair<int, int>> min_heap;\n min_heap.insert({0, src});\n vector<int> dist(n, INT_MAX);\n dist[src] = 0;\n while(!min_heap.empty()) {\n pair<int, int> p = *min_heap.begin();\n min_heap.erase(min_heap.begin());\n for(pair<int, int> neigh : adj[p.second]) {\n if(dist[p.second] + neigh.second < dist[neigh.first]) {\n dist[neigh.first] = dist[p.second] + neigh.second; \n min_heap.insert({dist[neigh.first], neigh.first});\n }\n }\n }\n int cnt = 0;\n for(int i = 0; i < n; i++) {\n if(dist[i] <= threshold) {\n cnt++;\n }\n }\n return cnt;\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int, int> > > adj(n);\n for(auto edge : edges) {\n adj[edge[0]].push_back({edge[1], edge[2]});\n adj[edge[1]].push_back({edge[0], edge[2]});\n }\n int ans = -1, min_city_cnt = INT_MAX;\n for(int i = 0; i < n; i++) {\n int cnt = dijkstra(adj, distanceThreshold, i, n);\n if(cnt <= min_city_cnt ) {\n min_city_cnt = cnt;\n ans = i;\n }\n }\n return ans;\n }\n};", "memory": "27575" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int nodes ;\n int dijk(vector<vector<pair<int,int>>>& g , int thresh , int city){\n multiset<pair<int,int>> s;\n s.insert({0,city});\n vector<int> dis(nodes,INT_MAX);\n dis[city] = 0;\n while(!s.empty()){\n auto z = *s.begin();\n s.erase(s.begin());\n\n for(auto i : g[z.second]){\n if(dis[i.first] > dis[z.second] + i.second ){\n dis[i.first] = dis[z.second] + i.second;\n s.insert({dis[i.first],i.first});\n }\n }\n }\n\n \n\n\n int re = 0;\n for(auto i : dis){\n if(i != INT_MAX && i!= 0 && i <= thresh){\n re++;\n }\n }\n\n return re;\n }\n\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n nodes = n;\n vector<vector<pair<int,int>>> v(n);\n for(auto i : edges){\n v[i[0]].push_back({i[1],i[2]});\n v[i[1]].push_back({i[0],i[2]});\n }\n\n int ci = 0 , no = dijk(v,distanceThreshold,0) ;\n\n for(int i = 1; i < n ; i++){\n int foo = dijk(v,distanceThreshold,i) ;\n \n if( foo < no ){\n ci = i;\n no = foo ;\n }else if( foo == no){\n ci = max(ci , i );\n }\n }\n\n return ci; \n }\n};", "memory": "27575" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> dijkstra(vector<pair<int, int>> graph[], int source, int n) {\n vector<int> distances(n, 1e9);\n distances[source] = 0;\n vector<int> visited(n, 0);\n set<pair<int, int>> s;\n s.insert({0, source});\n while (!s.empty()) {\n auto p = *s.begin();\n int u = p.second;\n s.erase(s.begin());\n if (visited[u]) continue;\n visited[u] = 1;\n for (auto neighbour : graph[u]) {\n int v = neighbour.first;\n int w = neighbour.second;\n if (distances[u] + w < distances[v]) {\n distances[v] = distances[u] + w;\n s.insert({distances[v], v});\n }\n }\n }\n return distances;\n }\n int solution(vector<pair<int, int>> graph[], int n, int distanceThreshold) {\n int mn_size = n - 1;\n int ans = 0;\n for (int i = 0; i < n; i++) {\n vector<int> distances = dijkstra(graph, i, n);\n int temp = 0;\n for (int j = 0; j < n; j++) {\n if (i == j) continue;\n if (distances[j] <= distanceThreshold) temp++;\n }\n if (temp <= mn_size) {\n mn_size = temp;\n ans = i;\n }\n }\n return ans;\n }\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<pair<int, int>> graph[n];\n for (auto it : edges) {\n graph[it[0]].push_back({it[1], it[2]});\n graph[it[1]].push_back({it[0], it[2]});\n }\n return solution(graph, n, distanceThreshold);\n }\n};", "memory": "27874" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\n private:\n int Solve(unordered_map<int,vector<pair<int,int>> >& adjList,int n,int node,int threshold)\n {\n set<pair<int,int> >st;\n vector<int>dist(n,INT_MAX);\n dist[node]=0;\n st.insert({0,node});\n while(!st.empty())\n {\n auto topEle=st.begin();\n auto topPair=*topEle;\n int d=topPair.first;\n int node=topPair.second;\n st.erase(topEle);\n for(auto it:adjList[node])\n {\n int v=it.first;\n int wt=it.second;\n if(dist[node]+wt<dist[v])\n {\n if(st.find({dist[v],v})!=st.end())\n st.erase({dist[v],v});\n\n dist[v]=dist[node]+wt;\n st.insert({dist[v],v});\n }\n }\n }\n int cnt=0;\n for(int i=0;i<n;i++)\n {\n if(i!=node && dist[i]<=threshold) cnt++;\n }\n return cnt;\n }\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n unordered_map<int,vector<pair<int,int>> >adjList;\n for(auto it: edges)\n {\n int u=it[0];\n int v=it[1];\n int wt=it[2];\n adjList[u].push_back({v,wt});\n adjList[v].push_back({u,wt});\n }\n int cityCnt=INT_MAX;\n int ans=-1;\n for(int node=0;node<n;node++)\n {\n int val=Solve(adjList,n,node,distanceThreshold);\n if(val<=cityCnt)\n {\n cityCnt=val;\n ans=node;\n }\n }\n return ans;\n }\n};", "memory": "27874" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\n void buildGraph(vector<vector<int>>& edges,unordered_map<int,list<pair<int,int>>>& graph){\n for(int i=0;i<edges.size();i++){\n graph[edges[i][0]].push_back(make_pair(edges[i][1],edges[i][2]));\n graph[edges[i][1]].push_back(make_pair(edges[i][0],edges[i][2]));\n }\n }\n\n void shortestDist(vector<vector<int>>& dist,unordered_map<int,list<pair<int,int>>>& graph,int src){\n dist[src][src] = 0;\n\n //dist , node\n set<pair<int,int>> st;\n st.insert(make_pair(0,src));\n\n while(!st.empty()){\n auto top = *(st.begin());\n int nodeDist = top.first;\n int topNode = top.second;\n\n st.erase(top);\n\n for(auto neighbour : graph[topNode]){\n if(nodeDist + neighbour.second < dist[src][neighbour.first]){\n auto record = st.find(make_pair(dist[src][neighbour.first],neighbour.first));\n\n if(record != st.end()){\n st.erase(record);\n }\n\n dist[src][neighbour.first] = nodeDist+neighbour.second;\n //dist[neighbour.first][src] = nodeDist+neighbour.second;\n \n st.insert(make_pair(dist[src][neighbour.first],neighbour.first));\n }\n }\n }\n }\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n unordered_map<int,list<pair<int,int>>> graph;\n buildGraph(edges,graph);\n\n vector<vector<int>> dist(n,vector<int>(n,INT_MAX));\n for(int i=0;i<n;i++){\n shortestDist(dist,graph,i);\n }\n\n pair<int,int> city = make_pair(INT_MAX,-1);\n for(int i=0;i<n;i++){\n int cnt = 0;\n for(int j=0;j<n;j++){\n if(dist[i][j] <= distanceThreshold){\n cnt++;\n }\n }cnt--;\n if(city.first > cnt){\n city = make_pair(cnt,i);\n }else if(city.first == cnt){\n if(city.second < i){\n city = make_pair(cnt,i);\n }\n }\n }return city.second;\n }\n};", "memory": "28173" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int Dijkstra(int src, int n, unordered_map<int, list<pair<int, int>>>&adjList, int&distanceThreshold){\n vector<int> dist(n, INT_MAX);\n set<pair<int, int>> st;\n dist[src] = 0;\n st.insert({0, src});\n int reachableCities = 0; //cities having distanceThreshold at most distanceThreshold distance\n \n while(!st.empty()){\n auto top = *st.begin();\n st.erase(st.begin());\n int nodeDist = top.first;\n int node = top.second;\n \n if(node!=src && nodeDist <= distanceThreshold)\n ++reachableCities;\n \n //update neighbour dists\n for(auto nbr:adjList[node]){\n int currDist = nodeDist + nbr.second;\n if(currDist < dist[nbr.first]){\n auto it = st.find({dist[nbr.first], nbr.first});\n if(it!=st.end())\n st.erase(it); //remove old entry\n dist[nbr.first] = currDist;\n st.insert({dist[nbr.first], nbr.first});\n }\n }\n }\n return reachableCities;\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n unordered_map<int, list<pair<int, int>>>adjList;\n for(auto edge:edges){\n int &u = edge[0];\n int &v = edge[1];\n int &w = edge[2];\n adjList[u].push_back({v,w});\n adjList[v].push_back({u,w});\n }\n \n int city = 0;\n int minReachableCities = INT_MAX;\n for(int u=0;u<n;++u){\n int reachableCitiesCount = Dijkstra(u, n, adjList, distanceThreshold);\n if(reachableCitiesCount <= minReachableCities){\n minReachableCities = reachableCitiesCount;\n city = u;\n }\n }\n return city;\n }\n};\n", "memory": "28173" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n\n int Dijkstra(int src, int n, unordered_map<int,list<pair<int,int>>>&adjList, int&distanceThreshold){\n\n vector<int>dist(n,INT_MAX);\n set<pair<int,int>>st;\n dist[src]=0;\n st.insert({0,src});\n int reachableCities=0;\n while(!st.empty()){\n auto top=*st.begin();\n st.erase(st.begin());\n int nodeDist=top.first, node=top.second;\n\n if(node!=src && nodeDist<=distanceThreshold){\n ++reachableCities;\n }\n\n for(auto nbr:adjList[node]){\n int currDist=nodeDist+nbr.second;\n if(currDist<dist[nbr.first]){\n auto it=st.find({dist[nbr.first],nbr.first});\n if(it!=st.end())\n st.erase(it);\n \n dist[nbr.first]=currDist;\n st.insert({dist[nbr.first],nbr.first});\n }\n }\n }\n return reachableCities;\n }\n\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n unordered_map<int, list<pair<int,int>>>adjList;\n\n for(auto edge:edges){\n int &u=edge[0];\n int &v=edge[1];\n int&w=edge[2];\n adjList[u].push_back({v,w});\n adjList[v].push_back({u,w});\n }\n \n int city=0;\n int minReachableCities=INT_MAX;\n for(int u=0;u<n;u++){\n int reachableCitiesCount= Dijkstra(u,n,adjList,distanceThreshold);\n if(reachableCitiesCount<=minReachableCities){\n minReachableCities= reachableCitiesCount;\n city=u;\n }\n }\n return city;\n }\n};", "memory": "28471" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int djikstra(int src,int n,unordered_map<int,list<pair<int,int>>>&adjList,int distanceThreshold)\n {\n vector<int>dist(n,INT_MAX);\n dist[src]=0;\n\n set<pair<int,int>>st;\n st.insert({0,src});\n\n int reachableCities = 0;\n while(!st.empty())\n {\n auto top = *st.begin();\n st.erase(st.begin());\n\n int nodeDist = top.first;\n int node = top.second;\n\n\n if(node !=src && nodeDist <= distanceThreshold)\n {\n reachableCities++;\n }\n\n for(auto nbr:adjList[node])\n {\n int currDist = nodeDist + nbr.second;\n\n if(currDist < dist[nbr.first])\n {\n auto it = st.find({dist[nbr.first],nbr.first});\n if(it!=st.end())\n {\n st.erase(it);\n }\n\n dist[nbr.first]=currDist;\n st.insert({dist[nbr.first],nbr.first});\n }\n }\n }\n\n return reachableCities;\n }\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n unordered_map<int,list<pair<int,int>>>adjList;\n for(auto edge:edges)\n {\n int &u=edge[0];\n int &v=edge[1];\n int &w=edge[2];\n\n adjList[u].push_back({v,w});\n adjList[v].push_back({u,w});\n }\n\n int minReachableCities = INT_MAX;\n int city=0;\n for(int i=0;i<n;i++)\n {\n int reachableCities = djikstra(i,n,adjList,distanceThreshold); \n if(reachableCities <= minReachableCities)\n {\n minReachableCities = reachableCities;\n city=i;\n }\n }\n\n return city;\n }\n};", "memory": "28471" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, std::vector<std::vector<int>>& edges, int distanceThreshold) {\n std::vector<std::vector<std::pair<int, int>>> nodes(n);\n for (std::vector<int> e : edges) {\n nodes[e[0]].push_back(std::pair<int,int>(e[1],e[2]));\n nodes[e[1]].push_back(std::pair<int, int>(e[0], e[2]));\n }\n\n int minCities = edges.size();\n int minCity = -1;\n\n for (int i = 0; i < nodes.size(); i++) {\n int reachableCities = dijkstraSearch(nodes,i,distanceThreshold,minCities);\n // As we are incrementing i, if reachable == minCities we always replace minCity with i because i will always be bigger.\n if (reachableCities <= minCities) {\n minCities = reachableCities;\n minCity = i;\n }\n }\n\n return minCity;\n }\nprivate:\n#define USE_VECTOR_VISITED 1\n int dijkstraSearch(std::vector<std::vector<std::pair<int, int>>>& nodes, const int startCity, const int distanceThreshold, const int currentMinCities) {\n std::priority_queue<std::pair<int, std::pair<int,int>>, std::vector<std::pair<int, std::pair<int, int>>>, std::greater<std::pair<int, std::pair<int, int>>>> q;\n q.push(std::pair<int, std::pair<int,int>>(0, std::pair<int,int>(startCity,-1)));\n int reachable = -1;\n#if USE_VECTOR_VISITED\n std::vector<bool> visited(nodes.size());\n#else\n std::unordered_set<int> visitedSet;\n#endif\n while (!q.empty()) {\n int thisNode = q.top().second.first;\n#if USE_VECTOR_VISITED\n if (visited[thisNode]) {\n#else\n if (visitedSet.contains(thisNode)) {\n#endif\n q.pop();\n continue;\n }\n int accumulatedDistance = q.top().first;\n int parentNode = q.top().second.second;\n#if USE_VECTOR_VISITED\n visited[thisNode] = true;\n#else\n visitedSet.insert(thisNode);\n#endif\n reachable++;\n if (reachable > currentMinCities) {\n // No point looking any further, even though the answer is wrong\n return reachable;\n }\n q.pop();\n for (int j = 0; j < nodes[thisNode].size(); j++) {\n#if USE_VECTOR_VISITED\n if(!visited[nodes[thisNode][j].first]) {\n#else\n if (!visitedSet.contains(nodes[thisNode][j].first)) {\n#endif\n int nextDistance = accumulatedDistance + nodes[thisNode][j].second;\n if (nextDistance > distanceThreshold) {\n continue;\n }\n q.push(std::pair<int, std::pair<int, int>>(nextDistance, std::pair<int, int>(nodes[thisNode][j].first, thisNode)));\n\n }\n }\n }\n return reachable;\n }\n};", "memory": "28770" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n std::unordered_map<int, std::vector<std::pair<int, int>>> graf;\n for(int i = 0; i < n; ++i) {graf[i] = {};}\n create(graf, edges);\n int city = 0, max = n;\n for (auto it = graf.begin(); it != graf.end(); ++it) {\n std::unordered_set<int> visited;\n std::unordered_map<int, int> dist;\n std::priority_queue<std::pair<int, int>, std::vector<pair<int, int>>, std::greater<pair<int, int>>> q;\n q.push({0, it->first});\n dist[it->first] = 0;\n while(!q.empty()) {\n auto [d, curent_city] = q.top();\n q.pop();\n if(visited.count(curent_city)) continue;\n visited.insert(curent_city);\n int ind = 0;\n for (auto &t = graf[curent_city]; ind < t.size(); ++ind) {\n int cur_dist = d + t[ind].second, c = t[ind].first;\n if(visited.count(c) || cur_dist > distanceThreshold) continue;\n q.push({cur_dist, c});\n if(cur_dist < dist[c]) dist[c] = cur_dist;\n }\n }\n int count = visited.size() - 1;\n if(count < max) {\n max = count;\n city = it->first;\n } else if (count == max && city < it->first) {\n city = it->first;\n }\n }\n return city;\n }\n void create(std::unordered_map<int, std::vector<std::pair<int, int>>> &graf, \n std::vector<std::vector<int>> &e) {\n for (int i = 0; i < e.size(); ++i) {\n graf[e[i][0]].push_back({e[i][1], e[i][2]});\n graf[e[i][1]].push_back({e[i][0], e[i][2]});\n }\n }\n};", "memory": "28770" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n\n std::vector<std::vector<std::pair<int, int>>> nodes(n);\n for (auto& v : edges) {\n nodes[v[0]].push_back({v[1], v[2]});\n nodes[v[1]].push_back({v[0], v[2]});\n }\n\n int best_result = INT_MIN;\n int city_count = INT_MAX;\n\n auto cmp = [](std::pair<int, int>& left, std::pair<int, int>& right) { return left.second > right.second; };\n \n \n std::vector<bool> closed_set(100);\n\n for (int city = 0; city < n;city++) {\n std::priority_queue<std::pair<int, int>,std::vector<std::pair<int, int>>,decltype(cmp)> open_list;\n open_list.push({city, 0});\n\n \n //for(int i = 0; i < 100;i++) closed_set[i] = false;\n std::fill(closed_set.begin(),closed_set.end(),false);\n\n while (!open_list.empty()) {\n auto& node = open_list.top();\n int current_node = node.first;\n int current_dist = node.second;\n\n open_list.pop();\n closed_set[current_node] = true;\n\n for (auto& n : nodes[current_node]) {\n if (current_dist + n.second <= distanceThreshold && !closed_set[n.first]) {\n open_list.push({n.first, current_dist + n.second});\n }\n }\n }\n\n int nodes_in_range = 0;\n for(int i = 0; i < 100;i++) if(closed_set[i]) nodes_in_range++;\n\n if (city_count > nodes_in_range) {\n best_result = city;\n city_count = nodes_in_range;\n } else if (city_count == nodes_in_range &&\n best_result < city) {\n best_result = city;\n }\n \n }\n\n return best_result;\n }\n};", "memory": "29069" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\n int dijkstra(int &n, unordered_map<int,list<pair<int,int>>> &g, int &d, int i){\n vector<int> ans(n,INT_MAX);\n vector<bool> visit(n);\n ans[i] = 0;\n set<pair<int,int>> s;\n s.insert({0,i});\n while(!s.empty()){\n pair<int,int> p = *s.begin();\n s.erase(s.begin());\n int cd = p.first;\n int u = p.second;\n visit[u] = true;\n for(auto i:g[u]){\n int v = i.first;\n int rd = i.second;\n if(!visit[v] && cd+rd<ans[v]){\n if(ans[v]!=INT_MAX){\n s.erase(s.find({ans[v],v}));\n }\n ans[v] = cd+rd;\n s.insert({cd+rd,v});\n }\n }\n }\n int result = 0;\n for(int i=0; i<n; i++){\n if(ans[i]<=d){\n result++;\n }\n }\n return result;\n }\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n unordered_map<int,list<pair<int,int>>> g;\n for(auto i:edges){\n int u = i[0];\n int v = i[1];\n int w = i[2];\n g[u].push_back({v,w});\n g[v].push_back({u,w});\n }\n int result = -1;\n int mini = INT_MAX;\n for(int i=0; i<n; i++){\n int x = dijkstra(n,g,distanceThreshold,i);\n if(x<=mini){\n mini = x;\n result = i;\n }\n } \n return result;\n }\n};\n\n", "memory": "29069" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int noOfCitesCanVisit(vector<pair<int, int>> nodes[], int curr, int sz, int &dthres) {\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n\n pq.push({0, curr});\n int count = 0;\n bool vis[sz];\n memset(vis, 0, sizeof(vis));\n vis[curr] = 1;\n\n while(!pq.empty()) {\n auto tp = pq.top();\n pq.pop();\n\n if(!vis[tp.second]) {\n vis[tp.second] = 1;\n count++;\n }\n\n for(auto &node: nodes[tp.second]) {\n\n int distanceToReach = node.second + tp.first;\n if(!vis[node.first] and distanceToReach <= dthres) {\n pq.push({distanceToReach, node.first});\n }\n }\n }\n\n return count;\n\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n \n vector<pair<int, int>> nodes[n];\n for(int i = 0; i<edges.size(); i++) {\n int from = edges[i][0], to = edges[i][1], weight = edges[i][2];\n nodes[from].push_back({to, weight});\n nodes[to].push_back({from, weight});\n }\n\n int ans = -1, mn = INT_MAX;\n\n for(int i = 0; i<n; i++) {\n\n int numberOfCitiesReached = noOfCitesCanVisit(nodes, i, n, distanceThreshold);\n\n cout << \"numberOfCitiesReached\" << \" for \" << i << \": \" << numberOfCitiesReached << endl;\n if(numberOfCitiesReached <= mn) {\n mn = numberOfCitiesReached;\n ans = i;\n }\n }\n\n return ans;\n\n }\n};", "memory": "29368" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int noOfCitesCanVisit(vector<pair<int, int>> nodes[], int curr, int sz, int &dthres) {\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n\n pq.push({0, curr});\n int count = 0;\n bool vis[sz];\n memset(vis, 0, sizeof(vis));\n vis[curr] = 1;\n\n while(!pq.empty()) {\n auto tp = pq.top();\n pq.pop();\n\n if(!vis[tp.second]) {\n vis[tp.second] = 1;\n count++;\n }\n\n for(auto &node: nodes[tp.second]) {\n\n int distanceToReach = node.second + tp.first;\n if(!vis[node.first] and distanceToReach <= dthres) {\n pq.push({distanceToReach, node.first});\n }\n }\n }\n\n return count;\n\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n \n vector<pair<int, int>> nodes[n];\n for(int i = 0; i<edges.size(); i++) {\n int from = edges[i][0], to = edges[i][1], weight = edges[i][2];\n nodes[from].push_back({to, weight});\n nodes[to].push_back({from, weight});\n }\n\n int ans = -1, mn = INT_MAX;\n\n for(int i = 0; i<n; i++) {\n\n int numberOfCitiesReached = noOfCitesCanVisit(nodes, i, n, distanceThreshold);\n\n cout << \"numberOfCitiesReached\" << \" for \" << i << \": \" << numberOfCitiesReached << endl;\n if(numberOfCitiesReached <= mn) {\n mn = numberOfCitiesReached;\n ans = i;\n }\n }\n\n return ans;\n\n }\n};", "memory": "29368" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n\n vector<vector<pair<int,int>>>v(n);\n for(int i=0;i<edges.size();i++)\n {\n v[edges[i][0]].push_back({edges[i][1],edges[i][2]});\n v[edges[i][1]].push_back({edges[i][0],edges[i][2]});\n }\n // for(int i=0;i<v.size();i++)\n // {\n // for(int j=0;j<v[i].size();j++)\n // {\n // cout<<v[i][j].first<<\",\"<<v[i][j].second<<\" \";\n // }\n // cout<<endl;\n // }\n int ans = graph(v,distanceThreshold,n);\n return(ans);\n }\n\n int findneighbors(vector<vector<pair<int,int>>> &v,int &d,int &n,int start)\n {\n vector<bool>b(n,0);\n int value=-1;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n pq.push({0,start});\n pair<int,int> temp;\n int cur;\n while(pq.size()>0)\n {\n temp=pq.top();\n pq.pop();\n\n if(b[temp.second]==0)\n {\n b[temp.second]=1;\n value++;\n }\n cur=temp.first;\n\n for(int i=0;i<v[temp.second].size();i++)\n {\n // cout<<temp.second<<\" \"<<v[temp.second].size()<<\" \"<<v[temp.second][i].first<<\" \"<<v[temp.second][i].second<<endl<<endl;\n if(b[v[temp.second][i].first]==0 && (cur + v[temp.second][i].second <=d))\n {\n pq.push({cur+v[temp.second][i].second,v[temp.second][i].first});\n }\n \n \n }\n\n }\n return(value);\n }\n\n int graph(vector<vector<pair<int,int>>> &v,int &d,int &n)\n {\n int mina=INT_MAX;\n int ans=-1;\n int val;\n for(int i=0;i<n;i++)\n {\n // cout<< \" for \"<<i<<endl;\n val = findneighbors(v,d,n,i);\n // cout<<\"value is \"<<val<< \" for \"<<i<<endl;\n if(val<mina)\n {\n mina=val;\n ans=i;\n }\n else if(val==mina)\n {\n ans=max(ans,i);\n }\n }\n return(ans);\n }\n};", "memory": "29666" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<int> citiesVis(n, 0);\n\n vector<pair<int, int>> adj[n];\n\n for(auto x : edges){\n adj[x[0]].push_back({x[1], x[2]});\n adj[x[1]].push_back({x[0], x[2]});\n }\n\n for(int i = 0; i < n; i++){\n vector<int> vis(n, 0);\n vis[i] = 1;\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n q.push({0, i});\n while(!q.empty()){\n int wt = q.top().first;\n int node = q.top().second;\n q.pop();\n\n if(wt > distanceThreshold) continue;\n\n vis[node] = 1;\n\n for(auto x : adj[node]){\n if(!vis[x.first] && x.second + wt <= distanceThreshold){\n // vis[x.first] = 1;\n q.push({x.second + wt, x.first});\n }\n }\n }\n\n int cnt = -1;\n for(auto x : vis) cnt += x;\n\n citiesVis[i] = cnt;\n }\n\n int miniCities = 1e9;\n int maxInd = -1;\n\n for(int i = 0; i < n; i++){\n if(citiesVis[i] <= miniCities){\n miniCities = citiesVis[i];\n\n maxInd = max(maxInd, i);\n }\n }\n\n return maxInd;\n }\n};", "memory": "29666" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int, int>>> graph(n);\n for (auto& edge : edges) {\n graph[edge[0]].push_back({edge[1], edge[2]});\n graph[edge[1]].push_back({edge[0], edge[2]});\n }\n\n auto dijkstra = [&](int start) {\n vector<int> dist(n, INT_MAX);\n dist[start] = 0;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n pq.push({0, start});\n vector<int> closed(n, 0);\n int res = 0;\n while (!pq.empty()) {\n auto [cur_w, cur_n] = pq.top();\n pq.pop();\n if (closed[cur_n] == 1) {\n continue;\n }\n closed[cur_n] = 1;\n if (dist[cur_n] > distanceThreshold) {\n break;\n }\n ++res;\n for (auto& nei : graph[cur_n]) {\n int nei_node = nei.first;\n int weight = nei.second;\n if (closed[nei_node] == 1) {\n continue;\n }\n dist[nei_node] = min(dist[nei_node], dist[cur_n] + weight);\n pq.push({dist[nei_node], nei_node});\n }\n }\n\n return res;\n };\n\n int min_city = INT_MAX;\n int res = 0;\n for (int i = 0; i < n; ++i) {\n int city = dijkstra(i);\n if (city <= min_city) {\n min_city = city;\n res = i;\n }\n }\n\n return res;\n }\n};", "memory": "29965" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int getMinReached(int i,int n, vector<pair<int,int>> *adj, int distanceThreshold) {\n if(distanceThreshold == 0) return 0;\n vector<int> visited(n,INT_MAX);\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q; // { dist covered so far, node}\n q.push({0, i});\n int cityReached = 0; // curr city\n while(!q.empty()) {\n auto node = q.top(); q.pop();\n int curr = node.second;\n int dist = node.first;\n for(auto nextNeigh: adj[curr]) {\n if(dist + nextNeigh.second <= distanceThreshold && visited[nextNeigh.first] > dist + nextNeigh.second) {\n q.push({dist + nextNeigh.second, nextNeigh.first});\n }\n }\n if(visited[curr] == INT_MAX) {\n cout<<\" curr \"<<curr<<\" dist for far \"<<dist<<endl;\n visited[curr] = dist;\n cityReached ++;\n }\n }\n\n return cityReached;\n }\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<pair<int,int>> adj[n];\n for(int i=0;i<edges.size();i++) {\n adj[edges[i][0]].push_back({edges[i][1], edges[i][2]});\n adj[edges[i][1]].push_back({edges[i][0], edges[i][2]});\n }\n int city = -1;\n int minReached = INT_MAX;\n\n for(int i=0;i<n;i++) {\n int reached = getMinReached(i, n, adj, distanceThreshold);\n cout<<\"reached \"<<reached<<\" node \"<<i<<endl;\n if(reached <= minReached) {\n minReached = reached;\n city = i;\n }\n }\n\n return city;\n }\n};\n/*\n0-1 2\n0-4 8\n1-2 3\n1-4 2\n2-3 1\n3-4 1\ndist thres = 2\n0 - {1,2}, {4,8}=> {1}\n1 - {0,2}, {2,3}, {4,2} => {0,4}\n2 - {1,3}, {3,1} => {3, 4}\n3 - {2,1}, {4,1} => {2}\n4 - {0,8}, {1,2}, {3,1}\n\n*/", "memory": "29965" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findNumberOfNeighbor(int n, int i, vector<vector<pair<int, int>>>& adj, int distanceThreshold) {\n priority_queue<pair<int, int>> pq;\n int numberOfNeighbor = 0;\n vector<bool> visited(n, false);\n visited[i] = true;\n pq.push({0, i});\n while(!pq.empty()) {\n if(pq.top().second != i && !visited[pq.top().second]) {\n numberOfNeighbor++;\n visited[pq.top().second] = true;\n } \n for (pair<int, int> neighbor : adj[pq.top().second]) {\n if(!visited[neighbor.first] && abs(pq.top().first) + neighbor.second <= distanceThreshold) {\n pq.push({-(neighbor.second + abs(pq.top().first)), neighbor.first});\n }\n }\n pq.pop();\n }\n return numberOfNeighbor;\n }\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int, int>>> adj(n);\n for(auto edge : edges) {\n adj[edge[0]].push_back({edge[1], edge[2]});\n adj[edge[1]].push_back({edge[0], edge[2]});\n }\n int maxNum = INT_MAX;\n int sol = 0;\n for(int i = 0; i < n; i++) {\n int numberOfNeighbor = findNumberOfNeighbor(n, i, adj, distanceThreshold);\n if(maxNum >= numberOfNeighbor) {\n maxNum = numberOfNeighbor;\n sol = i;\n }\n //cout << i << \" \" << numberOfNeighbor << endl;\n }\n return sol;\n }\n};", "memory": "30264" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n\n vector<pair<int, int>>adj[n];\n for(auto it : edges){\n int u = it[0],v = it[1], w = it[2];\n adj[u].push_back({v, w});\n adj[v].push_back({u, w});\n } \n\n\n int mini = INT_MAX;\n int mini_index = 0;\n\n\n vector<int>dist(n ,INT_MAX); \n for(int i = 0;i<n;i++){\n priority_queue<pair<int, int>>pq;\n pq.push({0 , i});\n dist[i] = 0;\n\n for(int k = 0;k<n;k++)\n {\n if(k!=i)\n dist[k] = INT_MAX; \n }\n\n\n while(pq.size()>0)\n {\n auto [d , node] = pq.top();\n pq.pop();\n\n d = -d;\n dist[node] = min(d, dist[node]);\n\n for(auto it : adj[node]){\n if(dist[it.first] > d+it.second and d+it.second<=distanceThreshold){\n pq.push({-(it.second + dist[node]) , it.first});\n }\n }\n\n }\n\n int count = 0;\n for(int j = 0;j<n;j++)\n {\n if(dist[j]!=INT_MAX and j!=i)\n count++;\n }\n\n if(count <= mini){\n mini = count;\n mini_index = i;\n }\n }\n\n\n \n return mini_index;\n\n\n\n }\n};", "memory": "30264" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n \n // Since weight > 0, we can treat 0 as INF.\n std::vector<std::vector<int>> adjMatrix(n, std::vector<int>(n));\n for (const auto &v : edges){\n adjMatrix[v[0]][v[1]] = adjMatrix[v[1]][v[0]] = v[2];\n }\n \n int smallestValidNeighbors = INT_MAX;\n int smallestVertex = -1;\n for (int i = 0; i < n; ++i){\n int nowValidNeighbors = dijkstra(i, adjMatrix, distanceThreshold);\n if (nowValidNeighbors <= smallestValidNeighbors){\n smallestValidNeighbors = nowValidNeighbors;\n smallestVertex = i;\n }\n }\n\n return smallestVertex;\n }\nprivate:\n int dijkstra(int origin, const std::vector<std::vector<int>> &adjMatrix, const int &distanceThreshold){\n \n const int NOF_VERTEX = adjMatrix.size();\n int nofVisitedVertex = 0;\n\n std::vector<bool> visited(NOF_VERTEX);\n using DataType = std::pair<int, int>;\n std::priority_queue<DataType, std::vector<DataType>, std::greater<DataType>> pq;\n pq.emplace(0, origin);\n while (!pq.empty()){\n \n // Get top and pop at first, because we'll insert other data later.\n int nowDist = pq.top().first;\n int now = pq.top().second;\n pq.pop();\n \n // Check if is already visited.\n if (visited[now]){\n continue;\n }\n else {\n visited[now] = true;\n }\n \n // Check if we reach the limit condition.\n if (nowDist > distanceThreshold)\n return nofVisitedVertex;\n\n // Since the vertex now is valid(not yet visited and can reach), we can record it.\n ++nofVisitedVertex;\n\n if (nofVisitedVertex == NOF_VERTEX)\n return nofVisitedVertex;\n\n\n // Main process\n const auto &nbs = adjMatrix[now];\n for (int i = 0; i < nbs.size(); ++i){\n if (!visited[i] && nbs[i] != 0){\n pq.emplace(nowDist + nbs[i], i);\n }\n }\n \n }\n\n return nofVisitedVertex;\n }\n};", "memory": "30563" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "struct compare{\n bool operator()(pair<int,int>& a, pair<int,int>& b){\n return b.second<a.second;\n }\n};\nclass Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) { \n int res=0, cur=INT_MAX;\n vector<vector<pair<int,int>>> edge(n);\n for (int i=0; i<edges.size(); i++){\n edge[edges[i][0]].push_back({edges[i][1], edges[i][2]});\n edge[edges[i][1]].push_back({edges[i][0], edges[i][2]});\n }\n for (int i=0; i<n; i++){\n int counter=1;\n vector<bool> visit(n);\n priority_queue<pair<int,int>,vector<pair<int,int>>,compare> Q;\n Q.push({i,0});\n while(counter<n && !Q.empty()){\n pair<int,int> temp=Q.top();\n Q.pop();\n if (visit[temp.first]) continue;\n if (temp.second>distanceThreshold) break;\n visit[temp.first]=true;\n counter++;\n for (int j=0; j<edge[temp.first].size(); j++){\n if (visit[edge[temp.first][j].first]) continue;\n Q.push({edge[temp.first][j].first, temp.second+edge[temp.first][j].second});\n }\n }\n if (counter<=cur) {\n cur=counter;\n res=i;\n }\n } \n return res;\n }\n};", "memory": "30563" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "//Mehmet Fuat Karakaya\n\nclass Solution {\npublic:\n int dijk(int node, int tresh, vector<vector<pair<int,int>>>& adj, int n, vector<bool> &visited){\n visited.assign(visited.size(), 0);\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n int count=-1;//it will count itself too making it 0\n pq.push({0,node});\n while(!pq.empty()){\n int cost = pq.top().first;\n int currentNode = pq.top().second;\n pq.pop();\n if(visited[currentNode])\n continue;\n visited[currentNode] = true;\n if(cost<=tresh)\n count++;\n if(cost>=tresh)\n continue;\n\n for(const auto& [weight, newNode] : adj[currentNode]){\n if(visited[newNode])\n continue;\n pq.emplace(cost + weight, newNode);\n }\n }\n return count;\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int,int>>> adj(n);\n vector<bool> visited(n, false); //visited or will not be visited\n for(vector<int>& edge : edges){\n adj[edge[0]].push_back({edge[2],edge[1]});\n adj[edge[1]].push_back({edge[2],edge[0]});\n }\n //adj created\n\n int min = n;\n int currentCity;\n for(int node=0; node<n; node++){\n int x = dijk(node, distanceThreshold, adj, n, visited);\n if(x<=min){\n currentCity=node;\n min=x;\n }\n }\n return currentCity;\n }\n};", "memory": "30861" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int djistra(int src, int n, vector<vector<pair<int, int>>>& adjL, int distanceThreshold){\n vector<int> dist(n, -1);\n\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n\n pq.push({0, src});\n\n int num_vertices = 0, ans =0;\n\n while(!pq.empty()){\n auto [d, u] = pq.top(); pq.pop();\n\n if(dist[u] != -1){\n continue;\n }\n\n dist[u] = d;\n\n ans++;\n\n for(auto& [v,w]: adjL[u]){\n int next_distance = d + w;\n\n if(next_distance > distanceThreshold) continue;\n\n pq.push({next_distance, v});\n }\n }\n\n return ans;\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int,int>>> adjL(n);\n\n for(auto& v: edges){\n adjL[v[0]].push_back({v[1], v[2]});\n adjL[v[1]].push_back({v[0], v[2]});\n }\n\n int ans = n, idx = -1;\n\n for(int i=0;i<n;i++){\n int val = djistra(i, n, adjL, distanceThreshold);\n\n if(val <= ans){\n idx = i;\n ans = val;\n }\n }\n\n return idx;\n }\n};", "memory": "31758" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findNumbNeighbor(int n, vector<vector<pair<int, int>>> &adj, int i, int distanceThreshold) {\n int ans = 0;\n vector<bool> visited(n, false);\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n pq.push({0, i});\n while (!pq.empty()) {\n auto p = pq.top();\n pq.pop();\n int weight = p.first;\n int city = p.second;\n if (visited[city] == true) continue;\n visited[city] = true;\n // cout<<city<<\" \"<<weight<<endl;\n ans++;\n for (auto it:adj[city]) {\n int distance = it.second + weight;\n if (distance > distanceThreshold) continue;\n pq.push({distance, it.first});\n }\n }\n return ans;\n }\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int, int>>> adj(n);\n for (auto it:edges) {\n int from = it[0];\n int to = it[1];\n int weight = it[2];\n adj[from].push_back({to, weight});\n adj[to].push_back({from, weight});\n }\n int minNum = n;\n int ans = 0;\n for (int i = 0; i<n; i++) {\n int temp = findNumbNeighbor(n, adj, i, distanceThreshold);\n // cout<<i<<\" -> \"<<temp<<endl;\n if (minNum >= temp) {\n minNum = temp;\n ans = i;\n }\n }\n return ans;\n }\n};\n// 1/ thiet lap edge\n// 2/ loang tim co bao nhieu thanh pho co the di den dc < threshold -> luu vao map\n// 3/ sort tim min", "memory": "31758" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "//Hilal Parlakçı\nclass Solution {\n vector<vector<pair<int,int>>> adj;\n int distance;\n int size;\nprivate:\n void dj(vector<int>& res, int target) {\n priority_queue<pair<int, int>, vector<pair<int, int>>,\n greater<pair<int, int>>> pq;\n vector<int> visited(size);\n\n pq.push({0,target});\n int count = -1;\n\n while(!pq.empty()) {\n auto [weight , node] = pq.top();\n pq.pop();\n if(visited[node]) continue;\n\n visited[node] = true;\n count++;\n\n int new_w;\n for(auto& [w , n] : adj[node]) {\n new_w = weight +w;\n if(new_w <= distance ) pq.push({new_w , n});\n }\n \n }\n\n res[target] = count;\n }\n\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n distance = distanceThreshold;\n adj.resize(n);\n vector<int> res(n);\n size = n;\n\n for(auto edge : edges) {\n adj[edge[0]].push_back({edge[2],edge[1]});\n adj[edge[1]].push_back({edge[2],edge[0]});\n }\n for(int i = 0 ; i < n ; i++ ) {\n dj(res, i);\n }\n int min_index = 0;\n for(int i = 1 ; i < n ; i++){\n if(res[i] <= res[min_index] ) min_index = i;\n }\n return min_index;\n \n }\n};", "memory": "32056" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "//Hilal Parlakçı\nclass Solution {\nprivate:\n vector<vector<pair<int,int>>> adj;\n int distance;\n int size;\n\n int dijkstra(int target) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n vector<int> visited(size);\n\n pq.push({0,target});\n int count = -1;\n\n while(!pq.empty()) {\n auto [weight , node] = pq.top();\n pq.pop();\n if(visited[node]) continue;\n\n visited[node] = true;\n count++;\n\n int new_w;\n for(auto& [w , n] : adj[node]) {\n new_w = weight +w;\n if(new_w <= distance ) pq.push({new_w , n});\n }\n \n }\n return count;\n }\n\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n distance = distanceThreshold;\n adj.resize(n);\n size = n;\n\n for(auto edge : edges) {\n adj[edge[0]].push_back({edge[2],edge[1]});\n adj[edge[1]].push_back({edge[2],edge[0]});\n }\n\n int min_city = 0;\n int min_val = INT_MAX;\n for (int i = 0; i < n; i++) {\n int val = dijkstra(i);\n if (val <= min_val) {\n min_val = val;\n min_city = i;\n }\n }\n\n return min_city;\n \n }\n};", "memory": "32056" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\nstruct cmp{\n bool operator()(pair<int,int> a, pair<int,int> b){\n return a.first>b.first;\n }\n};\nint findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) \n{\n unordered_map<int,unordered_map<int,int>> mp;\n for(auto edge:edges){\n mp[edge[0]][edge[1]]=edge[2];\n mp[edge[1]][edge[0]]=edge[2];\n }\n\n vector<int> canExplore(n);\n for(int i=0;i<n;i++)\n {\n priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> q;\n vector<bool> visited(n);\n q.push({0,i}); // dist,node\n while(!q.empty())\n {\n pair<int,int> temp = q.top(); q.pop();\n int dist=temp.first, node=temp.second; \n\n if(visited[node]) continue;\n visited[node]=true; \n canExplore[i]++;\n for(auto ele:mp[node])\n {\n int newDist = ele.second + dist;\n if(newDist > distanceThreshold) continue;\n q.push({newDist,ele.first});\n }\n }\n }\n\n int minmCities=INT_MAX, res=-1;\n for(int i=n-1;i>=0;i--)\n {\n if(canExplore[i]<minmCities) \n minmCities=canExplore[i], res=i; \n }\n return res;\n}\n};", "memory": "32355" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map<int, vector<pair<int, int>>> adj;\n \n int dijkstra(int src, int n, int dT){\n vector<int> visited(n, 0);\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n pq.push({0, src});\n\n while(!pq.empty()){\n int cur = pq.top().second;\n int cur_dist = pq.top().first;\n pq.pop();\n \n if(visited[cur])continue;\n\n visited[cur] = 1;\n\n for(auto nei : adj[cur]){\n if(cur_dist + nei.second <= dT ){\n pq.push({cur_dist + nei.second, nei.first});\n }\n }\n }\n\n int count = 0;\n for(auto x : visited)count += x;\n\n return count - 1;\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n \n for(auto ed : edges){\n adj[ed[0]].push_back({ed[1], ed[2]});\n adj[ed[1]].push_back({ed[0], ed[2]});\n }\n\n int reachableCity = n-1, city = n-1;\n\n for(int i=0; i<n; i++){\n int count = dijkstra(i, n, distanceThreshold);\n if(count <= reachableCity){\n reachableCity = count;\n city = i;\n }\n }\n\n return city;\n\n }\n}; ", "memory": "32355" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int dijkstra(int src, int distanceThreshold, vector<vector<pair<int, int>>>& adj){\n\n unordered_set<int> visited(src);\n priority_queue<pair<int, int>> pq; pq.push({0, src});\n while(pq.size()){ \n auto [distance, node] = pq.top(); pq.pop(); distance *= (-1);\n visited.insert(node);\n for(auto [nei, weight] : adj[node]){\n if(visited.count(nei) || distance + weight > distanceThreshold) continue;\n pq.push({-(distance + weight), nei}); \n }\n }\n \n return visited.size(); \n\n }\n\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n \n vector<vector<pair<int, int>>> adj(n);\n for(auto edge : edges){\n adj[edge[0]].push_back({edge[1], edge[2]});\n adj[edge[1]].push_back({edge[0], edge[2]});\n }\n\n int minCitys = n, ret = -1;\n for(int i = 0; i < n; i++){\n int citys = dijkstra(i, distanceThreshold, adj);\n if(citys <= minCitys){\n minCitys = citys;\n ret = i;\n }\n }\n\n return ret;\n\n }\n};", "memory": "32654" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\n struct Edge {\n int to;\n int weight;\n };\n\n struct EdgeComparator {\n bool operator()(const Edge& e1, const Edge& e2) {\n return e1.weight > e2.weight;\n }\n };\n using Path = Edge;\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<Edge>> adj(n);\n for (const auto& e : edges) {\n int u = e[0];\n int v = e[1];\n int w = e[2];\n adj[u].emplace_back(v, w);\n adj[v].emplace_back(u, w);\n }\n\n int min_neighbors = INT_MAX;\n int smallest_city = -1;\n for (int i = 0; i < n; i++) {\n priority_queue<Path, vector<Path>, EdgeComparator> pq;\n pq.emplace(i, 0);\n vector<bool> visited(n);\n int neighbors = 0;\n while (!pq.empty()) {\n auto top = pq.top();\n pq.pop();\n int u = top.to;\n int w = top.weight;\n\n if (visited[u]) {\n continue;\n }\n\n visited[u] = true;\n\n if (w <= distanceThreshold && w > 0) {\n neighbors++;\n }\n\n for (const auto& e : adj[u]) {\n if (!visited[e.to]) {\n pq.emplace(e.to, e.weight + w);\n }\n }\n }\n if (neighbors <= min_neighbors) {\n min_neighbors = neighbors;\n smallest_city = i;\n }\n }\n return smallest_city;\n }\n};", "memory": "32654" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n int f(int node,int n,int tar,vector<pair<int,int>> adj[]){\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq;\n\n pq.push({0,node});\n set<int> st;\n vector<int> vis(n,0);\n vis[node]=1;\n while(!pq.empty()){\n auto top = pq.top();\n int dist = top.first;\n int nd = top.second;\n vis[nd]=1;\n st.insert(nd);\n pq.pop();\n\n for(auto it: adj[nd]){\n int adjnode = it.first;\n int wt = it.second;\n\n if(dist+wt<=tar and !vis[adjnode]){\n pq.push({dist+wt,adjnode});\n }\n }\n }\n\n return st.size()-1;\n }\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<pair<int,int>> adj[n];\n\n for(auto it: edges){\n adj[it[0]].push_back({it[1],it[2]});\n adj[it[1]].push_back({it[0],it[2]});\n }\n\n vector<int> city(n,0);\n\n for(int i=0; i<n; i++){\n int cnt = f(i,n,distanceThreshold,adj);\n city[i] = cnt;\n }\n\n int mn = INT_MAX;\n int ans=-1;\n for(int i=0; i<n; i++){\n cout<<city[i]<<\" \";\n if(city[i]<=mn){\n ans = i;\n mn = city[i];\n }\n }\n\n return ans;\n }\n};", "memory": "32953" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int vis[101];\n int findTheCity(int n, vector<vector<int>>& x, int a) {\n ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(0);\n vector<vector<pair<int,int>>>v(n);\n vector<set<int>>s(101);\n for(int i=0;i<x.size();i++){\n v[x[i][0]].push_back({x[i][1],x[i][2]});\n v[x[i][1]].push_back({x[i][0],x[i][2]});\n }\n for(int i=0;i<n;i++){\n memset(vis,0,sizeof vis);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n pq.push({0,i});\n while(!pq.empty()){\n int node=pq.top().second;\n int dis=pq.top().first;\n pq.pop();\n if(vis[node])continue;\n vis[node]=1;\n for(auto it:v[node]){\n int x=dis+it.second;\n if(x<=a&&it.first!=i){\n \n s[i].insert(it.first);\n pq.push({x,it.first});\n }\n }\n }\n }\n int ans=-1,mi=1e9;\n for(int i=0;i<n;i++){\n if(s[i].size()<=mi){\n mi=s[i].size();\n ans=i;\n }\n }\n return ans;\n }\n};", "memory": "32953" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int, int>>> adj(n, vector<pair<int, int>>());\n for (const auto& e : edges) {\n adj[e[0]].push_back({e[1], e[2]});\n adj[e[1]].push_back({e[0], e[2]});\n }\n\n vector<int> ans(n);\n for (int i = 0; i < n; i++) {\n set<int> visited;\n priority_queue<pair<int, int>> pq;\n pq.push({0, i});\n while (pq.size()) {\n auto [cur_dis, city] = pq.top();\n pq.pop();\n\n if (visited.count(city))\n continue;\n\n visited.insert(city);\n\n for (int k = 0; k < adj[city].size(); k++) {\n int nei = adj[city][k].first;\n int dis = adj[city][k].second;\n // if (nei == i)\n // continue;\n \n if (-cur_dis + dis <= distanceThreshold) {\n pq.push({-(-cur_dis + dis), nei});\n // ans[i].insert(nei);\n } \n }\n }\n ans[i] = visited.size() - 1;\n }\n\n int res = -1, min_count = n;\n for (int k = 0; k < n; k++) {\n if (ans[k] <= min_count) {\n min_count = ans[k];\n res = k;\n }\n\n }\n return res;\n }\n};", "memory": "33251" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int, int>>> adj(n, vector<pair<int, int>>());\n for (const auto& e : edges) {\n adj[e[0]].push_back({e[1], e[2]});\n adj[e[1]].push_back({e[0], e[2]});\n }\n\n vector<int> ans(n);\n for (int i = 0; i < n; i++) {\n set<int> visited;\n priority_queue<pair<int, int>> pq;\n pq.push({0, i});\n while (pq.size()) {\n auto [cur_dis, cur] = pq.top();\n pq.pop();\n\n if (visited.count(cur))\n continue;\n\n visited.insert(cur);\n\n for (int k = 0; k < adj[cur].size(); k++) {\n int nei = adj[cur][k].first;\n int dis = adj[cur][k].second;\n if (-cur_dis + dis <= distanceThreshold)\n pq.push({-(-cur_dis + dis), nei});\n }\n }\n ans[i] = visited.size() - 1;\n }\n\n int res = -1, min_count = n;\n for (int k = 0; k < n; k++) {\n if (ans[k] <= min_count) {\n min_count = ans[k];\n res = k;\n }\n\n }\n return res;\n }\n};", "memory": "33251" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "struct cmp {\n bool operator()(pair<int, int> &v1, pair<int, int> &v2) {\n return v1.second > v2.second;\n }\n};\n\nclass Solution {\npublic:\n void dijkstra(vector<vector<pair<int, int>>> &g, int start, int threshold, vector<vector<int>> &dists) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;\n \n pq.push({start, 0});\n while (!pq.empty()) {\n auto [cur, cost] = pq.top();\n pq.pop();\n \n if (dists[start][cur] <= cost)\n continue;\n dists[start][cur] = cost;\n \n for (const auto [neigh, c] : g[cur]) {\n if (dists[start][neigh] > cost + c)\n pq.push({neigh, cost + c});\n }\n }\n }\n \n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n vector<vector<pair<int, int>>> g(n, vector<pair<int, int>>());\n vector<vector<int>> dists(n, vector<int>(n, INT_MAX));\n int minc = INT_MAX;\n int res = 0;\n \n for (const auto &e : edges) {\n g[e[0]].push_back({e[1], e[2]});\n g[e[1]].push_back({e[0], e[2]});\n }\n \n for (int i = 0; i < n; i++)\n dijkstra(g, i, distanceThreshold, dists);\n \n for (int i = 0; i < n; i++) {\n int cnt = 0;\n \n for (int j = 0; j < n; j++) {\n if (dists[i][j] <= distanceThreshold)\n cnt++;\n }\n \n if (cnt <= minc) {\n minc = cnt;\n res = i;\n }\n }\n \n return res;\n }\n};", "memory": "33550" }
1,456
<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p> <p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p> <p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 <strong>Output:</strong> 3 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 4 for each city are: City 0 -&gt; [City 1, City 2]&nbsp; City 1 -&gt; [City 0, City 2, City 3]&nbsp; City 2 -&gt; [City 0, City 1, City 3]&nbsp; City 3 -&gt; [City 1, City 2]&nbsp; Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p> <pre> <strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2 <strong>Output:</strong> 0 <strong>Explanation: </strong>The figure above describes the graph.&nbsp; The neighboring cities at a distanceThreshold = 2 for each city are: City 0 -&gt; [City 1]&nbsp; City 1 -&gt; [City 0, City 4]&nbsp; City 2 -&gt; [City 3, City 4]&nbsp; City 3 -&gt; [City 2, City 4] City 4 -&gt; [City 1, City 2, City 3]&nbsp; The city 0 has 1 neighboring city at a distanceThreshold = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li> <li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {\n // first convert graph to adjacency list representation\n vector<vector<pair<int, int>>> graph(n); // i-th nodes will have graph[i] neighbors\n for (const auto& edge : edges) {\n int node1 = edge[0], node2 = edge[1], distance = edge[2];\n graph[node1].emplace_back(node2, distance);\n graph[node2].emplace_back(node1, distance);\n }\n\n auto get_number_of_neighbors_in_distance = [&](int source) -> int {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> queue;\n queue.emplace(0, source); // distance to node itself is 0\n set<int> visited;\n\n while (!queue.empty()) {\n auto [distance_to_this_node, cur_node] = queue.top();\n queue.pop();\n if (!visited.count(cur_node)) {\n visited.insert(cur_node);\n for (const auto& [neighbor, distance] : graph[cur_node]) {\n int distance_from_source = distance_to_this_node + distance;\n if (distance_from_source <= distanceThreshold) { // ensure that we're allowed to go to this node\n queue.emplace(distance_from_source, neighbor);\n }\n }\n }\n }\n // actually you can return visited.size() and with math there will be nothing wrong but actually we have visited.size() - 1 neighbors since we're not neighbor of ourselves\n return visited.size() - 1;\n };\n\n int minimum_number = n;\n int res = -1;\n\n for (int source = 0; source < n; ++source) {\n int neighbors = get_number_of_neighbors_in_distance(source);\n // we iterate source from smaller to bigger this ensures that we choose node with greater value if they have equal number of neighbors\n if (neighbors <= minimum_number) {\n minimum_number = neighbors;\n res = source;\n }\n }\n\n return res;\n }\n};", "memory": "33550" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int dp[301][11];\n int solve(vector<int>& v,int i,int d){\n if(i==v.size()){\n if(d==0)return 0;\n else return 1e9;\n }\n if(d==0)return 1e9;\n if(dp[i][d]!=-1)return dp[i][d];\n int maxi=0;\n int ans=1e9;\n for(int j=i;j<v.size();j++){\n maxi=max(maxi,v[j]);\n int next=solve(v,j+1,d-1);\n if(next!=1e9)ans=min(ans,maxi+next);\n }\n \n return dp[i][d]=ans;\n }\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n if(jobDifficulty.size()<d)return -1;\n memset(dp,-1,sizeof(dp));\n int ans=solve(jobDifficulty,0,d);\n return (ans==1e9)?-1:ans;\n }\n};", "memory": "9200" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int dp[301][11];\n int solver(vector<int>& jd,int n,int idx,int d){\n if(d==1){\n int maxD = jd[idx];\n for(int i=idx;i<n;i++) maxD = max(maxD,jd[i]);\n\n return maxD;\n }\n if(dp[idx][d]!=-1) return dp[idx][d];\n int maxD = jd[idx];\n int ans = INT_MAX;\n for(int i=idx;i<=n-d;i++){\n maxD = max(maxD,jd[i]);\n int result = maxD + solver(jd,n,i+1,d-1);\n\n ans = min(ans,result);\n }\n return dp[idx][d] =ans;\n }\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n if(n<d) return -1;\n memset(dp,-1,sizeof(dp));\n return solver(jobDifficulty,n,0,d);\n }\n};", "memory": "9300" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
0
{ "code": "class Solution {\npublic:\nint n;\nint t[301][11];\nint solve(vector<int>& jobDifficulty,int index,int d){\n if(d==1){\n int maxi=jobDifficulty[index];\n for(int i=index;i<n;i++){\n maxi=max(maxi,jobDifficulty[i]);\n }\n return maxi;\n }\n if(t[index][d]!=-1){\n return t[index][d];\n }\n int maxi=jobDifficulty[index];\n int finalresult=INT_MAX;\n\n \n \n \n for(int i=index;i<=n-d;i++){\n maxi=max(maxi,jobDifficulty[i]);\n int result=maxi+solve(jobDifficulty,i+1,d-1);\n finalresult=min(finalresult,result);\n }\n return t[index][d]=finalresult;\n\n}\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n n=jobDifficulty.size();\n memset(t,-1,sizeof(t));\n if(n<d){\n return -1;\n }\n return solve(jobDifficulty,0,d);\n }\n};", "memory": "9400" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
0
{ "code": "class Solution {\npublic:\nint n;\nint t[301][11];\nint solve(vector<int>& jobDifficulty,int index,int d){\n if(d==1){\n //int maxi=jobDifficulty[index];\n int maxi=*max_element(jobDifficulty.begin()+index,jobDifficulty.end());\n /*for(int i=index;i<n;i++){\n maxi=max(maxi,jobDifficulty[i]);\n }*/\n return maxi;\n }\n if(t[index][d]!=-1){\n return t[index][d];\n }\n int maxi=jobDifficulty[index];\n int finalresult=INT_MAX;\n\n \n \n \n for(int i=index;i<=n-d;i++){\n maxi=max(maxi,jobDifficulty[i]);\n int result=maxi+solve(jobDifficulty,i+1,d-1);\n finalresult=min(finalresult,result);\n }\n return t[index][d]=finalresult;\n\n}\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n n=jobDifficulty.size();\n memset(t,-1,sizeof(t));\n if(n<d){\n return -1;\n }\n return solve(jobDifficulty,0,d);\n }\n};", "memory": "9400" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n return minDifficultyVersion(jobDifficulty, d);\n }\n\n int minDifficultyVersion(const vector<int>& jobDifficulty, int d) {\n const int n = static_cast<int>(jobDifficulty.size());\n\n if (n < d) {\n return -1;\n } else if (n == d) {\n int totalDifficulty = 0;\n for (int difficulty : jobDifficulty)\n totalDifficulty += difficulty;\n return totalDifficulty;\n }\n\n // dp[j]:\n // the minimum difficulty OF\n // the first (j+1) jobs, exactly scheduled in (i+1) days.\n vector<int> dp(n);\n dp[0] = jobDifficulty[0];\n\n // Initializing the dp array to store the maximum difficulty encountered so far.\n for (int i = 1; i < n; ++i) {\n dp[i] = max(jobDifficulty[i], dp[i - 1]);\n }\n\n vector<int> dpPrev(n);\n\n // Dynamic Programming to find the minimum difficulty.\n for (int i = 1; i < d; ++i) {\n dp.swap(dpPrev);\n for (int j = i; j < n; ++j) {\n int lastDayDifficulty = jobDifficulty[j];\n int tmpMin = lastDayDifficulty + dpPrev[j - 1];\n\n // Iterate to find the minimum difficulty for the current day.\n for (int t = j - 1; i - 1 < t; --t) {\n lastDayDifficulty = max(lastDayDifficulty, jobDifficulty[t]);\n tmpMin = min(tmpMin, lastDayDifficulty + dpPrev[t - 1]);\n }\n\n dp[j] = tmpMin;\n }\n }\n\n return dp.back();\n }\n};\n", "memory": "9500" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n if( n<d) return -1;\n vector<int> prev(n + 1, 0);\n vector<int> curr(n + 1, 0);\n\n // base case\n prev[n] = 0; // index >= n && d == 1.\n int maxi = INT_MIN;\n // jis index par hai us index se aage ka max nikalna hai.\n for (int i = n - 1; i >= 0; i--) {\n maxi = max(maxi, jobDifficulty[i]);\n prev[i] = maxi; // base case initialisation.\n }\n\n for (int dayRem = 2; dayRem <= d; dayRem++) {\n for (int index = n - 1; index >= 0; index--) {\n int maxi = jobDifficulty[index];\n int rightIndex = jobDifficulty.size() - dayRem;\n\n int result = INT_MAX;\n\n for (int i = index; i <= rightIndex; i++) {\n\n maxi = max(maxi, jobDifficulty[i]);\n int remaining = prev[i + 1];\n result = min(result, maxi + remaining);\n }\n\n curr[index] = result;\n }\n prev = curr;\n }\n\n return prev[0]; // curr[0]\n }\n};", "memory": "9600" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n if(d > n) return -1;\n\n vector<vector<int>> dp(n, vector<int>(2, -1));\n dp[n - 1][1 % 2] = jobDifficulty[n - 1];\n\n for (int i = n - 2; i >= 0; i--) {\n dp[i][1 % 2] = max(jobDifficulty[i] , dp[i + 1][1 % 2]);\n }\n\n for (int days = 2; days <= d; days++) {\n for (int i = 0; i <= (n - days); i++) {\n int curAns = 1e9;\n int maxi = -1e9;\n for (int j = i; j <= (n - days); j++) {\n maxi = max(maxi, jobDifficulty[j]);\n curAns = min(curAns, maxi + dp[j + 1][(days - 1) % 2]);\n }\n\n dp[i][days % 2] = curAns;\n }\n }\n\n return dp[0][d % 2];\n }\n};", "memory": "9700" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) \n {\n int jobs = jobDifficulty.size();\n\n if(jobs < d) return -1;\n if(jobs == d) return accumulate(jobDifficulty.begin(), jobDifficulty.end(), 0);\n\n int max_difficulty = *max_element(jobDifficulty.begin(), jobDifficulty.end()) + 1;\n\n // Initialize vectors to store difficulty for previous and current days\n vector<int> prv_day(jobs, max_difficulty), curr_day(jobs);\n\n // Iterate over each day\n for (int days = 0; days < d; ++days) \n {\n vector<int> stack;\n\n // Iterate over each job starting from the current day\n for (int i = days; i < jobs; i++) \n {\n // Calculate difficulty for the current day\n if (i > 0)\n curr_day[i] = prv_day[i - 1] + jobDifficulty[i];\n else \n curr_day[i] = jobDifficulty[i];\n // Start fresh day with the first job as ith\n\n // Handle job dependencies and find minimum difficulty\n while (!stack.empty() && jobDifficulty[stack.back()] <= jobDifficulty[i]) {\n int j = stack.back(); \n stack.pop_back();\n curr_day[i] = min(curr_day[i], curr_day[j] - jobDifficulty[j] + jobDifficulty[i]);\n }\n \n // Update difficulty based on previous days\n if (!stack.empty()) {\n curr_day[i] = min(curr_day[i], curr_day[stack.back()]);\n }\n\n // Add the current job to the stack\n stack.push_back(i);\n }\n\n // Update difficulty vectors for the next day\n swap(prv_day, curr_day);\n }\n\n // Return the minimum difficulty for the last day\n return prv_day[jobs - 1];\n }\n};", "memory": "9700" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int solve(vector<int>&jd, int n, int idx, int d, vector<vector<int>>&t) {\n if (d == 1) {\n return *max_element(jd.begin() + idx, jd.end());\n }\n\n int maxD = INT_MIN;\n int finalRes = INT_MAX;\n\n if(t[idx][d] != -1) return t[idx][d];\n\n for(int i = idx; i <= n - d; i++) {\n maxD = max(maxD, jd[i]);\n int currResult = maxD + solve(jd, n, i+1, d - 1, t);\n finalRes = min(finalRes, currResult);\n }\n\n return t[idx][d] = finalRes;\n }\n\n int minDifficulty(vector<int>& jd, int d) {\n if(jd.size() < d) return -1;\n \n int n = jd.size();\n vector<vector<int>>t(n+1, vector<int>(d+1, -1));\n return solve(jd, n, 0, d, t);\n }\n};", "memory": "9800" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int solve(int i, int d, int cur_max, vector<int>& jobDifficulty, vector<vector<int>>& memo) {\n // If all jobs are scheduled, return 0 if days are exhausted, else return infinity\n if (i == jobDifficulty.size()) return d == 0 ? 0 : INT_MAX;\n if (d == 0) return INT_MAX; // If no days left, but jobs are remaining\n \n if (memo[i][d] != -1) return memo[i][d];\n \n int curDayMax = -1; // Reset max for the current day\n int minDifficulty = INT_MAX;\n \n // Try splitting at different points\n for (int j = i; j < jobDifficulty.size(); j++) {\n curDayMax = max(curDayMax, jobDifficulty[j]);\n int result = solve(j + 1, d - 1, -1, jobDifficulty, memo);\n if (result != INT_MAX) {\n minDifficulty = min(minDifficulty, curDayMax + result);\n }\n }\n \n return memo[i][d] = minDifficulty;\n }\n \n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n if (n < d) return -1; // Not enough jobs for the number of days\n \n // Memoization table\n vector<vector<int>> memo(n, vector<int>(d + 1, -1));\n \n return solve(0, d, -1, jobDifficulty, memo);\n }\n};\n", "memory": "9900" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
1
{ "code": "#define LL long long\n#define PQ priority_queue\n#define VI vector<int>\n#define VL vector<long>\n#define VUL vector<unsigned long>\n#define VVI vector<vector<int>>\n#define VVVI vector<vector<vector<int>>>\n#define VVL vector<vector<long>>\n#define VVUL vector<vector<unsigned long>>\n#define VVB vector<vector<bool>>\n#define VVC vector<vector<char>>\n#define VB vector<bool>\n#define VS vector<string>\n#define VSEI vector<set<int>>\n#define VVS vector<vector<string>>\n#define PII pair<int, int>\n#define PLL pair<long, long>\n#define PLI pair<long, int>\n#define VPII vector<pair<int, int>>\n#define VPLL vector<pair<long, long>>\n#define VVPII vector<vector<pair<int, int>>>\n#define VVPLL vector<vector<pair<long, long>>>\n#define VVPLI vector<vector<pair<long, int>>>\n#define UMII unordered_map<int, int>\n#define UMSS unordered_map<string, string>\n#define UMCS unordered_map<char, string>\n#define UM unordered_map\n#define USI unordered_set<int>\n#define USS unordered_set<string>\n#define QPII queue<pair<int, int>>\n\n#define DSZ(g, M, N) const int M = g.size(), N = g[0].size()\n#define D4(dx, dy) \\\n int dx[4] = {-1, 1, 0, 0}; \\\n int dy[4] = {0, 0, -1, 1}\n#define D8(dx, dy) \\\n int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; \\\n int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}\n#define OB(x, y, M, N) ((x) >= (M) || (y) >= (N) || (x) < 0 || (y) < 0)\nusing AI3 = std::array<int, 3>;\nusing VAI3 = std::vector<std::array<int, 3>>;\nusing AI4 = std::array<int, 4>;\nusing VAI4 = std::vector<std::array<int, 4>>;\n\n#define G_DSZ(g, M, N) const int M = g.size(), N = g[0].size()\n#define G_D4D(d) vector<pair<int, int>> d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}\n#define G_D8D(d) vector<pair<int, int>> d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}\n#define G_SZ(g, M, N) M = g.size(), N = g[0].size()\n#define G_OB(x, y, M, N) ((x) >= (M) || (y) >= (N) || (x) < 0 || (y) < 0)\n#define FOR(i, a, b) for (int i = (a); i <= (b); i++)\n#define FORD(i, a, b) for (int i = (a); i >= (b); i--)\nclass Solution {\npublic:\n int minDifficulty(vector<int>& jobs, int d) {\n const int n = jobs.size();\n if (n < d) return -1;\n // solve: dp(n,d),\n // dp(i,k): min difficulty of schedule of first i jobs into k days\n // difficulty of schedule: sum of difficulties on each day\n // (that day's maxi difficult job)\n VVI dp(n + 1, VI(d + 1, INT_MAX));\n int maxiSoFar = jobs[0];\n FOR(i, 1, n) {\n maxiSoFar = max(maxiSoFar, jobs[i - 1]);\n FOR(k, 1, d) {\n if (k == 1) {\n dp[i][1] = maxiSoFar;\n } else {\n // for every j : let j be the index of first job in the last group\n // j<=i-1 && j >=k-1\n FOR(j, k - 1, i - 1) {\n // last day jobs: nums[j]...nums[i-1]\n int maxiLastDay = *max_element(jobs.begin() + j, jobs.begin() + i);\n dp[i][k] = min(dp[i][k], dp[j][k - 1] + maxiLastDay);\n }\n }\n }\n }\n return dp[n][d] == INT_MAX ? -1 : dp[n][d];\n }\n};\n", "memory": "9900" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
1
{ "code": "class Solution {\n int solve(int idx, int n, int d, vector<int> &difficulty, vector<vector<int>> &dp) {\n if((n - idx) < d) return -1;\n \n if(dp[idx][d] != -1) return dp[idx][d];\n\n if(d == 1) {\n int temp = difficulty[idx];\n for(int i = idx; i < n; i++) {\n temp = max(temp, difficulty[i]);\n }\n\n return dp[idx][d] = temp;\n } else {\n int maxi = difficulty[idx];\n int ans = 1e9;\n\n for(int i = idx; i <= n - d; i++){\n maxi = max(maxi, difficulty[i]);\n int small_ans = solve(i + 1, n, d - 1, difficulty, dp);\n if(small_ans != -1) {\n ans = min(ans, small_ans + maxi);\n }\n }\n\n return dp[idx][d] = ans;\n }\n }\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n\n vector<vector<int>> dp(n, vector<int> (d + 1, -1));\n\n return solve(0, n, d, jobDifficulty, dp);\n }\n};", "memory": "10000" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
1
{ "code": "class Solution {\npublic:\nint solve(vector<int>&jobDifficulty,int d,int idx,int n,vector<vector<int>>&dp)\n{\n if(d==1)\n {\n int maxD=jobDifficulty[idx];\n for(int i=idx;i<n;i++)\n {\n maxD=max(maxD,jobDifficulty[i]);\n }\n return maxD;\n }\nif(dp[idx][d]!=-1)\nreturn dp[idx][d];\n int maxD=jobDifficulty[idx];\n int finalResult=INT_MAX;\n for(int i=idx;i<=n-d;i++){\n maxD=max(maxD,jobDifficulty[i]);\n int result=maxD+solve(jobDifficulty,d-1,i+1,n,dp);\n \n finalResult=min(result,finalResult);\n }\n return dp[idx][d]=finalResult;\n}\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n=jobDifficulty.size();\n if(n<d)\n return -1;\n vector<vector<int>>dp(n+1,vector<int>(d+1,-1));\n return solve(jobDifficulty,d,0,n,dp);\n }\n};", "memory": "10000" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n \n // If we can't split jobs into exactly d days, return -1.\n if (d > n) return -1;\n \n // DP table: dp[i][cut] is the minimum difficulty to schedule jobs 0..i with 'cut' cuts.\n vector<vector<int>> dp(n, vector<int>(d, INT_MAX));\n \n // Base case: When we make d-1 cuts, the remaining jobs after the last cut are assigned to the last day.\n dp[0][0] = jobDifficulty[0];\n \n // Preprocessing to fill the first column (cut == 0, meaning no cuts have been made yet)\n for (int i = 1; i < n; ++i) {\n dp[i][0] = max(dp[i-1][0], jobDifficulty[i]);\n }\n \n // Fill the DP table for 1 to d-1 cuts\n for (int cut = 1; cut < d; ++cut) {\n // Loop through all job indices where we can place the cut\n for (int i = cut; i < n; ++i) {\n int maxDifficulty = 0;\n \n // Try placing the cut at different positions\n for (int j = i; j >= cut; --j) {\n maxDifficulty = max(maxDifficulty, jobDifficulty[j]);\n dp[i][cut] = min(dp[i][cut], dp[j-1][cut-1] + maxDifficulty);\n }\n }\n }\n \n // The answer will be in dp[n-1][d-1], the minimum difficulty for all jobs split into d days\n return dp[n-1][d-1];\n }\n};\n", "memory": "10100" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n if (n < d)\n return -1;\n\n vector < vector < int > > dp(n, vector < int > (d + 1, INT_MAX));\n\n dp[n - 1][d] = jobDifficulty[n - 1];\n\n for (int i = n - 2 ; i >= 0 ; i--) {\n dp[i][d] = max(dp[i + 1][d], jobDifficulty[i]);\n }\n\n for (int day = d - 1 ; day > 0 ; day--) {\n for (int i = day - 1 ; i < n - (d - day) ; i++) {\n int hardest = 0;\n for (int j = i ; j < n - (d - day) ; j++) {\n hardest = max(hardest, jobDifficulty[j]);\n dp[i][day] = min(dp[i][day], hardest + dp[j + 1][day + 1]);\n }\n }\n }\n\n return dp[0][1];\n }\n};", "memory": "10100" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n long long getMinDifficulty(vector<int>&a,int ind,int d,int n,vector<vector<long long>>&dp){\n\n if(ind>n || d<0) return INT_MAX;\n if(ind==n){\n return d==0 ? 0 : INT_MAX;\n }\n if(dp[ind][d]!=-1) return dp[ind][d];\n\n long long minDiff = INT_MAX;\n long long curMax = INT_MIN;\n\n for(int i=ind; i<n; i++){\n curMax = max(curMax , (long long)a[i]);\n //if(i==n-1) return d==1 ? curMax : INT_MAX;\n minDiff = min(minDiff , curMax+getMinDifficulty(a,i+1,d-1,n,dp));\n }\n return dp[ind][d] = minDiff;\n \n }\n long long getMinDifficultyTab(vector<int>&a,int d,int n){\n\n vector<vector<long long>>dp(n+1,vector<long long>(d+1,0));\n\n for(int i=1; i<=d; i++) dp[n][i] = INT_MAX;\n\n for(int i=n; i>=0; i--){\n for(int j=d; j>=0; j--){\n\n long long minDiff = INT_MAX;\n long long curMax = INT_MIN;\n\n for(int k=i; k<n; k++){\n curMax = max(curMax , (long long)(a[k]));\n minDiff = min(minDiff , j-1<0 | i+1>n ? INT_MAX : curMax+dp[k+1][j-1]);\n }\n dp[i][j]=minDiff;\n }\n }\n return dp[0][d];\n }\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n \n int n = jobDifficulty.size();\n if(d>n) return -1;\n //return getMinDifficultyTab(jobDifficulty,d,n);\n vector<vector<long long>>dp(n+1,vector<long long>(d+1,-1));\n return getMinDifficulty(jobDifficulty,0,d,n,dp);\n }\n};", "memory": "10200" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n long long getMinDifficulty(vector<int>&a,int ind,int d,int n,vector<vector<long long>>&dp){\n\n if(ind>n || d<0) return INT_MAX;\n if(ind==n){\n return d==0 ? 0 : INT_MAX;\n }\n if(dp[ind][d]!=-1) return dp[ind][d];\n\n long long minDiff = INT_MAX;\n long long curMax = INT_MIN;\n\n for(int i=ind; i<n; i++){\n curMax = max(curMax , (long long)a[i]);\n //if(i==n-1) return d==1 ? curMax : INT_MAX;\n minDiff = min(minDiff , curMax+getMinDifficulty(a,i+1,d-1,n,dp));\n }\n return dp[ind][d] = minDiff;\n \n }\n long long getMinDifficultyTab(vector<int>&a,int d,int n){\n\n vector<vector<long long>>dp(n+1,vector<long long>(d+1,0));\n\n for(int i=1; i<=d; i++) dp[n][i] = INT_MAX;\n\n for(int i=n-1; i>=0; i--){\n for(int j=d; j>=0; j--){\n\n long long minDiff = INT_MAX;\n long long curMax = INT_MIN;\n\n for(int k=i; k<n; k++){\n curMax = max(curMax , (long long)(a[k]));\n minDiff = min(minDiff , j-1<0 | i+1>n ? INT_MAX : curMax+dp[k+1][j]);\n }\n dp[i][j+1]=minDiff;\n }\n }\n return dp[0][d];\n }\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n \n int n = jobDifficulty.size();\n if(d>n) return -1;\n //return getMinDifficultyTab(jobDifficulty,d,n);\n vector<vector<long long>>dp(n+1,vector<long long>(d+1,-1));\n return getMinDifficulty(jobDifficulty,0,d,n,dp);\n }\n};", "memory": "10200" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n long long getMinDifficulty(vector<int>&a,int ind,int d,int n,vector<vector<long long>>&dp){\n\n if(ind>n || d==0) return INT_MAX;\n if(ind==n){\n return d==1 ? 0 : INT_MAX;\n }\n if(dp[ind][d]!=-1) return dp[ind][d];\n\n long long minDiff = INT_MAX;\n long long curMax = INT_MIN;\n\n for(int i=ind; i<n; i++){\n curMax = max(curMax , (long long)a[i]);\n //if(i==n-1) return d==1 ? curMax : INT_MAX;\n minDiff = min(minDiff , curMax+getMinDifficulty(a,i+1,d-1,n,dp));\n }\n return dp[ind][d] = minDiff;\n \n }\n long long getMinDifficultyTab(vector<int>&a,int d,int n){\n\n vector<vector<long long>>dp(n+1,vector<long long>(d+1,0));\n\n for(int i=1; i<=d; i++) dp[n][i] = INT_MAX;\n\n for(int i=n-1; i>=0; i--){\n for(int j=d; j>=0; j--){\n\n long long minDiff = INT_MAX;\n long long curMax = INT_MIN;\n\n for(int k=i; k<n; k++){\n curMax = max(curMax , (long long)(a[k]));\n minDiff = min(minDiff , j-1<0 | i+1>n ? INT_MAX : curMax+dp[k+1][j]);\n }\n dp[i][j]=minDiff;\n }\n }\n return dp[0][d];\n }\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n \n int n = jobDifficulty.size();\n if(d>n) return -1;\n //return getMinDifficultyTab(jobDifficulty,d,n);\n vector<vector<long long>>dp(n+1,vector<long long>(d+2,-1));\n return getMinDifficulty(jobDifficulty,0,d+1,n,dp);\n }\n};", "memory": "10300" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n long long getMinDifficulty(vector<int>&a,int ind,int d,int n,vector<vector<long long>>&dp){\n\n if(ind>n || d==0) return INT_MAX;\n if(ind==n){\n return d==1 ? 0 : INT_MAX;\n }\n if(dp[ind][d]!=-1) return dp[ind][d];\n\n long long minDiff = INT_MAX;\n long long curMax = INT_MIN;\n\n for(int i=ind; i<n; i++){\n curMax = max(curMax , (long long)a[i]);\n //if(i==n-1) return d==1 ? curMax : INT_MAX;\n minDiff = min(minDiff , curMax+getMinDifficulty(a,i+1,d-1,n,dp));\n }\n return dp[ind][d] = minDiff;\n \n }\n long long getMinDifficultyTab(vector<int>&a,int d,int n){\n\n vector<vector<long long>>dp(n+1,vector<long long>(d+2,0));\n\n for(int i=0; i<=n; i++) dp[i][0] = INT_MAX;\n for(int j=1; j<=d+1; j++) dp[n][j] = INT_MAX;\n dp[n][1]=0;\n\n for(int i=n-1; i>=0; i--){\n for(int j=d+1; j>0; j--){\n\n long long minDiff = INT_MAX;\n long long curMax = INT_MIN;\n\n for(int k=i; k<n; k++){\n curMax = max(curMax , (long long)(a[k]));\n minDiff = min(minDiff , curMax+dp[k+1][j-1]);\n }\n dp[i][j]=minDiff;\n }\n }\n return dp[0][d+1];\n }\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n \n int n = jobDifficulty.size();\n if(d>n) return -1;\n return getMinDifficultyTab(jobDifficulty,d,n);\n //vector<vector<long long>>dp(n+1,vector<long long>(d+2,-1));\n //return getMinDifficulty(jobDifficulty,0,d+1,n,dp);\n }\n};", "memory": "10300" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint n;\nint solve(int ind,vector<int>& jobDifficulty, int d, vector<vector<int>> &dp){\n \n if(dp[ind][d]!=-1)return dp[ind][d];\n int result=INT_MAX,sum=0,maxi=0;\n \n if(d==1){\n int maxii=-1;\n for(int j=ind;j<n;j++)maxii=max(maxii,jobDifficulty[j]);\n return maxii;\n }\n \n for(int i=ind;i<=n-d;i++){\n maxi=max(maxi,jobDifficulty[i]);\n sum=maxi + solve(i+1,jobDifficulty,d-1,dp);\n result=min(result,sum);\n } \n return dp[ind][d]=result;\n}\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n n=jobDifficulty.size();\n if(n<d)return -1;\n vector<vector<int>> dp(301,vector<int>(11,-1));\n return solve(0,jobDifficulty,d, dp);\n }\n};", "memory": "10400" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int ind, int days, vector<int> jd, int n,\n vector<vector<int>>& dp) {\n if (days == 1) {\n int maxi = jd[ind];\n for (int i = ind; i < n; i++)\n maxi = max(maxi, jd[i]);\n\n return maxi;\n }\n if (dp[ind][days] != -1)\n return dp[ind][days];\n\n int maxi = jd[ind];\n int finalResult = INT_MAX;\n\n for (int i = ind; i <= n - days; i++) {\n maxi = max(maxi, jd[i]);\n int result = maxi + solve(i + 1, days - 1, jd, n, dp);\n finalResult = min(result, finalResult);\n }\n return dp[ind][days] = finalResult;\n }\n int minDifficulty(vector<int>& jd, int d) {\n int n = jd.size();\n if (d > n)\n return -1;\n vector<vector<int>> t(301, vector<int>(11, 0));\n for (int i = 0; i < n; i++) {\n t[i][1] = *max_element(begin(jd) + i, end(jd));\n }\n for (int days = 2; days <= d; days++) {\n for (int i = 0; i <= n - days; i++) {\n int maxDifficulty = INT_MIN;\n int result = INT_MAX;\n\n for (int j = i; j <= n - days; j++) {\n maxDifficulty = max(maxDifficulty, jd[j]);\n result = min(result, maxDifficulty + t[j + 1][days - 1]);\n }\n\n t[i][days] = result;\n }\n }\n\n return t[0][d];\n }\n};", "memory": "10500" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int ind, int days, vector<int> jd, int n,\n vector<vector<int>>& dp) {\n if (days == 1) {\n int maxi = jd[ind];\n for (int i = ind; i < n; i++)\n maxi = max(maxi, jd[i]);\n\n return maxi;\n }\n if (dp[ind][days] != -1)\n return dp[ind][days];\n\n int maxi = jd[ind];\n int finalResult = INT_MAX;\n\n for (int i = ind; i <= n - days; i++) {\n maxi = max(maxi, jd[i]);\n int result = maxi + solve(i + 1, days - 1, jd, n, dp);\n finalResult = min(result, finalResult);\n }\n return dp[ind][days] = finalResult;\n }\n int minDifficulty(vector<int>& jd, int d) {\n int n = jd.size();\n if (d > n)\n return -1;\n vector<vector<int>> t(301, vector<int>(11, 0));\n for (int i = 0; i < n; i++) {\n t[i][1] = *max_element(begin(jd) + i, end(jd));\n }\n for (int days = 2; days <= d; days++) {\n for (int i = 0; i < n ; i++) {\n int maxDifficulty = INT_MIN;\n int result = INT_MAX;\n\n for (int j = i; j <= n - days; j++) {\n maxDifficulty = max(maxDifficulty, jd[j]);\n result = min(result, maxDifficulty + t[j + 1][days - 1]);\n }\n\n t[i][days] = result;\n }\n }\n\n return t[0][d];\n }\n};", "memory": "10500" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int index,vector<int>& jd, int d,vector<vector<int>> &dp){\n if(index>=jd.size()){\n return -1;\n }\n if(d==1){\n return *max_element(jd.begin()+index,jd.end());\n }\n\n if(dp[index][d]!=-1){\n return dp[index][d];\n }\n int minDiff=1e9;\n int maxi=jd[index];\n for(int i=index;i<jd.size();i++){\n maxi=max(maxi,jd[i]);\n int nextDay=solve(i+1,jd,d-1,dp);\n if(nextDay!=-1){\n minDiff=min(minDiff,maxi+nextDay);\n }\n }\n\n return dp[index][d]=minDiff;\n }\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n if(d>jobDifficulty.size()){\n return -1;\n }\n\n vector<vector<int>> dp(302,vector<int>(11,-1));\n return solve(0,jobDifficulty,d,dp);\n }\n};", "memory": "10600" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int index,vector<int>& jd, int d,vector<vector<int>> &dp){\n if(dp[index][d]!=-1){\n return dp[index][d];\n }\n\n if(index>=jd.size()){\n return 1e9;\n }\n if(d==1){\n return *max_element(jd.begin()+index,jd.end());\n }\n\n int minDiff=1e9;\n int maxi=jd[index];\n for(int i=index;i<jd.size()-(d-1);i++){ // agar array 5 size ka hai and day 3 hai tho hame make sure karna hai na ki har din kaam ho tho 1 din main at max ham 3 kaam hii kar paaenge na kyuki paancho ek din main kar lenge tho kaise hoga aage ka isko make sure karne k liye hamare pass 2 options hai ya tho ek check maar lo starting main jab index exceed kar lega jd k size ko tho 1e9 return kar do and minDiff main 1e9 wale add honge nhi aur hame minDIff mil jaaegaa\n maxi=max(maxi,jd[i]);\n minDiff=min(minDiff,maxi+solve(i+1,jd,d-1,dp)); // agar next day se start hoga tho next i+1 hoga n\n }\n\n return dp[index][d]=minDiff;\n }\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n if(d>jobDifficulty.size()){\n return -1;\n }\n\n vector<vector<int>> dp(302,vector<int>(11,-1));\n return solve(0,jobDifficulty,d,dp);\n }\n};", "memory": "10600" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dp[1001][301];\n int solverec(vector<int>& jobDifficulty, int d, int idx) {\n if (idx == jobDifficulty.size()) {\n if(d==0){\n return 0;\n }\n return INT_MAX;\n }\n if (d == 0) {\n return INT_MAX; \n }\n if (dp[d][idx] != -1) {\n return dp[d][idx]; \n }\n\n int ma = 0;\n int ans = INT_MAX;\n\n for (int i = idx; i < jobDifficulty.size(); ++i) {\n ma = max(ma, jobDifficulty[i]);\n int next = solverec(jobDifficulty, d - 1, i + 1);\n if (next != INT_MAX) {\n ans = min(ans, ma + next);\n }\n }\n\n return dp[d][idx] = ans;\n }\n\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n if (jobDifficulty.size() < d) {\n return -1; \n }\n memset(dp,-1,sizeof(dp));\n return solverec(jobDifficulty, d, 0);\n }\n};\n", "memory": "10700" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> j;\n int dp[301][1001];\n int solve(int start,int left)\n {\n if(j.size()==start&&left==0) return 0;\n if(j.size()==start||left==0||j.size()<start+left) return 1e9;\n if(dp[start][left]!=-1) return dp[start][left];\n int ans=1e9,mx=0;\n for(int i=start;i<j.size();i++)\n {\n mx=max(mx,j[i]);\n int res=solve(i+1,left-1);\n if(res!=1e9) ans=min(ans,mx+res);\n }\n return dp[start][left]=ans;\n }\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n j=jobDifficulty;\n if(j.size()<d) return -1;\n memset(dp,-1,sizeof(dp));\n return solve(0,d);\n }\n};", "memory": "10800" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class SGT_PU{\n // segement tree point update \n // like fenwick tree\n // this is for 0 based indexing so idx=0\npublic:\n vector<int> seg;\n SGT_PU(int n){\n seg.resize(4*n+1);\n }\n void build(int idx,int low,int high,vector<int> &arr){\n if(low==high){\n seg[idx]=arr[low];\n return;\n }\n int mid=low+(high-low)/2;\n build(2*idx+1,low,mid,arr);\n build(2*idx+2,mid+1,high,arr);\n seg[idx]=max({seg[2*idx+1],seg[2*idx+2]});\n }\n int query(int idx,int low,int high,int l,int r){\n // no overlap\n // l r low high , low high l r\n if(r<low or high<l) return INT_MIN;\n // completely overlap\n // l low high r\n if(low>=l and high<=r) return seg[idx];\n // partially overlap\n int mid=low+(high-low)/2;\n int left=query(2*idx+1,low,mid,l,r);\n int right=query(2*idx+2,mid+1,high,l,r);\n return max({left,right});\n }\n void update(int idx,int low,int high,int i,int val){\n if(low==high){\n seg[idx]=val;\n return;\n }\n int mid=low+(high-low)/2;\n if(i<=mid){\n update(2*idx+1,low,mid,i,val);\n }else{\n update(2*idx+2,mid+1,high,i,val);\n }\n seg[idx]=max({seg[2*idx+1],seg[2*idx+2]});\n }\n};\nclass Solution {\npublic:\n int n;\n long long dp[302][12];\n long long solve(int prev,vector<int> &arr,int d,SGT_PU &sg){\n if(d<=0 and prev>=n) return 0;\n if(prev>=n or d<=0) return INT_MAX;\n if(dp[prev][d]!=-1) return dp[prev][d];\n long long mn=INT_MAX;\n for(int i=prev+1;i<=n;i++){\n mn=min(mn,sg.query(0,0,n-1,prev,i-1)+solve(i,arr,d-1,sg));\n }\n return dp[prev][d]=mn;\n }\n int minDifficulty(vector<int>& arr, int d) {\n n=arr.size();\n SGT_PU sg(n);\n sg.build(0,0,n-1,arr);\n memset(dp,-1,sizeof(dp));\n if(d>n) return -1;\n return solve(0,arr,d,sg);\n }\n};", "memory": "10900" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int memo[301][11];\n memset(memo, -1, sizeof(memo));\n function<int(int, int)> dfs = [&](int index, int dd){\n if(index == jobDifficulty.size()){\n if(dd == 0) return 0;\n else return 100000;\n } if(dd < 0) return 100000;\n if(memo[index][dd] != -1) return memo[index][dd];\n int maxYet = -1, ans = INT_MAX;\n for(int i=index; i<jobDifficulty.size(); i++){\n maxYet = max(maxYet, jobDifficulty[i]);\n ans = min(ans, maxYet + dfs(i + 1, dd - 1));\n }\n return memo[index][dd] = ans;\n };\n int ans = dfs(0, d);\n return ans >= 100000 ? -1 : ans;\n }\n};", "memory": "11000" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& job, int d) {\n int n=job.size();\n vector<vector<int>> dp(n,vector<int>(d+1,-1));\n if(n<d) return -1;\n\n function<int(int,int)> solve = [&](int curr, int day){\n //if(day>d) return ;\n if(day>d && curr!=n) return (int)1e6;\n if(day>d) return 0;\n if(curr==n) return 0;\n\n if(dp[curr][day]!=-1) return dp[curr][day];\n //cout<<\"Calculating \"<<curr<<\" \"<<day<<endl;\n int maxi=0;\n int ans=INT_MAX;\n for(int i=curr;i<n-d+day;i++){\n maxi=max(maxi,job[i]);\n ans=min(ans, maxi+solve(i+1,day+1));\n //cout<<ans<<\" \";\n }\n\n return dp[curr][day]=ans;\n };\n\n return solve(0,1);\n }\n};", "memory": "11200" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n \n if (d > n)\n return -1;\n \n vector hardestRemaining(n, 0);\n hardestRemaining[n - 1] = jobDifficulty[n - 1];\n for (int i = n - 2; i >= 0; i--) \n hardestRemaining[i] = max(hardestRemaining[i + 1], jobDifficulty[i]);\n \n vector cache(n, vector<int>(d + 1, -1));\n \n function<int(int, int)> dp = [&] (int i, int day) {\n if (day == d)\n return hardestRemaining[i];\n \n if (cache[i][day] != -1)\n return cache[i][day];\n \n int maximum = jobDifficulty[i];\n long res = INT_MAX;\n for (int j = i; j < n - (d - day); j++) {\n maximum = max(jobDifficulty[j], maximum);\n res = min(res, (long)maximum + dp(j + 1, day + 1));\n }\n \n return cache[i][day] = res;\n \n };\n \n return dp(0, 1);\n }\n};", "memory": "11400" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n \n if (d > n)\n return -1;\n \n vector hardestRemaining(n, 0);\n hardestRemaining[n - 1] = jobDifficulty[n - 1];\n for (int i = n - 2; i >= 0; i--) \n hardestRemaining[i] = max(hardestRemaining[i + 1], jobDifficulty[i]);\n \n vector cache(n, vector<int>(d + 1, -1));\n \n function<int(int, int)> dp = [&] (int i, int day) {\n if (day == d)\n return hardestRemaining[i];\n \n if (cache[i][day] != -1)\n return cache[i][day];\n \n int maximum = jobDifficulty[i];\n long res = INT_MAX;\n for (int j = i; j < n - (d - day); j++) {\n maximum = max(jobDifficulty[j], maximum);\n res = min(res, (long)maximum + dp(j + 1, day + 1));\n }\n \n return cache[i][day] = res;\n \n };\n \n return dp(0, 1);\n }\n};", "memory": "11500" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n if (jobDifficulty.size() < d)\n return -1;\n\n map<pair<int,int>,int> memo;\n return md(jobDifficulty, d, 0, memo);\n }\n\n int md(vector<int>& jobDifficulty, int daysRemaining, int i, map<pair<int,int>,int>& memo) {\n if (daysRemaining == 0) {\n int m = 0;\n for (int j = i; j < jobDifficulty.size(); ++j)\n m = max(m, jobDifficulty[j]);\n return m;\n }\n\n int minDifficulty = INT_MAX;\n auto it = memo.find({i, daysRemaining});\n if (it == memo.end()) {\n for (int j = i; j < jobDifficulty.size() - daysRemaining + 1; ++j) {\n int maxDifficulty = 0;\n for (int k = i; k <= j; ++k)\n maxDifficulty = max(maxDifficulty, jobDifficulty[k]);\n \n minDifficulty = min(minDifficulty, maxDifficulty + md(jobDifficulty, daysRemaining - 1, j + 1, memo));\n }\n it = memo.insert(it, {{i, daysRemaining}, minDifficulty});\n }\n return it->second;\n }\n};\n", "memory": "11800" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n if (jobDifficulty.size() < d)\n return -1;\n\n map<pair<int,int>,int> memo;\n return md(jobDifficulty, d, 0, memo);\n }\n\n int md(vector<int>& jobDifficulty, int daysRemaining, int i, map<pair<int,int>,int>& memo) {\n if (daysRemaining == 0) {\n int m = 0;\n for (int j = i; j < jobDifficulty.size(); ++j)\n m = max(m, jobDifficulty[j]);\n return m;\n }\n\n int minDifficulty = INT_MAX;\n auto it = memo.find({i, daysRemaining});\n if (it == memo.end()) {\n for (int j = i; j < jobDifficulty.size() - daysRemaining + 1; ++j) {\n int maxDifficulty = 0;\n for (int k = i; k <= j; ++k)\n maxDifficulty = max(maxDifficulty, jobDifficulty[k]);\n \n minDifficulty = min(minDifficulty, maxDifficulty + md(jobDifficulty, daysRemaining - 1, j + 1, memo));\n }\n it = memo.insert(it, {{i, daysRemaining}, minDifficulty});\n }\n return it->second;\n }\n};\n", "memory": "11800" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(vector<int>& j, int d, int n, int idx,map<pair<int,int>,int>&dp) {\n if(dp.find({d,idx})!=dp.end())return dp.find({d,idx})->second;\n if(d==1){\n int t = j[idx];\n for(int i = idx; i<n; i++)t=max(t,j[i]);\n return t;\n }\n if(d<=0)return 0;\n int currMax = INT_MIN;\n int temp;\n int ans = INT_MAX;\n for(int i = idx; i<=n-d; i++){\n currMax = max(currMax, j[i]);\n temp = currMax + f(j,d-1,n,i+1,dp);\n ans = min(temp,ans);\n }\n\n dp.insert({{d,idx},ans});\n\n return ans;\n }\n\n int minDifficulty(vector<int>& j, int d) {\n int ans = INT_MAX;\n int n = j.size();\n map<pair<int,int>,int>dp;\n int value = f(j,d,n,0,dp);\n\n if(value==INT_MAX)return -1;\n\n return value;\n }\n};\n", "memory": "11900" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(vector<int>& j, int d, int n, int idx,map<pair<int,int>,int>&dp) {\n if(dp.find({d,idx})!=dp.end())return dp.find({d,idx})->second;\n if(d==1){\n int t = j[idx];\n for(int i = idx; i<n; i++)t=max(t,j[i]);\n return t;\n }\n if(d<=0)return 0;\n int currMax = j[idx];\n int temp;\n int ans = INT_MAX;\n for(int i = idx; i<=n-d; i++){\n currMax = max(currMax, j[i]);\n temp = currMax + f(j,d-1,n,i+1,dp);\n ans = min(temp,ans);\n }\n\n dp.insert({{d,idx},ans});\n\n return ans;\n }\n\n int minDifficulty(vector<int>& j, int d) {\n int ans = INT_MAX;\n int n = j.size();\n map<pair<int,int>,int>dp;\n int value = f(j,d,n,0,dp);\n\n if(value==INT_MAX)return -1;\n\n return value;\n }\n};\n", "memory": "12200" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(int i,int d,vector<int>&v,vector<vector<int>>&dp){\n int n=v.size();\n if(d==1){\n return *max_element(v.begin()+i,v.end());\n }\n if(dp[i][d]!=-1){\n return dp[i][d];\n }\n int maxi=INT_MIN;\n int ans=INT_MAX;\n for(int j=i;j<=n-d;j++){\n maxi=max(maxi,v[j]);\n ans=min(ans,maxi+f(j+1,d-1,v,dp));\n }\n return dp[i][d]=ans;\n }\n int minDifficulty(vector<int>& v, int d) {\n int n=v.size();\n if(n<d){\n return -1;\n }\n vector<vector<int>>dp(n,vector<int>(100,-1));\n return f(0,d,v,dp);\n }\n};", "memory": "12300" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "#include<bits/stdc++.h>\nclass Solution {\n \npublic:\n // unordered_map<pair<int,int>,int>mp; cant use unodered map for a pair\n map<pair<int,int>,int>mp;\n int solve(int ind,int d,vector<int>& jobDifficulty){\n // ind and day changes but d is dynamic\n // base case\n int n = jobDifficulty.size();\n \n if(d==1){\n // find the max of all the left over tasks and return\n int ans = INT_MIN;\n for(int i=ind;i<n;i++){\n ans = max(ans,jobDifficulty[i]);\n }\n return ans; // this is the maxD in this case\n }\n\n // find in map\n if (mp.find({ind, d}) != mp.end()) {\n return mp[{ind,d}];\n } \n \n // this base case doesnt matter here\n // if(ind>=n){\n // return -1;\n // }\n\n int maxD = jobDifficulty[ind];\n int finalResult = INT_MAX;\n\n // now check for all possible days\n for(int i=ind;i<=n-d;i++){\n maxD = max(maxD ,jobDifficulty[i]);\n // int result = maxD + solve(ind+1,d-1,jobDifficulty);\n // in recursive call i must be there\n int result = maxD + solve(i+1,d-1,jobDifficulty);\n finalResult = min(finalResult,result);\n }\n\n // before saving push in map\n mp[{ind,d}] = finalResult;\n \n return finalResult;\n }\n\n\n\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n // dp qn \n // fn has 2 parameters index and day \n // index i has range 0 to n-d \n // this is because everyday must have atleast 1 task \n\n\n\n if(jobDifficulty.size() <d){\n return -1;\n }\n\n return solve(0,d,jobDifficulty);\n }\n};", "memory": "12500" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "#include<bits/stdc++.h>\nclass Solution {\n \npublic:\n // unordered_map<pair<int,int>,int>mp; cant use unodered map for a pair\n map<pair<int,int>,int>mp;\n int solve(int ind,int d,vector<int>& jobDifficulty){\n // ind and day changes but d is dynamic\n // base case\n int n = jobDifficulty.size();\n \n if(d==1){\n // find the max of all the left over tasks and return\n int ans = INT_MIN;\n for(int i=ind;i<n;i++){\n ans = max(ans,jobDifficulty[i]);\n }\n return ans; // this is the maxD in this case\n }\n\n // find in map\n if (mp.find({ind, d}) != mp.end()) {\n return mp[{ind,d}];\n } \n \n // this base case doesnt matter here\n // if(ind>=n){\n // return -1;\n // }\n\n int maxD = jobDifficulty[ind];\n int finalResult = INT_MAX;\n\n // now check for all possible days\n for(int i=ind;i<=n-d;i++){\n maxD = max(maxD ,jobDifficulty[i]);\n // int result = maxD + solve(ind+1,d-1,jobDifficulty);\n // in recursive call i must be there\n int result = maxD + solve(i+1,d-1,jobDifficulty);\n finalResult = min(finalResult,result);\n }\n\n // before saving push in map\n mp[{ind,d}] = finalResult;\n \n return finalResult;\n }\n\n\n\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n // dp qn \n // fn has 2 parameters index and day \n // index i has range 0 to n-d \n // this is because everyday must have atleast 1 task \n\n\n\n if(jobDifficulty.size() <d){\n return -1;\n }\n\n return solve(0,d,jobDifficulty);\n }\n};", "memory": "12600" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n size_t getKey(int a, int b) {\n return (size_t) a << 32 | (unsigned int) b;\n }\n\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n unordered_map<size_t, int> memo;\n\n int current = dfs(0, d, memo, jobDifficulty);\n return current == INT_MAX ? -1 : current;\n }\n \n int dfs(int i, int d, unordered_map<size_t, int>& memo, vector<int>& jobDifficulty) {\n int n = jobDifficulty.size();\n if (i == n && d == 0) {\n return 0;\n }\n if (i == n) {\n return INT_MAX;\n }\n if (d == 0) {\n return INT_MAX;\n }\n\n size_t key = getKey(d, i);\n if (memo.count(key)) {\n return memo[key];\n }\n\n int maxDifficulty = 0;\n int minTotal = INT_MAX;\n for (int j = i; j < n; j++) {\n maxDifficulty = max(maxDifficulty, jobDifficulty[j]);\n\n int current = dfs(j + 1, d - 1, memo, jobDifficulty);\n if (current == INT_MAX) continue;\n\n minTotal = min(minTotal, maxDifficulty + current);\n }\n \n memo[key] = minTotal;\n return minTotal;\n }\n};", "memory": "12700" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int helper(int i, int d,vector<int>& arr,map<pair<int,int>, int>& mp){\n int n = arr.size();\n if(i==n && d==0){\n return 0;\n }\n if((i<n && d==0) || (i>=n && d>0))return -1;\n if(mp.count({i,d}))return mp[{i,d}];\n int mx = arr[i];\n int ans = INT_MAX/2;\n\n for(int j=i;j<n;j++){\n mx = max(mx,arr[j]);\n int smallans = helper(j+1,d-1,arr,mp);\n if(smallans != -1){\n ans = min(ans,mx+smallans);\n }\n }\n \n mp[{i,d}] = ans;\n return ans;\n\n }\n\n\n int minDifficulty(vector<int>& arr, int d) {\n int n = arr.size();\n if(n<d)return -1;\n map<pair<int,int>, int> mp;\n\n return helper(0,d,arr,mp);\n }\n};\n", "memory": "12800" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n \n struct Key{\n int job;\n int day;\n \n Key(int j,int d):job(j),day(d) {}\n \n bool operator==(const Key& other) const{\n return job==other.job && day==other.day;\n }\n };\n \n struct KeyHash{\n size_t operator()(const Key& k) const{\n return std::hash<int>()(k.job) ^ (std::hash<int>()(k.day) << 1); \n }\n };\n \n \n unordered_map<Key,int,KeyHash> memo;\n \n vector<int> jobDifficulty;\n int d; \n int* hardestToEnd;\n \n void hardest(){ \n int nJobs=jobDifficulty.size();\n hardestToEnd= new int[nJobs];\n hardestToEnd[nJobs-1]=jobDifficulty[nJobs-1];\n for (int i=nJobs-2;i>=0;--i){\n hardestToEnd[i]=max(jobDifficulty[i],hardestToEnd[i+1]);\n }\n }\n\n \n int dp(int day, int j){\n if (day==d){\n return (hardestToEnd[j]);\n }\n \n Key k(day,j);\n if (memo.find(k)!=memo.end())\n return memo[k];\n \n int nj=jobDifficulty.size();\n int best=hardestToEnd[0]*d;\n \n for (int job=j;job<nj-(d-day);++job){\n //find hardest job from j to i\n int difficultyOfDay=jobDifficulty[j];\n for (int i=j+1;i<=job;++i){\n difficultyOfDay=max(difficultyOfDay, jobDifficulty[i]);\n }\n best=min(dp(day+1,job+1)+difficultyOfDay,best);\n }\n \n memo[Key(day,j)]=best; \n return best;\n }\n \n \npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n this->d=d;\n this-> jobDifficulty=jobDifficulty;\n hardest();\n \n if (jobDifficulty.size()<d){\n return -1;\n }\n \n return dp(1,0);\n }\n};", "memory": "13000" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n using State = pair<int, int>;\n int solve(std::map<State, int> & dp, State const & s, vector<int> & difficulty, int days) {\n \n if(dp.find(s) != dp.end()) \n return dp.at(s);\n \n int index;\n int day;\n std::tie(index, day) = s;\n \n if(day > days) return 1e9;\n if(day == days and index == difficulty.size()) return 0;\n \n int max_value = difficulty[index];\n \n int min_value = 1e9;\n for(int i = index; i < difficulty.size(); ++i) {\n max_value = max(difficulty[i], max_value);\n \n min_value = min(min_value, max_value + solve(dp, State(i+1, day+1), difficulty, days));\n }\n \n dp[s] = min_value;\n \n return min_value;\n }\n \n int minDifficulty(vector<int>& jobDifficulty, int d) {\n std::map<State, int> dp;\n \n int x = solve(dp, State(0, 0), jobDifficulty, d);\n \n return x <= 300000 ? x: -1;\n }\n};", "memory": "13400" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\n int memo[1000][1000];\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n if (n < d) {\n return -1;\n }\n\n // Initialize memo array with -1\n memset(memo, -1, sizeof(memo));\n\n return solve(d, 0, jobDifficulty);\n }\n\n int solve(int d, int idx, vector<int>& jobDifficulty){\n int n = jobDifficulty.size();\n if (d == 1) {\n int maxD = 0;\n for (int i = idx; i < n; i++) {\n maxD = max(maxD, jobDifficulty[i]);\n }\n return maxD;\n }\n\n if (memo[d][idx] != -1) {\n return memo[d][idx];\n }\n\n int maxD = 0;\n int finalResult = INT_MAX;\n\n for (int i = idx; i <= n - d; i++) {\n maxD = max(maxD, jobDifficulty[i]);\n int result = maxD + solve(d - 1, i + 1, jobDifficulty);\n finalResult = min(finalResult, result);\n }\n\n return memo[d][idx] = finalResult;\n\n }\n};", "memory": "13600" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\n map<pair<int, int>, int> mp;\n int minDif(vector<int> &jobDifficulty, int d, int idx){\n if(mp.find({d, idx}) != mp.end()) return mp[{d, idx}];\n int n = jobDifficulty.size();\n if(d == 0) return mp[{d, idx}] = (idx == n) ? 0 : INT_MAX; \n if(idx == n) return mp[{d, idx}] = (d == 0) ? 0 : INT_MAX;\n int res = INT_MAX, mx = 0;\n for(int i = idx; i <= n-1; i++){\n mx = max(mx, jobDifficulty[i]);\n int temp = minDif(jobDifficulty, d-1, i+1);\n res = min(res, ((temp == INT_MAX) ? INT_MAX : temp + mx));\n }\n return mp[{d, idx}] = res;\n }\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n if(n < d) return -1;\n return minDif(jobDifficulty, d, 0);\n }\n};", "memory": "14500" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n map<string,int>dp;\n\n int f(int lvl, int currD, int d, vector<int>&a, int n){\n if(currD==d){\n int lstMx=INT_MIN;\n for(int i=lvl;i<n;i++) lstMx=max(lstMx, a[i]);\n return lstMx;\n }\n\n string key = to_string(lvl)+\"#\"+to_string(currD);\n if(dp.find(key)!=dp.end()) return dp[key];\n\n int ans=INT_MAX, mx=INT_MIN;\n\n for(int i=lvl; i<n-(d-currD); i++){\n mx = max(a[i], mx);\n int currAns = mx+f(i+1, currD+1, d, a, n);\n ans = min(ans, currAns);\n }\n\n return dp[key]=ans;\n }\n\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n dp.clear();\n int n=jobDifficulty.size();\n if(n<d)return -1;\n return f(0, 1, d, jobDifficulty, n);\n }\n};", "memory": "14700" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int solve(int cur, int day, vector<vector<int>> &dp, vector<int> &v, int n){\n if(day==0){\n if(cur>=n) return 0;\n return 1e9;\n }\n if(cur>=n){\n if(day>0) return 1e9;\n return 0;\n }\n if(dp[cur][day]!=-1) return dp[cur][day];\n int ans = INT_MAX;\n int maxi = 0;\n for(int i=cur; i<n; i++){\n maxi = max(maxi, v[i]);\n ans = min(ans, maxi + solve(i+1, day-1, dp, v, n));\n }\n return dp[cur][day] = ans;\n }\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n if(d>n) return -1;\n vector<vector<int>> dp(n, vector<int>(n+1, -1));\n int x = solve(0, d, dp, jobDifficulty, n);\n if(x>=1e9) return -1;\n return x;\n }\n};", "memory": "15400" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>& job, int n, int index, int d, vector<vector<int>>& dp) {\n // base case\n if (d == 1) {\n int maxD = job[index];\n\n for (int i = index; i < n; i++) {\n maxD = max(maxD, job[i]);\n }\n return maxD;\n }\n //base case\n if(dp[index][d] != -1){\n return dp[index][d];\n }\n\n int maxD = job[index];\n int finalResult = INT_MAX;\n\n for (int i = index; i <= n - d; i++) {\n maxD = max(maxD, job[i]);\n\n int result = maxD + solve(job, n, i + 1, d - 1, dp);\n finalResult = min(finalResult, result);\n }\n return dp[index][d] = finalResult;\n }\n\n int minDifficulty(vector<int>& job, int d) {\n int n = job.size();\n\n vector<vector<int>> dp(n+1, vector<int>(n+1, -1));\n\n\n\n if( n < d) return -1;\n\n return solve(job, n, 0, d, dp);\n\n \n }\n};", "memory": "15500" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n vector<int>arr(n+1);\n for(int i = 1; i<=n; i++){\n arr[i] = jobDifficulty[i-1];\n }\n vector<vector<int>>mx(n+1,vector<int>(n+1));\n for(int i = 1; i<=n; i++){\n for(int j = i; j<=n; j++){\n mx[i][j] = max(mx[i][j-1],arr[j]);\n }\n }\n vector<vector<int>>dp(d+1,vector<int>(n+1,(int)1e9));\n dp[0][0] = 0;\n for(int t = 1; t<=d; t++){\n for(int i = 1; i<=n; i++){\n for(int j = 0; j<i; j++){\n dp[t][i] = min(dp[t][i],dp[t-1][j] + mx[j+1][i]);\n }\n }\n }\n if(dp[d][n] == 1e9)return -1;\n return dp[d][n];\n }\n};", "memory": "15600" }