-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy path0763-PartitionLabels.cs
More file actions
34 lines (31 loc) · 988 Bytes
/
0763-PartitionLabels.cs
File metadata and controls
34 lines (31 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//-----------------------------------------------------------------------------
// Runtime: 224ms
// Memory Usage: 30.6 MB
// Link: https://leetcode.com/submissions/detail/391038298/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _0763_PartitionLabels
{
public IList<int> PartitionLabels(string S)
{
int[] last = new int[26];
for (int i = 0; i < S.Length; i++)
last[S[i] - 'a'] = i;
int start = 0, lastMax = 0;
var result = new List<int>();
for (int i = 0; i < S.Length; i++)
{
lastMax = Math.Max(lastMax, last[S[i] - 'a']);
if (i == lastMax)
{
result.Add(i - start + 1);
start = lastMax + 1;
}
}
return result;
}
}
}