Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 3 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using LL = long long;
class Solution {
LL dp[101][101];
LL dist[101][101][101];
public:
long long minimumTotalDistance(vector<int>& robot, vector<vector<int>>& factory)
{
int m = robot.size();
int n = factory.size();

sort(robot.begin(), robot.end());
sort(factory.begin(), factory.end());

for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
{
LL sum = 0;
for (int k=j; k<m; k++)
{
sum += abs(factory[i][0]-robot[k]);
dist[i][j][k] = sum;
}
}

dp[0][0] = 0;
for (int j=1; j<=m; j++)
{
if (j<=factory[0][1])
dp[0][j] = dist[0][0][j-1];
else
dp[0][j] = LLONG_MAX/2;
}

for (int i=1; i<n; i++)
for (int j=1; j<=m; j++)
{
dp[i][j] = dp[i-1][j];
for (int k=1; k<=min(j,factory[i][1]); k++)
dp[i][j] = min(dp[i][j], dp[i-1][j-k] + dist[i][j-k][j-1]);
}

return dp[n-1][m];
}
};
1 change: 1 addition & 0 deletions Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@
[1335.Minimum-Difficulty-of-a-Job-Schedule](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1335.Minimum-Difficulty-of-a-Job-Schedule) (M+)
[1478.Allocate-Mailboxes](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1478.Allocate-Mailboxes) (H)
[1977.Number-of-Ways-to-Separate-Numbers](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1977.Number-of-Ways-to-Separate-Numbers) (H)
[2463.Minimum-Total-Distance-Traveled](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2463.Minimum-Total-Distance-Traveled) (M+)
* ``区间型 II``
[131.Palindrome-Partitioning](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/131.Palindrome-Partitioning) (M+)
[312.Burst-Balloons](https://github.com/wisdompeak/LeetCode/tree/master/DFS/312.Burst-Balloons) (H-)
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@

class Solution {
unordered_map<int,int>Root;
public:
int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial)
{
int N = graph.size();
for (int i=0; i<N; i++)
Root[i] = i;
for (int i=0; i<N; i++)
for (int j=0; j<N; j++)
{
if (i==j) continue;
if (graph[i][j]==0) continue;
if (findRoot(i)!=findRoot(j))
Union(i,j);
}

unordered_map<int,vector<int>>Children;
for (int i=0; i<N; i++)
{
Root[i] = findRoot(i);
Children[Root[i]].push_back(i);
}

set<int>Set(initial.begin(),initial.end());

int MaxGroup = 0;
int result;
for (auto a:Children)
{
int count = 0;
int candidate;
for (auto b:a.second)
{
if (Set.find(b)!=Set.end())
{
count++;
candidate = b;
}
if (count>1) break;
}
if (count==1 && (a.second.size()>MaxGroup || a.second.size()==MaxGroup && candidate<result))
{
MaxGroup = a.second.size();
result = candidate;
}
}

if (MaxGroup!=0) return result;
else return *Set.begin();
}

int findRoot(int x)
{
if (Root[x]!=x)
Root[x] = findRoot(Root[x]);
return Root[x];
}

void Union(int x, int y)
{
x = Root[x];
y = Root[y];
if (x<y)
Root[y] = x;
else
Root[x] = y;
}
};

AltStyle によって変換されたページ (->オリジナル) /