ARTES #025
ARTES #025
##ARTES 025 este es el articulo 25
Pregunta sobre el algoritmo del algoritmo
264. Número feo II
Dificultad: Media
Escribe un programa para encontrar el feo número n.
Un número feo es un entero positivo que contiene solo factores primos 2, 3, 5.
Ejemplo:
输入: n = 10
输出: 12
解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。```
**Descripción: **
1. `1` es un número feo.
2. `n` **no excede**1690.
#### Solución
Idioma: **C**
```c
int nthUglyNumber(int n) {
if (n<7) {
return n;
}
int rs[1691] = {0};
for (int i = 1; i < 7; i++) {
rs[i] = i;
}
int N2 = 0;
int N2_flag = 0;
int N3 = 0;
int N3_flag = 0;
int N5 = 0;
int N5_flag = 0;
int loop = 1;
for (int i = 7; i <= n; i++) {
loop = i;
for (int j = 1; j < loop; j++) {
if (N2_flag ==0 && (rs[j] * 2 > rs[i-1])) {
N2 = rs[j] * 2;
N2_flag =1;
}
if (N3_flag ==0 && (rs[j] * 3 > rs[i-1])) {
N3 = rs[j] * 3;
N3_flag =1;
}
if (N5_flag ==0 && (rs[j] * 5 > rs[i-1])) {
N5 = rs[j] * 5;
N5_flag =1;
}
}
//取 N2 N3 N5 最小的一个
int r = N2;
if (r>N3) {
r = N3;
}
if (r>N5) {
r=N5;
}
rs[i] = r;
N2_flag = 0;
N3_flag = 0;
N5_flag = 0;
}
return rs[n];
}
53. Suma máxima de subsecuencia
Dificultad: Fácil
Dada una matriz de números enteros nums, busque una submatriz continua con la suma máxima (la submatriz contiene al menos un elemento) y devuelva su suma máxima.
Ejemplo:
输入: [-2,1,-3,4,-1,2,1,-5,4],
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
Avanzado:
Si ya ha implementado una solución O(n), intente utilizar la solución más elegante de divide y vencerás.
Solución
Idioma: C
int maxSubArray(int* nums, int numsSize){
if (numsSize < 1) {
return 0;
}
int sum = nums[0];
int max = sum;
for (int i = 1; i < numsSize; i++) {
if (sum + nums[i] > nums[i]) {
sum = sum + nums[i];
}
else{
sum = nums[i];
}
if (sum > max) {
max = sum;
}
}
return max;
}
121. El mejor momento para comprar y vender acciones
Dificultad: Fácil
Dada una matriz, el _i_ésimo elemento es el precio de una acción determinada el día i.
Si solo se le permite completar una transacción como máximo (es decir, comprar y vender acciones), diseñe un algoritmo para calcular el beneficio máximo que puede obtener.
Tenga en cuenta que no puede vender una acción antes de comprarla.
Ejemplo 1:
输入: [7,1,5,3,6,4]
输出: 5
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。
Ejemplo 2:
输入: [7,6,4,3,1]
输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
Solución
Idioma: C
int maxProfit(int* prices, int pricesSize) {
if (pricesSize < 2) {
return 0;
}
int min = prices[0];
int ret = 0;
for (int i = 0; i<pricesSize; i++) {
int tem = prices[i] - min;
if (tem > ret ) {
ret = tem;
}
if (min > prices[i]) {
min = prices[i];
}
}
return ret;
}
La idea inicial de este problema es encontrar los valores máximo y mínimo, y el valor máximo está detrás del valor mínimo. La forma más fácil de pensar suele ser la menos eficiente.
La clave de este problema es calcular el retorno cada vez que pasa por el ciclo y actualizar el valor mínimo.
Revisión
El artículo aquí habla sobre el proceso de búsqueda de empleo de un estadounidense, y finalmente obtuvo un salario anual de 300.000 dólares estadounidenses. El nivel salarial del emperador estadounidense es realmente alto. https://dandan2009.github.io/2019/03/01/how-I-negotiated-a-job-offer-in-silicon-valley/
Consejos
Utilice el siguiente método para medir el tiempo de ejecución de una determinada línea de código:
os_log_t log = os_log_create("com.example.your-app", "RefreshOperations");
os_signpost_id_t log_t = os_signpost_id_generate(log);
os_signpost_interval_begin(log, log_t, "Recuperar activo");
//El código que deseas medir
os_signpost_interval_end(log, log_t, "Obtener activo1");
Compartir
What to read next
Want more posts about ARTS?
Posts in the same category are usually the best next step for reading more on this topic.
View same categoryWant to keep following #iOS?
Tags are useful for related tools, specific problems, and similar troubleshooting notes.
View same tagWant to explore another direction?
If you are not sure what to read next, return to the homepage and start from categories, topics, or latest updates.
Back home