1365. How many number less than the current number
const int maxn=500+10; //int hash[maxn]={0}; vector<int>ans; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { int n=nums.size(); ans.clear(); for(int i=0;i<n;i++){ int cnt=0; for(int j=0;j<n;j++){ if(nums[i]>nums[j])cnt++; } ans.push_back(cnt); } //ans.resize(n); return ans; } };
1366. The vote by the team rankings
const int MAXN = 1E4 + 10; Map <char, Vector <int >> MP; int m; inline BOOL CMP (char & A, char & B) { for (int I = 0; I <min (m, 26 is); I ++ ) { IF (MP [A] [I]> MP [B] [I]) return to true; IF (MP [A] [I] <MP [B] [I]) return to false; // this step can not be less ! ! ! } Return A <B; // this comparison the following method is also possible, which is a characteristic map, the internal vector array from the first element begins a back automatically a comparison. . . MP return // [A] == MP [B] A <B: MP [A]> MP [B];? } class Solution { public: String rankTeams (Vector <String> & votes) { int = n-votes. size (); m = votes [0] .size (); for (Auto V: votes [0]) { MP [V] = Vector <int> (m, 0); // this step is a step of initialization is performed . . . } for (Auto V: votes) { for (int I = 0; I <m; I ++) { MP [v [i]] [I] ++; // this is represented by two-dimensional representation of the map mp v [i] in the rank characters i-bit number. . . << COUT MP // [V [I]] [I] << endl; } } String votes RES = [0]; Sort (res.begin (), res.end (), CMP); return RES; } };
List 1367. Binary Tree
{Solution class public: BOOL Check (* ListNode head, the TreeNode * the root) { IF (head == nullptr a) return to true; IF (the root == nullptr a) return to false; IF (directory root-> Val = head-> Val!) to false return; BOOL RET = to false; RET RET = || Check (head-> Next, directory root-> left); RET RET = || Check (head-> Next, directory root-> right); return RET; } BOOL DFS (* ListNode head, the TreeNode * the root) { BOOL RET = to false; // initially set to false, as long as this is true because then it can be, or a condition IF (the root == nullptr a) return to false; RET RET = | | check (head, root); // as long as there is a path you can meet the conditions, so as or condition. . = || DFS RET RET (head, directory root-> left); RET RET = || DFS (head, directory root-> right); return RET; } bool isSubPath(ListNode* head, TreeNode* root) { return dfs(head,root); } };
1368. trellis diagram so that at least a minimum cost valid path
{Solution class public: // 0-1BFS, need deque: deque // direction arrow may cost as 0, 1 cost as other directions, has been calculated down. . . minCost int (Vector <Vector <int >> & Grid) { int m = grid.size (); int = n-Grid [0] .size (); the deque <pair <int, int >> DQ; // initialize be the position of the start point is located. . . This is a double-ended queue, a first one-dimensional position data, the second data cost dq.push_front (the make_pair (0,0)); small // cost consideration on the front priority queued deque , the cost of a large priority queue behind, because we need to select a low-cost path vector <vector <int >> dir { {0,1}, {0, -1}, {1,0}, {- 1 , 0}}; // sequentially represents from 0: {0, 1} Right, 1: {0, -1} left, 2: {1,0}, the 3: {- 1,0} a vector <int> vis (m * n , 0); // represents been extended before. . . (! dq.empty ()) {the while // Auto [P, cost] = dq.front (); pair <int, int> dq.front Q = (); P = q.first int, cost = q.second; } dq.pop_front (); int PX = P / n-, Py = P n-%; IF (PX ==. 1-m-n-== && Py. 1) return cost; IF (VIS [P] ++) Continue; // if It has been extended too, then you do not have expanded. . . for (int I = 0; I <. 4; I ++) { int TX = PX + the dir [I] [0], TY = Py + the dir [I] [. 1]; int TP = TX * n-+ TY; IF ( TX <0 || TX> TY = m || <|| 0 TY> = || n-VIS [TP]) Continue; IF (Grid [PX] [Py] +. 1 == I) {// represents the not need to change direction dq.push_front (the make_pair (TP, cost)); } the else { dq.push_back ({TP, cost +. 1}); // indicates the need to change the direction of the tail was added at this time, because there may better choice than this. . . } } Return -1; } };