Skip to content

Commit cd33754

Browse files
author
Cary Hu
committed
20221218
1 parent b7282b2 commit cd33754

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

Diff for: code_hive/MinMoves/MinMoves.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

Diff for: code_hive/MinMoves/MinMoves.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33209.295
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinMoves", "MinMoves.csproj", "{D973D949-7FD0-4FA2-8847-EC93590A6D30}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{D973D949-7FD0-4FA2-8847-EC93590A6D30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D973D949-7FD0-4FA2-8847-EC93590A6D30}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D973D949-7FD0-4FA2-8847-EC93590A6D30}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D973D949-7FD0-4FA2-8847-EC93590A6D30}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {4A3DA420-0010-47E7-87FF-CDB4582910F6}
24+
EndGlobalSection
25+
EndGlobal

Diff for: code_hive/MinMoves/Program.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// <summary>
2+
/// 20221218
3+
/// https://leetcode.cn/problems/minimum-adjacent-swaps-for-k-consecutive-ones/
4+
/// </summary>
5+
public class Solution
6+
{
7+
public int MinMoves(int[] nums, int k)
8+
{
9+
IList<int> g = new List<int>();
10+
IList<int> preSum = new List<int>();
11+
preSum.Add(0);
12+
for (int i = 0; i < nums.Length; i++)
13+
{
14+
if (nums[i] == 1)
15+
{
16+
g.Add(i - g.Count);
17+
preSum.Add(preSum[preSum.Count - 1] + g[g.Count - 1]);
18+
}
19+
}
20+
int m = g.Count, res = int.MaxValue;
21+
for (int i = 0; i <= m - k; i++)
22+
{
23+
int mid = i + k / 2;
24+
int r = g[mid];
25+
res = Math.Min(res, (1 - k % 2) * r + (preSum[i + k] - preSum[mid + 1]) - (preSum[mid] - preSum[i]));
26+
}
27+
return res;
28+
}
29+
}

Diff for: fakedatehook.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
_() {
44
YEAR="2022"
55
MONTH="12"
6-
DAY="19"
6+
DAY="18"
77
git add .
88
GIT_AUTHOR_DATE="${YEAR}-${MONTH}-${DAY}T12:56:32" \
99
GIT_COMMITTER_DATE="${YEAR}-${MONTH}-${DAY}T12:56:32" \

0 commit comments

Comments
 (0)