leetcode--greedy

  1. Best Time to Buy and Sell Stock II
class Solution {
    public int maxProfit(int[] prices) {
        int[] deltas = new int[prices.length];
        
        for (int i = 0; i < prices.length - 1; i++) {
            deltas[i] = prices[i+1] - prices[i];
        }
        
        int ret = 0;
        
        for (int v : deltas) {
            if (v > 0)
                ret += v;
        }
        
        return ret;
    }
}
  1. Assign Cookies
class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        int g_i = g.length-1;
        int s_i = s.length-1;
        int count = 0;
        while(g_i >=0 && s_i >= 0) {
            if(s[s_i] >= g[g_i]) {
                count++;
                s_i--;
            }
            g_i--;
        }
        return count;
    }
}
  1. Queue Reconstruction by Height
class Solution {
    public int[][] reconstructQueue(int[][] people){
if(people == null || people.length == 0){
        return new int[][]{};
}
Arrays.sort(people, new Comparator<int[]>(){
    public int compare(int[] o1, int[] o2){
        return o1[0] != o2[0] ? o2[0] - o1[0] : o1[1] - o2[1];
}
});
List<int[]> res = new ArrayList<>();
for(int[] cur: people){
    res.add(cur[1], cur);
}
return res.toArray(new int[people.length][2]);
}

}
  1. Is Subsequence
sample 4 ms submission
class Solution {
    public boolean isSubsequence(String s, String t) {
        char[] cc = s.toCharArray();
        int last = 0;
        for (int i = 0; i < cc.length; i++) {
            int index = t.indexOf(cc[i], last);
            if (index < 0) {
                return false;
            }
            last = index + 1;
        }
        return true;
    }
}
  1. Task Scheduler
class Solution {
    public int leastInterval(char[] tasks, int n) {
        int[] taskCounts = new int[26];
        for(char task: tasks) {
            taskCounts[task-'A']++;
        }
        
        int max = 0;
        int elementsWithMax = 0;
        for(int count: taskCounts) {
            if (count == max) {
                elementsWithMax++;
            } else if (count > max) {
                max = count;
                elementsWithMax = 1;
            }
        }
        
        return Math.max(tasks.length, (max-1)*(n+1) + elementsWithMax);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,925評論 0 33
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,858評論 0 10
  • 每次特別迷茫的時(shí)候,我總試圖通過練字來轉(zhuǎn)移自己的注意力,也許只有這個方式對于我而言,才會顯得平靜安詳。 有朋友問我...
    優(yōu)優(yōu)和吖吖閱讀 260評論 0 1
  • 1、剛認(rèn)識的時(shí)候,福哥還在當(dāng)兵,那時(shí)候我牙齒痛,吃了一個月藥,前后去了醫(yī)院三四次,每天晚上疼的睡不著,福哥怕我難受...
    老三姑娘閱讀 414評論 0 0
  • 題圖:阿里巴巴集團(tuán)董事局主席 馬云先生 如今阿里已經(jīng)是市值4580億美元的商務(wù)巨頭,18年前馬云創(chuàng)辦這家企業(yè)時(shí)一定...
    投資人說閱讀 258評論 0 2

友情鏈接更多精彩內(nèi)容