mirror of
				https://git.wolves.top/wolves/leetcode.git
				synced 2025-11-04 17:26:32 +08:00 
			
		
		
		
	init
This commit is contained in:
		
							
								
								
									
										18
									
								
								greed/11.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								greed/11.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
#include <math.h>
 | 
			
		||||
 | 
			
		||||
// 核心优化,每次移动较小的那个,因为移动较大的那个,面积只会越来越小
 | 
			
		||||
int maxArea(int* height, int heightSize) {
 | 
			
		||||
    int left = 0;
 | 
			
		||||
    int right = heightSize - 1;
 | 
			
		||||
    int max = 0;
 | 
			
		||||
    while(left < right){
 | 
			
		||||
        max = fmax(max,(right-left)*fmin(height[left],height[right]));
 | 
			
		||||
        if (height[left] < height[right])
 | 
			
		||||
        {
 | 
			
		||||
            left++;
 | 
			
		||||
        }else{
 | 
			
		||||
            right--;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return max;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								greed/11.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								greed/11.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
from typing import List
 | 
			
		||||
 | 
			
		||||
class Solution:
 | 
			
		||||
    def maxArea(self, height: List[int]) -> int:
 | 
			
		||||
        left, right = 0,len(height)-1
 | 
			
		||||
        res = 0
 | 
			
		||||
        while left < right:
 | 
			
		||||
            res = max(res, (right-left) * min(height[left], height[right]))
 | 
			
		||||
            if height[left] < height[right]:
 | 
			
		||||
                left += 1
 | 
			
		||||
            else:
 | 
			
		||||
                right -= 1
 | 
			
		||||
        return res
 | 
			
		||||
							
								
								
									
										22
									
								
								greed/134.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								greed/134.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) {
 | 
			
		||||
    int i = 0;
 | 
			
		||||
    while (i < gasSize) {
 | 
			
		||||
        int sumOfGas = 0, sumOfCost = 0;
 | 
			
		||||
        int cnt = 0;
 | 
			
		||||
        while (cnt < gasSize) {
 | 
			
		||||
            int j = (i + cnt) % gasSize;
 | 
			
		||||
            sumOfGas += gas[j];
 | 
			
		||||
            sumOfCost += cost[j];
 | 
			
		||||
            if (sumOfCost > sumOfGas) {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
            cnt++;
 | 
			
		||||
        }
 | 
			
		||||
        if (cnt == gasSize) {
 | 
			
		||||
            return i;
 | 
			
		||||
        } else {
 | 
			
		||||
            i = i + cnt + 1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								greed/179.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								greed/179.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
long cmp(int *x, int *y) {
 | 
			
		||||
    unsigned long sx = 10, sy = 10;
 | 
			
		||||
    while (sx <= *x) {
 | 
			
		||||
        sx *= 10;
 | 
			
		||||
    }
 | 
			
		||||
    while (sy <= *y) {
 | 
			
		||||
        sy *= 10;
 | 
			
		||||
    }
 | 
			
		||||
    return sx * (*y) + (*x) - sy * (*x) - (*y);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char *largestNumber(int *nums, int numsSize) {
 | 
			
		||||
    qsort(nums, numsSize, sizeof(int), cmp);
 | 
			
		||||
    if (nums[0] == 0) {
 | 
			
		||||
        char *ret = malloc(sizeof(char) * 2);
 | 
			
		||||
        ret[0] = '0', ret[1] = '\0';
 | 
			
		||||
        return "0";
 | 
			
		||||
    }
 | 
			
		||||
    char *ret = malloc(sizeof(char) * 1000);
 | 
			
		||||
    char *p = ret;
 | 
			
		||||
    for (int i = 0; i < numsSize; i++) {
 | 
			
		||||
        sprintf(p, "%d", nums[i]);
 | 
			
		||||
        p += strlen(p);
 | 
			
		||||
    }
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										31
									
								
								greed/316.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								greed/316.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
char* removeDuplicateLetters(char* s) {
 | 
			
		||||
    int vis[26], num[26];
 | 
			
		||||
    memset(vis, 0, sizeof(vis));
 | 
			
		||||
    memset(num, 0, sizeof(num));
 | 
			
		||||
 | 
			
		||||
    int n = strlen(s);
 | 
			
		||||
    for (int i = 0; i < n; i++) {
 | 
			
		||||
        num[s[i] - 'a']++;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    char* stk = (char*)malloc(sizeof(char) * 27);
 | 
			
		||||
    int stkTop = 0;
 | 
			
		||||
    for (int i = 0; i < n; i++) {
 | 
			
		||||
        if (!vis[s[i] - 'a']) {
 | 
			
		||||
            while (stkTop > 0 && stk[stkTop - 1] > s[i]) {
 | 
			
		||||
                if (num[stk[stkTop - 1] - 'a'] > 0) {
 | 
			
		||||
                    vis[stk[--stkTop] - 'a'] = 0;
 | 
			
		||||
                } else {
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            vis[s[i] - 'a'] = 1;
 | 
			
		||||
            stk[stkTop++] = s[i];
 | 
			
		||||
        }
 | 
			
		||||
        num[s[i] - 'a'] -= 1;
 | 
			
		||||
    }
 | 
			
		||||
    stk[stkTop] = '\0';
 | 
			
		||||
    return stk;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										17
									
								
								greed/324.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								greed/324.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
static int cmp(const void *pa, const void *pb) {
 | 
			
		||||
    return *(int *)pa - *(int *)pb;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void wiggleSort(int* nums, int numsSize) {
 | 
			
		||||
    int * arr = (int *)malloc(sizeof(int) * numsSize);
 | 
			
		||||
    memcpy(arr, nums, sizeof(int) * numsSize);
 | 
			
		||||
    qsort(arr, numsSize, sizeof(int), cmp);
 | 
			
		||||
    int x = (numsSize + 1) / 2;
 | 
			
		||||
    for (int i = 0, j = x - 1, k = numsSize - 1; i < numsSize; i += 2, j--, k--) {
 | 
			
		||||
        nums[i] = arr[j];
 | 
			
		||||
        if (i + 1 < numsSize) {
 | 
			
		||||
            nums[i + 1] = arr[k];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    free(arr);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										20
									
								
								greed/409.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								greed/409.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <unordered_map>
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
class Solution {
 | 
			
		||||
    public:
 | 
			
		||||
        int longestPalindrome(string s) {
 | 
			
		||||
            unordered_map<char, int> count;
 | 
			
		||||
            int ans = 0;
 | 
			
		||||
            for (char c : s)
 | 
			
		||||
                ++count[c];
 | 
			
		||||
            for (auto p : count) {
 | 
			
		||||
                int v = p.second;
 | 
			
		||||
                ans += v / 2 * 2;
 | 
			
		||||
                if (v % 2 == 1 and ans % 2 == 0)
 | 
			
		||||
                    ++ans;
 | 
			
		||||
            }
 | 
			
		||||
            return ans;
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
							
								
								
									
										20
									
								
								greed/581.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								greed/581.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
#include <limits.h>
 | 
			
		||||
 | 
			
		||||
int findUnsortedSubarray(int* nums, int numsSize) {
 | 
			
		||||
    int n = numsSize;
 | 
			
		||||
    int maxn = INT_MIN, right = -1;
 | 
			
		||||
    int minn = INT_MAX, left = -1;
 | 
			
		||||
    for (int i = 0; i < n; i++) {
 | 
			
		||||
        if (maxn > nums[i]) {
 | 
			
		||||
            right = i;
 | 
			
		||||
        } else {
 | 
			
		||||
            maxn = nums[i];
 | 
			
		||||
        }
 | 
			
		||||
        if (minn < nums[n - i - 1]) {
 | 
			
		||||
            left = n - i - 1;
 | 
			
		||||
        } else {
 | 
			
		||||
            minn = nums[n - i - 1];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return right == -1 ? 0 : right - left + 1;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								greed/lcs01.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								greed/lcs01.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
int leastMinutes(int n){
 | 
			
		||||
    int i = 1;
 | 
			
		||||
    int j = 1;
 | 
			
		||||
    while(j < n){
 | 
			
		||||
        j <<= 1;
 | 
			
		||||
        i++;
 | 
			
		||||
    }
 | 
			
		||||
    return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include <cmath>
 | 
			
		||||
 | 
			
		||||
int leastMinutes(int n) {
 | 
			
		||||
    if (n <= 0) return 0;
 | 
			
		||||
    return static_cast<int>(std::ceil(std::log2(n))) + 1;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										6
									
								
								greed/lcs01.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								greed/lcs01.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
import math
 | 
			
		||||
 | 
			
		||||
def least_minutes(n: int) -> int:
 | 
			
		||||
    if n == 0:
 | 
			
		||||
        return 0
 | 
			
		||||
    return math.ceil(math.log2(n)) + 1
 | 
			
		||||
		Reference in New Issue
	
	Block a user