function solution(A) {
var sum = []
var current_sum = 0
sum.push(0)
for(var i=0;i<A.length;i++){
current_sum += A[i];
sum.push(current_sum)
}
var min = Infinity;
var min_start = -1
for(var i = 0;i<A.length;i++){
for(var j = i+1;j<A.length;j++){
var avg = (sum[j+1]-sum[i])/(j+1-i);
if(min > avg) {
min = avg;
min_start = i
}
}
}
return min_start
}
//這會有錯誤的結果
function solution(A) {
var sum = []
var current_sum = 0
sum.push(0)
var mustIncludeIndex = -1
var mustIncludeValue = Infinity
for(var i=0;i<A.length;i++){
current_sum += A[i];
sum.push(current_sum)
if(mustIncludeValue > A[i]) {
mustIncludeValue = A[i];
mustIncludeIndex = i
}
}
var min = Infinity;
var min_start = -1
for(var i = 0;i<mustIncludeIndex;i++){
for(var j = i+1;j<A.length;j++){
var avg = (sum[j+1]-sum[i])/(j+1-i);
if(min > avg) {
min = avg;
min_start = i
}
}
}
return min_start
}
萬般無助之下,詢問了ChatGPT,沒想到他都比我聰明(哭),好吧這就來改寫!!
以下為改寫後的程式碼
function solution(A) {
var sum = []
var current_sum = 0
sum.push(0)
for(var i=0;i<A.length;i++){
current_sum += A[i];
sum.push(current_sum)
}
var min = Infinity;
var min_start = -1
for(var i = 0;i<A.length;i++){
var max_possible = Math.min(A.length, i+3)
for(var j = i+1;j<max_possible;j++){
var avg = (sum[j+1]-sum[i])/(j+1-i);
if(min > avg) {
min = avg;
min_start = i
}
}
}
return min_start
}
陷阱在這: each element of arrays P and Q is an integer within the range [0..N – 1]; => 所以不一定一樣長
解題思路
先用暴力法寫出一個可行解
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S, P, Q) {
// Implement your solution here
factor = {'A':1,'C':2,'G':3,'T':4}
var length_M = Math.min(P.length, Q.length)
var M = []
for( var i = 0 ; i < length_M; i++){
var inclusive_S = S.slice(P[i], Q[i]+1).split("")
var minimum = Infinity
for(var j =0;j<inclusive_S.length;j++){
//console.log(minimum, factor[inclusive_S[j]])
if(minimum > factor[inclusive_S[j]]){
minimum = factor[inclusive_S[j]]
}
}
M.push(minimum)
}
return M
}
因為邏輯有一點複雜,先把暴力解送出驗證一下正確性,之後再來優化
好的!!62%!!至少邏輯正確,那我們在想想要如何優化。讓我先觀察一下是死在那些效能測試上。分別是GGGGGG..??..GGGGGG..??..GGGGGG、large random string, length、all max ranges,首先先看到almost_all_same_letters,所以先把CAGC對應數字的部分做掉,如: S = CAGCCTA = [2,1,3,2,2,4,1]
function solution(S, P, Q) {
// Implement your solution here
factor = {'A':1,'C':2,'G':3,'T':4}
var length_M = Math.min(P.length, Q.length)
var S = S.split("").map(x => factor[x])
var M = []
for( var i = 0 ; i < length_M; i++){
M.push(Math.min(...S.slice(P[i], Q[i]+1)))
}
return M
}
function solution(X, A) {
// Implement your solution here
var filled = new Array(X).fill(false);
var current_filled = 0;
for (let index = 0; index < A.length; index++) {
//console.log(filled)
if(!filled[A[index]-1]){
filled[A[index]-1] = true
current_filled ++;
if(current_filled == X){
return index
}
}
}
return -1
}
function solution(A) {
// Implement your solution here
var total_a = 0;
var total_b = A.reduce((partialSum, a) => partialSum + a, 0);
var min = Infinity;
for(var i=0;i<A.length-1;i++){
total_a = total_a+A[i];
total_b = total_b-A[i];
//console.log(total_a, total_b,Math.abs(total_a-total_b), i)
if(Math.abs(total_a-total_b) < min){
min = Math.abs(total_a-total_b)
}
}
return min
}
CMAF(Common Media Application Format,通用媒體應用格式)是一種專為網絡媒體傳輸設計的標準。CMAF旨在簡化不同裝置和網絡環境之間的媒體流適配和交付,從而提高流媒體的性能和覆蓋範圍。CMAF是一種媒體封裝格式。CMAF被設計為簡化在不同裝置和網絡環境之間的媒體流適配和交付,從而提高流媒體的性能和覆蓋範圍。CMAF檔案通常具有.cmf或.mp4擴展名。
CMAF(Common Media Application Format,通用媒體應用格式)是一種媒體封裝格式,類似於FLV(Flash Video)和MP4(MPEG-4 Part 14)。CMAF旨在簡化在不同裝置和網絡環境之間的媒體流適配和交付,以提高流媒體的性能和覆蓋範圍。
CMAF(Common Media Application Format,通用媒體應用格式)主要用於適配和交付HLS(HTTP Live Streaming)和MPEG-DASH(Dynamic Adaptive Streaming over HTTP)等流媒體協議。CMAF的目標是在不同裝置和網絡環境之間提供高效的媒體流適配和交付,而非專注於低延遲。
Each exporter should monitor exactly one instance application, preferably sitting right beside it on the same machine. That means for every HAProxy you run, you run a haproxy_exporter process. For every machine with a Mesos worker, you run the Mesos exporter on it, and another one for the master, if a machine has both.
The theory behind this is that for direct instrumentation this is what you’d be doing, and we’re trying to get as close to that as we can in other layouts. This means that all service discovery is done in Prometheus, not in exporters. This also has the benefit that Prometheus has the target information it needs to allow users probe your service with the blackbox exporter.