Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function splitString(s){
- let arr = [];
- let str = ``;
- for (i in s) {
- if (isNaN(s[i])) {
- str += s[i];
- arr.push(str);
- str = "";
- } else {
- str += s[i];
- }
- }
- return arr;
- }
- checkSort = (a,b) => {
- if (a.length > b.length) return 1;
- if (a.length < b.length) return -1;
- const tempA = a.substr(0, a.length - 1);
- const tempB = b.substr(0, b.length - 1);
- if (Number(tempA) > Number(tempB)) return 1;
- if (Number(tempA) < Number(tempB)) return -1;
- if (a[a.length - 1] > b[b.length - 1]) return 1;
- if (a[a.length - 1] < b[b.length - 1]) return -1;
- return 0;
- }
- countPhomTuQuy = (a) => {
- let count = 0;
- let num0 = a[0].substr(0, a[0].length - 1);
- let num1 = a[1].substr(0, a[1].length - 1);
- let num2 = a[2].substr(0, a[2].length - 1);
- let num3 = a[3].substr(0, a[3].length - 1);
- let num4 = a[4].substr(0, a[4].length - 1);
- let countTu = 0;
- let countSa = 0;
- if (num1 != num0) countTu++;
- if (num2 != num0) countTu++;
- if (num3 != num0) countTu++;
- if (a[0][a[0].length - 1] != a[1][a[1].length - 1] || Number(num0) + 1 != Number(num1)) countSa++;
- if (a[0][a[0].length - 1] != a[2][a[2].length - 1] || Number(num0) + 2 != Number(num2)) countSa++;
- if (a[0][a[0].length - 1] != a[3][a[3].length - 1] || Number(num0) + 3 != Number(num3)) countSa++;
- count += countTu > countSa ? countSa : countTu;
- for (let i = 5; i < 9; i++) {
- if (a[i][a[i].length - 1] !== a[4][a[4].length - 1]) {
- count++;
- continue;
- }
- let numi = a[i].substr(0, a[i].length - 1);
- if (Number(numi) != Number(num4) + i - 4) count++;
- }
- return count;
- }
- coutNumSam = (a) => {
- let count = 0;
- for(let i = 0; i < 3; i++) {
- let c1 = a[i *3];
- let c2 = a[i * 3 + 1];
- let c3 = a[i * 3 + 2];
- let num1 = c1.substr(0, c1.length - 1);
- let num2 = c2.substr(0, c2.length - 1);
- let num3 = c3.substr(0, c3.length - 1);
- countSam = 2;
- countSanh = 0;
- if (num1 == num2 && num2 == num3) {
- countSam = 0;
- } else if (num1 != num2 && num1 != num3 && num2 != num3) {
- countSam = 2;
- } else {
- countSam = 1;
- }
- if (Number(num1) > 11) {
- countSanh = 3;
- } else {
- if (c1[c1.length - 1] !== c2[c2.length - 1]) {
- countSanh++;
- } else if (Number(num2) - Number(num1) != 1) {
- countSanh++;
- }
- if (c1[c1.length - 1] !== c3[c3.length - 1]) {
- countSanh++;
- } else if (Number(num3) - Number(num1) != 2) {
- countSanh++;
- }
- }
- count += countSam > countSanh ? countSanh : countSam;
- }
- return count;
- }
- countChange = (arr) => {
- let count1 = countPhomTuQuy(arr);
- let count2 = coutNumSam(arr);
- return count1 > count2 ? count2 : count1;
- }
- generate = (a) => {
- let min = 9;
- while(true) {
- let i = a.length - 2;
- let temp = 0;
- while(i>=0&& checkSort(a[i],a[i+1]) == 1 ) i--;
- if(i < 0) return min; //hoán vị cuối
- else
- {
- let k = a.length - 1; //tim so nho nhat tu i tro di
- while(checkSort(a[k],a[i]) == -1) k--;
- //hoán vị a[i] với phần tử nhỏ nhất lớn hơn a[i]
- temp = a[k];
- a[k] = a[i];
- a[i] = temp;
- //đảo ngược dãy từ i+1 đến n-1
- let e=a.length-1,b=i+1;
- while(e>b)
- {
- temp = a[e];
- a[e] = a[b];
- a[b] = temp;
- b++;e--;
- }
- }
- temp = countChange(a);
- if (min > temp) min = temp;
- }
- }
- function xxxxxxx(s){
- let arr = splitString(s);
- arr = arr.sort(checkSort);
- let min = countChange(arr);
- let temp = generate(arr);
- if (min > temp) min = temp;
- return min;
- }
Add Comment
Please, Sign In to add comment