-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewAlgos.js
114 lines (96 loc) · 3 KB
/
newAlgos.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// You are given a string representing an attendance record for a student. The record only contains the following three characters:
// 'A' : Absent.
// 'L' : Late.
// 'P' : Present.
// A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).
// You need to return whether the student could be rewarded according to his attendance record.
// Example 1:
// Input: "PPALLP"
// Output: True
// Example 2:
// Input: "PPALLL"
// Output: False
// /**
// * @param {string} s
// * @return {boolean}
// */
// var checkRecord = function(s) {
// let count = 0;
// for(let i = 0; i < s.length; i++) {
// if(s[i] === 'A') {
// count += 1;
// }
// }
// let result = count > 1 || s.includes('LLL') ? false : true;
// return result;
// };
// console.log(checkRecord('PPALLLLP'));
// ----------------------------------------------------------------------------------------------------------------------
// /**
// * @param {number[]} heights
// * @return {number}
// */
// var heightChecker = function(heights) {
// let moves = 0;
// let temp = 0;
// for(var i = 0; i < heights.length; i++) {
// for(var j = 1; j < heights.length; j++) {
// if(heights[i] > heights[j]) {
// temp = heights[i];
// heights[i] = heights[j];
// heights[j] = temp;
// moves++;
// console.log(`Moves: ${moves}`);
// break;
// }
// }
// }
// console.log(`Moves returned: ${moves}`);
// return moves;
// };
// heightChecker([1,1,4,2,1,3]);
// [1,1,2,1,3,4]
// ------------------------------------------------------------------------------
// /**
// * @param {number[]} bits
// * @return {boolean}
// */
// var isOneBitCharacter = function(bits) {
// let length = bits.length;
// if(length % 2 === 0) {
// return false;
// }
// else {
// return true;
// }
// };
// console.log(isOneBitCharacter([1,1,1,0]));
// ______________________________________________________________________________
/**
* @param {number[][]} A
* @return {number[][]}
*/
var flipAndInvertImage = function(A) {
output = [];
for(let i = 0; i < A.length; i++) {
let subArray = A[i];
// console.log(`initial: ${subArray}`);
subArray.reverse();
// console.log(`reversed: ${subArray}`);//--------------
// output.push(subArray);
// console.log(`output: ${output}`);//--------------
for(let j = 0; j < subArray.length; j++) {
if(subArray[j] === 0) {
subArray[j] = 1;
// output.push(subArray[j]);
}
else if(subArray[j] === 1) {
subArray[j] = 0;
// output.push(subArray[j]);
}
}
output.push(subArray);
}
return output;
};
console.log(flipAndInvertImage([[1,1,0],[1,0,1],[0,0,0]]));