456. 132 Pattern
Given a sequence of n integers a1, a2, …, an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list.
function find132pattern(nums){
let min=100000000000;
for(let i=0;i<nums.length-1;i++){
min=Math.min(nums[i],min); //存当前最小值
for(let k=i+1;k<nums.length;k++){
if(nums[k]<nums[i]&&min<nums[k]){
return true;
}
}
}
return false;
}
650. 2 Keys Keyboard
Initially on a notepad only one character ‘A’ is present. You can perform two operations on this notepad for each step:
Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
Paste: You can paste the characters which are copied last time.
Given a number n. You have to get exactly n ‘A’ on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n ‘A’.
function minSteps(n){
let step=0;
let board='';
let all='A';
const copy=function (all){
return all; //复制的动作,将现有的A复制到剪贴板
}
const paste=function(board){
step++;
all=all+board; //粘贴,把现在的和剪贴板中的连起来
}
while(all.length<n){
if(n%all.length==0){ //可以整除就复制
board=copy(all);
step++;
}
paste(board); //不能整除就粘贴
}
return step;
}