1093. Statistics from a Large Sample
问题比较简单,主要是需要处理计算均值求和时溢出的问题。
class Solution {
public double[] sampleStats(int[] count) {
int sum=0,i=0,j=count.length-1,n_sample=0;
double[] ans=new double[5];
while(i<count.length)
{
if(count[i]!=0)
break;
i++;
}
ans[0]=(double)i;
while(j>=i)
{
if(count[j]!=0)
break;
j--;
}
ans[1]=(double)j;
int mode=i,frequency=count[i];
for(int k=i;k<=j;k++){
n_sample+=count[k];
if(count[k]>frequency)
{
mode=k;
frequency=count[k];
}
}
for(int k=i;k<=j;k++){
if(count[k]==0)
continue;
ans[2]+=(double)count[k]*k/n_sample;
}
ans[4]=(double)mode;
if(n_sample%2==0)
{
int left_index=n_sample/2;
int right_index=left_index;
for(int k=i;k<=j;k++){
if(left_index<=count[k])
{
left_index=k;
break;
}
left_index-=count[k];
}
for(int k=j;k>=i;k--){
if(right_index<=count[k])
{
right_index=k;
break;
}
right_index-=count[k];
}
ans[3]=((double)left_index+(double) right_index)/2;
}
else
{
int index=n_sample/2+1;
for(int k=i;k<=j;k++)
{
if(index<=count[k])
{
ans[3]=(double)k;
break;
}
index-=count[k];
}
}
return ans;
}
}
class Solution {
public boolean carPooling(int[][] trips, int capacity) {
int[] stops=new int[1001];
for(int[] tmp:trips){
stops[tmp[2]]-=tmp[0];
stops[tmp[1]]+=tmp[0];
}
for(int i=1;i<1001 && capacity>=0;i++){
capacity-=stops[i];
if(capacity<0)
return false;
}
return true;
}
}
1095. Find in Mountain Array
使用三次二分,第一次二分求峰值位置,然后对左右子区间进行二分查找。
class Solution {
public int findInMountainArray(int target, MountainArray mountainArr) {
int left=0,right=mountainArr.length()-1;
while(left<right){
int mid=left+(right-left)/2;
if(mountainArr.get(mid)<mountainArr.get(mid+1))
left=mid+1;
else
right=mid;
}
if(mountainArr.get(left)==target)
return left;
int l=0,r=left;
while(l<r){
int m=l+(r-l)/2;
if(mountainArr.get(m)<target)
l=m+1;
else
r=m;
}
if(mountainArr.get(l)==target)
return l;
l=left;
r=mountainArr.length();
while(l<r){
int m=l+(r-l)/2;
if(mountainArr.get(m)<target)
r=m-1;
else if(mountainArr.get(m)>target)
l=m+1;
else
return m;
}
System.out.print(l);
return -1;
}
}
class Solution {
public static int idx;
public List<String> braceExpansionII(String expression) {
idx=0;
Set<String> ans=parseRule1(expression);
List<String> list=new ArrayList<>();
for(String s:ans)
list.add(s);
Collections.sort(list);
return list;
}
public Set<String> merge(Set<String> set1, Set<String> set2) {
if(set1.size()==0)
return set2;
if(set2.size()==0)
return set1;
HashSet<String> ans = new HashSet<>();
for (String str1 : set1) {
for (String str2 : set2) {
ans.add(str1 + str2);
}
}
return ans;
}
public Set<String> parseRule1(String str) {
Set<String> ans = new HashSet<>();
while (idx < str.length()) {
char c = str.charAt(idx);
if (c == '}' || c == ',')
break;
if (c == '{') {
idx++;
Set<String> tmp = parseRule2(str);
idx++;
ans = merge(ans, tmp);
} else {
Set<String> tmp = new HashSet<>();
StringBuilder sb = new StringBuilder();
while (idx < str.length() && str.charAt(idx) >= 'a' && str.charAt(idx) <= 'z') {
sb.append(str.charAt(idx));
idx++;
}
tmp.add(sb.toString());
ans = merge(ans, tmp);
}
}
return ans;
}
public Set<String> parseRule2(String str) {
Set<String> ans;
ans = parseRule1(str);
while (idx < str.length()) {
char c = str.charAt(idx);
if (c != ',')
break;
idx++;
Set<String> tmp = parseRule1(str);
for (String s : tmp)
ans.add(s);
}
return ans;
}
}