// https://leetcode.com/problems/majority-element-ii/description/publicclassSolution {publicList<Integer> majorityElement(int[] nums) {//there should be at most 2 different elements in nums more than n/3//so we can find at most 2 elements based on Boyer-Moore Majority Vote algoList<Integer> res =newArrayList<Integer>();if(nums.length==0) return res;int count1=0,count2=0,m1=0,m2=0;for(int n : nums){if(m1==n) count1++;elseif(m2==n) count2++;elseif(count1==0) { m1=n; count1=1; }elseif(count2==0) { m2=n; count2=1; }else{ count1--; count2--; } } count1=0; count2=0;//count the number for the 2 elementsfor(int n:nums){if(n==m1) count1++;elseif(n==m2) count2++; }//if those appear more than n/3if(count1>nums.length/3) res.add(m1);if(count2>nums.length/3) res.add(m2);return res; }}
classSolution {int[][] dirs = {{0,1}, {0,-1}, {1,0}, {-1,0}};publicintslidingPuzzle(int[][] board) {// edge cases// find 0 positionint r0 =0, c0 =0, row =board.length, col = board[0].length;for (int r =0; r < row; r++) {for (int c =0; c < col; c++) {if (board[r][c] ==0) {r0 = r; c0 = c; break;} } }State begin =newState(board,0, r0, c0); // initial statePriorityQueue<State> pq =newPriorityQueue<>();Set<State> inQueue =newHashSet<>();pq.offer(begin);inQueue.add(begin);// BFSwhile (!pq.isEmpty()) {State curr =pq.poll();if (curr.reachTarget()) returncurr.step;for (int[] d : dirs) {int r =curr.r0+ d[0];int c =curr.c0+ d[1];State next =curr.move(r, c);if (next ==null||inQueue.contains(next)) continue;pq.offer(next);inQueue.add(next); } }return-1; }}classStateimplementsComparable<State> {int[][] board;int step, r0, c0, row, col;publicState(int[][] matrix,int k,int i,int j) { row =matrix.length; col = matrix[0].length;this.board=newint[row][col]; // deep copy passed matrixfor (int r =0; r < row; r++) {for (int c =0; c < col; c++) { board[r][c] = matrix[r][c]; } }this.step= k;this.r0= i;this.c0= j; }publicStatemove(int i,int j) {if (i <0|| i >=board.length|| j <0|| j >= board[0].length) returnnull; // out of boundaryState res =newState(this.board, step +1, i, j);int tmp =res.board[i][j];res.board[i][j] =res.board[r0][c0];res.board[r0][c0] = tmp;return res; } publicbooleanreachTarget() {returndistanceToTarget()==0; }publicintdistanceToTarget() {int sum =0;for (int r =0; r < row; r++) {for (int c =0; c < col; c++) {int val = board[r][c];if (val ==0) continue;int i = (val -1) /3; // this value suppossed to in this cellint j = (val -1) %3; sum +=Math.abs(r - i) +Math.abs(c - j); } }return sum; } @Overridepublicbooleanequals(Object obj) {State that = (State) obj;returnArrays.deepEquals(this.board,that.board); } @OverridepublicinthashCode() {returnArrays.deepHashCode(this.board); } @OverridepublicintcompareTo(State that) {return (distanceToTarget()+ step) - (that.distanceToTarget() +that.step); }}
Binary Indexed Tree
BIT explain: https://www.topcoder.com/thrive/articles/Binary%20Indexed%20Trees Key: one index is responsible for storing value representing a previous range, which is (index - getLSB(index), index]. E.g. index 12(1100) is responsible for (8, 12], i.e. (1000, 1100].
voidupdate(int idx,int val) {while (idx <= MaxIdx) { tree[idx] += val; idx += (idx &-idx); }}intread(int idx) {int sum =0;while (idx >0) { sum += tree[idx]; idx -= (idx &-idx); }return sum;}// "(idx & -idx)" is to get least significant non-zero bit, can also do idx & ~(idx-1), which is more intuitive.